/**
 * @author man
 */
 
var _currentItem;
var _prevItem;
var _playingTransition = false;

function enablePortfolio(){		
	$(".portfolioItem").hide(); // hide all
	_currentItem = $(".portfolioItem").eq(0); 
	_currentItem.fadeIn( 500 );
	$('#footerWrapper').animate({width: '100%'}, 2); // fix for IE6
	
	for (var i=0; i<$('#portfolioBox a').length; i++) {
		var link = $('#portfolioBox a').eq(i);
		var item = $(".portfolioItem").eq(i);	
		link.data('id', i);
		item.data('id', i);
		link.data('item', item);
		link.click(onClick); 
	};
}


function onClick (event){
	event.preventDefault();
	if(!_playingTransition){
		
		var linkId = $(this).data('id');
		if( linkId != _currentItem.data('id') ){
			_playingTransition = true;		
			_prevItem = _currentItem;			
			_currentItem = $(this).data('item');
			_prevItem.fadeOut( 300, 
				function(){
					$('#rightBox').animate({height: _currentItem.height()}, 500);
					$('#footerWrapper').animate({width: '100%'}, 500); // fix for IE6
					_currentItem.fadeIn( 500, function(){_playingTransition=false;} );
				}
			 );
			//Google analytics
			pageTracker._trackPageview('/item'+linkId);
		}
	}
}
 
 function enableMenu (){
	$("#menu a").hover(function() {			
        $(this).find("img").stop().fadeTo( 200, 0);
    },function(){
        $(this).find("img").stop().fadeTo( 200, 1);
    });
}

function enableForm(){
	$('#nameField').focus( function(){
		if (this.value == "Your Name") {	this.value = "";}
	} );
	$('#nameField').blur( function(){
		if (this.value == "") { this.value = "Your Name";	}
	} );
	
	$('#email').focus( function(){
		if (this.value == "E-mail") {	this.value = "";}
	} );
	$('#email').blur( function(){
		if (this.value == "") {this.value = "E-mail";	}
	} );
	
	$('#messageText').focus( function(){
		if (this.value == "Message") {	this.value = "";}
	} );
	$('#messageText').blur( function(){
		if (this.value == "") {this.value = "Message";	}
	} );
	
	$("#sendButton").click(function(event){
		event.preventDefault();
		
		$("#error").html('');
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var emailVal = $("#email").val();
		//alert( emailVal );
		if(emailVal == 'E-mail') {
			$("#msg").html('<span class="error">You forgot to enter your email address</span>');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$("#email").val( 'E-mail');
			$("#msg").html('<span class="error">Enter a valid email address.</span>');
			hasError = true;
		}
		
		var messageVal = $("#messageText").val();
		if(messageVal == 'Message') {
			$("#msg").html('<span class="error">You forgot to enter the message.</span>');
			hasError = true;
		}
		
		if(!hasError) {
			$(this).hide();
			
			$("#msg").html('Sending...');
			
			$.post("lib/sendemail.php", { email: emailVal, name: $("#nameField").val(), msg: messageVal },  onPost, "text" );
		}

		return false;
	});


}

function onPost ( data, textStatus ){
	if(data=='ok'){
		$("#msg").html("Your mesage has been sent. Thank you.");
		$("#nameField").val( 'Your Name');
		$("#messageText").val( 'Message');
		$("#email").val( 'E-mail');
		$("#sendButton").show();
	}else{
		$("#msg").html('<span class="error">Error sending message: ' + textStatus + '</span>');
	}
}
