var menuItem;
var isMobile = false;

$(window).load(function() {
	
	$("a.globalBrandMap").click(function() {
		$.fancybox({
		'padding' : 0,
		'href' : '/_lib/images/structure/Global_Brand_Map.jpg',
		'title' : $("a.globalBrandMap").text(),
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic'
		 });
	});
	
	$("a.fancybox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
	});
	
	//detect iPhone, iPod, iPad devices
	function isiPad(){
		return (
				(navigator.platform.indexOf("iPhone") != -1) ||
				(navigator.platform.indexOf("iPod") != -1) ||
				(navigator.platform.indexOf("iPad") != -1)
		);
	}
	
	if(isiPad()){
		isMobile = true;
	} else {
		isMobile = false;
	}
	
	//show arrow on active link
	var activeLink = $(".nav ul").find("li.active");
	var activeIdx = $(activeLink).index();
	//console.log(activeIdx);
	var arrowOffset = (-activeIdx*183) + "px " + (-activeIdx*34)+"px";
	$(activeLink).find("span").css("backgroundPosition", arrowOffset);
	
	//show arrow on link hover
	$(".nav ul li:not(li.selected) a").hover(
		//mouse in
		function(){
			var idx = $(this).parent().index();
			var newValue = (-activeIdx*183) + "px " + (-idx*34)+"px";
			menuItem = $(this).parent().find("span");
			$(menuItem).stop(true, true); //stop all animations
			$(menuItem).css("backgroundPosition", newValue);
			$(menuItem).css("left", "-105px"); //reset to default position
			
			//animate arrow in
			$(menuItem).animate({ opacity: 1, left: '0'}, 300, function() { /* animation end*/ });
			//$.browser.msie
		},
		
		//mouse out
		function() {
			//animate arrow out
			$(menuItem).animate({ opacity: 0}, 200, function() { /* animation end */ });
		}
	);
	
	//change background
	//var bgOffset = "0 " + (-activeIdx*600)+"px";
	//alert(activeIdx);
	//$("div.bg").css("backgroundPosition", bgOffset);
	
	if(isMobile) {
		//animate selected arrow in
		$(activeLink).find("span").animate({opacity: 1, left: '0'}, 500, function() {
			var ourStory = $(".ourstory");
			if($(ourStory).length > 0) {
				scrollPage();
			}
			
			var articles = $(".articles");
			if($(articles).length > 0) {
				scrollPage();
			}
			
			var advisors = $(".advisors");
			if($(advisors).length > 0) {
				scrollPage();
			}
			
			var whoWeAre = $(".whoweare");
			if($(whoWeAre).length > 0) {
				scrollPage();
			}
			
			var ourbrand = $(".ourbrand");
			if($(ourbrand).length > 0) {
				animateTimeline();
			}
			
			var contact = $(".contact");
			if($(contact).length > 0) {
				contactPageAnimations();
			}
		});
	} else {
		//animate selected arrow in
		$(activeLink).find("span").animate({opacity: 1, left: '0'}, 500, function() { /* animation end */ });
		
		var ourStory = $(".ourstory");
		if($(ourStory).length > 0) {
			scrollPage();
		}
		
		var articles = $(".articles");
		if($(articles).length > 0) {
			scrollPage();
		}
		
		var advisors = $(".advisors");
		if($(advisors).length > 0) {
			scrollPage();
		}
		
		var whoWeAre = $(".whoweare");
		if($(whoWeAre).length > 0) {
			scrollPage();
		}
		
		var ourbrand = $(".ourbrand");
		if($(ourbrand).length > 0) {
			animateTimeline();
		}
		
		var contact = $(".contact");
		if($(contact).length > 0) {
			contactPageAnimations();
		}
	}
	
	var home = $(".home");
	if($(home).length > 0) {
		homepageAnimations();
	}
});

function homepageAnimations() {
	$(".content div.logo").hide(); //hide original logo
	$(".content div.homeLogo").show(); //show animating logo
	$(".content div.homeLogo").animate({ top: '-205px', right: '-467px'}, 1300, function() { 
		$(".content div.logo").show(); //show original logo
		$(".content div.homeLogo").fadeOut('fast'); //hide animating logo
		$(".right div.arrowText").fadeIn('slow', function() { /* animation complete */ });
	});
	
	$(".right div.blueArrow").animate({ left: '35', top: '28'}, 1300, function() {
		/* blue arrow animation end - start green arrow animation */
		$(".right div.greenArrow").animate({ top: '234', right: '39'}, 1300, function() {
			/* green arrow animation end - start text fade in*/
			$(".right .news").fadeIn('slow', function() {
				scrollPage();
				getNewsFeeds();
				setInterval(function() { getNewsFeeds(); }, 30000);
			});
		});
	});
}

function contactPageAnimations() {
	$(".contact div.arrowA").css('left',-990);
	$(".contact div.arrowB").css('right',-750);
	$(".contact .scrollContent").css('display','none');
	$(".contact .emails").css('display','none');
	
	$(".contact div.arrowA").animate({ left: '-145px'}, 500, function() {
		/* blue arrow animation end - start green arrow animation*/
		$(".contact div.arrowB").animate({ right: '0'}, 500, function() {
			/* green arrow animation end - start text fade in*/
			$(".contact .scrollContent").fadeIn('fast', function() { /* animation complete */ });
			$(".contact .emails").fadeIn('fast', function() { /* animation complete */ });
		});
	});
}

function scrollPage() {
	$(function()
	{
		// Initialise the scrollpanes
		$('.scrollPane').jScrollPane();

	});
}

function animateTimeline() {
	$("div.timeline").css('left',-900);
	if(isMobile) {
		$("div.timeline").animate({opacity: 1, left: '-125'}, 1000, function() {
			scrollPage();
		});
	} else {
		scrollPage();
		$("div.timeline").animate({opacity: 1, left: '-125'}, 1000, function() { /* animation complete */ });
	}
}

function getNewsFeeds() {
	//remove all existing LI tags
	//$('#js-news').children().remove("li");
	$('div.home').children().remove("div#ticker-wrapper");
	
	//ajax call here
	$.ajax({
		type: "GET",
		url: vxBaseURL+"/?gettwitter",
		dataType: "json",
		success: function(response){
			$('div.home').append('<div id="ticker-wrapper" class="no-js"><ul id="js-news" class="js-hidden"></ul></div>');
			for(i=0; i<response.length; i++) {
				//twit user
				var twit_user = response[i].from_user;
				
				//crop twit text
				var twit_text = response[i].text;
				if(twit_text.length > 90) {
					twit_text = twit_user + " - " + twit_text;
					twit_text = twit_text.substr(0, 90) + "...";
				}
				
				//crop twit date
				var twit_date = response[i].created_at;
				var twit_date_trim = twit_date.indexOf("+");
				var twit_date_format = "00:00:00 " //+0000;
				var twit_date_len = parseInt(twit_date_trim - twit_date_format.length);
				twit_date = twit_date.substr(0, twit_date_len);
				
				var twit_link = "http://twitter.com/#!/HuffPostPol/status/" + response[i].id_str;
				
				//add twit
				$('#js-news').append('<li class="news-item"><a href=' + twit_link + ' target="_blank">' + twit_text + "<span>" + twit_date + '</span></a></li>');
			}
			
			$(function () {
		        $('#js-news').ticker({
		            speed: 0.10,
		            htmlFeed: true,
		            displayType: 'reveal',
		            controls: false,
		            fadeInSpeed: 600,
		            titleText: 'Market Tweets'
		        });
		    });
		}
	})
}
