// * -- iOS Compatibility -- * //
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);
if (agentID) {
	$("html").addClass("ios");
	window.onload = function(){
		setTimeout(function(){window.scrollTo(0, 1)}, 100);
	}
	if (navigator.userAgent.match(/(OS 5_\d like Mac OS X | OS 5_\d_\d like Mac)/)){
		$("html").addClass("ios5");
	}
}

var page = function(){
	
	var btnLike = $(".connect_widget_interactive_area");
	
	stLight.options({
		publisher:'a3bf1b40-4985-47ed-a85f-e82ac1d46dac',
		displayText: "Diego Díaz",
		onhover: false,
		publisherGA:"UA-28304364-1"
	});
	
	var $canvas = $("html,body"),
		$winHeight = $(window).height(),
		$winWidth = $(window).width(),
		$sections = $("section:not(#partners)"),
		s = parseFloat($(window).scrollTop());
	
	function buttons(){		
		if (s >= $winHeight*2 && s < $winHeight*3){
			$("nav li").removeClass("selected");
			$("nav li[title=Projects]").addClass("selected");			
			
		} else if (s >= $winHeight && s < $winHeight*2){
			$("nav li").removeClass("selected");
			$("nav li[title=About]").addClass("selected");
			
		} else if (s < $winHeight){
			$("nav li").removeClass("selected");
			$("nav li[title=Home]").addClass("selected");
		}
		$obj = $("section#about article");
		_height = $obj.height();
		$obj.css({
			top : ($winHeight*2 - s - _height) / 2
		});
	}
	
	$("nav a").live("click",function(e){
		$id = $(this).attr("href");
		if ($id=="#contact"){
			$("section#contact").fadeIn(500);
		}else {
			$.scrollTo
				('section'+$id.toString(),2000,
					{
						easing:'easeInOutBack',
						queue:false/*,
						onAfter:urls*/
					}
				);
		}
		e.preventDefault();
	});

	$close = $("section#contact a.close");
	$close.live("click",function(){
		$("section#contact").fadeOut(500);	
	});
	
	$("nav li").hover(
		function(){
			$(this).find(".tooltip").stop().animate({
				left: 50,
				opacity: 1	
			},500,"easeInQuint");
			$(".normal",this).stop().animate({opacity : 0},1000);
			$(".hover",this).stop().animate({opacity : 1},1000);
		},
		function(){
			$(this).find(".tooltip").stop().animate({
				left: 0,
				opacity: 0
			},500,"easeOutQuint");
			$(".normal",this).stop().animate({opacity : 1},1000);
			$(".hover",this).stop().animate({opacity : 0},1000);
		}
	);
	
	$("menu li").hover(
		function(){
			$(this).find(".tooltip").stop().animate({
				right: 50,
				opacity: 1	
			},500,"easeInQuint");
		},
		function(){
			$(this).find(".tooltip").stop().animate({
				right: 0,
				opacity: 0
			},500,"easeOutQuint");
		}
	);
	
	function general(s){
		buttons();
		
		var _url = document.location;
		_url = _url.toString();
		_url = _url.split("#");
		if (_url[1]=="contact"){
			$("section#contact").css("display","block");
		}
	}
	
	function asideText($winWidth){
		if ($winWidth<=1280){
			$("aside dt").text("Like is good. Try!");
		} else {
			$("aside dt").text("Like is so good. don’t you believe me? just try!");
		}	
	}
	
	asideText($winWidth);
		
		
	
	$(window).bind({
		
		"scroll" : function(e){
			s = parseFloat($(window).scrollTop());
			general(s);
		},
		
		"resize" : function(){
			$winHeight = $(window).height();
			$winWidth = $(window).width();
			$sections.css("height",$winHeight);
			$("#pxs_container").css({
				"height" : $winHeight - 200,
				"marginTop" : 100
			});
			s = parseFloat($(window).scrollTop());
			general(s);
			asideText($winWidth);
		}
		
	}).trigger("resize");
		
	$("footer dd").hover(
		function(){
			$(this).find(".tooltip").stop().animate({
				top: -30,
				opacity: 1	
			},500,"easeInQuint");	
		},
		function(){
			$(this).find(".tooltip").stop().animate({
				top: 30,
				opacity: 0	
			},500,"easeInQuint");	
		}
	);
	
	var $projects = $('#pxs_container');
	$projects.parallaxSlider();	

	$("section#contact .mail").draggable();
	$("section#contact .mailbox").droppable({
		drop: function(event, ui) {			
			$("form").submit();
		}
	});
	
	$("form").live("submit",function(){
		var name=$("input[name=name]").val();
		var email=$("input[name=email]").val();
		var phone=$("input[name=phone]").val();
		var message=$("textarea[name=message]").val();
		
		var datos="name="+name+"&phone="+phone+"&email="+email+"&message="+message;				
		$.ajax({
			type: "POST",
			url: "form/form.php",
			data: datos,
			beforeSuccess: function(datos){
				$('section#contact article .loader').css("display","block");
			},
			success: function(datos){
				$('section#contact article .loader').css("display","none");
				if (datos == 1) {
					$('section#contact article figcaption').text("Please complete the required field");
					$("section#contact .mail").animate({
						top:234,
						left:76	
					},1000);
				} else if (datos == 2){
					$('section#contact article figcaption').text("Please introduce a correct email");
					$("section#contact .mail").animate({
						top:234,
						left:76	
					},1000);
				} else {
					$("section#contact .mail, section#contact .mailbox, section#contact small").fadeOut(1000);
					$('section#contact article figcaption').text("Thanks for contact me, I'll answer you ASAP");
					$('section#contact article form').remove();
					$('section#contact article').append("<dl class='infoMail'><dt>Success! Your Email has been sent!</dt>"+datos+"</dl>");
					$('section#contact article .infoMail').delay(1000).fadeIn(500);
				}				
		   }
		});
		return false
	});
}





