var ComboCassette = Class.create({
	//Constructorul
	initialize:function(list, options)
	{

		var _self = this;
		this.list = list;
		if(options)
			_self.set_options(options);

		this.populate();
		$(this.list).stopObserving('click'); //Opreste eventul de click daca are
		$(this.list).observe('click', this.drop_down.bindAsEventListener(null,_self)); // Aduaga event de click
	},

	// 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 = 'Please select colour';
			$('cassette_color').value = 0;
			return;
		}

		this.populate_data.each( function(s, index){
      LI = html.LI({ 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
			//if (index == 0) _self.__call(_self.handler)(null, _self, LI);
			if(s.selected == 1 || index == 0){
				_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){
			element.remove();
		});
    this.clear_select(_self);
	},
	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();
    
//    my_custom_width = parseInt(document.form_calcvertical.custom_width.value);
//    if ( $('headrail').value == '1' && my_custom_width > 2500 ) {
//      alert("Sorry, the colour valance is available for sizes of up to 2500mm wide!");
//      return false;
//    }

    if (list_collection.length == 0) { alert("This option is only available for Contrasting fabric Cassette"); return false; }

		for(len = 0; len < list_collection.length; len++)
		{
      var default_handler = (obj.handler)? obj.handler : 'event_handler';
      var func_name = obj.__call(default_handler);
			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]));
				list_collection[len].makeClipping().setStyle({display:'block'});
			}
			else
			{
				list_collection[len].stopObserving('click');
				list_collection[len].undoClipping().setStyle({display:'none'});
			}
		}
	},
  clear_select:function(obj){
    $(obj.input).value = 0;
    $$('#'+obj.list+' span')[0].innerHTML = 'Please select colour';
    $$('#'+obj.list+' span')[0].setStyle({backgroundImage:''});
  },
	event_handler:function(e, obj, selected){
		var maintext = ('Please select colour' == selected.innerHTML.strip()) ? 'Please select colour' : '';
		$(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;
    GetCustomPrice();
	}
});

var RollerMMOptions = Class.create({
	initialize:function()
	{
		_self = this;
		this._request_url = 'http://www.blindsuk.net/inc/roller_scallops.php';
	},
	get_cassette:function(current_id, check)
	{
    if (check == 3) {
      new Ajax.Request(this._request_url, {
        method:'post',
        parameters:{action:'get_contrasting_cassette_values', id:current_id},
        onSuccess:function(transport)
        {
          if (check == 3) alert("Please select desired colour bellow");
          var contrasting_casseette = transport.responseText.evalJSON();
          new ComboCassette('cassette_color_container', {handler:'event_handler', populate_data:contrasting_casseette, input:'cassette_color'});
        }
      });
    }
    else new ComboCassette('cassette_color_container', {handler:'event_handler', populate_data:null, input:'cassette_color'});
	}
})
var dd = new RollerMMOptions();
Event.domReady.add(function(){
  dd.get_cassette($('rollermm_sample').value, $('top_cassette').value);
  $('top_cassette').observe('change', function(){
    //alert('Sorry, the cassettes are out of stock until the 24th of June');
    //$('top_cassette').selectedIndex = 0;
    dd.get_cassette($('rollermm_sample').value, $('top_cassette').value);
  });
});
function check_topcassette_submit() {
  if ( ($('top_cassette').value == 3) && ($('cassette_color').value == 0)) {
    alert('Please select a Contrasting fabric Cassette Colour');
    return false;
  }
  return true;
}

