$(document).ready(function(){
     
   //Loads from feed for the news timeline. 
    var interval;
    start = 1;
	//this fixes the footer issue where if the browser is too large, a white space shows under the footer. 
	
	
	
	if($(window).height() > $("body").height()){
		var diff = $(window).height() - $("body").height(),
			footer = $("#footer").height();
		$("#footer").css("height", footer+diff);
	}
	
	//auto scroll
	
	$(window).scroll(function(){
		var val = $(window).scrollTop();
		var scrollval = val + 100;
		$('#rates').animate({top: scrollval+"px"},{queue:false, duration:500, easing: 'easeInOutSine'});
	});
	
	$('.answer').hide();
	$('.tabcontent div,.tabcontent2 div').hide();
	$('.activecon').show();

	//hover options
	
	$('.node').hover(
		function(){$(this).css('z-index',10);},
		function(){$(this).css('z-index',5);}
	);
	
	$('.imging').hover(
		function(){$(this).siblings().stop().animate({opacity: 0.5});
			$(this).parent().siblings().children().stop().animate({opacity: 0.5});
		},
				   
		function(){$(this).siblings().stop().animate({opacity: 1.0});
			$(this).parent().siblings().children().stop().animate({opacity: 1.0});})
		
	$('.nav-options ul li').mouseover(
		function(){
			
			var pos = $(this).attr('data');
			$(this).siblings().css('background-image','url("/images/barbg.jpg")');
			$(this).css('background-image','url("/images/barbgblue.jpg")');
			if(start != pos){
			stopRotation();
			$('.nav-image' + start).hide();
			$('.nav-image' + pos).fadeIn();
			start = pos;
			startRotation();
			}
		})
		
	
	
	//click options
	
	$('#exit').click(function(){
		$('#marketprice').fadeOut();
	})
	
	$('.expandall').click(function(){$('.answer').slideDown();})
	$('.hideall').click(function(){$('.answer').slideUp();})
	$('.expandthis').click(function(){$(this).parent().parent().next().slideDown();})
	$('.hidethis').click(function(){$(this).parent().parent().next().slideUp();})
	$('.tabs li').click(function(){
		if(!$(this).hasClass('activetab')) {
			$(this).siblings().removeClass('activetab');
			var cls = ".tabcontent ." + $(this).attr('class');
			$(this).addClass('activetab');	
			$(cls).siblings().hide();
			$(cls).fadeIn();
		}
	})
	
	$('.tabs2 li').click(function(){
		if(!$(this).hasClass('activetab')) {
			$(this).siblings().removeClass('activetab');
			var cls = ".tabcontent2 ." + $(this).attr('class');
			$(this).addClass('activetab');	
			$(cls).siblings().hide();
			$(cls).fadeIn();
		}
	})
	
	LoadData();
	startRotation();
	
	function LoadData() {
		$.ajax({
			url: "/newsfeed/",
			dataType: "html",
			success: function(xml){
				var msg = "";
				for(i = 2; i < 8; i++) { //only 8 articles
					var title = $(xml).find('title').eq(i).text();
					var link  = $(xml).find('fakelink').eq(i).text();
					var date  = $(xml).find('pubDate').eq(i).text();
					
					msg +='<li><span>'+date+'</span><a target="_blank" href="'+link+'">'+title+'</a></li>';
				}
				$('#ticker').html(msg);	
					$(function(){
						$("ul#ticker").liScroll({travelocity: 0.05});
					});
			}
		})
	}
	function startRotation() {
		interval = setInterval(rotation,5000);
	}
	function rotation(){
		$('.nav-image' + start).hide();
		if(start == 4){
			start = 1;
		}
		else start++;
		$('.nav-image' + start).fadeIn();
		$('.nav-options ul li[data=' + start +']').siblings().css('background-image','url("/images/barbg.jpg")');
		$('.nav-options ul li[data=' + start +']').css('background-image','url("/images/barbgblue.jpg")');
	}
	function stopRotation(){
		clearInterval(interval);
	}
	/*	
		Apply a settimeout function.  
		find class "now"
		increment if data != 4
		if == 4, set to 0.
		delay 8 seconds.
		add rotation() to hover. 
		*/

	
	
	
	
});
