/*****
 * Combo box
 *
 */
var Combo = Class.create({
	//Constructorul
	initialize:function(list, options)
	{

		var _self = this;
		this.list = list;
		if(options)
			_self.set_options(options);

		this.populate();
		this.list_collection = this.get_option_list();
		$(this.list).stopObserving('click'); //Opreste eventul de click daca are
		$(this.list).observe('click', this.drop_down.bindAsEventListener(null,_self)); // Aduaga event de click
	},
	get_list:function()
	{

	},
	// caller
	__call: function(func){
		if(!/\W/.test(func.toString())) {
			var func = eval("this."+func.toString());
			if(typeof(func) === "function"){
				return func;
			}else{
				return false;
			}
		}
	},
	//seteaza atributele obiectului
	set_options:function(options)
	{
		var opt = $H(options).collect(function(el){
			return $A(el);
		});
		for(var i = 0; i< opt.length; i++){
			this[opt[i][0]] = opt[i][1];
		}
	},
	populate:function()
	{
		_self = this;
		this.reset_list();
		var html = DOMBuilder.apply();

		if(this.populate_data == null){
			this.reset_list();
			$(_self.input).value = 0;
			$$('#'+_self.list+' span')[0].innerHTML = 'No Braids';
			$('bottombar').value = 0;
			ScallopsTools.bottom_bar_braids_panel('show_braids');
			var price = new ScallopPrice();
			price.braids_increment();
			return;
		}

		this.populate_data.each( function(s, index){
			LI = html.LI({ /*"value":parseInt(s.id),*/ style:"background-image:url(../rollermm/images/"+s.image+");"},
				html.A({rel:s.id, title:s.option_name},"")
			);
			Element.writeAttribute(LI, {'value':parseInt(s.id)}); //Fix stupid for ie6
			//Selecteaza prima optiune
			if(s.option_type == 'bottom bar' && index == 0 && s.selected != 1){
				_self.__call(_self.handler)(null, _self, LI);
			}
			// Selecteaza ce e bifat selected in baza de date
			else if(s.selected == 1){
				_self.__call(_self.handler)(null, _self, LI);
			}
			$$('#'+_self.list+' ul')[0].appendChild(LI);
		});
		return true;
	},
	reset_list:function(){
		_self = this;
		list_collection = this.get_option_list();
		list_collection.each(function(element,index){
			//if(element.className != 'first')
			//{
				element.remove(); // resetare lista braids
			//}
		});
	},
	get_option_list:function()
	{
		//var list_collection = (this.list_collection)? this.list_collection : $$('#'+this.list+' li');
		var list_collection =  $$('#'+this.list+' li');
		return list_collection;
	},
	drop_down:function(e,obj)
	{
		var list_collection = obj.get_option_list();
		for(len = 0; len < list_collection.length; len++)
		{
			var default_handler = (obj.handler)? obj.handler : 'event_handler';
			var func_name = obj.__call(default_handler);
			selected_scallop = $('scallops')[$('scallops').options.selectedIndex].innerHTML.strip();
			if(list_collection[len].style.display == 'none' || list_collection[len].style.display == '')
			{
				list_collection[len].observe('click', func_name.bindAsEventListener(null, obj, list_collection[len]));
				if(selected_scallop == 'straight finish with braids' && list_collection[len].className == 'first')
					list_collection[len].makeClipping().setStyle({display:'none'});
				else
					list_collection[len].makeClipping().setStyle({display:'block'});
			}
			else
			{
				list_collection[len].stopObserving('click');
				list_collection[len].undoClipping().setStyle({display:'none'});
			}
		}
	},
	event_handler:function(e, obj, selected){
		var maintext = ('No bottom bar' == selected.innerHTML.strip()) ? 'No bottom bar ' : '';
		$(obj.input).value = 0;
		$$('#'+obj.list+' span')[0].innerHTML = maintext;
		$$('#'+obj.list+' span')[0].setStyle({backgroundImage:selected.style.backgroundImage});
		$(obj.input).value = selected.getElementsByTagName('a')[0].rel;
		var price = new ScallopPrice();
		price.bottombar_increment();
	},
	braids_event_handler:function(e, obj, selected)
	{
		if(selected.innerHTML.strip() == 'No Braids'){
			$(obj.input).value = 0;
			$$('#'+obj.list+' span')[0].innerHTML = 'No Braids';
			var price = new ScallopPrice();
			price.braids_increment();
			return;
		}
		try{
			$(obj.input).value = selected.getElementsByTagName('a')[0].rel;
			$$('#'+obj.list+' span')[0].innerHTML = selected.getElementsByTagName('a')[0].title;
			var price = new ScallopPrice();
			return price.braids_increment();
		}catch(ex){
			alert('Error!');
		}
	}
});

var ComboSelect = Class.create(Combo, {
	initialize:function(combo)
	{
		this.combo = combo;
		this.list = $(combo).getElementsByTagName('li');

	},
	select_braids:function(selected)
	{
		list = this.list
		for(i=1; i < list.length; i++)
		{
			if(selected && list[i].getElementsByTagName('a')[0].rel == selected)
			{
				$$('#'+this.combo+' span')[0].innerHTML = '';
				$$('#'+this.combo+' span')[0].innerHTML = list[i].getElementsByTagName('a')[0].title;
				$('braid').value = selected;
			}
		}
	},
	select_bb:function()
	{
		list = this.list
		for(i=1; i < list.length; i++)
		{
			if(selected && list[i].getElementsByTagName('a')[0].rel == selected)
			{
				$$('#'+this.combo+' span')[0].innerHTML = '';
				$$('#'+this.combo+' span')[0].setStyle({backgroundImage:list[i].style.backgroundImage});
				$('bottombar').value = selected;
			}
		}
	}

})

/** Adauga preturile **/
var ScallopPrice = Class.create({
	initialize:function()
	{
		this.form = document.form_calcblackoutmm
	},
	braids_increment:function()
	{
		//VerifyBlackoutMMForm(this.form)
		if(this.form.custom_width.value != '' && this.form.custom_drop.value != '')
		{
			GetCustomPrice();
		}
	},
	bottombar_increment:function()
	{
		if(this.form.custom_width.value != '' && this.form.custom_drop.value != '')
		{
			GetCustomPrice();
		}
	},
	caferods_increment:function()
	{
		if(this.form.custom_width.value != '' && this.form.custom_drop.value != '')
		{
			GetCustomPrice();
		}
	}
})

/**** Scoate preturile din baza de date ( Ajax asincro ) ****/
var GetScallopsPrice = Class.create({
	initialize:function(id, width, drop){
			var _self = this;
			this._request_url = 'http://www.blindsuk.net/inc/roller_scallops.php';
		if(id > 0){
			new Ajax.Request(this._request_url,{
				method:'post',
				asynchronous:false,
				parameters:{action:'get_price',id:id, width:width, drop:drop},
				onSuccess: function(transport){
					_self.s_price = transport.responseText;
				}
			});
			return _self.s_price;
		}
		_self.s_price = 0;
		return 0;
	}
})
var GetContrastingBarPrice = Class.create({
	initialize:function(id, width, drop){
			var _self = this;
			this._request_url = 'http://www.blindsuk.net/inc/roller_scallops.php';
		if(id > 0){
			new Ajax.Request(this._request_url,{
				method:'post',
				asynchronous:false,
				parameters:{action:'get_contrastingbar_price',id:id, width:width, drop:drop},
				onSuccess: function(transport){
					_self.s_price = transport.responseText;
				}
			});
			return _self.s_price;
		}
		_self.s_price = 0;
		return 0;
	}
})
/*********************************************************************/
var ScallopsOptions = Class.create({
	initialize:function(product_id)
	{
		_self = this;
		this._request_url = 'http://www.blindsuk.net/inc/roller_scallops.php';
		this.ptoduct_id = product_id;
		this.get_scallops(assigned_scallop.scallops_tree);
		//$('scallops').options[0].selected = true;
		ScallopsTools.bottom_bar_braids_panel('show_braids');
	},
	__call: function(func){
		if(!/\W/.test(func.toString())) {
			var func = eval("this."+func.toString());
			if(typeof(func) === "function"){
				return func;
			}else{
				return false;
			}
		}
	},
	what_type:function(scallop)
	{
		for(i = 0; i< $('scallops').length; i++){
			if($('scallops')[i].value == scallop && $('scallops')[i].innerHTML == 'contrasting bottom bar'){
				alert ("Please select desired Bottom Bar Color. See bellow!");
				return 'bottom_bar';
			}
		}

		type = null;
		new Ajax.Request(this._request_url,{
			method:'post',
			asynchronous:false,
			parameters:{action:'what_type', scallop:scallop},
			onSuccess: function(transport){
				type = transport.responseText;
			}
		});
		return type;
	},
	get_scallops:function(product_id)
	{
		_self = this;
		var AjReq = new Ajax.Request(this._request_url,{
			method:'post',
			parameters:{action:'get_scallops', product_id:product_id},
			onSuccess:function(transport){
				var scallops = transport.responseText.evalJSON();
				var i = 1;
				$('scallops').length = 1;
				scallops.each(function(pair){
					$('scallops')[i++] = new Option(pair.label, pair.id);
					if(pair.selected == 1){
						$('scallops').options[i-1].selected = true;
						_self.scallop_handler(null, _self);
					}
				});
			}
		});
		//Pune actiunea de change pe scallops
		$('scallops').stopObserving('change');
		$('scallops').observe('change', this.scallop_handler.bindAsEventListener(null, this));
	},
	get_cafe_rods:function(scallop){
		_self = this;
		var AjReq = new Ajax.Request(this._request_url,{
			method:'post',
			parameters:{action:'get_cafe_rods', scallop:scallop},
			onSuccess:function(transport){
				var caferods = transport.responseText.evalJSON();
				$('cafe_rods').length = 0;
				if(caferods.length > 0)
				{
					alert ('Please select desired cafe rod. See bellow!');
					caferods.each(function(pair,index){
						$('cafe_rods')[index] = new Option(pair.label, pair.id);
					})
					price = new ScallopPrice();
					price.caferods_increment();
					changeControls(true); // no spring controls when we have cafe rods
				}else{
					changeControls(false); // all controls for no cafe rods
					$('cafe_rods')[0] = new Option('No', 0);
					price = new ScallopPrice();
					price.caferods_increment();
				}
			}
		});
		//Event handler pe dropdown-ul cafe rods
		$('cafe_rods').stopObserving('change');
		$('cafe_rods').observe('change', this.cafe_rods_handler.bindAsEventListener(null, this));
	},
	scallop_handler:function(e, obj)
	{
		if($('scallops').value != 0){
			try{
				obj.get_cafe_rods($('scallops').value);
				type = obj.what_type($('scallops').value);
				caller = 'get_'+type.sub(' ', '_');
				obj.__call(caller)($('scallops').value, obj);
				if($('scallops')[$('scallops').selectedIndex].innerHTML == 'straight finish with braids' && e != null){
     				alert('Please select desired Braids. See bellow!');
    			}
				GetCustomPrice();
			}catch(ex){
				alert('ERROR!')
			}
		}else{
			$('cafe_rods').length = 0;
			$('cafe_rods')[0] = new Option('No', 0);
			new Combo('braids', {handler: 'braids_event_handler', populate_data:null, input:'braid'});
			changeControls(false);
		}
	},
	cafe_rods_handler:function(e, obj){
		price = new ScallopPrice();
		price.caferods_increment();
	},
	get_braids:function(scallop, obj)
	{
		new Ajax.Request(obj._request_url,{
			method:'post',
			asynchronous:false,
			parameters:{action:'get_braids', scallop:scallop},
			onComplete:function(transport)
			{
				var braids = transport.responseText.evalJSON();
				new Combo('braids', {handler: 'braids_event_handler', populate_data:braids, input:'braid'});
			}
		});
		$('bottombar').value = 0;
		ScallopsTools.bottom_bar_braids_panel('show_braids');
	},
	get_bottom_bar:function(scallop, obj)
	{
		new Ajax.Request(obj._request_url+'?prod_id='+assigned_scallop.blackoutmm_product_id,{
			method:'post',
			parameters:{action:'get_bottom_bar', scallop:scallop, color:prod_color, type:'blackoutmm'},
			onSuccess:function(transport)
			{
				var bottom_bar = transport.responseText.evalJSON();
				new Combo('bottom_bar', {handler:'event_handler', populate_data:bottom_bar, input:'bottombar'});
			}
		});
		$('braid').value = 0;
		ScallopsTools.bottom_bar_braids_panel('show_bottom_bar');
	}
})



//Load Scallops Tools
Event.domReady.add(function(){
	$('braid').value = 0;
	$('bottombar').value = 0;
	var ScallopsObject = new ScallopsOptions();
});
