$(document).ready(function() {    
	var musicRegex = new RegExp("music.htm");    
    var musicMatch = musicRegex.exec(window.location);
     //alert('musicMatch = ' + musicMatch);
	var twitRegex = new RegExp("twitter.htm");    
    var twitMatch = twitRegex.exec(window.location);
     //alert('twitMatch = ' + twitMatch);	
	if (musicMatch) {
		//alert('music');
		$('img#mainLogo').attr("src","/img/logo-music.png");	
	}
	else if(twitMatch) {
		//alert('twitter');
		$('img#mainLogo').attr("src","/img/logo-twitter.png");	
	}
	else {
		//alert('default');
		$('img#mainLogo').attr("src","/img/wp-large-logo.png");	
	}
	/*$('#twitterCarousel').jcarousel({        
        scroll: 1
    });*/
	
	var isLoggedIn = $("#loggedIn").val(); //alert("isLoggedIn is " + isLoggedIn);
	if(isLoggedIn == 1)
	{		
		processClientInvoices();
		processClientRequests();
		processClientInvoiceDetails();
		processClientRequestDetails();
	}
	else
	{
		$(".loginLink").removeClass("hide");	
	}
});

/* Check the client invoices for the most recent invoice
   and set the update payment details href accordingly.	*/
function processClientInvoices() {
	var inv = $("#clientOrders tr:contains('Recurring'):last td.invoiceNo").html(); //alert('last recurring invoice is ' + inv);		
	if(inv != null) {	
		var href = $("a#updatePaymentDetailsLink").attr("href"); //alert('href ' + href);
		var newHref = href + "?inv=" + inv; //alert('newHref ' + newHref);
		$("a#updatePaymentDetailsLink").attr("href",newHref);
	}
	else
	{
		// Unable to locate relevant invoice number so hide the link
		$("li#updatePaymentDetails").css("display","none"); //alert('hide update payment details');
	}
	// now set the amounts to be red (if outstanding amount remains)
	$("#clientOrders .outstanding").each(function()
	{
		var amount = $(this).html().substr(1); //alert('outstanding amount is ' + amount);
		if(amount > 0)
		{
			//alert('highlight outstanding amount');
			$(this).addClass("red");
		}
	});
}
/* Set up the requests table (if any) */
function processClientRequests()
{
	var check = $("div#mainContent:contains('No cases found')"); //alert('check is ' + check);
	if(check == null)
	{		
		$("table.case-list").addClass("hide");
	}
}
/* Sets the invoice details page up ready to print.
 BC template(s) not working so need to hide default template content */
function processClientInvoiceDetails()
{
	var clientOptions = $("#clientOptions").html();
	if(clientOptions != null) {
		// hide the unecessary content
		$("#contentContainer").css("background","#FFF");
		$("#contentContainer").css("height","100%");
		$("#footer").addClass("hide");
		
		// Hook up the print link to the printable div
		$("#printInvoice")
		.attr( "href", "javascript:void( 0 )" )
		 .click(function(){
			// print the invoice & cancel click event
		 	$(".printable" ).print();
			return( false );
		});
	}
}
/* Sets the request details page ready to view.
 BC template(s) not working so need to hide default template content */
function processClientRequestDetails()
{
	var requestDetails = $("#requestDetails").html();
	if(requestDetails != null) {
		// hide the unecessary content
		$("#contentContainer").css("background","#FFF");
		$("#contentContainer").css("height","100%");
		$("#footer").addClass("hide");
	}
}
/* Toggles the visibility of the given element  */
function toggleVisibility(elementId) 
{ 
	var element = document.getElementById(elementId);  
	if(element.style.display == "none" || element.style.display == "") {
		element.style.display = "block"; }
	else {
		element.style.display = "none"; }
}
/* Retrns the safe payment url for processing the given invoice & amount */
function getPaymentUrl(invoice, amount) 
{				
	var result = 'https://webpurity.worldsecuresystems.com/client/accept_payment.htm?inv=' + invoice + '&amt=' + amount.substring(1);		
	location.replace(result);
}

 // Create a jquery plugin that prints the given element.
 jQuery.fn.print = function(){
	 // NOTE: We are trimming the jQuery collection down to the
	 // first element in the collection.
	 if (this.size() > 1){
	 this.eq( 0 ).print();
	 return;
	 } else if (!this.size()){
	 return;
	 }
	  
	 // ASSERT: At this point, we know that the current jQuery
	 // collection (as defined by THIS), contains only one
	 // printable element.
	  
	 // Create a random name for the print frame.
	 var strFrameName = ("printer-" + (new Date()).getTime());
	  
	 // Create an iFrame with the new name.
	 var jFrame = $( "<iframe name='" + strFrameName + "'>" );
	  
	 // Hide the frame (sort of) and attach to the body.
	 jFrame
	 .css( "width", "1px" )
	 .css( "height", "1px" )
	 .css( "position", "absolute" )
	 .css( "left", "-9999px" )
	 .appendTo( $( "body:first" ) )
	 ;
	  
	 // Get a FRAMES reference to the new frame.
	 var objFrame = window.frames[ strFrameName ];
	  
	 // Get a reference to the DOM in the new frame.
	 var objDoc = objFrame.document;
	  
	 // Grab all the style tags and copy to the new
	 // document so that we capture look and feel of
	 // the current document.
	  
	 // Create a temp document DIV to hold the style tags.
	 // This is the only way I could find to get the style
	 // tags into IE.
	 var jStyleDiv = $( "<div>" ).append(
	 $( "style" ).clone()
	 );
	  
	 // Write the HTML for the document. In this, we will
	 // write out the HTML of the current element.
	 objDoc.open();
	 objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	 objDoc.write( "<html>" );
	 objDoc.write( "<body>" );
	 objDoc.write( "<head>" );
	 objDoc.write( "<title>" );
	 objDoc.write( document.title );
	 objDoc.write( "</title>" );
	 objDoc.write( jStyleDiv.html() );
	 objDoc.write( "</head>" );
	 objDoc.write( this.html() );
	 objDoc.write( "</body>" );
	 objDoc.write( "</html>" );
	 objDoc.close();
	  
	 // Print the document.
	 objFrame.focus();
	 objFrame.print();
	  
	 // Have the frame remove itself in about a minute so that
	 // we don't build up too many of these frames.
	 setTimeout(
	 function(){
	 jFrame.remove();
	 },
	 (60 * 1000)
);
 }
