/**
	Projeto - Projeto (CLIENTE) v: 0.1
	Developer(s):
		+ Fernando Basilio (fernando.basilio@rede106.com)
*/

var hHeader = 26;
			
// metricasGA -------------------------------------------------------------------------------- //
function metricasGA(area,obj){
	var strGA = area+': '+obj+' ';
	_gaq.push(['_trackEvent', strGA , ' click']);
	//console.log(strGA);
}

// initScroll -------------------------------------------------------------------------------- //
function initScroll(){

	$("header nav ul li a, header h1 a").bind("click",function(){
		var xTop = $($(this).attr("href")).offset().top - hHeader;
		$('html, body').animate({
			scrollTop: xTop
		}, 500 , "easeInOutCubic");
		$('header nav ul li a').removeClass('selected');
		$(this).addClass('selected');
		
		metricasGA("menu",$(this).attr("href"));
		
		return false;
	});

}

// initCarousel -------------------------------------------------------------------------------- //
function initCarousel(){

	$('#mycarousel').jcarousel({scroll:1});

	$.each($('#mycarousel li'), function(index, value){
		//alert(index + " - " + value);
		var legenda = "<p>"+$(value).find("a").attr("title")+"</p>";
		
		if(legenda){
			//console.log(legenda);
			$(value).find("a").append(legenda);
		}
	})
	
	$("#mycarousel li a").lightBox();
	
	$(".jcarousel-skin-tango").bind("mouseenter", function(){
		$(".jcarousel-next-horizontal, .jcarousel-prev-horizontal").fadeIn("fast");
	}).bind("mouseleave", function(){
		$(".jcarousel-next-horizontal, .jcarousel-prev-horizontal").fadeOut("fast");
	});
	
	
	
	/*
	$("#mycarousel li a").bind("click",function(){
		var nCliente = $(".item_portfolio h2 span").text();
		var nWork = $(this).attr("title");
		metricasGA("portfolio: "+ nCliente,nWork);
	})
	*/
	

}

// initAgency -------------------------------------------------------------------------------- //
function initAgency(){

	$("#container section#agencia ul li a").bind("click", function(){
		return false;
	})
	
	/*
	//quando houver descritivo dos socios descomentar este bloco e comentar o bloco anterior
	$("#container section#agencia ul li a").bind("click", function(){
		var targetUnderdog = $(this).attr("href");

		$(".desc_underdog").fadeOut("fast");
		$(targetUnderdog).fadeIn("fast");
		
		$("#container section#agencia ul li a").removeClass("on");
		$(this).addClass("on");
		
		metricasGA("underdogs",$(this).attr("href"));
		
		$(".btn_fechar").bind("click",function(){
			$("#container section#agencia ul li a").removeClass("on");
			$(this).parent().fadeOut("fast");
			return false;
		});				
		
		return false;
	}).neoHover();
	*/
}

// initPortfolio -------------------------------------------------------------------------------- //
function initPortfolio(){

	$("#container section#portfolio nav ul li a").bind("mouseenter", function(){
		$(this).find("span").fadeOut("fast");
	}).bind("mouseleave", function(){
		if(!$(this).hasClass("on")){
			$(this).find("span").fadeIn("fast");
		}
	});
	
	$(".content_portfolio nav ul li a").bind("click", function(){
		
		xtopPortfolio = $("#portfolio").offset().top - hHeader;
		$('html, body').animate({
			scrollTop: xtopPortfolio
		}, 500 , "easeInOutCubic");
		
			
		$("#container section#portfolio article .load").remove();
		$("#container section#portfolio article").append("<img src='img/load.gif' class='load' />");
		
		var idCliente = $(this).attr("href");
		var nomeCliente = $(this).find("small").text();
		
		$("#container section#portfolio nav ul li a span").fadeIn();
		$("#container section#portfolio nav ul li a").removeClass("on");
		$(this).find("span").hide();
		$(this).addClass("on");
		
		metricasGA("portfolio",$(this).attr("href"));
		
		$(".item_portfolio").find("object").hide();
		
		// start Animation
		$(".item_portfolio").animate({
			opacity: .0
			}, 1000, function() {
			
			$.ajax({
				type: 'GET',
				url: 'getTrabalhos.php',
				data: { idCliente: idCliente, nomeCliente: nomeCliente  },
				success: function( response ){
				
					$(".item_portfolio").empty().append(response);

					// start Animation
					$(".item_portfolio").animate({
						opacity: 1
						}, 800, function() {
							//$(this).append("<img src='<?php echo get_option('home'); ?>/core/wp-content/themes/aymore/img/load.gif' class='load' />");
					});
					// Animation complete
					initCarousel();


				}
			});
			
		});
		// Animation complete
		
		return false;
	});
}



// setHighlight -------------------------------------------------------------------------------- //
var sectionIds = ['#home', '#agencia', '#portfolio', '#clientes', '#underdogs', '#contato'];
featuredHeight = Math.min($(window).height());

function setHighlight(value) {
    currentSection = value;
    $('header nav ul li a').removeClass('selected');
	$('header nav ul li a.' + value.substring(1)).addClass('selected');
	//console.log(value.substring(1));
}

function onScrollAndResize() {

    var pos = $(window).scrollTop();
	//var halfWindowHeight = Math.floor($(window).height() / 2);
	//var halfWindowHeight = Math.floor($(window).height());
	
    // highlight appropriate category based on where we are on the page
	for(var i = 0; i < sectionIds.length; i++) {
	    if(i == sectionIds.length - 1) {
	        setHighlight(sectionIds[i]);
			//console.log("i == sectionIds.length - 1");
	        break;
	    }
	    
		//var scrollThreshold = $(sectionIds[i+1]).offset().top - halfWindowHeight;
		var scrollThreshold = $(sectionIds[i+1]).offset().top - hHeader;
		
		if(pos < scrollThreshold) {
			//console.log("pos < scrollThreshold");
			setHighlight(sectionIds[i]);
			break;
		}
		
		var hClientes = $("#clientes").offset().top;
		//console.log("hClientes - " + hClientes);
		
		var hBody = Math.floor($(window).height());
		//console.log("hBody - " + hBody);
		
		var diff = hClientes - $("#container section#portfolio article").height() - 50;
		//console.log(diff);
		
		var hPortfolio = $("#portfolio").height();
		//console.log("hPortfolio - " + hClientes);

	} 

	var currentSection = sectionIds[i];
	var currentPosition;
	
	/*
	if(currentSection == "#portfolio"){
		$("#container section#portfolio article").css({
			position: "fixed",
			top: "26px",
			height: "625px"
		})

		if(pos>diff){
			var hDiff = $("#portfolio").height()-$("#container section#portfolio article").height() - 50 + "px";
			//top: "940px"
			$("#container section#portfolio article").css({
				position: "relative",
				top: hDiff
			})
		}
	
	}else{
		$("#container section#portfolio article").css({
			position: "relative",
			top: "0px"
		})
	}
	*/
	
	$("header h3").empty().text(pos + " - " +currentSection);
	
}



// copyHeight -------------------------------------------------------------------------------- //
function copyHeight(obj){
	hMax = $(obj).eq(0).height();
	for (i=0; i<$(obj).length; i++){
		if(hMax < $(obj).eq(i).height()){
			hMax = $(obj).eq(i).height();
		}
	}
	$(obj).height(hMax);
}


// centralizeElement -------------------------------------------------------------------------------- //
function centralizeElement(obj){
	var obj = obj;
	if ($("body").height() > $(obj).height()) {
		$(obj).css({top: '50%', marginTop: -($(obj).height()/2) + 'px'});
	}else {
		$(obj).css({marginTop: '0px', top: '0px'});
	}
	if ($("body").width() > $(obj).width()) {
		$(obj).css({left: '50%', marginLeft: -($(obj).width()/2) + 'px'});
	}else {
		$(obj).css({marginLeft: '0px', left: '0px'});
	}
}


// Return GET params -------------------------------------------------------------------------------- //
function returnParam(url,paramName){
	for(var qs = url.split("&"), i=0; i < qs.length; i++)
		if(qs[i].indexOf(paramName)>=0)
			return qs[i].split("=")[1];
	return false;
}


// Flash Calls -------------------------------------------------------------------------------- //
function openLink(url) {
	if(!url.match('http')) {
		switch(url) {
			case "example": window.location = 'http://www.example.com.br/'; break;
			case "exampl2": doSomething(); return true; break;
			default: return false;
		}
	}else
		window.location = url;
	return true;
}


// Popup handler -------------------------------------------------------------------------------- //
function openPopup(u,n,w,h,o,c) {
	var l = t = 18;
	if (c) {
		l = (screen.availWidth-w) / 2;
		t = (screen.availHeight-h) / 2;
	}
	p=window.open(u,'pop_'+n,'left='+l+',top='+t+',width='+w+',height='+h+''+((o)?','+o:''));
}
function comunicadoPop(url){
	openPopup(url,'Comunicado',415,350,'scrollbars=1',true);
}


// initMaps -------------------------------------------------------------------------------- //
function initMaps() {
	//javascript:void(prompt("",gApplication.getMap().getCenter()));
	
	var latlng = new google.maps.LatLng(-23.588722, -46.686927);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var image = 'img/bullet_pin.png';
	var myLatLng = new google.maps.LatLng(-23.588722, -46.686927);
	var beachMarker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		icon: image
	});

	var latlng = new google.maps.LatLng(-22.98909372419306, -43.227864503860474);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map_canvas2"), myOptions);
	var image = 'img/bullet_pin.png';
	var myLatLng = new google.maps.LatLng(-22.98909372419306, -43.227864503860474);
	var beachMarker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		icon: image
	});
}
