
var cleartimer;

$(document).ready(function() {
	var url_match = function(href) {
		var cur_url = document.location.pathname;
		var pathToMatch = /.*\/([^\/]*)\/.*/i;
		var match = pathToMatch.exec(cur_url);
		if (match) {
			pathToMatch = new RegExp(".*" + match[1] + ".*", 'i');
			if (pathToMatch.test(href)) {
				return true;
			}
		}
		return false;
	}
	
	var activeIndex = 0;
	var lis = $.makeArray($('ul.primary-links li'));
	for(var i = lis.length; i--;) {
		if ($(lis[i]).hasClass('active') || url_match($(lis[i]).children('div').children('a').attr('href'))) {
			activeIndex = i;
			break;
		}
	}
	$("ul.primary-links").lavaLamp({fx: 'backout', speed: 800, target: 'a', startItem: activeIndex});
	
});

$(document).ready(function(){
	
	// Footer animation
	$('.footerColumns').hover(function(e){
		if ($.data(this, "hover") != true) {
			$.data(this, "hover", true);
			$('.footerColumns .column:not(.connect) p, .footerColumns .column:not(.connect) h2').stop().animate({color: '#fff'}, 500);
			$('.footerColumns .column:not(.connect) a').stop().animate({color: '#73b7d3'},500);
		}
	},
	function(){
		if ($.data(this, "hover") == true) {
			$.data(this, "hover", false);
			$('.footerColumns .column:not(.connect) p, .footerColumns .column:not(.connect) h2').stop().animate({color: '#999'}, 500);
			$('.footerColumns .column:not(.connect) a').stop().animate({color: '#999'},500);
		}
	});
	

	// Videos thumbnail hover
	$('.playlist td').each(function() {
		if ($(this).html() != '') {
			$(this).hover(function(e){
				if ($.data(this, "hover") != true) {
					$.data(this, "hover", true);
					$(this).stop().animate({backgroundColor: '#000'}, 400);
				}
			},
			function(){
				if ($.data(this, "hover") == true) {
					$.data(this, "hover", false);
					$(this).stop().animate({backgroundColor: '#222'}, 200);
				}
			});
		}
	});
	
	
	// Photos page thumbnail hover
	$('.thumbNails td').each(function() {
		$(this).hover(function(e){
			if ($.data(this, "hover") != true) {
				$.data(this, "hover", true);
				$(this).stop().animate({backgroundColor: '#222', borderColor: '#222'}, 400);
			}
		},
		function(){
			if ($.data(this, "hover") == true) {
				$.data(this, "hover", false);
				$(this).stop().animate({backgroundColor: '#444', borderColor: '#444'}, 200);
			}
		});
	});
	//$('.thumbnails td') {
		
	
	// Billboard changer
	var numBillboards = $('.billboards div').size();
	if (numBillboards > 1) {
		var curBillboard = 1;
		$('.billboards div:first').show();
		var changeBillboard = function() {
			var nextBillboard = curBillboard + 1 <= numBillboards ? curBillboard + 1 : 1;
			$('.billboards div:nth-child(' + curBillboard + ')').fadeOut(1500);
			$('.billboards div:nth-child(' + nextBillboard + ')').fadeIn(1500);
			curBillboard = nextBillboard;
			cleartimer = setTimeout(changeBillboard, 5000);
		}
		cleartimer = setTimeout(changeBillboard, 5000);
	} else if (numBillboards == 1) {
		$('.billboards div').show();
	}
	
	// article dropdown
	$('.openLink').click(function(){
		var id = $(this).attr('href');
		var nid = $(this).attr('rel');
		if ($(id).size() > 0) {
			$(id).slideToggle("slow"/*, function() { $('#' + nid + '-archived').scrollTop(0);  }*/);
		}
		return false;
	});
	$('.closeLink').click(function(){
		var nid = $(this).children('a').attr('rel');
		$(this).parent().slideToggle("slow"/*, function() { $('#' + nid + '-summary').scrollTop(0); }*/);
		return false;
	});
});
