/* ------------------------------------------------------------------ 
*
* SET VARS
*
 ------------------------------------------------------------------*/
$float_speed=1500; //milliseconds
$float_easing="easeOutQuint";
$float_slide_speed=750; //milliseconds
$scrollFrom=0;

$(document).ready(function(){		
	$(".navigation li a").click(function(event)
	{
		if($(this).attr('href').indexOf('#') >=0)
		{
			event.preventDefault();
			if($('body').hasClass('negative'))
			{
				$scrollTo = $scrollFrom-($('.footer').height()-$('.header').height());
				$('.negative .footer').stop().animate({top: $scrollTo}, $float_speed, $float_easing);
				$('.content').bind({
					click: function() {
						$('.negative .footer').stop().animate({top: $scrollFrom}, $float_speed, $float_easing);
						$(this).unbind('click');
				}});
			}else{
				scrollTo($(this).attr('href'));
			}
		}
	})


	$(".popup").fancybox({
		'overlayShow'	: true,
		'hideOnOverlayClick':true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'overlayColor' 	: '#666',
		'width':		'90%',
		'height':		'90%',
		'type'          : 'iframe'
	});
	
	$(".popup2").fancybox({
		'overlayShow'	: true,
		'hideOnOverlayClick':true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'width':		'90%',
		'height':		'90%',
		'type'          : 'iframe'
	}); 
	
	
	//PLAYGROUND THUMBS
	$('.playground .item').css({cursor:'pointer'});
	
    $('.playground .item').mouseenter(function() {
		$('.playground .item').each(function(){
			clearTimeout($(this).data('timeoutId'));
		})
		
		if ($.browser.msie) 
		{
			$('.playground .item a.thumb').not($(this).find('a.thumb')).stop().animate({opacity: .4}, 500);
			$(this).find('a.thumb').stop().animate({opacity: 1}, 250);	
			
			$('.playground .item h5').not($(this).find('h5')).stop().animate({color: '#ababab'}, 250);	
			$('.playground .item p').not($(this).find('p')).stop().animate({color: '#ababab'}, 250);	
			
			$(this).find('h5').stop().animate({color: '#000000'}, 250);	
			$(this).find('p').stop().animate({color: '#000000'}, 250);
		}else{
			$('.playground .item').not($(this)).stop().animate({opacity: .4}, 500);
			$(this).stop().animate({opacity: 1}, 250);	
		}
    }).mouseleave(function() {
		var timeoutId = setTimeout(function()
		{ 
			if ($.browser.msie) 
			{
				$('.playground .item a.thumb').stop().animate({opacity: 1}, 500);
				
				$('.playground .item h5').stop().animate({color: '#000000'}, 250);	
				$('.playground .item p').stop().animate({color: '#000000'}, 250);
			}else{
				$('.playground .item').stop().animate({opacity: 1}, 250);	
			}
         }, 300);	
         $(this).data('timeoutId', timeoutId);
    });
    
         
	$('.playground .item').click(function(event)
	{
		event.preventDefault();
		var href= $(this).find('a').attr('href');
		$(location).attr('href',href);	
	});
	
	
	//LETTERPRESS ITEMS	
	var ltpHeight = $('.letterpress .item').not('.itp').height();
	var ltpNewHeight = ($('.letterpress .item').not('.itp').height()*2) + parseInt($('.letterpress .item').not('.itp').css('margin-bottom')) + parseInt($('.letterpress .item').not('.itp').css('padding-bottom'))+ parseInt($('.letterpress .item').not('.itp').css('padding-top'));
	

	
	$('.letterpress .item').not('.itp').hover(function() {
				
		$(this).addClass('hover');
		$(this).find('.desc').show();
		$(this).stop().animate({
			height: ltpNewHeight+'px'
			}, 500);
		}, function() {
		$(this).removeClass('hover');
		$(this).find('.desc').hide();
		$(this).stop().animate({
			height: ltpHeight+'px'
		});
	}, 250);
	
	// CONTACT FORM (SUBMIT VIA AJAX)
	//$(".footer form .primary-button").fadeOut();
	//$('.footer textarea.field').addClass('oneline');
	$('.footer .error').hide();
	//var originalHeight = $('.footer textarea.field').height();
	//var openHeight = (originalHeight/2)*3;	
	
	var labelEmail = 'Email';//$('.footer input[name=email]').attr('value');
	var labelMessage = 'Message';//$('.footer textarea[name=message]').val();
	
	
	setDefaults(true,true);
	
	$(".footer .field").focus(function(){
		if($(this).attr('value') == labelEmail || $(this).attr('value') == labelMessage)
		{
			$(this).attr('value','');
		}		
		/*if($('.footer textarea.field').height() != openHeight)
		{
			$('.footer textarea.field').animate({height:openHeight+'px'}, 500,showBtn);
		}*/
	})
	$(".footer .field").blur(function(){
		if($(this).attr('name') == 'email' && $(this).attr('value') == '')
		{
			setDefaults(true,false);
		}
		
		if($(this).attr('name') == 'message' && $(this).val() == '')
		{
			setDefaults(false,true);
		}
	});
	
	$(".footer form").submit(function(event){
		event.preventDefault();
		// CHECK INPUT
		var action = $(".footer form").attr('action');
		var email = $('.footer input[name=email]').attr('value');
		var validEmail = isValidEmail(email);
		if(validEmail)
		{
			var message = $('.footer textarea[name=message]').val();
			if(message !='' && message!=labelMessage && message.length>2)
			{
				// SEND
				$('.footer .error').hide(500);
				//$(".footer form .primary-button").fadeOut(500, hideFrm);
				
				//POST VIA AJAX
				  var dataString = 'email='+ email + '&message=' + message;
				  $.ajax({
				    type: "POST",
				    url: action,
				    data: dataString,
				    success: function() {
				       $('.footer .error').addClass('submitted');
				      $('.footer .submitted').html('Your message have been send');
				      $('.footer .submitted').show(500).delay(3000).hide(500);	
				      	
				      setTimeout(function() { setDefaults(true,true); }, 4000);
				    }
				  });
				  return false;
  
			}else
			{
				$('.footer .error').html('please fill in a valid message');
				$('.footer .error').show(500);
			}
		}
		else{
			$('.footer .error').html('please fill in a valid email');
			$('.footer .error').show(500);
		}
		
	});
	
	function showBtn()
	{
		$(".footer form .primary-button").fadeIn(500);
	}
	
	/*function hideFrm()
	{
		$('.footer textarea.field').animate({height:originalHeight+'px'}, 500, setDefaults(true,true));
		
	}*/
	
	
	 function scrollTo(target)
	{
    
	    if(target.indexOf('#') >=0)
	    {
	    	var parts = target.split("#");
	        var trgt = parts[1];
	        
	    	var target_offset = $("#"+trgt).offset();
	    	var target_top = target_offset.top;
	    	
	    	if(trgt == 'overview')
	    	{
	    		//location.hash = '';
	    		target_top = target_top-150;
	    	}
	    	
	    	$('html, body').animate({scrollTop:target_top}, 1500);
	    }
	}
	
	function setDefaults(email, message)
	{
		if(email)
		{
			$('.footer input[name=email]').attr('value',labelEmail);
		}
		if(message)
		{
			$('.footer textarea[name=message]').val(labelMessage);
		}
		$('.footer .error').removeClass('submitted');
	}
	
	function isValidEmail(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}

});
