var rotatorIndex = 0;
var rotatorTotal = 0;
var rotatorPage = 1;
var rotatorTime = 5000;
var rotatorTimer = null;
var rotatorArrow = 0;
var rotatorPageTotal = 0;
var historytimer = null;

jQuery(document).ready(function () {
	
	if(jQuery("#mainimage").html() != null ) {
		rotatorPageTotal = jQuery("#rotatorpages").html();
		rotatorTotal = jQuery("#rotatortotal").html();
	
		rotatorSetup();
		rotate();
	}
	
	jTwitterText = jQuery("#twitter .tweetText");
	addLinkToTwitter();
	
	jQuery(".hasSlider").each(function() {
		jQuery(this).find(".slideleft").click(function() {
			slideLeft(jQuery(this).parent().parent());
		});
		jQuery(this).find(".slideright").click(function() {
			slideRight(jQuery(this).parent().parent());
		});
	});
	
	jQuery("#meachistory .link").each(function() {
		var linkwidth = 15 + jQuery(this).find("a.block").width();
		jQuery(this).width(linkwidth);
	});
	
	historytimer = setTimeout('historyAutoSlide()', 6000);	
});

function historyAutoSlide() {
	jQuery("#meachistory .slideright").trigger('click');
	historytimer = setTimeout('historyAutoSlide()', 6000);	
}

function slideLeft(slider) {
	var slideleft = jQuery(slider).find(".slider").position().left;
	var parentwidth = jQuery(slider).find(".content").width();
	var slidewidth = jQuery(slider).find(".slider").width();
	//var itemborder = jQuery(slider).find(".item").css('border-left-width').replace('px', '');
	//var itemborder2 = jQuery(slider).find(".item").css('border-right-width').replace('px', '');
	var slidemove = jQuery(slider).find(".item").outerWidth();
	//slidemove = parseInt(slidemove) + parseInt(itemborder2) + parseInt(itemborder);
	
	if (slideleft < 0) {
		jQuery(slider).find(".slider").animate({
			left: (slideleft + slidemove)
		}, 500);
	}
	else {
		jQuery(slider).find(".slider").animate({
			left: -(slidewidth - slidemove)
		}, 500);
	}
	
	if (historytimer)
		clearTimeout(historytimer);
}

function slideRight(slider) {
	var slideleft = jQuery(slider).find(".slider").position().left;
	var parentwidth = jQuery(slider).find(".content").width();
	var slidewidth = jQuery(slider).find(".slider").width();

	//var itemborder = jQuery(slider).find(".item").css('border-left-width').replace('px', '');
	//var itemborder2 = jQuery(slider).find(".item").css('border-right-width').replace('px', '');
	var slidemove = jQuery(slider).find(".item").outerWidth();
	//slidemove = parseInt(slidemove) + parseInt(itemborder2) + parseInt(itemborder);
	
	if ((slidemove + parentwidth + (-slideleft)) <= slidewidth) {
		jQuery(slider).find(".slider").animate({
			left: (slideleft - slidemove)
		}, 500);
	}
	else {
		jQuery(slider).find(".slider").animate({
			left: 0
		}, 500);
	}
	
	if (historytimer)
		clearTimeout(historytimer);
}


function rotatorSetup() {
	jQuery	("#subimages .subitem").bind("click", function() {
		jumpTo(jQuery("#subimages .subitem").index(this));
	});
	
	jQuery("#subpage .page").bind("click", function() {
		pageTo((jQuery("#subpage .page").index(this)+1));
	});
	
	jQuery("#subpage .subparrow.first").bind("click", function() {
		if (jQuery("#subpage .page:eq("+(rotatorPage-2)+")").length > 0)
			pageTo(rotatorPage-1);
	});
	
	jQuery("#subpage .subparrow.last").bind("click", function() {
		if (jQuery("#subpage .page:eq("+(rotatorPage)+")").length > 0)
			pageTo(rotatorPage+1);
		else
			pageTo(1);
	});
	
	jQuery("#rotator .main-image-cont").mouseenter(function() {
		jQuery(this).animate({
			left: (-jQuery(this).width() + 33)
		}, 500);
	});
	jQuery("#rotator .main-image-cont").mouseleave(function() {
		jQuery(this).animate({
			left: (-jQuery(this).width() + 28)
		}, 500);
	});
	jQuery("#rotator .main-image-cont .sliderTitle").click(function() {
		jQuery(this).parent().animate({
			left: 0
		}, 500);
		jQuery(this).parent().unbind('mouseenter');
		jQuery(this).parent().unbind('mouseleave');
		rotatorStop();
	});
	
	jQuery("#rotator .main-image-x").click(function() {
		jQuery(this).parent().animate({
			left: (-jQuery(this).parent().width() +28)
		}, 500);
		rotate();
		jQuery(this).parent().mouseenter(function() {
			jQuery(this).animate({
				left: (-jQuery(this).width() + 33)
			}, 500);
		});
		jQuery(this).parent().mouseleave(function() {
			jQuery(this).animate({
				left: (-jQuery(this).width() + 28)
			}, 500);
		});
	});
}

function rotate() {
	rotatorTimer = setTimeout("rotateForward()", rotatorTime);
}

function rotateForward() {
	//before increment
	jQuery("#mainimage .mainstory:eq("+rotatorIndex+")").fadeOut(1000);
	
	rotatorIndex++;
	rotatorArrow = rotatorArrow + 131;
	if (rotatorIndex == rotatorTotal) {
		rotatorIndex = 0;
		rotatorArrow = 0;
	}
	
	//after increment
	jQuery("#mainimage .mainstory:eq("+rotatorIndex+")").fadeIn(1000);
	
	if( ((rotatorIndex) % 5 == 0)) {
		//console.log('pageforward');
		pageForward();
		rotatorArrow = 0;
	}
	
	jQuery("#subrotator .arrowline .arrow").animate({
		left: rotatorArrow
	}, 500);
	
	clearTimeout(rotatorTimer);
	rotatorTimer = setTimeout("rotate()", rotatorTime);
}

function rotateBackward() {
	//before decrement
	jQuery("#mainimage .mainstory:eq("+rotatorIndex+")").fadeOut(1000);
	
	rotatorIndex--;
	rotatorArrow = rotatorArrow - 131;
	
	if (rotatorIndex == -1) {
		rotatorIndex = rotatorTotal-1;
		
		if (rotatorIndex >= 10)
			arrowNum = rotatorIndex - 10;
		else if (rotatorIndex >=5)
			arrowNum = rotatorIndex - 5;
			
		rotatorArrow = arrowNum*131;
	}
	
	//console.log(rotatorIndex);
	
	//after decrement
	jQuery("#mainimage .mainstory:eq("+rotatorIndex+")").fadeIn(1000);
	if( rotatorIndex == (rotatorTotal-1) || rotatorIndex == 4 || rotatorIndex == 9) {
		pageBackward();

		switch(rotatorPage) {
			case 1:
				tempPage = 'one';
			break;
			case 2:
				tempPage = 'two';
			break;
			case 3:
				tempPage = 'three';
			break;
		}
		
		rotatorArrow = jQuery("#subimages .page."+tempPage+" .subitem:last").index() * 131;
	}
	
	jQuery("#subrotator .arrowline .arrow").animate({
		left: rotatorArrow
	}, 500);
	
	clearTimeout(rotatorTimer);
	rotatorTimer = setTimeout("rotate()", rotatorTime);
}

function pageForward() {
	switch(rotatorPage) {
		case 1:
			currentPage = 'one';
			nextPage = 'two';
			slideLeft = 654;
		break;
		case 2:
			currentPage = 'two';
			if (rotatorPageTotal == 3) {
				nextPage = 'three';
				slideLeft = 1308;
			}
			else {
				nextPage = 'one';
				slideLeft = 0;
			}	
		break;
		case 3:
			currentPage = 'three';
			nextPage = 'one';
			slideLeft = 0;
		break;
	}
	
	jQuery("#subimages").animate({
		left: -slideLeft
	}, 500);
	
	jQuery("#subpage .page."+currentPage).css('background', 'url(/fls/20800/site_graphics/subpage_off.png) no-repeat');
	jQuery("#subpage .page."+nextPage).css('background', 'url(/fls/20800/site_graphics/subpage_on.png) no-repeat');
	
	rotatorPage++;
	if (rotatorPage > rotatorPageTotal)
		rotatorPage = 1;
}

function pageBackward() {
	switch(rotatorPage) {
		case 1:
			currentPage = 'one';
			if (rotatorPageTotal == 3) {
				nextPage = 'three';
				slideLeft = 1308;
			}
			else {
				nextPage = 'two';
				slideLeft = 654;
			}	
		break;
		case 2:
			currentPage = 'two';
			nextPage = 'one';
			slideLeft = 0;
		break;
		case 3:
			currentPage = 'three';
			nextPage = 'two';
			slideLeft = 654;
		break;
	}
	
	jQuery("#subimages").animate({
		left: -slideLeft
	}, 500);
	
	jQuery("#subpage .page."+currentPage).css('background', 'url(/fls/20800/site_graphics/subpage_off.png) no-repeat');
	jQuery("#subpage .page."+nextPage).css('background', 'url(/fls/20800/site_graphics/subpage_on.png) no-repeat');
	
	rotatorPage--;
	if (rotatorPage == 0)
		rotatorPage = rotatorPageTotal;
}

function jumpTo(itemNum) {
	//before increment
	jQuery("#mainimage .mainstory:eq("+rotatorIndex+")").fadeOut(1000);
	
	arrowNum = itemNum;
	
	if (itemNum >= 10)
		arrowNum = itemNum - 10;
	else if (itemNum >= 5)
		arrowNum = itemNum - 5;
	
	rotatorIndex = itemNum;
	rotatorArrow = arrowNum*131;
		
	//after increment
	jQuery("#mainimage .mainstory:eq("+itemNum+")").fadeIn(1000);
	
	jQuery("#subrotator .arrowline .arrow").animate({
		left: rotatorArrow
	}, 500);
	
	clearTimeout(rotatorTimer);
}

function pageTo(pageNum) {
	hideNeuLionVid();
	
	jumpTo((pageNum-1)*5);
	switch(rotatorPage) {
		case 1:
			currentPage = 'one';
		break;
		case 2:
			currentPage = 'two';
		break;
		case 3:
			currentPage = 'three';
		break;
	}
	
	switch(pageNum) {
		case 1:
			nextPage = 'one';
			slideLeft = 0;
		break;
		case 2:
			nextPage = 'two';
			slideLeft = 654;
		break;
		case 3:
			nextPage = 'three';
			slideLeft = 1308;
		break;
	}
	
	rotatorPage = pageNum;
	
	jQuery("#subimages").animate({
		left: -slideLeft
	}, 500);
	
	jQuery("#subpage .page."+currentPage).css('background', 'url(/fls/20800/site_graphics/subpage_off.png) no-repeat');
	jQuery("#subpage .page."+nextPage).css('background', 'url(/fls/20800/site_graphics/subpage_on.png) no-repeat');
	
	clearTimeout(rotatorTimer);
	rotatorTimer = setTimeout("rotate()", rotatorTime);
}

function rotatorStop() {
	clearTimeout(rotatorTimer);	
}

function showNeuLionVid(vidid){
	rotatorStop();
	jQuery("#NeulionVidContainer").show();
	neulionPlayer(vidid);
	rotate = true;
	playerRunning = true;
}

function hideNeuLionVid(){
	if (jQuery("#NeulionVidContainer").css('display') != 'none'){
		jQuery("#NeulionVidContainer").hide();
		slProxy("hide"); // handle Silverlight
		if (jQuery("#jtvshlembed") && document.getElementById('jtvshlembed').pauseVideo){
			document.getElementById('jtvshlembed').pauseVideo();
			playerRunning = false;
		}
		rotate();
	}
}

function showNeuLionVidArticle(vidid){
	jQuery("#articleVideoContainer").show();
	jQuery("#NeulionVidContainer").show();
	neulionPlayer(vidid);
	playerRunning = true;
}

function hideNeuLionVidArticle(){
	if (jQuery("#NeulionVidContainer").css('display') != 'none'){
		jQuery("#articleVideoContainer").hide();
		jQuery("#NeulionVidContainer").hide();
		if (jQuery("#jtvshlembed") && document.getElementById('jtvshlembed').pauseVideo){
			document.getElementById('jtvshlembed').pauseVideo();
			playerRunning = false;
		}
		rotate();
	}
}

function addLinkToTwitter() {
	if(jTwitterText.length>0) {
		data = jTwitterText.html();
		data = jQuery.trim(data);
		start = data.indexOf("http://");
		if(start>0) {

			for(x=start;x<=data.length;x++) {
				if(data.charAt(x)==' ') {
					
					end = x;
					break;
				}	
				end = x;
			}
			linkText = data.substring(start,end); 
			newLinkText = '<a target="_BLANK" class="twitter-link" href="'+linkText+'">'+linkText+'</a>';

			start2 = data.indexOf("http://", end);

			if (start2>0) {
				
				for(x=start2;x<=data.length;x++) {
					if(data.charAt(x)==' ') {
						end2 = x;
						break;
					}	
					end2 = x;
				}
				linkText2 = data.substring(start2,end2); 
				newLinkText2 = '<a target="_BLANK" class="twitter-link" href="'+linkText2+'">'+linkText2+'</a>';
				data = data.replace(linkText2,newLinkText2);
			}
			
			data = data.replace(linkText,newLinkText);

			jTwitterText.html(data);
		}
	}
}

function scoreDrop() {
	if (jQuery("#scoreboard .sportdropdown").css('display') == 'none') {
		jQuery("#scoreboard .sportdropdown").fadeIn(500);
	}
	else {
		jQuery("#scoreboard .sportdropdown").fadeOut(500);
	}
}

function scoreShow(spid) {
	jQuery("#scoreboard .sportdropdown").fadeOut(500);
	
	jQuery.ajax({
		type:"POST",
		url:getBaseUrl()+"/ajax/scoreguts.dbml",
		data: "DB_OEM_ID="+oemid+"&key="+key+"&SPID="+spid,
		success: function(data) {
			jQuery("#scoreboard .content").html(data);
		}
		,error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("errorThrown:"+errorThrown);
		}
	});

}

function tabsDrop() {
	if (jQuery("#tabs .sportdropdown").css('display') == 'none') {
		jQuery("#tabs .sportdropdown").fadeIn(500);
	}
	else {
		jQuery("#tabs .sportdropdown").fadeOut(500);
	}
}

function tabsShow(spid) {
	jQuery("#tabs .sportdropdown").fadeOut(500);
	
	cindex = jQuery("#tabs .header .tabbtn").index(jQuery("#tabs .header .tabbtn.active"));
	current = jQuery("#tabs .tabbtn:eq("+cindex+")");
	if (jQuery(current).hasClass('schBtn')) {
		contentId = 'schedule';
	}
	else if (jQuery(current).hasClass('resBtn')) {
		contentId = 'results';
	}
	else if (jQuery(current).hasClass('staBtn')) {
		contentId = 'standings';
	}
	
	jQuery.ajax({
		type:"POST",
		url:getBaseUrl()+"/ajax/"+contentId+".dbml",
		data: "DB_OEM_ID="+oemid+"&key="+key+"&SPID="+spid,
		success: function(data) {
			jQuery("#"+contentId).html(data);
		}
		,error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("errorThrown:"+errorThrown);
		}
	});
}

function getBaseUrl() {
     urlTokens = window.location.href.split('/');
     url = urlTokens[0]+'//'+urlTokens[2];
     return url;
}

function changetab(tab) {
	if (!jQuery(tab).hasClass('active') ) {
		cindex = jQuery("#tabs .header .tabbtn").index(jQuery("#tabs .header .tabbtn.active"));
		current = jQuery("#tabs .tabbtn:eq("+cindex+")");
		if (jQuery(tab).hasClass('schBtn')) {
			imageToUse = '/fls/20800/site_graphics/schedule_on.png';
			contentId = 'schedule';
		}
		else if (jQuery(tab).hasClass('resBtn')) {
			imageToUse = '/fls/20800/site_graphics/results_on.png';
			contentId = 'results';
		}
		else if (jQuery(tab).hasClass('staBtn')) {
			imageToUse = '/fls/20800/site_graphics/standings_on.png';
			contentId = 'standings';
		}
		
		if (jQuery(current).hasClass('schBtn')) {
			oldcontentId = 'schedule';
		}
		else if (jQuery(current).hasClass('resBtn')) {
			oldcontentId = 'results';
		}
		else if (jQuery(current).hasClass('staBtn')) {
			oldcontentId = 'standings';
		}
		
		jQuery(current).removeClass('active');
		jQuery(tab).addClass('active');
		
		jQuery("#tabimage").attr('src', imageToUse);
		
		jQuery("#"+oldcontentId).hide();
		
		if (!jQuery("#"+contentId).length > 0) {
			jQuery.ajax({
				type:"POST",
				url:getBaseUrl()+"/ajax/"+contentId+".dbml",
				data: "DB_OEM_ID="+oemid+"&key="+key+"&SPID="+spid,
				success: function(data) {
					jQuery("#tabs .content").append(data);
				}
				,error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert("errorThrown:"+errorThrown);
				}
			});
		}
		else {
			jQuery("#"+contentId).show();
		}
	}
}

function showlogin() {
	if (jQuery("#loginbox").css('display') == 'none') {
		jQuery("#loginbox").fadeIn(500);
	}
	else {
		jQuery("#loginbox").fadeOut(500);
	}
}
