var opt = [];
var res;
var cur=[];
var tt=[];
var all=[];
var s=[];

var aaa = '';

var g_desc=' | ';




function set_opt() {
tt=[];

    res.each( function( item) {
	if ( $type(item) != 'function' ) {
	    item.val.each ( function(val) {
		
                    if ( !tt[val.variation_name] ) tt[val.variation_name] = [];    
		    if ( !tt[val.variation_name].contains(val.variation_value) ) tt[val.variation_name].push(val.variation_value);
		
	    })
	}
    })


set_interface();

}


function get_all() {
all=[];

if (opt.length >0 ) {
    opt.each( function( item) {
	if ( $type(item) != 'function') {
	    item.val.each ( function(val) {
		
                    if ( !all[val.variation_name] ) all[val.variation_name] = [];    
		    if ( !all[val.variation_name].contains(val.variation_value) ) all[val.variation_name].push(val.variation_value);
		
	    })
	}
    })
}

}


function get_price() {
    // $('price').setText('--');
    $('prod_var_id').setProperty('value','');
s=[];

g_desc=' | ';

$$('select').each(function(it){
var ob=[];
ob['variation_name'] = it.name;
ob['variation_value'] = it.value;
g_desc=g_desc+ob['variation_name']+' : '+ob['variation_value']+' | ';
s.push( ob );



})


opt.each( function( item) {

if ( $type(item) != 'function') {

if  ( item.val.every( function(val) {
        return (	s.some(function(sval) {
                    if (sval['variation_name'] == val.variation_name && sval['variation_value'] == val.variation_value)
                        return true;
                    else
                        return false;
                })	
        );

	    })   ) {



    $('price').setText (item.price );
    $('prod_var_id').setProperty('value',item.prod_var_id);

   $('g_price').setProperty ('value', item.price );



	$('g_desc').setProperty('value',g_desc);



        }

	}

});


}





function get_opt(sel) {
cur['sel'] = sel.id;
cur['val'] = sel.value;

if (!sel.value) {res = opt}
else {

res = opt.filter(function(item, index){

 if (   item.val.filter( function(val,index){
        return val.variation_value==sel.value;
        }).length > 0
) {
return item;
}
 


});
}

set_opt();




}




function set_interface() {

    for (v in tt) {
        if ($type(tt[v])!='function') {
           var sel = new Element('select').setProperties({'id':v,'name':v,'class':"input_product"}).setStyles({'display':'block','width':200,'text-align':'left'}).addEvent('change', function() {get_opt(this);});
            var o = new Element('option').setText('Select...').setProperties({'value':''}).injectInside(sel);
          tt[v].each( function(item){
                var o = new Element('option').setProperties({'value':item}).setText(item).injectInside(sel);
                if ( v == cur['sel'] && item == cur['val']) {
                    o.setProperties({'selected':true});
                }
            });

         if ( v != cur['sel'] ) {
            sv = $(v).value;    
            sel.getElements('option').each( function(op){
                if (op.value == sv) op.selected=true;
            })
            $(v).replaceWith(sel);

         }
         else {

           var sel = new Element('select').setProperties({'id':v,'name':v,'class':"input_product"}).setStyles({'display':'block','width':200,'text-align':'left'}).addEvent('change', function() {get_opt(this);});
            var o = new Element('option').setText('Select...').setProperties({'value':''}).injectInside(sel);
          all[v].each( function(item){
                var o = new Element('option').setProperties({'value':item}).setText(item).injectInside(sel);
                if ( v == cur['sel'] && item == cur['val']) {
                    o.setProperties({'selected':true});
                }
            });
              $(v).replaceWith(sel);
              }
        }

    }

get_price();
};





function ch_g(frm) {


document.getElementById('g_quant').value = frm.value
var tot = frm.value*document.getElementById('g_price').value 

if (tot < 35) document.getElementById('uk_delivery').value=4;
else
document.getElementById('uk_delivery').value=0;


;return true;

}




window.addEvent('domready', function() {

var accordion = new Accordion('span.atStart', 'div.atStart', {
	opacity: false,
        start:'all-close',
	onActive: function(toggler, element){
		toggler.setStyle('color', '#7AB910');
	},
 
	onBackground: function(toggler, element){
		toggler.setStyle('color', '#03679B');
	}
}, $('accordion'));

accordion.togglers.each(function(toggler){
	toggler.addEvent('mouseover',function(){
		this.fireEvent('click');
	});
	});


if ($('slideshowContainer')) {

				var obj = {
					wait: 5000, 
					effect: 'fade',
					duration: 1000, 
					loop: true, 
					thumbnails: true,
					backgroundSlider: true
				}
				show = new SlideShow('slideshowContainer','slideshowThumbnail',obj);
				show.play();

}


get_all();


accordion.display(opened);


if ( $('form') ) {
    if ( $$('select').length > 0 ) {
	    $('form').addEvent('submit', function(e) {
		if ( $$('select').every( function(val) { return val.value; }) ) {
	               ;
		}
		else {
			e = new Event(e).stop();
                        alert("Please select options for this product");
		    }
	});
    }
}

if ( $('gform') ) {
    if ( $$('select').length > 0 ) {
	    $('gform').addEvent('submit', function(e) {
		if ( $$('select').every( function(val) { return val.value; }) ) {
	               ;
		}
		else {
			e = new Event(e).stop();
                        alert("Please select options for this product");
		    }
	});
    }
}


});