$j( document ).ready(function(){
	$j("#tablink2").bind("click", getRecentlyPublished);
	$j("#tablink3").bind("click", getTopRatedArticles);
	$j("#tablink4").bind("click", getTopRatedCompilations);
	
	$j("#mobile_promo_pause").bind("mouseover", function(){
		clearTimeout(mp_t);

	});
	$j("#mobile_promo_pause").bind("mouseout", function(){
		rotate_mobile_promo();
	});
	
	rotate_mobile_promo();
	loadTopRanked();
});

function getRecentlyPublished(){
	if( empty.test( $j("#tabcontent2").find("div").html()) ){
		$j("#tabcontent2").find("div").load(g_webroot + "publications/ajax_getRecentlyPublished");
	}
}

function getMostReadArticles(){
	if( empty.test( $j("#tabcontent2").find("div").html()) ){
		$j("#tabcontent2").find("div").load(g_webroot + "articles/ajax_getMostRead");
	}
}

function getTopRatedArticles(){
	if( empty.test( $j("#tabcontent3").find("div").html() ) ){
		$j("#tabcontent3").find("div").load(g_webroot + "articles/ajax_getTopRated");
	}
}

function getTopRatedCompilations(){
	if( empty.test( $j("#tabcontent4").find("div").html() ) ){
		$j("#tabcontent4").find("div").load(g_webroot + "compilations/ajax_getTopRated");
	}
}

//mobile ad
var mp_t;
var mp_count = 3;
var mp_curr = 3;
var mp_time = 6000;

function rotate_mobile_promo(){
	
		var mp_next = mp_curr+1;
		if(mp_curr==mp_count)
			mp_next = 1;
		//alert(mp_next);
		$j("#mobile_promo").animate({
			backgroundPosition: "700px 0px"
		}, function(){
			$j("#mobile_promo").css("background-image", "url(" + g_img_url + "/img/ads/mobile/mobile_promo_" + mp_next + ".jpg)");
			$j("#mobile_promo").animate({backgroundPosition: "320px 0px"}, function(){
				mp_curr = mp_next;
				clearTimeout(mp_t);
				mp_t=setTimeout("rotate_mobile_promo()", mp_time);
			});
			$j("#mobile_promo_blurb_" + mp_curr).hide();
			$j("#mobile_promo_blurb_" + mp_next).fadeIn(800);
			
		});
		
	
}
//carousel - new
var topRankedData = "";
var topRankedMax = 5;
var topRankedTotal = 0;
var topRankedRotateTime = 7000;
var topRankedCurr = 0;
var c_timer;
function loadTopRanked(){
	$j.getJSON(g_webroot + "utils/ajax_getTopRankedPublications/?",
	function(data){
		
		topRankedData = data;
		topRankedTotal = data.length;
		loadTopRankedRow(0);
		
});
}
function loadTopRankedRow(n){
	
	if(n==null)
		n=0;
	if((n+topRankedMax)>topRankedTotal)
		n=0;
	
	topRankedCurr = n;
		
	$j("#carousel_container").html("");
	$j("#carousel_cover").html("");
	var cc = "";
	var cover = "";
	
	$j.each(topRankedData, function(i,item){
		
		if(i>=n && i<(n+topRankedMax)){
			var id = item.Issue.id;
			var w = item.Issue.w1;
			var h = item.Issue.h1;
			
			var newW = 105;
			var newH = parseInt((h/w)*newW);
			var url = item.Issue.url;
			url = url.replace("-0.jpg", "-w-"+newW+".jpg");
			
			cc = cc + "<div id='cc_" + id + "' class='cc' style='width:" + newW+"px;height:"+newH+"px;margin-top:"+(180-newH)+"px'>";
			cc = cc + "<img id='cover_" + id + "'src='" + url + "' /></div>";
			cover = cover + "<div id='cover_" + id + "' style='width:"+newW+"px;height:"+newH+"px;margin-top:"+(180-newH)+"px'>";
			cover = cover + "<a id='cc_link_" + id + "' title='" + item.Title.title + " - " + item.Issue.name + "' href='" + g_webroot + "publications/view/" + id+"'>&nbsp;</a></div>";
		}
		
		clearTimeout(c_timer);
		c_timer = setTimeout("loadTopRankedRow(" + (n+topRankedMax) + ")", topRankedRotateTime);
	
	});
	
	//fill into containers
	$j("#carousel_cover").html(cover);
	$j("#carousel_container").html(cc);

}

function carouselNext(){
	clearTimeout(c_timer);
	var row = parseInt(topRankedCurr+topRankedMax);
	loadTopRankedRow(row);
}

function carouselPrev(){
	var row;
	clearTimeout(c_timer);
	if(topRankedCurr==0)
		row= topRankedTotal-topRankedMax;
	else
		row = parseInt(topRankedCurr-topRankedMax);
	loadTopRankedRow(row);
}
function pauseAll(){
	clearTimeout(mp_t);
	clearTimeout(c_timer);
}
function restartAll(){
	rotate_mobile_promo();
	loadTopRanked();
}
