/* 

1. background();
2. validation();
3. form();
4. gaExternalTracking();
5. mobile();
6. desktop();
7. mobileDetect();
*  document ready & window resize

*/




// rotate background image
function background() {
	
	// position container in the middle
	function positionContainer() {
		var leftMargin = ($(window).width() - 1100) / 2;
		if (leftMargin < 0) {
			$("#container").css({"left":0});
		} else {
			$("#container").css({"left":leftMargin});
		}
	}
	positionContainer();
	$(window).resize(function() {
		positionContainer();
	});
	
	// size the background image to 100%
	function resizeImage() {
		var documentH = $(document).height();
		$("#bg, #bg-01, #bg-02, #bg-03").height(documentH);
	}
	resizeImage();
	$(window).resize(function() {
		resizeImage();
	});
	
	// activate cycle plugin
	$("#bg").cycle({ 
    	fx: "fade", 
	    speed: 300, 
    	timeout: 7000 
	});
	
}



// validate form
function validation() {

	// parameters for validation
	var validateEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
	
	// errors logic
	// blank input function
	function errorBlank(obj) {																		
		obj.addClass("error-textfield");															
		obj.next(".error").addClass("error-message");											
		obj.next(".error").text("Please fill in the field");									
	}
	
	// invalid input function
	function errorInvalid(obj) {																	
		obj.addClass("error-textfield");															
		if (obj.attr("id")=="form-email") {															
			obj.next(".error").addClass("error-message");										
			obj.next(".error").text("Please enter a valid email");										
		}		
	}
	
	// no error function
	function errorFree(obj) {																		
		obj.removeClass("error-textfield");															
	    obj.next(".error").removeClass("error-message").text("");								
	}
	
	// logic for "email" text field
	$("input#form-email").focus(function(obj) {		
	}).blur(function(obj) {												
		var obj = $(this);						
		if (obj.val() == "") {		
		    errorBlank(obj);															
		} else if (!validateEmail.test(obj.val())) {	
		    errorInvalid(obj);											
		} else {																				
		    errorFree(obj);																		
		};		
	}).keydown(function(obj) {												
		var obj = $(this);						
		if (obj.val() == "") {		
		    errorBlank(obj);															
		} else if (!validateEmail.test(obj.val())) {	
		    errorInvalid(obj);											
		} else {																				
		    errorFree(obj);																		
		};		
	});

}



// clear email field on focus and restore value on blur
function form() {

	var getOriginalValue = $("#form-email").val();
	$("form input[type=text]").focus(function(obj){
		var obj = $(this);
		if(obj.val() == getOriginalValue) {
			obj.val("");
		} 
	}).blur(function(obj){
		var obj = $(this);
		if(obj.val() == "") {
			obj.val(getOriginalValue);
		} 
	});
	
}



// track external clicks for google analytics
function gaExternalTracking() {
	
	$('a[target=_blank]').click(function(){
	    try{
	        _gaq.push(['_trackEvent', 'External Links', 'Click', $(this).attr('href')]);
	    } catch(err) {}
	    return true;
	});		

}



// mobile scripts
function mobile() {

	$("#mobile-content").show();

	var introText = '<p id="intro-text">' + $("#intro img").attr("alt") + "</p>";
	var signup = '<div id="signup">' + $("#signup").html() + '</div>';
	var social = '<div id="social">' + $("#social").html() + '</div>';	
	
	// use append instead of appendTo for #social & #signup (keyboard issue with android)
	if($("p.back-to-home").length && !$("#mobile-content p.back-to-home").length) {
		$("p.back-to-home").prependTo("#mobile-content");
	}
	if($("#main #social").length && !$("#mobile-content #social").length) {
	 	$("#mobile-content").prepend(social);
	}
	if($("#main #signup").length && !$("#mobile-content #signup").length) {
	 	$("#mobile-content").prepend(signup);
	}
	if($("#main #intro").length && !$("#mobile-content #intro-text").length) {
		$("#mobile-content").prepend(introText);
	}	

}



// detect mobile devices and load functions accordingly
function mobileDetect() {
	var windowW = $(window).width();
	if (windowW < 768) {
	    $("body").addClass("mobile");
	    mobile();
		form();
	   	validation();
		gaExternalTracking();
	} else {
	    $("body").removeClass("mobile");
		background();
		form();
		validation();
		gaExternalTracking();
	}
}



// activate functions
$(document).ready(function() {
	mobileDetect();
	setTimeout(function () {
	    window.scrollTo(0, 1);
	}, 300);
});
$(window).resize(function() {
	mobileDetect();
});




