/**
 * @author frankcastellucci
 */
$(document).ready(function() {

	var		lastMenuSelector;
	var 	lastPageSelected = new String(document.URL.substr(document.URL.indexOf(".html")));
	
	/** 
	 * If there is not filename extension, then we are dealing
	 * with the root, or home page
	 */
	
	if( document.URL.indexOf("index.html") != -1 ||
			document.URL.indexOf(".html") == -1) {
		lastMenuSelector = $('a[id=menu.home]')[0];
		$('#banner').load('axiom1/banner.html');
		$('#sideline').load('axiom1/sideline.html');
		$('#footer').load('axiom1/footer.html');
	}
	else {
		var	pageIdentifier = $('meta[name=menuselection]').attr("content");
		lastMenuSelector = $('a[id='+$('meta[name=menuselection]').attr("content")+']' )[0];
		if(pageIdentifier === 'menu.contact') {
			setupContactControls();
		}
		$('#banner').load('banner.html');
		$('#sideline').load('sideline.html');
		$('#footer').load('footer.html');
	}
	
	//	Highlight the menu selection
	lastMenuSelector.setAttribute("style", "background-color:#355CA7;color:yellow");

	// Predefined
	// Accordians
	$("#accordion").accordion({ header: "h3", active:false, collapsible:true,autoHeight: false });
	
	// Tabs
	$('#tabs').tabs();
				
	//hover states on the static widgets
	$('#dialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
	function setupContactControls() {
		// State information for the contact page
			
		$('.error').hide();
		$('input.text-input').css({backgroundColor:"#FFFFFF"});
	  	$('input.text-input').focus(function(){
	    	$(this).css({backgroundColor:"#FFDDAA"});
	  	});
		$('input.text-input').blur(function(){
	    	$(this).css({backgroundColor:"#FFFFFF"});
	  	});
		$('input.text-area').css({backgroundColor:"#FFFFFF"});
	  	$('input.text-area').focus(function(){
	    	$(this).css({backgroundColor:"#FFDDAA"});
	  	});
		$('input.text-area').blur(function(){
	    	$(this).css({backgroundColor:"#FFFFFF"});
	  	});
		$("input#firstName").select().focus();
	
		//	Contact validation and submit form process
		
		$("#contactForm").validate({
			rules: {
				firstName:{required:true},
				lastName:{required:true},
				companyName:{required:true},
				email: {
					required:true,
					email:true
				},
				comment:{required:true}
			},
	 		submitHandler: function(form) {
				var arr = $(form).serializeArray();
						var json = "";
		    	jQuery.each(arr, function(){
		        	jQuery.each(this, function(i, val){
		                if (i=="name") {
		                        json += '"' + val + '":';
		                } else if (i=="value") {
							var val1 = val.replace(new RegExp( "\\n", "g" ),"@");
		                    json += '"' + val1.replace(/"/g, '\\"') + '",';
		                }
		        	});
		    	});
		    	json = "{" + json.substring(0, json.length - 1) + "}";
				
				$.ajax({
					url:"http://www.axiom1inc.com/rocket/contact",
					type: "POST",
					data: json,
					contentType: "application/json; charset=utf-8",
					dataType:"json",
					timeout:2000,
					success: function(data){
						},
					error: function(xinst,status,except){
						console.log(xinst);
						console.log(status);
						console.log(except);
						},
					complete: function(xinst, status){
						;
						}
					});
					
	
				$('#form_wrapper').html("<div id='message'></div>");
				$('#message').html("<h2>Contact Form Submitted!</h2>")  
					.append("<p>We will be in touch soon.</p>")  
					.hide()  
					.fadeIn(1000, function() {  
							$('#message').append("<img id='checkmark' src='../img/check.png' />");  
						});  
				//return false;				
	 		}
		});
		
	}	
 });
 

