$(document).ready(function(){

	$('.cartmsgok:visible').each(coolReveal);
    	$('.cartmsgerror:visible').each(coolReveal);
    	
   	$.tablesorter.defaults.widgets = ['zebra'];
	$('table.tablesorter').tablesorter();
	
	renderX();
	
	$('.toggleForm').click(function(){
		if ($bShowXForm)
		{
			$bShowXForm = false;
		}
		else
		{
			$bShowXForm = true;
		}
		renderX();
	});
	
	$('.toggleArt').click(function(){
		if ($bShowXArt)
		{
			$bShowXArt = false;
		}
		else
		{
			$bShowXArt = true;
		}
		renderX();
	});
	
	$('.toggleProd').click(function(){
		if ($bShowXProd)
		{
			$bShowXProd = false;
		}
		else
		{
			$bShowXProd = true;
		}
		renderX();
	});
	
	$('.toggleBill').click(function(){
		if ($bShowXBill)
		{
			$bShowXBill = false;
		}
		else
		{
			$bShowXBill = true;
		}
		renderX();
	});

});




$bShowXForm = false;
$bShowXArt = false;
$bShowXProd = false;
$bShowXBill = false;

function renderX()
{
	if ($bShowXForm)
	{
		$('.xForm').show();
		$('.toggleForm').text('[-] Hide Billing Info');
	}
	else
	{
		$('.xForm').hide();
		$('.toggleForm').text('[+] Show Billing Info');
	}
	
	if ($bShowXArt)
	{
		$('.xArt').show();
		$('.toggleArt').text('[-] Hide Art Details');
	}
	else
	{
		$('.xArt').hide();
		$('.toggleArt').text('[+] Show Art Details');
	}
	
	if ($bShowXProd)
	{
		$('.xProd').show();
		$('.toggleProd').text('[-] Hide Production Details');
	}
	else
	{
		$('.xProd').hide();
		$('.toggleProd').text('[+] Show Production Details');
	}
	
	if ($bShowXBill)
	{
		$('.xBill').show();
		$('.toggleBill').text('[-] Hide Billing Details');
	}
	else
	{
		$('.xBill').hide();
		$('.toggleBill').text('[+] Show Billing Details');
	}
}













function coolReveal()
{
    $(this).hide();
    $(this).slideDown('slow');
}

function launchPopup(sUrl, iWidth, iHeight)
{
	iWidth = iWidth + 40;
	iHeight = iHeight + 40;
	var newwindow = window.open(sUrl,'newwindow','height='+iHeight+', width='+iWidth+'');
	if (window.focus)
	{
		newwindow.focus()
	}
	return false;
}

function updateGrandTotal()
{
	var oGrandTotal = $('span#thetotal');
	var oAmount1 = $('input#sAmount1');
	var oAmount2 = $('input#sAmount2');
	var oAmount3 = $('input#sAmount3');
	var oAmount4 = $('input#sAmount4');
	var oAmount5 = $('input#sAmount5');
	var fTotal = 0;
	
	if (oAmount1.val() != '')
	{
		fTotal = fTotal + parseFloat(oAmount1.val());
	}
	
	if (oAmount2.val() != '')
	{
		fTotal = fTotal + parseFloat(oAmount2.val());
	}
	
	if (oAmount3.val() != '')
	{
		fTotal = fTotal + parseFloat(oAmount3.val());
	}
	
	if (oAmount4.val() != '')
	{
		fTotal = fTotal + parseFloat(oAmount4.val());
	}
	
	if (oAmount5.val() != '')
	{
		fTotal = fTotal + parseFloat(oAmount5.val());
	}
	
	oGrandTotal.html('$' + CurrencyFormatted(fTotal));
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}










var iNumDaysToRememberChat = 120;
var iNumMinutesToWaitBeforeAutoChat = 10;
var iNumMillisecondsToWaitBeforeAutoChat = (iNumMinutesToWaitBeforeAutoChat * 60 * 1000);
var sChatCookieName = 'pas_chatalready';
var bStayingOnSite = false;

$(document).ready(function(){

	$('.chatpopuplink').click(function(e){
		e.preventDefault();
		openChatPopup();
	});

	setTimeout(openChatPopupIfNoPreviousChat, iNumMillisecondsToWaitBeforeAutoChat);

	$(document).click(function(){
		bStayingOnSite = true;
	});

	$(window).unload(function(){
		if (!bStayingOnSite)
		{
			//alert('Leaving!');
			openChatPopupIfNoPreviousChat();
		}
	});

	//eraseChatUseRecord();

});

function openChatPopup()
{
	recordChatUse();
	window.open('http://www.plaquesandsuch.com/livezilla/livezilla.php','','width=590,height=550,left=0,top=0,resizable=yes,menubar=no,location=no,status=yes,scrollbars=yes');
}

function openChatPopupIfNoPreviousChat()
{
	if (!didUserChatAlready())
	{
		openChatPopup();
	}
}

function didUserChatAlready()
{
	var bRetVal = false;
	
	if ($.cookie(sChatCookieName) === 'yes')
	{
		bRetVal = true;
	}
	
	return bRetVal;
}

function recordChatUse()
{
	$.cookie(sChatCookieName, 'yes', { expires: iNumDaysToRememberChat });
}

function eraseChatUseRecord()
{
	$.cookie(sChatCookieName, null);
}








