//On document load... do this stuff... weeee.
$(document).ready(function(){
	//Contact Us Popup no trigger, but add launcher
	var formload = function(hash){ 
		//Set the submit button to post the form results to the php and handle the results
		//via jquery
		$('.submit',hash.w).click(function(event){		
			$('#alertdiv').html('Submitting Form...');
			$('#alertdiv').jqmShow();
			$.post("scripts/proc.send-email.php", $('form',hash.w).serialize(),
					function(data){
						$('#alertdiv').html('<button class="jqmClose" onclick="$(\'#alertdiv\').jqmHide()">x</button> '+data);    //NEED TO MANUALLY ADD CLOSE METHOD						
					}
			   );
			return false;					
		});
	};
	
	//Setup contact form popup (div on main page is used)
	$('#contactusformdiv').jqm({ajax: 'content/contactusform.html',onLoad:formload});	 		    		    		
	
	//Setup alert popup 
	$('#alertdiv').jqm({modal:true});
	
	$("a").click(function(event){  
		var addr = event.target.href.toString();       		
		switchMainContentOnAddr(addr);  	
	});
	//Load main page content code     		 
	var addr = document.location.toString();
	switchMainContentOnAddr(addr);  		
	
	
	//Nav menu animation
	 $(".mainnav li").mouseover(function(){          		     
	 	$(this).stop().animate({height:'50px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
	 });  
	 $(".mainnav li").mouseout(function(){  
	 	$(this).stop().animate({height:'30px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
	 });  
	
	 $(".compnav li").mouseover(function(){          		     
	 	$(this).stop().animate({height:'30px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
	 });  
	 $(".compnav li").mouseout(function(){  
	 	$(this).stop().animate({height:'20px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
	 });   		
				
 });  


function switchContent(tgt, contname){
	$('#'+tgt).load('content/'+contname+'.html');
}

function switchMainContent(contname){	
	if(contname == 'contactusform'){
		$('#contactusformdiv').jqmShow(); 	
		return true;		
	}
	//Load content into the mainccontent div via the load method 	
	$('#maincontent').fadeOut(300, function() {
		$('#maincontent').load('content/'+contname+'.html',function(responseText, textStatus, XMLHttpRequest){			
			
			$("#maincontent a").click(function(event){  
       			var addr = event.target.href.toString();       		
       			switchMainContentOnAddr(addr);  	
   		   	});			

		});	
    });		
	$('#maincontent').fadeIn(300);
	
	/*
	var ftop = $('#fisharea').position().top;
	alert(ftop);
	if(ftop > -100 || ftop > 50)
		$('#fisharea').animate({top: '-=10'}, 500);
	else
		$('#fisharea').animate({top: '+=10'}, 500);
	*/

}

//Need to fix this, as any external link with an anchor will do the same
function switchMainContentOnAddr(addr){
	if (addr.match('#')) { // the URL contains an anchor        		
		var theid = addr.split('#')[1];	        		      	
		if(theid){
			if(theid == 'contactusform'){
				$('#contactusformdiv').jqmShow(); 				
			}
			switchMainContent(theid);
		}
		else{		
			switchMainContent('home');		
		}
	}	
	else{		
		switchMainContent('home');		
	}
}



