/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only things you may need to change in this file are the following
variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)

The numbers you set for checkboxHeight and radioHeight should be one quarter
of the total height of the image want to use for checkboxes and radio
buttons. Both images should contain the four stages of both inputs stacked
on top of each other in this order: unchecked, unchecked-clicked, checked,
checked-clicked.

You may need to adjust your images a bit if there is a slight vertical
movement during the different stages of the button activation.

The value of selectWidth should be the width of your select list image.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "15";
var radioHeight = "15";
var selectWidth = "190";


/* No need to change anything after this */


document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		
		$('input.styled').each(function(a, element) {
			if (element.type == "checkbox" || element.type == "radio") {
				span[a] = document.createElement("span");
				span[a].className = element.type;
				
				if (element.checked == true) {
					if(element.type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				
				element.parentNode.insertBefore(span[a], element);
				element.onchange = Custom.clear;
				if (!element.getAttribute("disabled")) {
					$(span[a]).bind('mousedown', Custom.pushed);
					$(span[a]).bind('mouseup', Custom.check);
					$(span[a]).bind('disable', Custom.disable);
				} else {
					$(span[a]).bind('enable', Custom.enable);
					
					$(span[a]).addClass("disabled");
					span[a].style.backgroundPosition = "0 -" + checkboxHeight*4 + "px";
				}
			}
		});
		
		$('select.styled').each(function(a, element) {
			for (b = 0; b < element.options.length; b++) {
				if (b==0 || element.options[b].selected == true) {
					textnode = document.createTextNode($(element.options[b]).text());
				}
			}
			
			span[a] = document.createElement("span");
			span[a].className = "select";
			span[a].id = "select" + element.name;
			span[a].appendChild(textnode);
			element.parentNode.insertBefore(span[a], element);
			if(!element.getAttribute("disabled")) {
				element.onchange = Custom.choose;
			} else {
				element.previousSibling.className = element.previousSibling.className += " disabled";
			}
		});
		
		document.onmouseup = Custom.clear;
	},
	disable: function() {
		this.style.backgroundPosition = "0 -" + checkboxHeight*4 + "px";
		$(this).addClass("disabled");		
		$(this).unbind('mousedown');
		$(this).unbind('mouseup');
		$(this).unbind('disable');
		$(this).bind('enable', Custom.enable);
		
		var element = this.nextSibling;
		element.checked == false;
		$(element).attr('disabled','disabled');
	},
	enable: function() {
		this.style.backgroundPosition = "0 0";
		$(this).removeClass("disabled");		
		$(this).bind('mousedown', Custom.pushed);
		$(this).bind('mouseup', Custom.check);
		$(this).bind('disable', Custom.disable);
		$(this).unbind('enable');
		
		var element = this.nextSibling;
		$(element).removeAttr('disabled');
	},
	pushed: function() {
		var element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		var element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				var group = this.nextSibling.name;
				var inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
/*---------------------------------------------------------*/
/*	KN - obsługa formularza w koszyku
/*---------------------------------------------------------*/	
						if (group == 'payment_type' || group == 'delivery_type') {
							$(inputs[a].previousSibling).trigger('enable');
						} else {
/*---------------------------------------------------------*/
							inputs[a].previousSibling.style.backgroundPosition = "0 0";
						}
					}
				}
			}
/*---------------------------------------------------------*/
/*	KN - obsługa formularza rejestracji / edycji danych
/*---------------------------------------------------------*/	
			if (element.name == 'suser_type') {
				refreshForm(element);
			}
/*---------------------------------------------------------*/
/*	KN - obsługa formularza kontaktowego
/*---------------------------------------------------------*/	
			if (element.name == 'FORMnew_1_kontakt') {
				$('#FORMnew_1_placowka').hide();
				$('#FORMnew_1_sklep').hide();
				if (element.value == 6) {			// placowki
					$('#FORMnew_1_placowka').show();
				} else if (element.value == 7) {	// sklepy
					$('#FORMnew_1_sklep').show();
				}
			}
/*---------------------------------------------------------*/
			element.checked = true;
		}
/*---------------------------------------------------------*/
/*	KN - obsługa formularza w koszyku
/*---------------------------------------------------------*/	
		if (element.name == 'payment_type' || element.name == 'delivery_type') {
			recalcDevPrice();			
		}
/*---------------------------------------------------------*/
	},
	clear: function() {
/*---------------------------------------------------------*/
/*	KN - obsługa formularza w koszyku
/*---------------------------------------------------------*/	
		if (this.name == 'payment_type' || this.name == 'delivery_type') {
			recalcDevPrice();
		}
/*---------------------------------------------------------*/
		var inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if (!inputs[b].getAttribute("disabled")) {
				if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
					inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
				} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
					inputs[b].previousSibling.style.backgroundPosition = "0 0";
				} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
					inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
					inputs[b].previousSibling.style.backgroundPosition = "0 0";
				}
			}
		}
	},
	choose: function() {
		var option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
	//----------------------- KN ----------------------- 
				// wybor z listy szybkich linkow
				if (this.id == 'rejs_quick_links') {
					window.location = option[d].value;
				}
				
				// wybor z listy wojewodztw - odswiezenie listy powiatow
				if (this.id == 'region_select_chooser') {
					var listPrompt = option[0].text;
					
					$('#subregion_select_chooser').val(0);
					$('#subregion_select_chooser').attr('disabled',true);

					$('#selectsubregion_select_chooser').addClass('disabled');
					$('#selectsubregion_select_chooser').html(listPrompt);
					
					$.getJSON("index.php?ctr=AJAX&mod=Addresses&action=getSubregions",
								{ 'regionid': this.value },
								function(data) {
									$('#subregion_select_chooser').html('<option value="">'+listPrompt+'</option>');
									$.each(data, function(i,item){
										$('<option value="'+item.ID+'">'+item.WARTOSC+'</option>').appendTo('#subregion_select_chooser');
									});
									$('#subregion_select_chooser').removeAttr('disabled');
									$('#selectsubregion_select_chooser').removeClass('disabled');
								});					
				}
				
				// wybor z listy kategorii produktowych - odswiezenie listy produktow
				if (this.id == 'productcategory_select_chooser') {
				
					var listPrompt = $('#product_select_chooser > option:first').html();
					
					$('#product_select_chooser').val(0);
					$('#product_select_chooser').attr('disabled',true);

					$('#selectproduct_select_chooser').addClass('disabled');
					$('#selectproduct_select_chooser').html(listPrompt);
					
					$.getJSON("index.php?ctr=AJAX&mod=Products&action=getProducts",
								{ 'prdcid': this.value },
								function(data) {
									$('#product_select_chooser').html('<option value="">'+listPrompt+'</option>');
									$.each(data, function(i,item){
										$('<option value="'+item.ID+'">'+item.WARTOSC+'</option>').appendTo('#product_select_chooser');
									});
									if (data.length>0) {
										$('#product_select_chooser').removeAttr('disabled');
										$('#selectproduct_select_chooser').removeClass('disabled');
									}
								});					
				}
				
	//----------------------- /KN ---------------------- 
				break;
			}
		}
		
	}
}
$(document).ready(function() {
	Custom.init();
});
