loadRandomHeader();
loadRotatingBanner();
loadFashionWheel();
startPopulatingGarments();
startPopulatingBrands();
startPopulatingStyles();
startPopulatingStores();

function loadFashionWheel() {
	var gender = "all";
	var clearance = "all";
	switch(location.hash) {
		case "#women":
			gender = "woman";
		break;
		case "#men":
			gender = "man";
		break;
		case "#clearance":
			clearance = "yes";
		break;
	}
	fashionWheelFilter = {
		gender: gender,
		garment: "all",
		brand: "all",
		style: "all",
		clearance: clearance,
		generateUrl: function() {
			return "/services/outfits.xml.php" +
				"?gender=" + this.gender +
				"&type=" + escape(this.garment) +
				"&brand=" + escape(this.brand) +
				"&style=" + escape(this.style) +
				"&clearence=" + this.clearance;
		},
		reset: function() {
			this.gender = "all";
			this.garment = "all";
			this.brand = "all";
			this.style = "all";
			this.clearance = "all";
		}
	};
	
	swfobject.embedSWF(
		"includes/wheel.swf",
		"fashionWheelDiv",
		"620",
		"470",
		"9.0.0",
		"includes/expressInstall.swf",
		{xml_string: fashionWheelFilter.generateUrl()},
		{allowscriptaccess: "always"},
		{
			id: "fashionWheel",
			name: "fashionWheel"
		}
	);
}

function startPopulatingGarments() {
	if(window.brands === undefined) {
		var xhr = createServiceXHR("GET", "garments");
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(xhr.status == 200) {
					garments = JSON.parse(xhr.responseText);
					var sel = document.getElementById("garmentSelect");
					var opt = sel.childNodes[1];
					sel.removeChild(opt);
					for(var i = 0; i < garments.length; i++) {
						var garment = garments[i];
						var newOpt = opt.cloneNode(true);
						newOpt.firstChild.nodeValue = garment.type;
						newOpt.payload = garment;
						sel.appendChild(newOpt);
					}
				} else {
					alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
				}					
			}
		};
		xhr.send();
	}
}

function startPopulatingBrands() {
	if(window.brands === undefined) {
		var xhr = createServiceXHR("GET", "brands");
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(xhr.status == 200) {
					brands = JSON.parse(xhr.responseText);
					var sel = document.getElementById("brandSelect");
					var opt = sel.childNodes[1];
					sel.removeChild(opt);
					for(var i = 0; i < brands.length; i++) {
						var brand = brands[i];
						var newOpt = opt.cloneNode(true);
						newOpt.firstChild.nodeValue = brand.brand;
						newOpt.payload = brand;
						sel.appendChild(newOpt);
					}
				} else {
					alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
				}					
			}
		};
		xhr.send();
	}
}

function startPopulatingStyles() {
	if(window.styles === undefined) {
		var xhr = createServiceXHR("GET", "styles");
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(xhr.status == 200) {
					styles = JSON.parse(xhr.responseText);
					var sel = document.getElementById("styleSelect");
					var opt = sel.childNodes[1];
					sel.removeChild(opt);
					for(var i = 0; i < styles.length; i++) {
						var style = styles[i];
						var newOpt = opt.cloneNode(true);
						newOpt.firstChild.nodeValue = style.style;
						newOpt.payload = style;
						sel.appendChild(newOpt);
					}
				} else {
					alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
				}					
			}
		};
		xhr.send();
	}
}

function startPopulatingStores() {
	if(window.stores === undefined) {
		var xhr = createServiceXHR("GET", "locations");
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(xhr.status == 200) {
					stores = JSON.parse(xhr.responseText);
					var sel = document.getElementById("store");
					var opt = sel.firstChild;
					sel.removeChild(opt);
					for(var i = 0; i < stores.length; i++) {
						var newOpt = opt.cloneNode(true);
						newOpt.name = stores[i].store;
						newOpt.firstChild.nodeValue = stores[i].store;
						sel.appendChild(newOpt);
					}
				} else {
					alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
				}					
			}
		};
		xhr.send();
	}
}

document.getElementById("send").onclick = function() {
	var inq = {
		type: "availability",
		name: document.getElementById("name").value,
		email: document.getElementById("email").value,
		phone: document.getElementById("phone").value,
		contactBy: document.getElementById("contactByPhone").checked? "phone": document.getElementById("contactByPhone").checked? "email": null,
		newsletter: document.getElementById("newsletter").checked,
		appointment: document.getElementById("appointment").checked,
		garment1Type: currentOutfit[0].type,
		garment1: currentOutfit[0].product_id,
		garment2Type: (currentOutfit.length == 2 && currentOutfit[1].product_id != null)? currentOutfit[1].type: null,
		garment2: (currentOutfit.length == 2 && currentOutfit[1].product_id != null)? currentOutfit[1].product_id: null,
		bottoms: document.getElementById("bottoms").checked,
		tops: document.getElementById("tops").checked,
		jackets: document.getElementById("jackets").checked,
		outfit: document.getElementById("outfit").checked,
		bottomSize: document.getElementById("bottomSize").value,
		topSize: document.getElementById("topSize").value,
		jacketSize: document.getElementById("jacketSize").value,
		message: document.getElementById("message").value,
		store: document.getElementById("store").value
	};
	var errors = "";
	if(inq.name == "") {
		errors += "- A name is required.\n";
	}
	if(inq.email == "") {
		errors += "- An e-mail address is required.\n";
	}
	if(errors != "") {
		alert("Please correct the following problems before sending your inquiry:\n\n" + errors);
	} else {
		var data = JSON.stringify(inq);
		var xhr = createServiceXHR("POST", "inquiry");
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(xhr.status == 201) {
					var returnedInq = JSON.parse(xhr.responseText);
					
					alert(
						"Your inquiry has been received. Thank you.\n\nInquiry Details:\n" +
						"Name: " + returnedInq.name + "\n" +
						"Email: " + returnedInq.email + "\n" +
						"Store: " + returnedInq.store + "\n" +
						"Inquiry Type: " + returnedInq.type + "\n" +
						"Date Received: " + returnedInq.dateSubmitted + "\n");
					/*
					alert(
						returnedInq.id + " " +
						returnedInq.type + " " +
						returnedInq.name + " " +
						returnedInq.email + " " +
						returnedInq.phone + " " +
						returnedInq.contactBy + " " +
						returnedInq.newsletter + " " +
						returnedInq.appointment + " " +
						returnedInq.garment1Type + " " +
						returnedInq.garment1 + " " +
						returnedInq.garment2Type + " " +
						returnedInq.garment2 + " " +
						returnedInq.bottoms + " " +
						returnedInq.tops + " " +
						returnedInq.jackets + " " +
						returnedInq.outfit + " " +
						returnedInq.bottomSize + " " +
						returnedInq.topSize + " " +
						returnedInq.jacketSize + " " +
						returnedInq.message + " " +
						returnedInq.store + " " +
						returnedInq.dateSubmitted
					);
					*/
					document.getElementById("formContainer").style.visibility = "hidden";
					document.getElementById("blackVeil").className = "blackVeilHidden";
				} else {
					alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
				}					
			}
		};
		xhr.send(data);
	}
}
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function filter() {
	document.getElementById("fashionWheel").SwitchXML(fashionWheelFilter.generateUrl());
}

var currentOutfit;
function getAvailability(outfit) {
	document.getElementById("blackVeil").className = "blackVeil";
	scroll(0, 0);
	document.getElementById("formContainer").style.visibility = "visible";
	
	var xhr = createServiceXHR("GET", "outfit", "id=" + outfit);
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {
			if(xhr.status == 200) {
				currentOutfit = JSON.parse(xhr.responseText);
				document.getElementById("outfitItem1TypeEl").firstChild.nodeValue = currentOutfit[0].type;
				document.getElementById("outfitItem1BrandEl").firstChild.nodeValue = currentOutfit[0].brand;
				document.getElementById("outfitItem2TypeEl").firstChild.nodeValue =  (currentOutfit.length == 2 && currentOutfit[1].product_id != null)? currentOutfit[1].type: "N/A";
				document.getElementById("outfitItem2BrandEl").firstChild.nodeValue = (currentOutfit.length == 2 && currentOutfit[1].product_id != null)? currentOutfit[1].brand: "N/A";
			} else {
				alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
			}					
		}
	};
	xhr.send();
}

function buyOnline(outfit, garment) {
	var xhr = createServiceXHR("GET", "outfit", "id=" + outfit);
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {
			if(xhr.status == 200) {
				currentOutfit = JSON.parse(xhr.responseText);
				var win = window.open("http://shop.jeanmachine.com/SearchResults.asp?Search=" + currentOutfit[(garment - 1)].productString, "checkBalance","width=1050,height=700,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes,resizable=no");
			} else {
				alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
			}					
		}
	};
	xhr.send();
}

document.getElementById("checkBalanceLeft").onclick = function() {
	openCheckBalance();
};

document.getElementById("closeButton").onclick = function() {
	document.getElementById("formContainer").style.visibility = "hidden";
	document.getElementById("blackVeil").className = "blackVeilHidden";
};


document.getElementById("womenMenuItem").onclick = function() {
	fashionWheelFilter.reset();
	fashionWheelFilter.gender = "woman";
	filter();
};

document.getElementById("menMenuItem").onclick = function() {
	fashionWheelFilter.reset();
	fashionWheelFilter.gender = "man";
	filter();
};

document.getElementById("seeWomen").onclick = function() {
	document.getElementById("genderSelect").selectedIndex = 1;
	document.getElementById("garmentSelect").selectedIndex = 0;
	document.getElementById("brandSelect").selectedIndex = 0;
	document.getElementById("styleSelect").selectedIndex = 0;
	fashionWheelFilter.reset();
	fashionWheelFilter.gender = "woman";
	filter();
};

document.getElementById("seeMen").onclick = function() {
	document.getElementById("genderSelect").selectedIndex = 2;
	document.getElementById("garmentSelect").selectedIndex = 0;
	document.getElementById("brandSelect").selectedIndex = 0;
	document.getElementById("styleSelect").selectedIndex = 0;
	fashionWheelFilter.reset();
	fashionWheelFilter.gender = "man";
	filter();
};

document.getElementById("seeClearance").onclick = function() {
	document.getElementById("genderSelect").selectedIndex = 0;
	document.getElementById("garmentSelect").selectedIndex = 0;
	document.getElementById("brandSelect").selectedIndex = 0;
	document.getElementById("styleSelect").selectedIndex = 0;
	fashionWheelFilter.reset();
	fashionWheelFilter.clearance = "yes";
	filter();
};

document.getElementById("genderSelect").onchange = function() {
	var gender = this.options[this.selectedIndex].value;
	fashionWheelFilter.gender = gender;
	filter();
};

document.getElementById("garmentSelect").onchange = function() {
	var garment = this.options[this.selectedIndex].payload;
	fashionWheelFilter.garment = garment != undefined? garment.type: "all";
	filter();
};

document.getElementById("brandSelect").onchange = function() {
	var brand = this.options[this.selectedIndex].payload;
	fashionWheelFilter.brand = brand != undefined? brand.brand: "all";
	filter();
};

document.getElementById("styleSelect").onchange = function() {
	var style = this.options[this.selectedIndex].payload;
	fashionWheelFilter.style = style != undefined? style.style: "all";
	filter();
};

document.getElementById("resetButton").onclick = function() {
	document.getElementById("genderSelect").selectedIndex = 0;
	document.getElementById("garmentSelect").selectedIndex = 0;
	document.getElementById("brandSelect").selectedIndex = 0;
	document.getElementById("styleSelect").selectedIndex = 0;
	fashionWheelFilter.reset();
	filter();
};

loadGA();
