//
//	General JavaScript setup/setings
//

// Get the curernt Y position from the CSS top setting
	var currentY = null;
	$(document).ready(function(){
		currentY = parseInt($("#toplink").css("top").substring(0,$("#toplink").css("top").indexOf("px")));
	});
	
	// Run on scroll of window
	$(window).scroll(function () {
		// Get the current vertical position of the scroll bar for the first element in the set of matched elements.
		var scrollTop = $(document).scrollTop();
		scrollTop = parseInt(scrollTop);
		
		// Get the offset from scrolltop and the current Y "top" position from the CSS
		var offset = currentY+scrollTop+"px"; 
		 
		 // animate the #top link and set speed
		$("#toplink").animate({top:offset},{duration:325,queue:false});
		
	});


	
	
	$(document).ready(function() {		
		
		// Dynamically generate navigation items since they are not needed if JS disabled
		var promoNavigation = "<div id='portfolio-nav'><p>Loading...<\/p><a href='javascript:void(0);' class='prev' title='Previous Item'>&lt;<\/a><ul><\/ul><a href='javascript:void(0);' class='next' title='Next Item'>&gt;<\/a><\/div>";
		
		// Promo slider init
		$('#promos').after(promoNavigation).cycle({
			fx: 		'fade',
			pager:		'#portfolio-nav ul',
			prev:		'#portfolio-nav .prev',
			next:		'#portfolio-nav .next',
			timeout:	0,
			speed:		500,
			before:		getImageAlt /* */
		});
		
		// Grab alt='' from image and display as a title
		function getImageAlt() {
			$('#portfolio-nav p').html($(this).find('img').attr('alt'));
		} 
		/* */
		
		// Fade out to reveal background on hover
		$("#promos a img").hover(
		  function () {	$(this).stop().fadeTo(450, 0.4); }, 
		  function () {	$(this).stop().fadeTo(200, 1); }
		);		


		// contact form validation
		var hasChecked = false;
		$("#submit").click(function () { 
			hasChecked = true;
			return checkForm();
		});
		$("#name,#email,#message").live('change click', function(){
			if(hasChecked == true)
			{
				return checkForm();
			}
		});
		function checkForm()
		{
			var hasError = false;
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			
			if($("#name").val() == '') {
				$("#error-name").fadeIn();
				hasError = true;
			}else{
				$("#error-name").fadeOut();
			}
			if($("#email").val() == '') {
				$("#error-email").fadeIn();
				hasError = true;
			}else if(!emailReg.test( $("#email").val() )) {
				$("#error-email").fadeIn();
				hasError = true;
			}else{
				$("#error-email").fadeOut();
			}
			if($("#message").val() == '') {
				$("#error-message").fadeIn();
				hasError = true;
			}else{
				$("#error-message").fadeOut();
			}
			if(hasError == true)
			{
				return false;
			}else{
				return true;
			}
		}
		// end contact form validation
		
		
		
		// Delay to let images load first
		setTimeout(function() { 
			// Load latest tweets
			getTwitters('tweets', {
				id: 'cudazi', 
				clearContents: false, 
				count: 2, 
				timeout: 5, 
				slideIn: true,
				onTimeout: function () { $(".error-tweets").fadeIn(); }, 
				withFriends: false, 
				ignoreReplies: false, 
				enableLinks: true,
				template: '<p>%text%</p>'
			});
			// end tweets	
			$(".tweets-loading").hide();
		}, 2000);

		
	});
