﻿//hoover.com

String.prototype.trim = function(){
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

String.prototype.format = function(){
	var s = this,
        i = arguments.length;

	while (i--){
		s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
	}
	return s;
}

//show or hide panels based on tabs
function activatePanel(clicked) {
	var tabs = new Array("details", "parts", "fits", "specs", "manuals", "reviews");
	var hash = (clicked != undefined ? clicked.replace("tab_", "") : (window.location.hash != "" ? window.location.hash.replace("#", "") : tabs[0]));
	hash = hash.substring(hash.indexOf("#"), hash.length).replace("#", "");

	for (var i = 0; i < tabs.length; i++) {
		var panel = document.getElementById("panel_" + tabs[i]);
		var tab = document.getElementById("tab_" + tabs[i]);

		if (panel != null && tab != null) {
			panel.style.display = (hash != tabs[i] ? "none" : "");
			tab.className = (hash != tabs[i] ? "" : "active");
		}
	}

	//prevent hash from displaying in url
	return false;
}

//allow only one radio button checked when nested in repeater
function distinctRadio(nameregex, current) {
	re = new RegExp(nameregex);

	for (i = 0; i < document.forms[1].elements.length; i++) {
		elm = document.forms[1].elements[i]

		if (elm.type == 'radio') {
			if (re.test(elm.name)) {
				elm.checked = false;
			}
		}
	}
	current.checked = true;
}

//main site search validation
function siteSearch() {
	return document.getElementById("q").value.replace(/ /g,"") != "" ? true : false;
}

//set search text box and select
function setSearch(searchTerm, searchType) {
	if(searchTerm != ""){
		document.getElementById("q").value = searchTerm;
	}

	if(searchType != ""){
		document.getElementById("t").value = searchType;
	}
}

//set compare item
function setCompare(chk) {
	var xmlhttp = new XMLHttpRequest();

	var url = "/products/compare/?action={0}&itemid={1}&timestamp={2}".format((chk.checked ? "add" : "remove"), chk.value, new Date().getTime());

	with(xmlhttp){
		open("GET", url, true);
		onreadystatechange = function(){
			if(xmlhttp.readyState == 4){
				if(xmlhttp.responseText != "") {
					chk.checked = false;
					if(confirm(xmlhttp.responseText)){
						window.location = "/products/compare/";
					};
				}
			}
		}
		send(null);
	}
}

//update item quantity
function updateQuantity(index, qty) {
	var a = document.getElementById("update" + index);
	var href = a.href.substr(0, a.href.indexOf("&quantity="));
	a.href = href + "&quantity=" + qty;
}

function collapse(sender) {
	sender.className = sender.className.indexOf("collapsible") >= 0 ? sender.className.replace("collapsible", "expandable") : sender.className.replace("expandable", "collapsible");

	//change text
	var a = sender.getElementsByTagName("a");
	//a[0].innerHTML = a[0].innerHTML == "Collapse" ? "Expand" : "Collapse";

	//change icon
	var icon = sender.getElementsByTagName("span");
	icon[0].className = icon[0].className == "icon collapse image_text" ? "icon expand image_text" : "icon collapse image_text";

	return false;
}

//update image index on image gallery
function updateImage(intIndex) {
	if (index >= 0 && intIndex < count) {
		with (document.getElementById("imgLarge")) {
			src = document.getElementsByName("aGalleryThumb")[intIndex].href;
			width = images[intIndex][0];
			height = images[intIndex][1];
			alt = images[intIndex][2];
		}

		var a = document.getElementsByName("aGalleryThumb")
		for (var i = 0; i < a.length; i++) {
			a[i].className = (i == intIndex) ? "selected" : "";
		}

		//move nav list
		if (count > 4) {
			var ul = document.getElementById("gallery_thumbnails");

			if (intIndex < 3) {
				ul.style.top = 0;
			} else if (intIndex >= (count - 1) || index == (count - 1)) {
				//don't move
			} else if (intIndex > index) {
				ul.style.top = (-85 * (intIndex - 2)) + "px";
			} else if (intIndex < index) {
				ul.style.top = (-85 * (intIndex - 1)) + "px";
			}
			/*
			if ((intIndex < 3 && intIndex % 3 == 0) || (intIndex >= 3)) {
				document.getElementById("gallery_thumbnails").style.top = (-170) + "px";
			}
			*/
		}

		//set new index
		index = intIndex;

		document.getElementById("counter").innerHTML = index + 1;

		//set button state
		var prev = document.getElementById("prev").getElementsByTagName("a")[0];
		var next = document.getElementById("next").getElementsByTagName("a")[0];
		if (intIndex <= 0) {
			prev.className += " prev_disabled";
			next.className = next.className.replace(" next_disabled", "");
		} else if (intIndex + 1 >= count) {
			next.className += " next_disabled";
			prev.className = prev.className.replace(" prev_disabled", "");
		} else {
			prev.className = prev.className.replace(" prev_disabled", "");
			next.className = next.className.replace(" next_disabled", "");
		}
	}
}

//bubble
function createBubble(sender, content) {
	if(document.getElementById("bubble") == null){
		var container = document.createElement("div");	//need separate container for css3 pie effects to work
		container.id = "bubble_container";
		container.style.position = "absolute";
		var bubble = document.createElement("div");
		bubble.id = "bubble";
		bubble.className = "bubble rounded_full"
		bubble.innerHTML = document.getElementById(content).innerHTML;

		//exit if no savings
		if (bubble.innerHTML.trim() == "") {
			return;
		}

		container.appendChild(bubble);

		//document.body.appendChild(bubble);
		sender.parentNode.insertBefore(container, sender.parentNode.firstChild);

		container.style.left = 0;
		container.style.top = -bubble.offsetHeight - 10 + "px";
	}else{
		//document.body.removeChild(document.getElementById("bubble"));
		sender.parentNode.removeChild(document.getElementById("bubble_container"));
	}
}

function swapCheckBox(sender) {
	var c = document.getElementById("partSearchList").getElementsByTagName("input");

	if(sender.value == "All"){
		for(var i=0; i<c.length; i++){
			if(c[i].value != sender.value){
				c[i].checked = false;
			}
		}

		sender.checked = true;
	}else{
	var checked = false;
		for(var i=0; i<c.length; i++){
			if(c[i].value != "All"){
				if(c[i].checked){
					checked = true;
					break;
				}
			}
		}

		c[0].checked = !checked;
	}
}

function autoSizeFrame() {
	if (parent.location != location) {
		parent.document.getElementById("panel_reviews").getElementsByTagName("iframe")[0].style.height = document.getElementById("modal").offsetHeight + 10 + "px";
	}
}

//create a modal window
function createModal(url, heading, width, height) {
	if (document.getElementById("modal_window") == null) {
		//layer doesn't exist
		var modal = document.createElement("div");
		modal.className = "shadow";

		//external domain
		var internalUrl = /(https?:\/\/)(hoover|beta\.hoover\.com|hoover\.com)/ig;
		var isExternal = !internalUrl.test(url);

		//close button
		var close = document.createElement("a");
		close.className = "icon close image_text";
		close.innerHTML = "Close";
		close.onclick = function () {
			document.body.removeChild(document.getElementById("modal_window"));
			document.body.removeChild(document.getElementById("modal_overlay"));
		}

		//viewport
		vWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		vHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

		width = width != undefined ? width : (960 / 1.5);
		height = height != undefined ? height : 400; //document.body.offsetHeight / 1.25 + 36

		//external urls--can't change default margin / padding of body
		width = isExternal ? width + 16 : width;
		height = isExternal ? height + 16 : height;

		//check if modal dimensions > viewport (40px padding width (account for scroll bars) and 20px padding height)
		width = width > vWidth ? vWidth - 40 : width;
		height = height > vHeight ? vHeight - 20 : height;

		//account for url with hash
		var hash = "";
		if(url.indexOf("#") >= 0){
			hash = url.substring(url.indexOf("#"));
			url = url.replace(hash, "");
		}

		//iframe
		var iframe = document.createElement("iframe");
		with (iframe) {
			frameBorder = "0";
			src = url + (url.indexOf("?") > 0 ? "&master=modal" : "?master=modal") + hash;
		}
		iframe.height = height - 36;

		//heading
		var h = document.createElement("h1");
		h.innerHTML = heading != undefined ? heading : "";
		modal.appendChild(h);

		modal.appendChild(iframe);
		modal.appendChild(close);

		document.body.appendChild(modal);

		modal.id = "modal_window";
		iframe.id = "ilayer_content";

		var top = ((document.documentElement.clientHeight - height) / 2); //fixed position
		with (modal) {
			style.width = width + "px";
			style.height = height + "px";
			style.left = (document.body.offsetWidth - modal.offsetWidth) / 2 + "px";
			style.top = (top < 0 ? 0 : top) + "px";
		}

		//transparent overlay
		var overlay = document.createElement("div");
		with (overlay) {
			id = "modal_overlay";
			style.mozOpacity = "0.35";
			style.opacity = "0.35";
			style.filter = "alpha(opacity=35)";
		}
		document.body.appendChild(overlay);
	} else {
		//layer exists
		var iframe = document.getElementById("ilayer_content");
		if (iframe != null && iframe.src.indexOf(url) < 0 && url != undefined) {
			//account for url with hash
			var hash = "";
			if (url.indexOf("#") >= 0) {
				hash = url.substring(url.indexOf("#"));
				url = url.replace(hash, "");
			}
			iframe.src = url + (url.indexOf("?") > 0 ? "&master=modal" : "?master=modal") + hash;
		} else {
			document.body.removeChild(document.getElementById("modal_window"));
			document.body.removeChild(document.getElementById("modal_overlay"));
		}
	}

	return false;
}

//resize modal on window resize
/*
window.onresize = function () {
	var modal = document.getElementById("modal_window");
	if (modal != null) {
		//viewport
		vWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		vHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

		width = modal.offsetWidth;
		height = modal.offsetHeight;

		//check if modal dimensions > viewport (40px padding width (account for scroll bars) and 20px padding height)
		width = width > vWidth ? vWidth - 40 : width;
		height = height > vHeight ? vHeight - 20 : height;

		var top = ((document.documentElement.clientHeight - height) / 2); //fixed position
		with (modal) {
			style.width = width + "px";
			style.height = height + "px";
			style.left = (document.body.offsetWidth - modal.offsetWidth) / 2 + "px";
			style.top = (top < 0 ? 0 : top) + "px";
		}
	}
}
*/
