//
// Title: product_catalogue.js
//
// This document is the copyright and property of Tim Mathias.
// It must not be copied (in whole or in part) used for manufacture or
// otherwise disclosed without prior written consent. Any copies of this
// document made by any method must also include a copy of this Legend,
// © Tim Mathias 2005. All rights reserved.
//
// **************************
// *     Change History     *
// **************************
//
// Version  Author          Date      Description
// -------  --------------  --------  -----------------------------------------
//       1  Tim Mathias     23-12-04  Original.
//
//       2  Tim Mathias     08-06-05  Put scripts for Product Catalogue into
//                                    a seperate file (i.e. this one).
//
//                                    Prepended function names with PC_.
//
//                                    Fixed multiple downloads of images using
//                                    style.display and style.visibility
//                                    instead of changing img.src.
//
//                                    Added Follow functionality to Cat Nav Bar.
//
//       3  Tim Mathias     17-12-05  Added cross-browser functionality.
//
// ****************************************************************************
//
// 1.0     Introduction
// --------------------
//
//   Configure catalogue navigation bar and
//   help the shopper with their shopping chores.
//
// 2.0     Program Listing
// -----------------------

//
// Product Catalogue Navigation Bar
//

try
{
	attachEvent("onload", PC_DoLoad);
}
catch (ex)
{
	try
	{
		addEventListener("load", PC_DoLoad, false);
	}
	catch (ex)
	{
		window.onload = PC_DoLoad;
	}
}

function PC_DoLoad()
{
	try
	{
		attachEvent("onunload", PC_DoUnload);
		attachEvent("onresize", PC_DoResize);
	}
	catch (ex)
	{
		try
		{
			addEventListener("unload", PC_DoUnload, false);
			addEventListener("resize", PC_DoResize, false);
		}
		catch (ex)
		{
			window.onunload = PC_DoUnload;
			window.onresize = PC_DoResize;
		}
	}
	try
	{
		if (document.getElementById("div_cat_nav_bar_shadow").firstChild)
		{
			attachEvent("onscroll", PC_DoScroll);
		}
	}
	catch (drop) { }

	PC_InitCatalogueNavBar(1);
	scrollposx = GetCookie("wnd_jaja_product_catalogue_scrollposx");
	scrollposy = GetCookie("wnd_jaja_product_catalogue_scrollposy");
	if (scrollposx != null && scrollposy != null)
	{
		window.scrollTo(scrollposx, scrollposy);
		//alert ("Product Catalogue onload scrolled to " + scrollposx + "," + scrollposy);
	}
	g_cat_nav_bar_width = document.getElementById("div_cat_nav_bar").offsetWidth;
	PC_UpdateCatNavBar();
	PC_InitCatNavBarImgs();
}

function PC_DoUnload()
{
	SetCookie("wnd_jaja_product_catalogue_scrollposx", document.documentElement.scrollLeft);
	SetCookie("wnd_jaja_product_catalogue_scrollposy", document.documentElement.scrollTop);
	//alert ("Product Catalogue unload scrollposx,scrollposy " + scrollposx + "," + scrollposy);
}

function PC_DoResize()
{
	//alert ("PC_DoResize () BEGIN");
	PC_UpdateCatNavBar();
}

var g_i_scrolls = 0;
var g_a_selects = document.getElementsByTagName("SELECT");

function PC_DoScroll()
{
	return;

	if (g_b_follow_cat_nav_bar == "nonfollow")
	{
		return;
	}

	g_i_scrolls++;
	if (g_i_scrolls == 1)
	{
		for (i = 0; i < g_a_selects.length; i++)
		{
			g_a_selects[i].style.visibility = "hidden";
		}
	}
	setTimeout(PC_ShowSelects, 100);
}

function PC_ShowSelects()
{
	g_i_scrolls--;
	if (g_i_scrolls < 1)
	{
		for (i = 0; i < g_a_selects.length; i++)
		{
			g_a_selects[i].style.visibility = "visible";
		}
		g_i_scrolls = 0;
	}
}

window.setTimeout(PC_UpdateCatNavBar, 20);

var g_cat_nav_bar_width = 0;
var g_cat_nav_bar_min_y = 0;
var g_b_follow_cat_nav_bar = GetCookie("jaja_g_b_follow_cat_nav_bar");
if (g_b_follow_cat_nav_bar != "nonfollow" && g_b_follow_cat_nav_bar != "follow")
{
	g_b_follow_cat_nav_bar = "nonfollow";
	SetCookie("jaja_g_b_follow_cat_nav_bar", g_b_follow_cat_nav_bar);
}

function PC_InitCatalogueNavBar(dir)
{
	// Define the category for each button
	var n = 0;
	var a_categories = new Array();
	a_categories[n++] = "All";
	a_categories[n++] = "Body-Warmers";
	a_categories[n++] = "Boiler-Suits";
	a_categories[n++] = "Casualwear";
	a_categories[n++] = "Gloves";
	a_categories[n++] = "High-Visibility-Safety-Clothing";
	a_categories[n++] = "Jackets";
	a_categories[n++] = "Safety-Boots";
	a_categories[n++] = "Shirts";
	a_categories[n++] = "Sweatshirts";
	a_categories[n++] = "Trousers";
	a_categories[n++] = "Vests-Waistcoats";

	// Initialise cell attributes
	n = 0;
	obj = document.getElementById("nav_bar_catalogue_cell_" + n);
	while (obj)
	{
		obj.category = a_categories[n];
		obj.img_n = n;
		obj.onmouseover = PC_CatNavBtnOver;
		obj.onmouseout = PC_CatNavBtnOut;
		obj.onclick = PC_CatNavBtnClick;
		obj = document.getElementById("nav_bar_catalogue_cell_" + ++n);
	}

	var o_nav_bar_cat = document.getElementById("div_cat_nav_bar");
	if (o_nav_bar_cat.firstChild.tagName == "IFRAME")
	{
		o_nav_bar_cat.firstChild.style.width = o_nav_bar_cat.offsetWidth + "px";
		o_nav_bar_cat.firstChild.style.height = o_nav_bar_cat.offsetHeight + "px";
	}

	var o_shadow = document.getElementById("div_cat_nav_bar_shadow");
	if (o_shadow.firstChild && o_shadow.firstChild.tagName == "IFRAME")
	{
		o_shadow.firstChild.style.width = o_nav_bar_cat.offsetWidth + "px";
		o_shadow.firstChild.style.height = o_nav_bar_cat.offsetHeight + "px";
	}
}

function PC_SetFollow(obj)
{
	if (obj.checked)
	{
		g_b_follow_cat_nav_bar = "follow";
	}
	else
	{
		g_b_follow_cat_nav_bar = "nonfollow";
	}
	SetCookie("jaja_g_b_follow_cat_nav_bar", g_b_follow_cat_nav_bar);
	PC_UpdateCatNavBar();
}

function PC_UpdateCatNavBar()
{
	var td_cat_nav_bar = document.getElementById("td_cat_nav_bar");
	var o_nav_bar_cat = document.getElementById("div_cat_nav_bar");
	var o_shadow = document.getElementById("div_cat_nav_bar_shadow");
	var o_ipf = document.getElementById("ip_follow");

	if (!(td_cat_nav_bar && o_nav_bar_cat && o_shadow && o_ipf))
	{
		return;
	}

	var new_x = (document.documentElement.offsetWidth - o_nav_bar_cat.offsetWidth) / 2.0;
	if (new_x < 0)
	{
		new_x = 0;
	}

	g_cat_nav_bar_min_y = 0;
	var obj = td_cat_nav_bar;
	do
	{
		g_cat_nav_bar_min_y += obj.offsetTop;
	} while ((obj = obj.offsetParent));

	if (g_b_follow_cat_nav_bar == "follow")
	{
		new_y = document.documentElement.scrollTop;
		if (new_y < g_cat_nav_bar_min_y)
		{
			new_y = g_cat_nav_bar_min_y;
		}

		if (o_nav_bar_cat.style.left != new_x)
		{
			o_nav_bar_cat.style.left = new_x + "px";
		}

		if (o_nav_bar_cat.style.top != new_y)
		{
			o_nav_bar_cat.style.top = new_y + "px";
		}

		if (o_nav_bar_cat.firstChild.tagName == "IFRAME")
		{
			o_nav_bar_cat.firstChild.style.width = o_nav_bar_cat.offsetWidth + "px";
			o_nav_bar_cat.firstChild.style.height = o_nav_bar_cat.offsetHeight + "px";
		}

		if (o_ipf.checked != true)
		{
			o_ipf.checked = true;
		}

		o_shadow.style.left = o_nav_bar_cat.offsetLeft + 10 + "px";
		o_shadow.style.top = o_nav_bar_cat.offsetTop + 10 + "px";
		o_shadow.style.width = o_nav_bar_cat.offsetWidth + "px";
		o_shadow.style.height = o_nav_bar_cat.offsetHeight + "px";
		o_shadow.style.visibility = "visible";

		if (o_shadow.firstChild && o_shadow.firstChild.tagName == "IFRAME")
		{
			o_shadow.firstChild.style.width = o_nav_bar_cat.offsetWidth + "px";
			o_shadow.firstChild.style.height = o_nav_bar_cat.offsetHeight + "px";
		}
	}
	else
	{
		if (o_nav_bar_cat.style.left != new_x)
		{
			o_nav_bar_cat.style.left = new_x + "px";
		}
		if (o_nav_bar_cat.style.top != g_cat_nav_bar_min_y)
		{
			o_nav_bar_cat.style.top = g_cat_nav_bar_min_y + "px";
		}

		if (o_ipf.checked != false)
		{
			o_ipf.checked = false;
		}

		o_shadow.style.visibility = "hidden";
	}
	if (o_nav_bar_cat.style.visibility != "visible")
	{
		o_nav_bar_cat.style.visibility = "visible";
	}

	td_cat_nav_bar.style.height = o_nav_bar_cat.clientHeight + 10 + "px";

	//alert ("PC_UpdateCatNavBar () END");

	//alert (g_cat_nav_bar_min_y);

	window.setTimeout(PC_UpdateCatNavBar, 20);
}

function PC_InitCatNavBarImgs()
{
	// Define images for image window
	var n = 0;
	document.getElementById("img_cat_" + n++).src = "images/products/all.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/bw225.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/bsa065.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/swr238.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/gl612.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/jhvj200.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/jwj003.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/btj210.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/spw196.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/swr238.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/tje212.jpg";
	document.getElementById("img_cat_" + n++).src = "images/products/whv008.jpg";
	window.setTimeout(PC_ShowCatNavBarImgs, 250);
}

function PC_ShowCatNavBarImgs()
{
	var n = 0;
	var obj = document.getElementById("img_cat_" + n++);
	while (obj)
	{
		obj.style.visibility = "visible";
		obj = document.getElementById("img_cat_" + n++);
	}
}

function PC_CatNavBtnOver()
{
	this.className = "btn-hover";
	this.style.cursor = "pointer";
	document.getElementById("img_cat_" + this.img_n).style.display = "";
}

function PC_CatNavBtnOut()
{
	this.className = "btn";
	document.getElementById("img_cat_" + this.img_n).style.display = "none";
}

function PC_CatNavBtnClick()
{
	location.replace("product_catalogue.asp?category=" + this.category);
}

function PC_SendQuery(query)
{
	location.replace(query);
}

function PC_OrderByOver(obj)
{
	obj.className = obj.className.replace("btn_green_normal", "btn_green_over");
	obj.style.cursor = "pointer";
	window.status = "Click to sort by " + obj.innerText + ". Click repeatedly to toggle ascending/descending order.";
}

function PC_OrderByOut(obj)
{
	obj.className = obj.className.replace("btn_green_over", "btn_green_normal");
	obj.style.cursor = "";
	window.status = "";
}

function PC_OrderByClick(s_order_by)
{
	PC_OrderBy(s_order_by);
}

function PC_OrderBy(s_order_by)
{
	if (s_order_by == str_order_by)
	{
		str_direction = str_direction == "ASC" ? "DESC" : "ASC";
	}
	else
	{
		str_direction = "ASC";
	}

	var query = "product_catalogue.asp?category=" + str_category + "&orderby=" + s_order_by + "&direction=" + str_direction;
	PC_SendQuery(query);
}

function PC_PicOver(obj)
{
	obj.className = "pic_over";
	obj.style.cursor = "pointer";
	window.status = "Click to view item in Shop Window.";
}

function PC_PicOut(obj)
{
	obj.className = "pic_normal";
	obj.style.cursor = "";
	window.status = "";
}

function PC_PicClick(product_code)
{
	OpenShopWindow(product_code);
}

function PC_AddOver(obj)
{
	obj.className = "btn_orange_over";
	obj.style.cursor = "pointer";
	window.status = "Click to add this item to shopping basket.";
}

function PC_AddOut(obj)
{
	obj.className = "btn_orange_normal";
	obj.style.cursor = "";
	window.status = "";
}

function PC_AddClick(str_id)
{
	PC_AddToBasket(str_id);
}

var wi;
var wi_obj;
var wi_item;
var wi_status;

function PC_AddToBasket(str_id)
{
	var temp;
	var query = "action=ADD";
	query += "&product_code=" + str_id;
	temp = document.getElementById(str_id + "_sizes");
	if (temp)
	{
		query += "&size=" + temp.selectedIndex;
	}
	else
	{
		query += "&size=0";
	}
	temp = document.getElementById(str_id + "_colours");
	if (temp)
	{
		query += "&colour=" + temp.selectedIndex;
	}
	else
	{
		query += "&colour=0";
	}
	temp = document.getElementById(str_id + "_quantity");
	if (temp)
	{
		query += "&quantity=" + temp.value;
	}
	else
	{
		query += "&quantity=1";
	}
	//alert (query);
	SetCookie("b_wnd_jaja_shopping_basket_loaded", 0);
	if (GetCookie("b_wnd_jaja_shopping_basket_loaded") != null)
	{
		wi_obj = document.getElementById(str_id + "_addbtn");
		wi_item = str_id;
		wi_status = "Adding item " + wi_item + "to shopping basket. Please wait...";
		window.status = wi_status;
		wi_obj.style.cursor_old = wi_obj.style.cursor;
		wi_obj.style.cursor = "wait";
		document.documentElement.style.cursor = "wait";
		window.clearInterval(wi);
		wi = window.setInterval(PC_IntervalShoppingBasketLoaded, 1000);
	}
	OpenShoppingBasket(query);
}

function PC_IntervalShoppingBasketLoaded()
{
	window.status = wi_status;
	if (GetCookie("b_wnd_jaja_shopping_basket_loaded") == 1)
	{
		window.clearInterval(wi);
		window.status = "Done.";
		wi_obj.style.cursor = wi_obj.style.cursor_old;
		document.documentElement.style.cursor = "";
	}
}
