// JavaScript Document

// Creating custom :external selector
$.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/)
            && (obj.hostname != location.hostname)
			&& (!obj.href.match(/^javascript/));
};


$(document).ready(function() {
	
	adjustNumbers();
	$(window).resize(function() {
		adjustNumbers();
	});	
	
	// All external links open in new window, rel=nofollow
	$('a:external').click(function() {
		$(this).attr("target", "_blank");
		
		//add nofollow if 5 or more are external...
		if($('a:external').length > 4){
			$(this).attr("rel", "nofollow");
		}
		
		if($(this).attr("onclick")==undefined){
			var linkHostname = get_hostname_from_url($(this).attr('href'));
			pageTracker._trackEvent('link', 'click', linkHostname);
		}
	});


	//all PDF links in new window
	$("a[href*=.pdf]").click(function(){
		$(this).attr("target", "_blank");
	});
	//all DOC links in new window
	$("a[href*=.doc]").click(function(){
		$(this).attr("target", "_blank");
	});
	//all DOCX links in new window
	$("a[href*=.docx]").click(function(){
		$(this).attr("target", "_blank");
	});
	//all JPG links in new window
	$("a[href*=.jpg]").click(function(){
		$(this).attr("target", "_blank");
	});

	scrollNews(0);
	
});

function adjustNumbers(){
	var wWidth = parseInt($(window).width());
	if(wWidth < $("#container").width()){
		wWidth = $("#container").width();
	}
	var mWidth = parseInt($("#main").outerWidth());
	
	var numbersWidth = wWidth - mWidth - 2;
	/*alert("Window Width: " + wWidth + "\n"
	 + "Outer Width: " + mWidth
	 + "\n"
	 + "numbersWidth: " +numbersWidth
	)*/
	$("#numbersContainer").css('width', numbersWidth + 'px');
}

function scrollNews(currentNewsItem){
	var fadeDuration = 1000;
	$("#newsPost" + currentNewsItem).delay(5000).fadeOut( fadeDuration, function () {fadeOutComplete(currentNewsItem);} ); 

}

function fadeOutComplete(currentNewsItem){
	var fadeDuration = 1000;
	var nextNewsItem = currentNewsItem + 1;
	if(nextNewsItem == 3){
		nextNewsItem = 0;	
	}
	$("#newsPost" + nextNewsItem).fadeIn( fadeDuration, function(){ scrollNews(nextNewsItem); } ); 
}

function getCurrentNewsItem(){
	if($("#newsPost0").css('display')  == "block"){
		return(0);
	}
	if($("#newsPost1").css('display')  == "block"){
		return(1);
	}
	if($("#newsPost2").css('display')  == "block"){
		return(2);
	}
	return(null);
}

