/************************************
* © 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,
   	});
   	
   	// Navigation hover effects

	$("div.menu-item").hover(
	
		function(event){
			// Animate in
			$('a.menu-text', this).stop().animate({ 
        		paddingLeft: "96px"
    	  	}, 200, "easeOutElastic" );
		},
		
		function(event){
			// Animate out
			$('a.menu-text', this).stop().animate({ 
    	    	paddingLeft: "86px"
    	}, 200, "swing" );
	});	
	
	
	// 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").parent().after('<span class="error">You forgot to enter your email address.</span>');
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {
			$("#emailFrom").parent().after('<span class="error">Enter a valid email address.</span>');
			hasError = true;
		}

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

		var messageVal = $("#message").val();
		if(messageVal == '') {
			$("#message").parent().after('<span class="error">You forgot to enter the message.</span>');
			hasError = true;
		}
		
		// Send data
		
		if(hasError == false) {
			
			// Show the ajax spinner and hide the button
			$("#loader").show();
			$("button#submit").hide();
			$.post("sendemail.php",
   				{ emailFrom: emailFromVal, subject: subjectVal, message: messageVal },
   					function(data){
   						$(".alert").hide();
						$("li#contact-status").prepend('<span class="done">Message sent successfully.</span>');
						// Hide the spinner and show the button
						$("#loader").hide();
						$("button#submit").show();
						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('');
						}, 5000);	
   					}
				 );
		}

		return false;

	});
	
	getTwitters('tweet', { 
  		id: 'bukovinski', 
  		count: 3, 
  		enableLinks: true, 
  		ignoreReplies: true, 
  		clearContents: true,
  		template: '<img class="twImg" src="%user_profile_image_url%" /><span class="tweetText">"%text%"<br/><a href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a></span>'
	});
	
});

// 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);
})();
		