/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only thing you need to change in this file is the following
variables: checkboxHeight, radioHeight and selectWidth.

Replace the first two numbers with the height of the checkbox and
radio button. The actual height of both the checkbox and radio
images should be 4 times the height of these two variables. The
selectWidth value should be the width of your select list image.

You may need to adjust your images a bit if there is a slight
vertical movement during the different stages of the button
activation.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "25";
var radioHeight = "25";
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; }</style>');


var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				//span[a].className = inputs[a].type + inputs[a].value;
				var tooltip = new String(inputs[a].getAttribute("tooltip"));
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				
				if (tooltip=="null") { tooltip=""; }
				
				switch (inputs[a].name) {
				case "img":
				    span[a].innerHTML = "<img src='prodimages/" + inputs[a].getAttribute("img") + "' />";
				    break;
				case "color":
				    if (tooltip.length>0) {
				        span[a].innerHTML = "<a href='#' onclick='return false;' class=tooltip title='" + tooltip + "'><img src='swatches/" + inputs[a].getAttribute("swatch") + "' /></a>";
				    } else {
				        span[a].innerHTML = "<img src='swatches/" + inputs[a].getAttribute("swatch") + "' />";
				    }
				    break;
				default:
				    span[a].innerHTML = inputs[a].value;
				    break;
				}

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						span[a].className="selected";
						switch (span[a].nextSibling.name) {
			            case "size":
			                span[a].className="selected";
			                break;
                        case "color":
                        case "img":
                            span[a].className="selected";
                            break;
                        }
					} else {
						//position = "0 -" + (radioHeight*2) + "px";
						//span[a].style.backgroundPosition = position;
						
						switch (inputs[a].name) {
						case "color":
						case "img":
						    span[a].className="selected";
						    break;
					    default:
						    span[a].className="selected";
						    if (inputs[a].getAttribute("prodPrice")==inputs[a].getAttribute("regPrice")) {
						        document.getElementById("prodPrice").innerHTML="<h1>Price: $" + inputs[a].getAttribute("prodPrice") + "</h1>";
						    } else {
						        document.getElementById("prodPrice").innerHTML="<h1>Price: $" + inputs[a].getAttribute("prodPrice") + "</h1><span style='font-size:12px;'>Regular Price $" + inputs[a].getAttribute("regPrice") + "</span>";
						    }
						    document.prodForm.price.value=inputs[a].getAttribute("prodPrice");  
						    break;
						} 
					}
				}
				inputs[a].onchange = Custom.clear;
				span[a].onmousedown = Custom.pushed;
				span[a].onmouseup = Custom.check;
				document.onmouseup = Custom.clear;
				switch (inputs[a].name) {
                case "size":
				    span[a].onmouseover = function () {if(this.style.backgroundColor=="") {this.style.backgroundColor="gray"};}
				    span[a].onmouseout = function () {if(this.style.backgroundColor=="gray") {this.style.backgroundColor=""}}
				    break;
			    case "color":
			    case "img":
			        span[a].onmouseover = function () {if(this.style.border.indexOf("313131")==-1) {this.style.border="1px solid gray"};/*this.style.padding="1px"*/;}
				    span[a].onmouseout = function () {if(this.style.border.indexOf("gray")>-1) {this.style.border="1px solid #313131"};/*this.style.padding="2px"*/;}
			        break;
			    }
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.choose;
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.className="";
		} else if(element.checked == true && element.type == "radio") {
			//this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
			switch (element.name) {
			case "size":
			    this.className="";
			    //document.getElementById("prodPrice").innerHTML="<h1>Price: $" + element.getAttribute("prodPrice") + "</h1>";
			    if (element.getAttribute("prodPrice")==element.getAttribute("regPrice")) {
		            document.getElementById("prodPrice").innerHTML="<h1>Price: $" + element.getAttribute("prodPrice") + "</h1>";
		        } else {
		            document.getElementById("prodPrice").innerHTML="<h1>Price: $" + element.getAttribute("prodPrice") + "</h1><span style='font-size:12px;'>Regular Price $" + element.getAttribute("regPrice") + "</span>";
		        }
			    document.prodForm.price.value=element.getAttribute("prodPrice");
			    break;
			case "color":
			case "img":
			    this.className="";
			    break;
			default:
			    this.className="";
			    break;
			}
		} else if(element.checked != true && element.type == "checkbox") {
			this.className="pushed";
		} else {
			//this.style.backgroundPosition = "0 -" + radioHeight + "px";
			switch (element.name) {
			case "size":
			    this.className="";
			    //document.getElementById("prodPrice").innerHTML="<h1>Price: $" + element.getAttribute("prodPrice") + "</h1>";
			    if (element.getAttribute("prodPrice")==element.getAttribute("regPrice")) {
			        document.getElementById("prodPrice").innerHTML="<h1>Price: $" + element.getAttribute("prodPrice") + "</h1>";
			    } else {
			        document.getElementById("prodPrice").innerHTML="<h1>Price: $" + element.getAttribute("prodPrice") + "</h1><span style='font-size:12px;'>Regular Price $" + element.getAttribute("regPrice") + "</span>";
			    }
			    document.prodForm.price.value=element.getAttribute("prodPrice");
			    break;
			case "color":
			case "img":
			    this.className="";
			    break;
			default:
			    this.className="";
			    break;
			}
		}
		
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.className="";
			element.checked = false;
			if (element.type=="checkbox" && element.getAttribute("table")!="") {
			    switch (element.getAttribute('table')) {
			    case 't_Cat':
			        updateCatImg (element);
			        break;
			    case 't_Prod_Img':
			        updateProdImg (element);
			        break;
			    case 't_Prod_Color':
			        updateProdColor (element);
			        break;
			    case 't_Prod_Size':
			        updateProdSize (element);
			        break;
			    }
			}
		} else {
		    element.checked = true;
			if(element.type == "checkbox") {
				this.className="selected";
				if (element.getAttribute("table")!="") {
				    switch (element.getAttribute('table')) {
				    case 't_Cat':
				        updateCatImg (element);
				        this.className="selected";
				        break;
				    case 't_Prod_Img':
				        updateProdImg (element);
				        this.className="selected";
				        break;
				    case 't_Prod_Color':
			            updateProdColor (element);
			            this.className="selected";
			            break;
			        case 't_Prod_Size':
			            updateProdSize (element);
			            this.className="selected";
			            break;
			        }
				};
				//if (inputs[a].name=="color" || inputs[a].name=="img") {inputs[a].previousSibling.style.border="1px solid gray";/*inputs[a].previousSibling.style.padding="2px"*/;};
				if (this.name=="color" || this.name=="img") {this.className="";};
			} else {
				this.className="selected";
				group = this.nextSibling.name;
				//inputs = document.getElementsByTagName("input");
				inputs = document.getElementsByName(group);
				for(a = 0; a < inputs.length; a++) {
					//if(inputs[a].name == group && inputs[a] != this.nextSibling) {
					if (inputs[a].type=="radio" && inputs[a] != this.nextSibling) {
						switch (inputs[a].name) {
						case "size":
						    inputs[a].previousSibling.className=""
						    break;
					    case "color":
					    case "img":
					        inputs[a].previousSibling.className="";
					        break;
					    }
					}
				}
			}
			//element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				switch (inputs[b].name) {
				case "size":
				    inputs[b].previousSibling.className="selected";
				    break;
				case "cat":
				    inputs[b].previousSibling.className="selected";
				    break;
                case "color":
                    inputs[b].previousSibling.className="selected";
                    break;
                case "img":
                    inputs[b].previousSibling.className="selected";
                    break;
                }
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				switch (inputs[b].name) {
				case "size":
				    inputs[b].previousSibling.className="";
				    break;
                case "color":
                case "img":
                    inputs[b].previousSibling.className="";
                    break;
                }
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				switch (inputs[b].name) {
				case "size":
				    inputs[b].previousSibling.className="selected";
				    break;
                case "color":
                case "img":
                    inputs[b].previousSibling.className="selected";
                    break;
                }
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				//inputs[b].previousSibling.style.backgroundPosition = "0 0";
				switch (inputs[b].name) {
				case "size":
				    inputs[b].previousSibling.className="";
				    break;
                case "color":
                case "img":
                    inputs[b].previousSibling.className="";
                    break;
                }
			}
		}
	},
	choose: function() {
		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;
			}
		}
	}
}

window.onload = Custom.init;