

var Mayflex = {
	configure: function(){
		new SmoothScroll();
		
		var section = 0;
		var repsect = -1;
		
		if($("products")){
			$("products").getElements("ul").each(function(el, index){
				if(el.hasClass("open")){
					section = index;
				}
			});
		}

		if($("replist")){
			$("replist").getElements("div").each(function(el, index){
				if(el.hasClass("open")){
					repsect = index;
				}
			});
		}

		new Accordion($$("#products li span"), $$("#products li ul"), { "alwaysHide":true, "show":section });
		new Accordion($$("#faqlist li strong"), $$("#faqlist li div"));	
		new Accordion($$("#serviceslist li strong"), $$("#serviceslist li div"));	
		new Accordion($$("#accountpage li strong"), $$("#accountpage li div.memberentry"));	
		new Accordion($$("#replist li strong"), $$("#replist li div"), { "alwaysHide":true, "show":repsect });
		new Accordion($$("#literaturelist div h2"), $$("#literaturelist div ul"));	
		new Accordion($$(".addNote"), $$(".note"), { "alwaysHide":true, "show":-1 });

		$$(".volatile").each(function(el){
			$(el).addEvent("focus", function(){
				if(this.getValue() == this.getProperty("title")) {
					this.value = "";
				}
			});
		});
		
		$$("a[rel*='external']").each(function(a){ 
			$(a).setProperty("target", "_blank");
		});
	
		$$("input.textfieldcolourpicker").each(function(el){
			var v = el.getValue();
			if(v.length == 0) v = "#ffffff";
			el.value = v;
			new MooRainbow('sxColourPicker_' + el.id, {
				'id':'moo' + el.id,
				'imgPath':'/_images/mooRainbow/',
				'startColor': new Color(v),
				'onChange': function(color) {
					el.value = color.hex;
					el.fireEvent("change");
				}
			});
		});
	},
	
	formCheckboxSelected: function(formID){
		var returnVal = false;
		if($(formID)){
			$(formID).getElements("input[type=checkbox]").each(function(cb){
				if(cb.getValue()){
					returnVal = true;
				}
			});
		}
		return returnVal;
	},
	
	attachProductOptionEvents: function(){
		$$(".productoption_row").each(function(row){
			var currency = row.getElement(".productoption_currency").getText();
			var price = row.getElement("input").getValue();
			var subTotal = row.getElement(".productoption_subtotal");
			var qtyEl = row.getElement("input[type=text]");
			
			var qtyStock = row.getElement(".StockLevel");
			
			if(qtyEl){
				qtyEl.addEvent("change", function(){
					if(currency == "€")
						subTotal.setHTML((this.getValue() * price).numberFormat(2, ",", ".") + currency);
					else
						subTotal.setHTML(currency + (this.getValue() * price).numberFormat(2));
					
					var val = this.getValue().toInt();
					
					if(isNaN(val) || val < 1) this.value = 1;
					
					//alert(this.getValue());
					//alert(qtyStock.getValue());
					if(parseInt(this.getValue()) > parseInt(qtyStock.getValue())){
						row.getElement(".tick").setStyle("display", "none");
						row.getElement(".telephone").setStyle("display", "inline");
					} else {
						row.getElement(".telephone").setStyle("display", "none");
						row.getElement(".tick").setStyle("display", "inline");
					}
					
				});
			}
		});
	}
};

window.addEvent("domready", function(){

	Mayflex.configure();

	if($("planninglist_form")){
		$("addtobasket_fromplanninglist").addEvent("click", function(e){
            new Event(e).stop();
            
            if(Mayflex.formCheckboxSelected("planninglist_form")) {
				new Element("input", {"name":"AddToBasket", "type":"hidden", "value":"x=0,y=0"}).injectInside($("planninglist_form"));
				$("planninglist_form").submit();
			} else {
				alert("You must select at least one product before adding.");
			}
		});
	}

	if($("addtoplanning_frombasket")) {
    	$("addtoplanning_frombasket").addEvent("click", function(e){
            new Event(e).stop();

			if(Mayflex.formCheckboxSelected("basketform")) {
				$("basketform").action = "/planning-lists/addto/";
				$("basketform").submit();
			} else {
				alert("You must select at least one product before adding to the planning list.");
			}
    	});
    }
	
	if($("options_purchase_form")) {
		$("options_purchase_form").addEvent("submit", function(e){
			if(!Mayflex.formCheckboxSelected("options_purchase_form")) {
				new Event(e).stop();
				alert("You must select at least one product before adding.");
			}
		});

		Mayflex.attachProductOptionEvents();
	}
	
	if($("options_filter_form")){
		new Element("input",{
			"type":"hidden",
			"name":"ajax_filter",
			"value":"true"
		}).injectInside("options_filter_form");
		
		$("option_filter_submit").setStyles({
			"display":"none",
			"visibility":"hidden"
		});
		
		$("options_filter_form").getElements("select").each(function(sb){
			sb.addEvent("change", function(){
				new Ajax(window.location.href,{
					"method": "post",
					"data": $("options_filter_form"),
					"update": $("options_purchase_form"),
					"onComplete": function(){
						Mayflex.attachProductOptionEvents();
					},
					"onFailure": function(){
						$("options_purchase_form").setHTML("<p>Unable to retrieve filtered results, please try again</p>");
					}
				}).request();
			});
		});		
	}
});



