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

function GetWidth()
{
        var x = 0;
        if (self.innerHeight)
        {
                x = self.innerWidth;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                x = document.documentElement.clientWidth;
        }
        else if (document.body)
        {
                x = document.body.clientWidth;
        }
        return x;
}


function GetHeight()
{
        var y = 0;
        if (self.innerHeight)
        {
                y = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                y = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
                y = document.body.clientHeight;
        }
        return y;
}

// hide cases -------------------------------------------------------------------------------- //
function hidecases(){
	$('#cases_in').hide();
}



// initScroll -------------------------------------------------------------------------------- //
function initScroll(){
	
	$("header nav ul li a, header h1 a").bind("click",function(e){
		
			var n = $(this).attr("href");

	var t = $(this);
	
	if (n == "#blog") {
		window.location = "/blog/";
	}else{
		e.preventDefault();
		}
		
	
	if( $('#cases_in').is(":visible") ) {
	  $('#cases_in').hide(10, function() {
   		
		var xTop = $($(t).attr("href")).offset().top - hHeader;
		$('html, body').animate({
			scrollTop: xTop
		}, 800 , "easeInOutCubic");
		$('header nav ul li a').removeClass('selected');
		$(t).addClass('selected');
		metricasGA("menu",$(t).attr("href"));
		
	
		
		
		
 		 });
	}
	else {
	  // element is not visible
	  
	  	var xTop = $($(this).attr("href")).offset().top - hHeader;
		$('html, body').animate({
			scrollTop: xTop
		}, 800 , "easeInOutCubic");
		$('header nav ul li a').removeClass('selected');
		$(this).addClass('selected');
		metricasGA("menu",$(this).attr("href"));
		
		
	
	  
	}

  

		

	
	
		
		



		
		return false;
	});

}




// setHighlight -------------------------------------------------------------------------------- //
var sectionIds = ['#home', '#agencia', '#cases', '#clientes', '#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#cases article").height() - 50;
		//console.log(diff);
		
		var hPortfolio = $("#cases").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.509366,-46.875903);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var image = 'images/sta_pin.png';
	var myLatLng = new google.maps.LatLng(-23.509366,-46.875903);
	var beachMarker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		icon: image
	});

}
