// JavaScript Document
//var $j = jQuery.noConflict();
$(function(){
	pbHome();
	getQueue();
	$("#product_browser > div.menu > select").change(function(){
		loadUnits();
	});
	
	$("#product_browser > div.menu > #go").click(function(){
		loadUnits();
		return false;
	});
	
});
function sendToFriend(){
	var err = "";
	if($("#from_name").val().length < 1)
		err += "Please enter your name.\n";
	if($("#from_email").val().length < 6)
		err += "Please enter your valid email address.\n";
	if($("#to_email").val().length < 6)
		err += "Please enter your friend's valid email address.\n";
	if(err == ""){
		var str = $("#frmSendtoFriend").serialize();
		$("#divSendtoFriendContainer").load("xmlobject.php?cmd=sendtofriend&"+str);
	} else {
		alert(err);
	}
}
function pbHome(){
	$("#product_browser > #nav").slideUp("slow");
	$("#units").load("xmlobject.php?cmd=featured");
}
function viewUnit(id){
	showUnitWindow(id);
}
function showUnitWindow(id){ 
	var iClientHeight = f_clientHeight();
	if(iClientHeight < $("#unitWindow").height())
		$("#unitWindow").height(iClientHeight-20);
	
	var iClientWidth = f_clientWidth();
	if(iClientWidth < $("#unitWindow").width())
		$("#unitWindow").width(iClientWidth-20);
		
	var scrollAdjust = $(document).scrollTop();
	var iLeft = (iClientWidth/2)-($("#unitWindow").width()/2);
	var iTop = (iClientHeight/2)-($("#unitWindow").height()/2); // + scrollAdjust;
	var strFile = "xmlobject.php?cmd=getunit&id="+id;
	//$("#unitWindow").css("left",iLeft).css("top",iTop).fadeIn("slow").html(ajaxLoadHtml()).load(strFile);
	window.open (strFile,"unitwindow","resizable=1,width=600,height=500"); 
	//$("#product_container").css("height",$("#unitWindow").height()-20);
	//alert($("#product_container").height());
}
function showWindow(div){ 
	var iClientHeight = f_clientHeight();
	if(iClientHeight < $(div).height())
		$(div).height(iClientHeight-20);
	
	var iClientWidth = f_clientWidth();
	if(iClientWidth < $(div).width())
		$(div).width(iClientWidth-20);
		
	var scrollAdjust = $(document).scrollTop();
	var iLeft = (iClientWidth/2)-($(div).width()/2);
	var iTop = (iClientHeight/2)-($(div).height()/2) + scrollAdjust;
	var strFile = "xmlobject.php?cmd=emailform";
	$(div).css("left",iLeft).css("top",iTop).fadeIn("slow").html(ajaxLoadHtml()).load(strFile);
}
function closeUnitWindow(){
	//$("#unitWindow").fadeOut("slow");
	window.close;
}
function loadCategory(obj){
	var strFile = "xmlobject.php?cmd=content&name="+$(obj).attr("id");
	$("#product_browser > #nav").slideDown("slow");
	$("#product_browser > #units").html(ajaxLoadHtml()).load(strFile);
}
function loadUnits(){
	$("#product_browser > #nav").slideDown("slow");
	var str = $("#frmProductBrowser").serialize()+"&"+Math.random();
	$("#product_browser > #units").html(ajaxLoadHtml()).load("xmlobject.php?"+str);
}
function showUnits(sCategory){
	$("#product_browser > #nav").slideDown("slow");
	//var str = $("#frmProductBrowser").serialize()+"&"+Math.random();
	var str = "category="+sCategory+"&"+Math.random();
	$("#product_browser > #units").html(ajaxLoadHtml()).load("xmlobject.php?"+str);
}
function ajaxLoadHtml(){
	return "<div class=\"ajax_load\"><img src=\"/images/ajax_load.gif\" /></div>";
}

function toggleNavCat(str){
	if ($(str).is(":hidden")) {
		$(str).slideDown("slow");
	} else {
		$(str).slideUp();
	}
	
	//$(str).slideDown("slow");
}
var t;
function showtab(id){
	$("#product_container > #details").hide();	
	$("#product_container > #threed").hide();
	$("#product_container > #movie").hide();
	$("#product_container > #detail_images").hide();
	$("#product_container > #schematic").hide();
	
	$("td#details").css("background-color","#FFFFFF");
	$("td#threed").css("background-color","#FFFFFF");
	$("td#movie").css("background-color","#FFFFFF");
	$("td#detail_images").css("background-color","#FFFFFF");
	$("td#schematic").css("background-color","#FFFFFF");
	
	$("#product_container > #"+id).show();
	$("td#"+id).css("background-color","#CCCCCC");
}
function addToQuote(id){
	try {window.opener.addToQuote(id);} catch(e){};
	
	closeUnitWindow();
	$("div.btn_wide > div.add").attr("class","remove").html("Remove from quote").attr("onclick","removeFromQuote("+id+")");
	$("#product_browser > #queue").load("xmlobject.php?cmd=addtoquote&id="+id, function(data){
		showQueue();
	});
	
}
function removeFromQuote(id){
	//alert(id);
	try {window.opener.removeFromQuote(id);} catch(e){};

	closeUnitWindow();
	$("div.btn_wide > div.remove").attr("class","add").html("Add to quote").attr("onclick","addToQuote("+id+")");
	$("#product_browser > #queue").load("xmlobject.php?cmd=removefromquote&id="+id, function(data){
		showQueue();
	});
}
function getQuote(id){
	//alert("get quote: "+id);
	window.location = "reservations.php";
}
function getQueue(){
	$.get("xmlobject.php?cmd=addtoquote&id=0",function(data){
		if(data != "")
			$("#product_browser > #queue").html(data);
		showQueue();
	});
}
function showQueue(){
	if($("#product_browser > #queue").html() != ""){
		$("#product_browser > #queue_header").slideDown();
		$("#product_browser > #queue").slideDown();
	} else {
		$("#product_browser > #queue_header").slideUp();
		$("#product_browser > #queue").slideUp();
	}
}
function processRes(obj){
	if($(obj).attr("checked"))
		$.get("xmlobject.php?cmd=addtoquote&id="+obj.id);
	else
		$.get("xmlobject.php?cmd=removefromquote&id="+obj.id);
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

