/*********************************************************************
(c) Copyright Comet Plc 1999-2007. All rights reserved.
	Last updated: 2009/07/21 - Stewart Orr
/********************************************************************/

Object.prototype.nextObject = function() {
	var n = this;
	do n = n.nextSibling;
	while (n && n.nodeType != 1);
	return n;
}
 
Object.prototype.previousObject = function() {
	var p = this;
	do p = p.previousSibling;
	while (p && p.nodeType != 1);
	return p;
}

function createLayoutOptions() {
	var pagination_summary = document.getElementById("pagination_summary");
	if (pagination_summary) {
		var ourspan = document.createElement("span");
		
		ourspan.innerHTML = '<a id="List_view_a" class="btn_list" href="javascript:setActiveStyleSheet(\'List view\');" title="Change product listing to a list view"><span>List view</span></a> <a  id="Grid_view_a" class="btn_grid" href="javascript:setActiveStyleSheet(\'Grid view\');" title="Change product listing to a grid view"><span>Grid view</span></a>';
		pagination_summary.insertBefore(ourspan, pagination_summary.firstChild);
		var title = getActiveStyleSheet();
		var colViewLink = gID(title.replace(" ", "_") + "_a");
		if (colViewLink) {
			colViewLink.className += " btn_selected";
		}
	}
	addRollOverToBtns();
}

function setActiveStyleSheet(titlea) {
	title=titlea;
	var i, a, main;
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			// disable them all...
			a.disabled = true;
			// is this the current col? if so, expand it
			if (a.getAttribute("title") == title) {
				a.disabled = false;

				// set this link to say less etc
				if (gID("List_view_a") && gID("Grid_view_a")) {
					gID("List_view_a").className = gID("List_view_a").className.replace(" btn_selected", "");
					gID("Grid_view_a").className = gID("Grid_view_a").className.replace(" btn_selected", "");
				}
				
				var colViewLink = gID(title.replace(" ", "_") + "_a");
				if (colViewLink) {
					colViewLink.className += " btn_selected";
				}
			}
		}
	}
	checkColHeight();
}

function getActiveStyleSheet() {
	var i, a;
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return null;
}

function getPreferredStyleSheet() {
	var i, a;
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (a.getAttribute("title") == "List view") return a.getAttribute("title");
	}
	return null;
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}
function deleteStyleCookie() {
	createCookie("prodlistingstyle", title, -1000);
}
function styleonload(e) {
	var cookie = readCookie("prodlistingstyle");
	var title = (cookie!="null" && cookie!=null && cookie!="undefined") ? cookie: "List view";
	setActiveStyleSheet(title);
}

window.onunload = function(e) {
	var title = getActiveStyleSheet();
	createCookie("prodlistingstyle", title, 365);
}

var cookie = readCookie("prodlistingstyle");
var title = cookie ? cookie: getPreferredStyleSheet();

function checkColHeight() {
	try {
		row = gID("product_listing");
	
		if (row) {
			cols = row.getElementsByTagName("li");
			for (var i = 0; i < cols.length; i++) {
				if (cols[i].parentNode == row) {				
					var thisli, leftli, rightli, leftdiv, rightdiv;
					thisli = cols[i];
					if (thisli.className.indexOf("left") > -1) {
						leftli = thisli;
						leftli.getElementsByTagName("div")[1].style.minHeight = "0";
					} else if (thisli.className.indexOf("right") > -1) {
						rightli = thisli;
						rightli.getElementsByTagName("div")[1].style.minHeight = "0";
						if (title == "Grid view") {
							var padding = (document.all) ? 0 : 1;
							if (leftli.clientHeight>cols[i].clientHeight) {
								rightli.getElementsByTagName("div")[1].style.minHeight = (leftli.getElementsByTagName("div")[1].clientHeight + padding) + "px";
							} else {
								leftli.getElementsByTagName("div")[1].style.minHeight = (rightli.getElementsByTagName("div")[1].clientHeight + padding) + "px";
							}
						}
					}
				}
			}
		}
	}
	catch(e) {alert(e);}
}

// Once page has loaded, do these:
addLoadEvent(createLayoutOptions);
//addLoadEvent(styleonload);
styleonload();
window.onresize = checkColHeight;
addLoadEvent(checkColHeight);