var plist = null;

$(function() {
	$("#details a").lightBox();
	$("#diagrams a").lightBox();
	$("#product-select").click(init_plist);
	make_toc();
});

function make_toc() {
	$(".toc-anchor")
	.each(function() {
		var name = jQuery(this).attr("id");
		$("<li/>")
		.append(
			$("<a/>")
			.attr("href", "#"+name)
			.html("&raquo; "+name)
		)
		.appendTo("#toc");
	});
}

function init_plist() {
	
	/**
	 * just redirect if IE6, it can't support the plist placement at this time.
	 * /
	if(jQuery.browser.msie && jQuery.browser.version=="6.0")
		window.location = "/products.php";
	*/

	init_plist_container();
	
	if(plist == null) {
		$.ajax({
			type:"get",
			url:"tpl/plist.php",
			data:{columns:1},
			dataType:"html",
			success:function( response, status ) {
				plist = response;
				attach_plist();
			}
		});
		return false;
	} else {
		attach_plist();
	}
}

function attach_plist() {
	$("#plist-wrapper").append(plist);
	//$("#plist").hover( function(){}, remove_plist );
	
	//plist_distribute_columns();
	
	$(".plist-products li").click(function() {});
	
	// prevent link from navigating away
	return false;
}

function remove_plist(){ 
	$("#plist-wrapper").remove(); 
	$("#jquery-overlay").remove();
}

function plist_distribute_columns() {
	var lc = jQuery( "#plist-left" );
	var rc = jQuery( "#plist-right" );
	
	ul = function() { return jQuery( "#plist-left .plist-series:last" ); };
	
	var max = jQuery( "#plist-left ul" ).length;
	var i = 0;
	
	while( ( lc.height()-ul().height() ) > ( rc.height()+ul().height() ) ) {
		ul().prependTo( rc );
		if( ++i > max ) break;
	}
}

function init_plist_container() {
	$('<div id="jquery-overlay" />').css({"background-color":"#000000",opacity:"0.8"}).click(remove_plist).appendTo("body");
	$('<div id="plist-wrapper" />').appendTo("body");
	$('<div id="plist-controls" />').html(
		'<h3>Product Catalog</h3><a id="plist-close" href="#close" onclick="remove_plist();" title="close" >close</a>'
	).appendTo("#plist-wrapper");
	
	$('<div id="plist-loading" />').html('<img src="images/lightbox-ico-loading.gif" alt="Loading..." />').appendTo("#plist-wrapper");
	
	$(window).resize(position_plist);
	
	position_plist();
}

function position_plist() {
	var arrPageSize  = getPageSize();
	var arrScroll    = getPageScroll();
	var top   = arrScroll[1] + 75;
	var left  = arrPageSize[0]/2 - 300;
	$("#plist-wrapper").css({top:top+"px", left:left+"px"});
	$("#jquery-overlay").width(arrPageSize[0]).height(arrPageSize[1]);
}

/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
