$(document).ready(function() {
	$("#leftarr").hide();
	$('#wrapper').height(600);

	// BEGIN change the links for SEO
	$("#nav_rapp_logo").attr("href", "#inicio");
	$("#nav_agencia").attr("href", "#agencia");
	$("#nav_punto").attr("href", "#puntovista");
	$("#nav_experiencia").attr("href", "#experiencia");
	$("#nav_showreel").attr("href", "#experiencia");
	$("#nav_premios").attr("href", "#premios");
	$("#nav_trabaja").attr("href", "#trabaja");
	$("#nav_conta").attr("href", "#conta");	

	// END change the links for SEO
	$('html, body').animate({scrollTop: 0}, 0);
	$("#rightarr").css({"display":"block"});
	/*$('a').live('click', function(){
		$('html, body').animate({scrollTop: 0}, 0);
	});*/
	if ($(window).width() <=1056){
		$("#rightarr").css({"position":"absolute", "left":"1038px", "right":"auto"});
		$("#leftarr").css({"position":"absolute", "left":"20px"});
	} else  {
		$("#rightarr").css({"position":"fixed", "left":"auto", "right":"20px"});
		$("#leftarr").css({"position":"fixed", "left":"20px"});
	}
	
	var currentPage = 0;
	var page=new Array();

	page[0] = "inicio";
	page[1] = "agencia";
	page[2] = "puntovista";
	page[3] = "experiencia";
	page[4] = "premios";
	page[5] = "trabaja";
	page[6] = "conta"; 
	page[7] = "privacy";
			
	$("#worksub, #agencysub").hide();
	
	var officeTitle = "";
	$.historyInit(function(hash) {
		if(hash){
			var query = hash.split("-");
			$('html, body').animate({scrollTop: 0}, 0);
			(query[0]=="offices") ? $("#rightarr").hide() : $("#rightarr").show();
			(query[0]=="home") ? $("#leftarr").hide() : $("#leftarr").show();
			if(query[0]=="conta") $("#rightarr").hide();
			
			if (hash=="premios"){
				$('#wrapper').height(1500);
			}
									
			if(query[0]=="offices" && typeof(query[1]) !== 'undefined'){
				$('#wrapper').height(600);
				$('#overlay').load(hash+".html", function(){
					$("ul#topnav li a").removeClass("active");
					$('ul#topnav li a[href="#offices"]').addClass("active");

					officeTitle = $('.officehead h2').text();
					officeTitle = officeTitle.toLowerCase();
					var newTitle = '';
			        officeTitle = officeTitle.split(' ');

			        for(var c=0; c < officeTitle.length; c++) {
			        	newTitle += officeTitle[c].substring(0,1).toUpperCase() + officeTitle[c].substring(1,officeTitle[c].length) + ' ';
			        }

			        document.title = 'Agency '+newTitle+' | RAPP';	
				});
				$('#overlay').fadeIn(500);
			}
			else if(query[1]=="project"){
				$('#overlay').load(hash+".html", function(){
					currentPage = 5;
					$("ul#topnav li a").removeClass("active");
					$("ul#topnav li a[href='#work-projects']").addClass("active");
					$('#overlay').fadeIn(500);
				});
				
			}else{
				$(".peoplecont").animate({left: '0'}, 500);
				for(var i=0; i<page.length; i++){
					if(page[i]==hash){
						var pheight = $('#page-'+hash).height();
						$('#wrapper').height(pheight);
						
						if(hash=="agency-pov"){
							$('#wrapper').height(1900);
						}
						
						var whereX = ( ( (i*980) + (i*100) )*-1 );
						
						$('#contentslider').stop().animate({left: whereX}, {duration: 1000, easing: 'easeOutExpo'});
						currentPage = i;
					}
				}
				
				$('#overlay').fadeOut(500, function(){
					$('#overlay').html("");
				});
								
				//Title Changer
				var newTitle = "";
				
				
				switch(hash){
					case 'inicio':
						newTitle = 'Agencia de CRM | RAPP';
						break;
					case 'agencia':
						newTitle = 'La Agencia | RAPP';
						break;
					case 'puntovista':
						window.location.href = "blog/";
						newTitle = 'Punto de Vista | RAPP';
						break;
					case 'agency-people':
						newTitle = 'Our Best Names | RAPP';
						break;
					case 'agency-pov':
						newTitle = 'Top Agency Thoughts | RAPP';
						break;
					case 'experiencia':
						newTitle = 'Nuestra Experiencia | RAPP';
						break;
					case 'trabaja':
						newTitle = 'Trabaja con Nosotros | RAPP';
						break;
					case 'premios':
						newTitle = 'Lo mejor de la Agencia | RAPP';
						break;
					case 'conta':
						newTitle = 'Contacto | RAPP';
						break;
					case 'offices':
						newTitle = 'Locations & Network | RAPP';
						break;
					case 'privacy':
						newTitle = 'Privacy | RAPP';
						break;
					default:
						newTitle ='**Title Error**';					
				}
				
				document.title = newTitle;
				
				$(".left").addClass("not");
				$(".right").removeClass("not");
				
				var tempurl = "#"+hash;
				$("ul#topnav li a").removeClass("active");
				$("ul#topnav li a[href='"+tempurl+"']").addClass("active");
			}
		}
	});
	
	$("ul#topnav li a, .clients li a, .officehead a, a.viewproj, .projhead a, #footer_privacy, #nav_rapp_logo").live('click',function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		//alert(hash);
		return false;
	});
	
	
	$(".subhit").mouseover(function() { //Hover over event on list item
		$('#menuline').stop().animate({top: '155'}, {duration: 300, easing: 'easeOutExpo'});
		$(this).find("span").show();
		
	});
	$(".subhit").mouseout(function(event) {
		$(this).find("span").hide();
		$('#menuline').stop().animate({top: '140'}, {duration: 300, easing: 'easeOutExpo'});
		
	});
	
	$("#rightarr a").click(function(event) {
		var nextpage = page[currentPage+1];
		$.historyLoad(nextpage);
		
		//GA Tracking
		_gaq.push(['_trackEvent', 'nav_arrow_right', 'clicked']);
	});
	
	$("#leftarr a").click(function(event) {
		var nextpage = page[currentPage-1];
		$.historyLoad(nextpage);
		//GA Tracking
		_gaq.push(['_trackEvent', 'nav_arrow_left', 'clicked']);
	});
	
	//ALL GA TRACKING; Other than Dynamic News, Tweets, and Left/Right Arrows
	$('.tracked').live('click',function(){
		var label = $(this).attr('id');
		_gaq.push(['_trackEvent', label, 'clicked']);
	});
	$('.tracked').click(function() {
		var label = $(this).attr('id');
		_gaq.push(['_trackEvent', label, 'clicked']);
	});
	$("#awards tr:odd").css("background-color", "#eee")
	
	// BEGIN IF THE WINDOW IS LESS THAN 1270px wide, than adjust the arrow buttons
	$(window).resize(function() {
		if ($(window).width() <=1056){
			$("#rightarr").css({"position":"absolute", "left":"1038px", "right":"auto"});
			$("#leftarr").css({"position":"absolute", "left":"20px"});
			var scrollvalue = $(window).scrollTop();
			$("#top").css({"position":"absolute", "left":"0px", "top": scrollvalue});
			
			$(window).scroll(function () {
				var scrollvalue = $(window).scrollTop();
		    	$("#top").css({"position":"absolute", "left":"0px", "top": scrollvalue});
		    });
			
		} else  {
			$("#rightarr").css({"position":"fixed", "left":"auto", "right":"20px"});
			$("#leftarr").css({"position":"fixed", "left":"20px"});
			$("#top").css({"position":"fixed", "left":"0px", "top": scrollvalue});
			
		}
	});
	

	var agent = navigator.userAgent;
	var mobiRegExp = /nintendo ds|nintendo wii|ipad|iphone|ipod|blackberry|android/i;
	if(mobiRegExp.test(agent)){
		$("body").css("width", "100%");
		//$("#leftarr, #rightarr").hide();
		$("#top").css("margin-left", "60px");
		$("#top").css("margin-right", "60px");
		$("#wrapper").css("margin-left", "60px");
		$("#wrapper").css("margin-right", "60px");
		$(".topcontent").css("padding-left", "0px");
		$(".worksub, .agencysub").css("margin-left", "-430px");
		$("#menuline").css({"left" : "0px", "margin-left" : "60px"});
		$("#top").css("position", "static");
		$("#wrapper").css("margin-top", "-50px");
	}
	
	$("#b_echo2010").click(function(){
		$("#echo2010").show();
		$("div#painani2009").hide();
		$("div#painani2008").hide();
		$("div#painani2007").hide();
		$("div#painani2006").hide();
		$("div#painani2005").hide();
		$("div#amauta2009").hide();
		$("div#amauta2006").hide();
		$("#ojo").hide();
	});
	$("#b_painani2009").click(function(){
		$("#echo2010").hide();
		$("div#painani2009").show();
		$("div#painani2008").hide();
		$("div#painani2007").hide();
		$("div#painani2006").hide();
		$("div#painani2005").hide();
		$("div#amauta2009").hide();
		$("div#amauta2006").hide();
		$("#ojo").hide();
	});
	$("#b_painani2008").click(function(){
		$("#echo2010").hide();
		$("div#painani2009").hide();
		$("div#painani2008").show();
		$("div#painani2007").hide();
		$("div#painani2006").hide();
		$("div#painani2005").hide();
		$("div#amauta2009").hide();
		$("div#amauta2006").hide();
		$("#ojo").hide();
	});
	$("#b_painani2007").click(function(){
		$("#echo2010").hide();
		$("div#painani2009").hide();
		$("div#painani2008").hide();
		$("div#painani2007").show();
		$("div#painani2006").hide();
		$("div#painani2005").hide();
		$("div#amauta2009").hide();
		$("div#amauta2006").hide();
		$("#ojo").hide();
	});
	$("#b_painani2006").click(function(){
		$("#echo2010").hide();
		$("div#painani2009").hide();
		$("div#painani2008").hide();
		$("div#painani2007").hide();
		$("div#painani2006").show();
		$("div#painani2005").hide();
		$("div#amauta2009").hide();
		$("div#amauta2006").hide();
		$("#ojo").hide();
	});
	$("#b_painani2005").click(function(){
		$("#echo2010").hide();
		$("div#painani2009").hide();
		$("div#painani2008").hide();
		$("div#painani2007").hide();
		$("div#painani2006").hide();
		$("div#painani2005").show();
		$("div#amauta2009").hide();
		$("div#amauta2006").hide();
		$("#ojo").hide();
	});
	$("#b_amauta2009").click(function(){
		$("#echo2010").hide();
		$("div#painani2009").hide();
		$("div#painani2008").hide();
		$("div#painani2007").hide();
		$("div#painani2006").hide();
		$("div#painani2005").hide();
		$("div#amauta2009").show();
		$("div#amauta2006").hide();
		$("#ojo").hide();
	});
	$("#b_amauta2006").click(function(){
		$("#echo2010").hide();
		$("div#painani2009").hide();
		$("div#painani2008").hide();
		$("div#painani2007").hide();
		$("div#painani2006").hide();
		$("div#painani2005").hide();
		$("div#amauta2009").hide();
		$("div#amauta2006").show();
		$("#ojo").hide();
	});
	$("#b_ojo").click(function(){
		$("#echo2010").hide();
		$("div#painani2009").hide();
		$("div#painani2008").hide();
		$("div#painani2007").hide();
		$("div#painani2006").hide();
		$("div#painani2005").hide();
		$("div#amauta2009").hide();
		$("div#amauta2006").hide();
		$("#ojo").show();
	});
	//#echo2010, #painani2009, #amauta2009, #painani2009, #painani2008, #painani2007, #amauta2006, #painani2006, #ojo, #painani2005	
	
	
	
});


