/************************************
* © Matej Bukovinski, 2009
* All rights resaved
* www.bukovinski.com
*************************************/

$(document).ready(function(){
	
	// Check for iphones
	var agent = navigator.userAgent.toLowerCase();
	var is_iphone = (agent.indexOf('iphone') != -1);

	// Accordion
    
    $("#wrapper").accordion({ 
    	collapsible: true,
    	header: '.expander',
    	active: false,
    	animated: 'easeInOutCubic',
    	autoHeight: false,
    	changestart: function(event, ui) {
			var thissound=document.getElementById("open");
			if (!is_iphone) {thissound.Play();}
		}
   	});

   	
   	// Navigation hover effects

	var soundID = 0;

	$("div.menu-item").hover(
	
		function(event){
			// Animate in
			$('a.menu-text', this).stop().animate({ 
        		paddingLeft: "96px"
    	  	}, 200, "easeOutElastic" );
    	  	// Play tick sound
			var thissound=document.getElementById("tick" + soundID);
			if (!is_iphone) {thissound.Play();}
			soundID = (soundID + 1) % 3;
		},
		
		function(event){
			// Animate out
			$('a.menu-text', this).stop().animate({ 
    	    	paddingLeft: "86px"
    	}, 200, "swing" );
	});	

	// Navigation click effects

	$("div.menu-item").click(function(){
		
	});
	
	
	// Preload images
	if (document.images) {
		pic1 = new Image(350,33); 
		pic1.src="img/hilight-bg.png";
	}
	
	// Contact form ajax
	
	$("#submit").click(function(){
		$(".error").hide();
		
		// Form checking
		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var emailFromVal = $("#emailFrom").val();
		if(emailFromVal == '') {
			$("#emailFrom").after('<span class="error">You forgot to enter your email address.</span>');
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {
			$("#emailFrom").after('<span class="error">Enter a valid email address.</span>');
			hasError = true;
		}

		var subjectVal = $("#subject").val();
		if(subjectVal == '') {
			$("#subject").after('<span class="error">You forgot to enter the subject.</span>');
			hasError = true;
		}

		var messageVal = $("#message").val();
		if(messageVal == '') {
			$("#message").after('<span class="error">You forgot to enter the message.</span>');
			hasError = true;
		}
		
		// Send data
		
		if(hasError == false) {
		
			$.post("sendemail.php",
   				{ emailFrom: emailFromVal, subject: subjectVal, message: messageVal },
   					function(data){
   						$(".alert").hide();
						$("li#contact-status").prepend('<span class="done">Message sent successfully.</span>');
						window.setTimeout(function() {
 							// Remove done message and show the alert
 							$('span.done').remove();
 							$('span.alert').show();
 							// Clear the form 
 							$("#emailFrom").val('');
 							$("#subject").val('');
 							$("#message").val('');
						}, 10000);	
   					}
				 );
		}

		return false;

	});
	
	
	// Google analytics
	
	//$.trackPage("UA-651183-1");
	
	/*var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

	try{
		var pageTracker = _gat._getTracker("UA-651183-1");
		pageTracker._trackPageview();
	} catch(err) {}*/
	
});

// Google analytics

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-651183-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script');
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  ga.setAttribute('async', 'true');
  document.documentElement.firstChild.appendChild(ga);
})();	
		