// JavaScript Document
//These are very specific Javascript functions meant for very specific tasks.
//So don't be surprised if they give errors.

//Detect Browser Document Width, this is for positioning of the content when viewed in netbooks, 4:3 or widescreen monitors
function getBrowserWidth()
{
	var myWidth = 0;

	if( typeof( window.innerWidth ) == 'number' ) //Non-IE
		myWidth = window.innerWidth;
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) //IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) //IE 4 compatible
		myWidth = document.body.clientWidth;

	return myWidth;
}

function one_size_fits_all()
{
	var width = getBrowserWidth();

	var IE6 = (navigator.appVersion.indexOf("MSIE 6.")==-1) ? false : true;
	var IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;

	//product display page
	if ((document.getElementById("pdt_big_img"))&&(document.getElementById("pdt_desc_div")))
	{
		var pdt_big_img = document.getElementById("pdt_big_img");
		var pdt_desc_div = document.getElementById("pdt_desc_div");
		var pdt_desc_text = document.getElementById("pdt_desc_text");

		var pdt_options_cart = document.getElementById("pdt_options_cart");

		if (document.getElementById("also_purchased"))
			var also_purchased = document.getElementById("also_purchased");

		var desc_width = (width-890);

		if (width > 1250)
		{//set float:left for pdt_big_image and pdt_desc_div
			pdt_big_img.style.cssFloat = "left";
			pdt_desc_div.style.cssFloat = "left";
			pdt_big_img.style.styleFloat = "left";
			pdt_desc_div.style.styleFloat = "left";

			pdt_desc_div.style.marginLeft = "10px";

			pdt_big_img.style.width = "450px";
			pdt_desc_div.style.width = desc_width+"px";

			pdt_desc_text.style.width = (desc_width > 500) ? "500px" : "";

			if ((desc_width - 500 - 80) > 200) //right gap more than 200 pixels wide
			{
				pdt_options_cart.style.cssFloat = "right";
				pdt_options_cart.style.styleFloat = "right";
				pdt_options_cart.style.marginTop = "-"+pdt_desc_text.offsetHeight+"px";
				pdt_options_cart.style.width = (desc_width - 500 - 80 )+"px";
			}
			else
			{
				pdt_options_cart.style.cssFloat = "";
				pdt_options_cart.style.styleFloat = "";
				pdt_options_cart.style.marginTop = "";
				pdt_options_cart.style.width = "";
			}

			if (document.getElementById("also_purchased"))
			{
				if (pdt_big_img.offsetHeight > pdt_desc_div.offsetHeight)
					also_purchased.style.marginTop = pdt_big_img.offsetHeight+30+"px";
				else
					also_purchased.style.marginTop = pdt_desc_div.offsetHeight+30+"px";

				var big_img = pdt_big_img.getElementsByTagName("img")
				var big_img_height = big_img[0].height;

				//get highest also_purchased box height
				var also_height = 0;

				for (i=12;i>0;i--)
				{
					if (document.getElementById("pdt_small_"+i))
					{
						var temp_height = document.getElementById("pdt_small_"+i).offsetHeight;
						if (temp_height > also_height)
							also_height = temp_height;
					}
				}


				//combined height of also purchased and description against the height of the product image
				var height_diff = pdt_desc_div.offsetHeight + also_height + 40 + 20 - pdt_big_img.offsetHeight; //20 is for margin bottom of the pdt_desc div

				if (height_diff < 80) //description and also_purchased is shorter than pdt image
				{
					also_purchased.style.marginTop = "";

					also_purchased.style.cssFloat = "left";
					also_purchased.style.styleFloat = "left";

					also_purchased.style.marginLeft = "10px";
					also_purchased.style.width = desc_width+"px";
				}
				else
				{//give an insane value to the margin-top of the also_purchased to push it down away from the big image and description
					if (pdt_desc_div.offsetHeight > pdt_big_img.offsetHeight) //the amount to push the also_purchased down is determined by whether pdt_image or desc is taller.
						var big_margin = pdt_desc_div.offsetHeight;
					else
						var big_margin = pdt_big_img.offsetHeight;

					also_purchased.style.marginTop = big_margin+20+"px";

					also_purchased.style.cssFloat = "none";
					also_purchased.style.styleFloat = "none";

					also_purchased.style.marginLeft = "";
					also_purchased.style.width = "";
				}
			}
			
		}
		else
		{//set the float values back to none.
			pdt_big_img.style.cssFloat = "none";
			pdt_desc_div.style.cssFloat = "none";
			pdt_big_img.style.styleFloat = "none";
			pdt_desc_div.style.styleFloat = "none";

			pdt_desc_div.style.marginLeft = "0px";

			pdt_big_img.style.width = "";
			pdt_desc_div.style.width = "";

			pdt_desc_text.style.width = "";

			pdt_options_cart.style.width = '';

			if (document.getElementById("also_purchased"))
			{
				also_purchased.style.cssFloat = "none";
				also_purchased.style.styleFloat = "none";

				also_purchased.style.marginLeft = "";
				also_purchased.style.width = "";

				also_purchased.style.marginTop = '';
			}

			pdt_options_cart.style.cssFloat = "";
			pdt_options_cart.style.styleFloat = "";
			pdt_options_cart.style.marginTop = "";
			pdt_options_cart.style.width = "";
		}

		if (document.getElementById("also_purchased")&&document.getElementById("pdt_small_1"))
		{
			var ap_width = also_purchased.offsetWidth;
			var ap1_width = document.getElementById("pdt_small_1").offsetWidth;
			var ap_to_display = Math.floor(ap_width / ap1_width);

			for (i=12;i>ap_to_display;i--)
			{
				if (document.getElementById("pdt_small_"+i))
				{
					document.getElementById("pdt_small_"+i).style.visibility = "hidden";
					document.getElementById("pdt_small_"+i).style.position = "absolute";
				}
			}
	
			for (i=ap_to_display;i>1;i--)
			{
				if (document.getElementById("pdt_small_"+i))
				{
					document.getElementById("pdt_small_"+i).style.visibility = "visible";
					document.getElementById("pdt_small_"+i).style.position = "relative";
				}
			}
		}
	}

	//new pvc figures
	if ((document.getElementById("pvc_content"))&&(document.getElementById("pvc_1")))
	{
		var pvc_box = document.getElementById("pvc_content");
		var pvc_one = document.getElementById("pvc_1");

		var thumb_width = pvc_one.offsetWidth;

		if ((IE6 == true)||(IE7 == true))
		{
			thumb_width = thumb_width+6; //add in the margin value
		}

		var per_line = Math.floor(pvc_box.offsetWidth/thumb_width);

		var pad = Math.floor((pvc_box.offsetWidth - per_line*thumb_width)/2);
		pad = pad -1;

		pvc_box.style.padding = "0px "+pad+ "px 0px " +pad+ "px";
//		pvc_box.style.padding = "0px "+pad+ "px 0px 0px";
	}

	//new arivals - middle of page
	if ((document.getElementById("rl_thumbs"))&&(document.getElementById("rl_1")))
	{
		var showall = document.getElementById("rl_showall"); //the SHOW ALL thumbnail

		var rl_box = document.getElementById("rl_thumbs");
		var rl_one = document.getElementById("rl_1");

		var thumb_width = rl_one.offsetWidth;

		if ((IE6 == true)||(IE7 == true))
		{
			thumb_width = thumb_width+6; //add in the margin value
		}

		var per_line = Math.floor(rl_box.offsetWidth/thumb_width);

		var pad = Math.floor((rl_box.offsetWidth - per_line*thumb_width)/2);
		pad = pad -1;

		rl_box.style.padding = "0px "+pad+ "px 0px " +pad+ "px";
//		rl_box.style.padding = "0px "+pad+ "px 0px 0px";

		//now we get rid of the stragglers
		var max_thumbs = per_line*2;

		for (i=28;i>max_thumbs;i--)
		{
			if (document.getElementById("rl_"+i))
			{
				var thumb = document.getElementById("rl_"+i);
				thumb.style.position = "absolute";
				thumb.style.visibility = "hidden";
			}
		}

		for (i=1;i<=max_thumbs;i++)
		{
			if (document.getElementById("rl_"+i))
			{
				var thumb = document.getElementById("rl_"+i);
				thumb.style.position = "relative";
				thumb.style.visibility = "visible";
			}
		}

		showall.style.visibility = "visible";

		//overlay last thumbnail with "SHOW ALL >>" icon
		var thumb = document.getElementById("rl_"+max_thumbs);
		rl_box.insertBefore(showall, thumb);

	}

	//new products - bottom of index page
	if ((document.getElementById("new_products"))&&(document.getElementById("new_pdt_1")))
	{
		var showall = document.getElementById("new_pdt_showall"); //the SHOW ALL thumbnail

		var new_pdt = document.getElementById("new_pdt_content");
		var pdt_one = document.getElementById("new_pdt_1");

		var thumb_width = pdt_one.offsetWidth;

		if ((IE6 == true)||(IE7 == true))
		{
			thumb_width = thumb_width+6; //add in the margin value
		}

		var per_line = Math.floor(new_pdt.offsetWidth/thumb_width);

		var pad = Math.floor((new_pdt.offsetWidth - per_line*thumb_width)/2);
		pad = pad -1;

		new_pdt.style.padding = "0px "+pad+ "px 0px " +pad+ "px";
//		new_pdt.style.padding = "0px "+pad+ "px 0px 0px";

		//now we get rid of the stragglers
		var max_thumbs = per_line*2;

		for (i=28;i>max_thumbs;i--)
		{
			if (document.getElementById("new_pdt_"+i))
			{
				var thumb = document.getElementById("new_pdt_"+i);
				thumb.style.position = "absolute";
				thumb.style.visibility = "hidden";
			}
		}

		for (i=1;i<=max_thumbs;i++)
		{
			if (document.getElementById("new_pdt_"+i))
			{
				var thumb = document.getElementById("new_pdt_"+i);
				thumb.style.position = "relative";
				thumb.style.visibility = "visible";
			}
		}

		showall.style.visibility = "visible";

		//overlay last thumbnail with "SHOW ALL >>" icon
		var thumb = document.getElementById("new_pdt_"+max_thumbs);
		new_pdt.insertBefore(showall, thumb);

	}
}

function check_pdt_opt_div()
{
	if (document.getElementById("pdt_options_div"))
	{
		var div = document.getElementById("pdt_options_div");
		var selects = div.getElementsByTagName("select");

		var num_of_select = selects.length;

		for (i=0;i<num_of_select;i++)
		{
			var sel_index = selects[i].selectedIndex;
			var sel_text = selects[i].options[sel_index].text;
	
			if ((sel_text.substring(0, 13).toLowerCase() == 'please select')||(selects[i].value == ''))
			{
				alert('Please Specify a Size!');
				return false;
			}
		}
	}

	return true;
}

function tip_hide(container)
{
	var index = document.getElementById(container);
	var f_layer = document.getElementById("tooltip");
	if(f_layer)
		index.removeChild(f_layer);
}

function tip_disp(e, self, container)
{
	var index = document.getElementById(container);

	var offsety = 20;
	var width = 180;//width of tooltip

	var tooltip = document.createElement("div");
	tooltip.setAttribute("id", "tooltip");

	self.onmousemove = function(e)
	{
		if (!e) var e = window.event;
		if (e.pageX || e.pageY)
		{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{
			if (document.documentElement)
			{
				posx = e.clientX + document.documentElement.scrollLeft;
				posy = e.clientY + document.documentElement.scrollTop;
			}
			else if (document.body)
			{
				posx = e.clientX + document.body.scrollLeft;
				posy = e.clientY + document.body.scrollTop;
			}
		}

		var f_layer = document.getElementById("tooltip");
		if (f_layer)
			index.removeChild(f_layer);

		if (!tooltip.hasChildNodes())
		{
			var description = document.createElement("div");
			description.innerHTML = self.getAttribute('alt');
			tooltip.appendChild(description);
		}

		index.appendChild(tooltip);

		if (self.innerWidth) // all except Explorer
			var doc_width = self.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth)	// Explorer 6 Strict Mode
			var doc_width = document.documentElement.clientWidth;
		else if (document.body) // other Explorers
			var doc_width = document.body.clientWidth;

		if (document.documentElement)
			var tip_align = ((doc_width - posx  + document.documentElement.scrollLeft)/posx)+width-20;
		else if (document.body)
			var tip_align = ((doc_width - posx  + document.body.scrollLeft)/posx)+width-20;

		tooltip.style.width = width+"px";
		tooltip.style.left = (posx+tip_align-width-80)+"px";

		tooltip.style.top = offsety+posy+"px";
	}//end onmousemove
}

function showhide_layer(layer_id)
{
	function setOpacity(value, layer)
	{
		layer.style.opacity = value/10;
		layer.style.filter = 'alpha(opacity=' + value*10 + ')';
	}

	var layer = document.getElementById(layer_id);
	var vid_hid = (layer.style.visibility == "visible") ? "hidden" : "visible";
	if (vid_hid == "visible")//fadein
	{
		setTimeout(function(){setOpacity(0, layer)}, 0);
		setTimeout(function(){layer.style.visibility = vid_hid}, 0);
		setTimeout(function(){setOpacity(1, layer)}, 10);
		setTimeout(function(){setOpacity(2, layer)}, 20);
		setTimeout(function(){setOpacity(3, layer)}, 30);
		setTimeout(function(){setOpacity(4, layer)}, 40);
		setTimeout(function(){setOpacity(5, layer)}, 50);
		setTimeout(function(){setOpacity(6, layer)}, 60);
		setTimeout(function(){setOpacity(7, layer)}, 70);
		setTimeout(function(){setOpacity(8, layer)}, 80);
		setTimeout(function(){setOpacity(9, layer)}, 90);
		setTimeout(function(){setOpacity(10, layer)}, 100);
	}
	else if (vid_hid == "hidden")//fadeout
	{
		setTimeout(function(){setOpacity(10, layer)}, 0);
		setTimeout(function(){setOpacity(9, layer)}, 10);
		setTimeout(function(){setOpacity(8, layer)}, 20);
		setTimeout(function(){setOpacity(7, layer)}, 30);
		setTimeout(function(){setOpacity(6, layer)}, 40);
		setTimeout(function(){setOpacity(5, layer)}, 50);
		setTimeout(function(){setOpacity(4, layer)}, 60);
		setTimeout(function(){setOpacity(3, layer)}, 70);
		setTimeout(function(){setOpacity(2, layer)}, 80);
		setTimeout(function(){setOpacity(1, layer)}, 90);
		setTimeout(function(){setOpacity(0, layer)}, 100);
		setTimeout(function(){layer.style.visibility = vid_hid}, 100);
	}
}

function check_quick_find()
{
	var quick_find = document.getElementById("quick_find");
	if (quick_find.value == '')
	{
		alert('Please specify a keyword in the search box!');
		return false;
	}
}

function check_contact()
{
	var name = document.getElementById("contact_name");
	var email = document.getElementById("contact_email");
	var enquiry = document.getElementById("contact_enquiry");

	if ((name.value == '')||(email.value == '')||(enquiry.value == ''))
	{
		alert('One or more required fields is empty!');
		return false;
	}
}

function check_login()
{
	var email = document.getElementById("login_email");
	var password = document.getElementById("login_password");

	if ((email.value == '')||(password.value == ''))
	{
		alert('Your email or password is empty!');
		return false;
	}
}

function show_cat_info(e)
{
	//hide any stragglers
	var name = document.getElementById("categories");
	var spans = name.getElementsByTagName("span");
	for (i=0;i<spans.length;i++)	
	{
		if (spans[i].className == 'cat_full_text')
			if (spans[i].style.visibility == 'visible')
				spans[i].style.visibility = "hidden";
	}

	var full_text = e.parentNode.getElementsByTagName("span");
	full_text[0].style.visibility = "visible";
	full_text[0].style.zIndex = "10";
}

function hide_cat_info(e)
{
//	var full_text = e.parentNode.getElementsByTagName("span");
	e.style.visibility = "hidden";
	e.style.zIndex = "5";
}

function adult_alert(adult_status)
{
	if (adult_status == '1')
		return true;
	else
	{
		alert('Before you can view products in this section, please verify that you are over 18 with our staff members.\n\n If your age has already been verified, please log into the site.');
		return false;
	}
}

function init()
{
	//check if the page has been resized
	//there IS a difference. Notice one of them has brackets and the other does not.

	window.onresize = one_size_fits_all;
	one_size_fits_all();

	if (document.getElementById("kknm_fb"))
		FB.init("31ed68e42d07f2c3ff1fc7fee45bdc37");
}