var setRotate = true;
var rotateTimer = null;
var rotatorTime = 7000;

jQuery(document).ready(function () {
	jQuery(".flyout .tab").click(function() {
		flyout(this);
	});	
	
	//jQuery("#list-menu li.sports ul.subnav div.grid:eq(0)").css('padding', '0px 20px 0px 0px');
	jQuery("#list-menu li.sports ul.subnav").css('width', '300px');
	
	jQuery("#rotator .subrotator .item").click(function() {
		jumpto(this);	
	});
	
	jQuery("#sportrotator .subrotator .item").click(function() {
		sportjumpto(this);
	});
	
	if (jQuery("#rotator").length > 0) {
		rotateTimer = setTimeout("rotate();", rotatorTime);
	}
	
	if (jQuery("#sportrotator").length > 0) {
		rotateTimer = setTimeout("sportrotate();", rotatorTime);
	}
	
	if (jQuery("#videos").length > 0) {
		jQuery("#videos .tab").click(function() {
			videotab(this);
		});	
	}
	
	jQuery(".slideleft").click(function() {
		slideLeft(this);
	});
	
	jQuery(".slideright").click(function() {
		slideRight(this);
	});
	
	jQuery("#adbelt a").each(function() {
		jQuery(this).addClass('relative');
		jQuery(this).find('img').addClass('absolute');
		jQuery(this).find('img:eq(1)').addClass('trans');
		
		jQuery(this).mouseenter(function() {
			jQuery(this).find('img:eq(1)').animate({
				opacity: 1
			}, 500);
		}).mouseleave(function() {
			jQuery(this).find('img:eq(1)').animate({
				opacity: 0
			}, 500);
		});
	});
	
	jQuery("#adbelt .ad-rotation").each(function() {
		jQuery(this).css('width', '188px !important');
		jQuery(this).css('height', '60px !important');
		
		jQuery(this).find("div.adspace").css('width', '188px');
		jQuery(this).find("div.adspace").css('height', '60px');
	});
	
	
	jQuery("#cal-event-slider-content .score").each(function() {
		jQuery(this).find(".bottom a:last").css('border', 'none');
		if (jQuery(this).find(".live_VIDEO, .live_AUDIO_ONLY").length > 0) {
			isfirst = true;
			jQuery(this).find(".live_VIDEO, .live_AUDIO_ONLY").each(function() {
				if (!isfirst) {
					jQuery(this).css('margin', '0px 0px 0px 3px');
				}
				else
					isfirst = false;
			});
		}
	});
	
	jQuery("#cal-event-slider-content .schedule").each(function() {
		jQuery(this).find(".bottom a:last").css('border', 'none');
	});
	
	jQuery(".articlecopy").each(function() {
		if (jQuery(this).find('a').attr('target') == 'PDFWIN') {
			jQuery(this).hide();
		}
	});
	
	/* 
		start calendar slider 
	*/
	
	var ulWidth = setSliderUlWidth();
	var sliderContentUl = jQuery("#cal-event-slider-content ul");
	var startUl = centerSliderUl(sliderContentUl);
	var singleLiWidth = 166;	//total width of li tags
	var displayAreaWidth = 660;
	var startUl = (startUl * singleLiWidth) - (singleLiWidth * 2);
	sliderContentUl.css("left","-"+startUl+"px");
	jQuery("#cal-slider-bar .left").click(function () {
		currentpos = jQuery("#cal-event-slider-content ul").position().left;
		if ((currentpos) < (0)) {
			sliderContentUl.animate({'left' : (currentpos + 660)}, 750);
			jQuery('#cal-slider').slider('option', 'value', (Math.abs(currentpos + 660)));
		}
	});
	
	jQuery("#cal-slider-bar .right").click(function () {
		currentpos = jQuery("#cal-event-slider-content ul").position().left;
		if ((currentpos - 660) > (-ulWidth)) {
			sliderContentUl.animate({'left' : (currentpos - displayAreaWidth)}, 750);
			jQuery('#cal-slider').slider('option', 'value', Math.abs(currentpos - 660));
		}
	});
	
	jQuery("#cal-slider").slider({
		min: 0,
		max: Math.abs(ulWidth - displayAreaWidth),
		animate: true,
		orientation: 'horizontal',
		step: singleLiWidth,
		value :startUl,
		stop: function (event, ui) {
        	sliderContentUl.animate({'left' : ui.value * -1}, 750);
        },
        slide: function (event, ui) {
        	sliderContentUl.css('left', ui.value * -1);
        }
	});
	
	/*
		end calendar slider
	*/
});

function slideLeft(arrowimg) {
	cont = jQuery(arrowimg).parent().parent().parent();
	slider = jQuery(cont).find('.slider');
	divcontent = jQuery(cont).find('.content');
	controls = jQuery(arrowimg).parent();
	leftpos = jQuery(slider).position().left;
	slidelength = jQuery(divcontent).width();
	sliderwidth = jQuery(slider).width();
	
	if (leftpos < 0) {
		jQuery(slider).animate({
			left: leftpos + slidelength
		}, 500);	
		
		if (jQuery(controls).length > 0) {
			current = jQuery(controls).find(".active");
			currentindex = jQuery(controls).find(".page").index(current);
			updateindex = currentindex - 1;
			update = jQuery(controls).find(".page:eq("+updateindex+")");
			
			jQuery(current).removeClass('active');
			jQuery(update).addClass('active');
			
			jQuery(current).attr('src', '/fls/19900/site_graphics/page_off.png');
			jQuery(update).attr('src', '/fls/19900/site_graphics/page_on.png');
		}
	}		
}

function slideRight(arrowimg) {
	cont = jQuery(arrowimg).parent().parent().parent();
	slider = jQuery(cont).find('.slider');
	divcontent = jQuery(cont).find('.content');
	controls = jQuery(arrowimg).parent();
	leftpos = jQuery(slider).position().left;
	slidelength = jQuery(divcontent).width();
	sliderwidth = jQuery(slider).width();
	
	if (leftpos > (-sliderwidth + slidelength)) {
		jQuery(slider).animate({
			left: leftpos - slidelength
		}, 500);
		
		if (jQuery(controls).length > 0) {
			current = jQuery(controls).find(".active");
			currentindex = jQuery(controls).find(".page").index(current);
			updateindex = currentindex + 1;
			update = jQuery(controls).find(".page:eq("+updateindex+")");
			
			jQuery(current).removeClass('active');
			jQuery(update).addClass('active');
			
			jQuery(current).attr('src', '/fls/19900/site_graphics/page_off.png');
			jQuery(update).attr('src', '/fls/19900/site_graphics/page_on.png');
		}
	}
}

function stopRotate() {
	if (rotateTimer)
		clearTimeout(rotateTimer);
	setRotate = false;
}

function startRotate() {
	setRotate = true;
	rotateTimer = setTimeout("rotate();", rotatorTime);
}

function toggleRotate() {
	if (setRotate)
		stopRotate();
	else
		startRotate();	
}

function rotate() {
	if (setRotate) {
		activetab = jQuery("#rotator .subrotator .item.active");	
		activeindex = jQuery("#rotator .subrotator .item").index(activetab);
		nextindex = activeindex + 1;
		if (nextindex == 4)
			nextindex = 0;
		jumpto(jQuery("#rotator .subrotator .item:eq("+nextindex+")"));
	}
}

function sportrotate() {
	if (setRotate) {
		activetab = jQuery("#sportrotator .subrotator .item.active");	
		activeindex = jQuery("#sportrotator .subrotator .item").index(activetab);
		nextindex = activeindex + 1;
		if (nextindex == 5)
			nextindex = 0;
		sportjumpto(jQuery("#sportrotator .subrotator .item:eq("+nextindex+")"));
	}
}

function rotateForward() {
	activetab = jQuery("#rotator .subrotator .item.active");	
	activeindex = jQuery("#rotator .subrotator .item").index(activetab);
	nextindex = activeindex + 1;
	if (nextindex == 4)
		nextindex = 0;
	jumpto(jQuery("#rotator .subrotator .item:eq("+nextindex+")"));
}

function rotateBack() {
	activetab = jQuery("#rotator .subrotator .item.active");	
	activeindex = jQuery("#rotator .subrotator .item").index(activetab);
	nextindex = activeindex - 1;
	if (nextindex == -1)
		nextindex = 3;
	jumpto(jQuery("#rotator .subrotator .item:eq("+nextindex+")"));
}

function jumpto(tabobj) {
	tabindex = jQuery("#rotator .subrotator .item").index(tabobj);
	oldtab = jQuery("#rotator .subrotator .item.active");	
	oldindex = jQuery("#rotator .subrotator .item").index(oldtab);
	
	if (tabindex != oldindex) {
		jQuery(tabobj).addClass('active');
		jQuery(oldtab).removeClass('active');
		
		jQuery("#rotator .story:eq("+oldindex+")").fadeOut(500);
		jQuery("#rotator .story:eq("+tabindex+")").fadeIn(500);
	}
	
	if (rotateTimer)
		clearTimeout(rotateTimer);
	if (setRotate)
		rotateTimer = setTimeout("rotate();", rotatorTime);
}

function sportjumpto(tabobj) {
	tabindex = jQuery("#sportrotator .subrotator .item").index(tabobj);
	oldtab = jQuery("#sportrotator .subrotator .item.active");	
	oldindex = jQuery("#sportrotator .subrotator .item").index(oldtab);
	
	if (tabindex != oldindex) {
		jQuery(tabobj).addClass('active');
		jQuery(oldtab).removeClass('active');
		
		jQuery("#sportrotator .story:eq("+oldindex+")").fadeOut(500);
		jQuery("#sportrotator .story:eq("+tabindex+")").fadeIn(500);
	}
	
	if (rotateTimer)
		clearTimeout(rotateTimer);
	if (setRotate)
		rotateTimer = setTimeout("sportrotate();", rotatorTime);
}

function flyout(tabobj) {
	tab = jQuery(tabobj).parent();
	tabx = jQuery(tab).css('left').replace('px', '');
	if (tabx == -250) {
		jQuery(tab, this).animate({
			left: 0
		}, 500);	
		jQuery(tab).find(".close").show();
	}
	else if (tabx == 0) {
		jQuery(tab, this).animate({
			left: -250
		}, 500);	
		jQuery(tab).find(".close").hide();
	}
	else if (tabx == 953) {
		jQuery(tab, this).animate({
			left: 703
		}, 500);	
		jQuery(tab).find(".close").show();
	}
	else if (tabx == 703) {
		jQuery(tab, this).animate({
			left: 953
		}, 500);	
		jQuery(tab).find(".close").hide();
	}
}

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

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

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;
		}
	}
}

function videotab(tabObj) {
	oldtab = jQuery("#videos .tab.active");
	oldtabindex = jQuery("#videos .tab").index(oldtab);
	tabindex = jQuery("#videos .tab").index(tabObj);
	
	if (oldtab != tabObj) {
		jQuery(oldtab).removeClass('active');
		jQuery(tabObj).addClass('active');
		jQuery("#videos .arrow:eq("+oldtabindex+")").removeClass('active');
		jQuery("#videos .arrow:eq("+tabindex+")").addClass('active');
		jQuery("#videos .box:eq("+oldtabindex+")").removeClass('active');
		jQuery("#videos .box:eq("+tabindex+")").addClass('active');
		
		if (jQuery("#videos .box.active").find(".loading").length > 0) {
			if (tabindex == 1) {
				filename = 'replays.dbml';	
			}
			else if (tabindex == 2) {
				filename = 'highlights.dbml';	
			}
			jQuery.ajax({
				type:"POST",
				url:getBaseUrl()+"/tabs/"+filename,
				data: "DB_OEM_ID="+oemid+"&SPID="+spid+"&SPSID="+spsid+"&key="+key+"&is_home_page="+ishome,
				success: function(data) {
					jQuery("#videos .box:eq("+tabindex+")").html(data);
				}
				,error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert("errorThrown:"+errorThrown);
				}
			});
		}
	}
	
}

/******************************************************************************
	START cal-slider
******************************************************************************/

function setSliderUlWidth() {
	var sliderContent = jQuery("#cal-event-slider-content");
	var liTags = jQuery("li",sliderContent);
	//oneLiWidth = jQuery("#cal-event-slider-content:eq(0)").width();
	oneLiWidth = 158;
	oneLiMargin = 8; //4 left 4 right
	oneLiTotalWidth = oneLiWidth + oneLiMargin;
	newUlWidth  = oneLiTotalWidth * liTags.length;
	jQuery("ul",sliderContent).css("width",newUlWidth+"px");
	return newUlWidth;
}

function centerSliderUl(sliderContentUl) {
	for(x=0;x<jQuery("li",sliderContentUl).length;x++) 
		if(jQuery("li:eq("+x+")",sliderContentUl).hasClass("center"))
			return x;	
	return 0;
}

/******************************************************************************
	END cal-slider
******************************************************************************/

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

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