jQuery(window).load(function () {
	setTimeout("clearInput('#search-input')",100);	//clear search box after page load
	setTimeout("clearInput('#login-box input.email')",100);	//clear search box after page load
	setTimeout("clearInput('#login-box input.password')",100);	//clear search box after page load
	setTimeout("clearInput('#Login_ID')",100);	//clear search box after page load
	fixLeftColumnHeight();
});

jQuery(document).ready(function(){
	jQuery("#search-input").focus(function() {
		jQuery("#search-input").css("background","#212121");
	});	
	jQuery("#search-input").blur(function() {
		if(jQuery("#search-input").val()=='') 
			jQuery("#search-input").css("background-image","url(/fls/23600/site_graphics/bg-search.jpg)").css("background-repeat","no-repeat");
	});
	/**/
	jQuery("#login-box input.email").focus(function() {
		jQuery("#login-box input.email").css("background","#212121");
	});	
	jQuery("#login-box input.email").blur(function() {
		if(jQuery("#login-box input.email").val()=='') 
			jQuery("#login-box input.email").css("background-image","url(/fls/23600/site_graphics/bg-username.jpg)").css("background-repeat","no-repeat");
	});
	jQuery("#login-box input.password").focus(function() {
		jQuery("#login-box input.password").css("background","#212121");
	});	
	jQuery("#login-box input.password").blur(function() {
		if(jQuery("#login-box input.password").val()=='') 
			jQuery("#login-box input.password").css("background-image","url(/fls/23600/site_graphics/bg-password.jpg)").css("background-repeat","no-repeat");
	});
	jQuery("#Login_ID").focus(function() {
		jQuery("#Login_ID").css("background","#212121");
	});	
	jQuery("#Login_ID").blur(function() {
		if(jQuery("#Login_ID").val()=='') 
			jQuery("#Login_ID").css("background-image","url(/fls/23600/site_graphics/enews-bg.jpg)").css("background-repeat","no-repeat");
	});
	fixLeftColumnHeight();
	/*
	//support parent menu active image background changing
	jQuery("li",listMenu).mouseover(function () {
		jQuery(this).addClass("active");
		jQuery(this).bind("mouseleave", function(e){
      		jQuery(this).removeClass("active");
    	});
    });
    */
    jQuery("#photo-album-thumbs .album-item").mouseover(function() {
    	jQuery(this).addClass("active");
    	jQuery(this).bind("mouseleave",function(e) {
    		jQuery(this).removeClass("active");
    	});
    });
    jQuery("#video-item-thumbs .video-item").mouseover(function() {
    	jQuery(this).addClass("active");
    	jQuery(this).bind("mouseleave",function(e) {
    		jQuery(this).removeClass("active");
    	});
    });
   preloadMenuRollovers();
});

function preloadMenuRollovers() {
	var baseUrl = getBaseUrl();
	var menuImages = new Array();
	menuImages[0] = baseUrl + "/fls/23600/site_graphics/menu/menu-home-on.jpg";       
	menuImages[1] = baseUrl + "/fls/23600/site_graphics/menu/menu-schedule-on.jpg";
	menuImages[2] = baseUrl + "/fls/23600/site_graphics/menu/menu-news-on.jpg";
	menuImages[3] = baseUrl + "/fls/23600/site_graphics/menu/menu-fighters-on.jpg";
	menuImages[4] = baseUrl + "/fls/23600/site_graphics/menu/menu-results-on.jpg";
	menuImages[5] = baseUrl + "/fls/23600/site_graphics/menu/menu-video-on.jpg";
	menuImages[6] = baseUrl + "/fls/23600/site_graphics/menu/menu-photos-on.jpg";
	menuImages[7] = baseUrl + "/fls/23600/site_graphics/menu/menu-community-on.jpg";
	menuImages[8] = baseUrl + "/fls/23600/site_graphics/menu/menu-about-on.jpg";
	menuImages[9] = baseUrl + "/fls/23600/site_graphics/menu/menu-shop-on.jpg";
	menuImages[10] = baseUrl + "/fls/23600/site_graphics/menu/menu-tickets-on.jpg";
	menuImages[11] = baseUrl + "/fls/23600/site_graphics/menu/menu-tournaments-on.jpg";
	for(var i=0;i<menuImages.length;i++) {
		var newImage = new Image();
		//console.log(menuImages[i]);
		newImage.src = menuImages[i];	
	}		
}

function fixLeftColumnHeight() {
	var leftColHeight = jQuery("#left-main-content").height();	
	var rightColHeight = jQuery("#right-col").height();	
	if(rightColHeight>leftColHeight) {
		jQuery("#left-main-content").css("height",rightColHeight+"px");
	}
}

function clearInput(jSelector) {
	jQuery(jSelector).val('');
}

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

function playArticleVideo(videoId) {
	//jQuery(".play-button").hide();
	jQuery("#neulion-video").css("display","block");
    neulionPlayer(videoId);
    jQuery("#close-player").show();
}


/******************************************************************************
	START Twitter
******************************************************************************/
jQuery(document).ready(function () {
	if(jQuery("#community").length>0) {
		getLastTweet();	
	}
});

function getLastTweet() {
	var num = 3;
	if(jQuery("body").hasClass('community'))
		num = 6;
	jQuery("#tweets").html('');
	jQuery.ajax({
		type:"post",
   		url: getBaseUrl()+"/ajaxLastTweet.dbml",
   		dataType: 'json',
   		data: "DB_OEM_ID=23600&NUM_TWEETS="+num,
   		success: function(data){
   			if(data) 
   				for(var x=0;x<data.length;x++) 
   					addTwit(data[x].title,data[x].date);   			
   		}
 	});
}

function addTwit(twitterText,twitterDate) {
	var start = twitterText.indexOf("http://");
	if(start>0) {
		for(var x=start;x<=twitterText.length;x++) {
			var currentCharacter = twitterText[x];	//non-IE browsers
			if(currentCharacter = 'undefinied')
				currentCharacter = twitterText.charAt(x);	//IE only
			if(currentCharacter==" ") {
				var end = x;
				break;
			}	
			var end = x;
		}				
		var linkText = twitterText.substring(start,end); 
		var newLinkText = '<a target="_BLANK" class="twitter-link" href="'+linkText+'">'+linkText+'</a>';
		var twitterText = twitterText.replace(linkText,newLinkText); 
	}

	//List of twitter names to remove from start of twitter postings
	var replaceTwitterNames = new Array('BellatorMMA:');
	for(x=0;x<replaceTwitterNames.length;x++) {
		twitterText = twitterText.replace(replaceTwitterNames[x],"");
	}
	jQuery("#tweets").append('<div class="tweet">'+twitterText+'</div><div class="tweet-time">'+twitterDate+'</div>');	
}

/******************************************************************************
	END Twitter
******************************************************************************/

/******************************************************************************
	START rotator
******************************************************************************/

var rotate = true;
var rotateStart = 0;
var rotateMax = 4;
var rotateTimer = 8000;
var rotateGroupTable = new Array();
rotateGroupTable[0] = 1;
rotateGroupTable[1] = 1;
rotateGroupTable[2] = 1;
rotateGroupTable[3] = 1;
rotateGroupTable[4] = 2;
rotateGroupTable[5] = 2;
rotateGroupTable[6] = 2;
rotateGroupTable[7] = 2;

jQuery(document).ready(function () {
	jRotator = jQuery("#rotator");	//local cache jquery object
	if(jRotator.length>0) 
		buildRotator();
	jQuery("div.box",jRotator).click(function () {
		stopRotate();
		selectRotItem(this);
		
	});
	jQuery("#prevBtn").click(function () {
		stopRotate();
	});
	jQuery("#nextBtn").click(function () {
		stopRotate();
	});
});

function selectRotItem(jThis) {
	//console.log(jThis);
	jQuery(".red",jRotator).removeClass("selected");
	jQuery("div.box .red",jRotator).hide();	
	jQuery("div.box .dark",jRotator).show();
	jQuery(".red",jThis).show();	
	jQuery(".red",jThis).addClass("selected");
	jQuery(".dark",jThis).hide();	
	/**/
	for(x=0;x<jQuery("div.box",jRotator).length;x++) 
		if(jQuery(".red:eq("+x+")",jRotator).hasClass('selected')) {break;}
	rotSelectItem(x,false);
}

function setupRotatorEasySlider() {
	jQuery("#rotator-slider").easySlider({
		trigger: 'slider-data',
		firstShow: true,
		lastShow: true,
		auto: false,
		continuous: true,
		pause: 10000,
		speed: 1000
	});
	jQuery("#firstBtn").hide();
	jQuery("#lastBtn").hide();
}

function rotSelectItem(num,first) {
	if(rotatorData[num]['video-id']!='') {
		jQuery(".play-button",jRotator).fadeIn(500);
	}
	else {
		jQuery(".play-button",jRotator).fadeOut(500);
	}
	
	if(rotatorData[num]["article-id"]>' ') {
		rotatorData[num]["link-url"] = '/ViewArticle.dbml?DB_OEM_ID=23600&ATCLID='+rotatorData[num]["article-id"];
	}

	if(first) {
		if(rotatorData[num]["link-url"]>' ') {
			jQuery(".text-overlay .headline",jRotator).html('<a href="'+rotatorData[num]["link-url"]+'">'+rotatorData[num]['headline']+'</a>').css("opacity","1");
			jQuery(".text-overlay .sub-headline",jRotator).html('<a href="'+rotatorData[num]["link-url"]+'">'+rotatorData[num]['sub-headline']+'</a>').css("opacity","1");
			jQuery(".text-overlay .link",jRotator).html('<a href="'+rotatorData[num]["link-url"]+'"><img class="red-arrow" alt="arrow" src="fls/23600/site_graphics/red-arrow.png"/>'+rotatorData[num]["link-text"]+'</a>').css("opacity","1");
			jQuery(".text-overlay",jRotator).animate({ height:"66px"},1000);	
		}
		else {
			jQuery(".text-overlay .headline",jRotator).html('<a onclick="rotPlayVideo()">'+rotatorData[num]['headline']+'</a>').css("opacity","1");
			jQuery(".text-overlay .sub-headline",jRotator).html('<a onclick="rotPlayVideo()">'+rotatorData[num]['sub-headline']+'</a>').css("opacity","1");
			jQuery(".text-overlay .link",jRotator).html('<a onclick="rotPlayVideo()"><img class="red-arrow" alt="arrow" src="fls/23600/site_graphics/red-arrow.png"/>'+rotatorData[num]["link-text"]+'</a>').css("opacity","1");
			jQuery(".text-overlay",jRotator).animate({ height:"66px"},1000);	
		}
	}
	else {
		if(rotatorData[num]["link-url"]>' ') {
			jQuery(".text-overlay",jRotator).animate({
				height:"0px"
			},500, function() {
		    	jQuery(".text-overlay .headline",jRotator).html('<a href="'+rotatorData[num]["link-url"]+'">'+rotatorData[num]['headline']+'</a>').css("opacity","1");
				jQuery(".text-overlay .sub-headline",jRotator).html('<a href="'+rotatorData[num]["link-url"]+'">'+rotatorData[num]['sub-headline']+'</a>').css("opacity","1");
				jQuery(".text-overlay .link",jRotator).html('<a href="'+rotatorData[num]["link-url"]+'"><img class="red-arrow" alt="arrow" src="fls/23600/site_graphics/red-arrow.png"/>'+rotatorData[num]["link-text"]+'</a>').css("opacity","1");
				jQuery(".bar",jRotator).show();
				jQuery(".text-overlay",jRotator).animate({ height:"66px"},500);	
			});		
		}
		else {
			jQuery(".text-overlay",jRotator).animate({
				height:"0px"
			},500, function() {
		    	jQuery(".text-overlay .headline",jRotator).html('<a onclick="rotPlayVideo()">'+rotatorData[num]['headline']+'</a>').css("opacity","1");
				jQuery(".text-overlay .sub-headline",jRotator).html('<a onclick="rotPlayVideo()">'+rotatorData[num]['sub-headline']+'</a>').css("opacity","1");
				jQuery(".text-overlay .link",jRotator).html('<a onclick="rotPlayVideo()"><img class="red-arrow" alt="arrow" src="fls/23600/site_graphics/red-arrow.png"/>'+rotatorData[num]["link-text"]+'</a>').css("opacity","1");
				jQuery(".bar",jRotator).show();
				jQuery(".text-overlay",jRotator).animate({ height:"66px"},500);	
			});
		}
		
	}
	if(first) {
		if(rotatorData[num]["link-url"]>' ')
			jQuery(".content .image",jRotator).prepend('<a href="'+rotatorData[num]["link-url"]+'"><img alt="article image" width="460" src="'+rotatorData[num]['image']+'" /></a>');
		else
			jQuery(".content .image",jRotator).prepend('<img alt="article image" width="460" src="'+rotatorData[num]['image']+'" />');
	}
	else {
		jQuery('.image img',jRotator).addClass("active");
		jQuery('.image img',jRotator).css('opacity','1').css("z-index","2");
		
		if(rotatorData[num]["link-url"]>' ')
			jQuery(".content .image",jRotator).prepend('<a href="'+rotatorData[num]["link-url"]+'"><img alt="article image" width="460" src="'+rotatorData[num]['image']+'" /></a>');
		else
			jQuery(".content .image",jRotator).prepend('<img alt="article image" width="460" src="'+rotatorData[num]['image']+'" />');
		
		jQuery(".image img.new",jRotator).css("opacity","1");
			
		jQuery(".image img.active",jRotator).animate({
			opacity:"0"	
		},1000,function () {
			jQuery(".image img.active",jRotator).remove();
			jQuery(".image img",jRotator).removeClass("active").removeClass("new");
		});
	}
}

function buildRotator() {
	/*
	setTimeout("jQuery('.small-image',jRotator).css('opacity','1')",10);	//fix IE
	setTimeout("jQuery('.image img',jRotator).css('opacity','1')",10);		//fix IE
	setTimeout("jQuery('.red',jRotator).css('opacity','1')",10);			//fix IE
	setTimeout("jQuery('.play-button',jRotator).css('opacity','1')",10);	//fix IE
	*/
	//setTimeout("jQuery('.text-overlay',jRotator).css('opacity','1')",10);	//fix IE
	//jQuery('.text-overlay',jRotator).fadeIn(100).css('opacity','1');

	var groupSize = 4;
	var currentGroup = 1;
	var total = rotatorData.length;
	var numGroups = Math.ceil(total/groupSize);
	var groupItemCount = 0;
	var x = 0;

	jQuery("#rotator-slider",jRotator).append('<ul></ul>');	
	for(x=0;x<numGroups;x++) {jQuery("ul",jRotator).append('<li></li>');}
	for(x=0;x<rotatorData.length;x++) {
		if(groupItemCount==groupSize) {currentGroup ++;groupItemCount = 0;}
		createRotatorItem(rotatorData[x],(currentGroup-1));
		groupItemCount ++;
	}
	
	setupRotatorEasySlider();
	
	if(rotatorData.length>groupSize) {
		jQuery("#prevBtn",jRotator).show();	
		jQuery("#nextBtn",jRotator).show();	
	}

	jQuery(".red:eq(0)",jRotator).show();	
	jQuery(".red:eq(0)",jRotator).addClass("selected");
	jQuery(".dark:eq(0)",jRotator).hide();	
	
	rotSelectItem(0,true); 	//activate first item
	/*
	jRotator.animate({
		height:"359px",
		opacity:"1"	
	},1000,function() {
		//after animate

	});
	*/
	rotateMax = total;
	setInterval("rotateContent()",rotateTimer);
}		

function stopRotate() {
	rotate = false;
}

function doRotate() {
	/*
	var rotate = true;
	var rotateStart = 0;
	var rotateMax = 4;
	*/
	
	var currentItem = 0;
	//console.log("doRotate()");
	for(var x=0;x<jQuery("div.box",jRotator).length;x++) 
		if(jQuery(".red:eq("+x+")",jRotator).hasClass('selected')) {break;}
	currentItem = x;
	if(currentItem<(rotateMax-1))
		currentItem = currentItem + 1;
	else
		currentItem = rotateStart;
	if(rotateGroupTable[currentItem]==1)
		jQuery("#firstBtn").trigger("click");
	else 
		jQuery("#lastBtn").trigger("click");
	var jThis = jQuery("div.box:eq("+currentItem+")");
	selectRotItem(jThis);
}

function rotateContent() {
	//console.log("rotateContent()");
	if(rotate)	
		doRotate();
}

function createRotatorItem(item,currentGroup) {
	if(item['video-id']!='') {
		jQuery("li:eq("+currentGroup+")",jRotator).append('<div class="box"><img class="play" src="/fls/23600/site_graphics/small-play-button.png"/><img class="small-image" src="'+item['small-image']+'"/><div class="dark"></div><div class="red"><div class="arrows"><div class="left"></div><div class="right"</div></div></div></div>');	
	}
	else {
		jQuery("li:eq("+currentGroup+")",jRotator).append('<div class="box"><img class="small-image" src="'+item['small-image']+'"/><div class="dark"></div><div class="red"><div class="arrows"><div class="left"></div><div class="right"</div></div></div></div>');	
	}
}

function rotPlayVideo(videoId) {
	stopRotate();
	jQuery(".play-button",jRotator).hide();
	for(x=0;x<jQuery("div.box",jRotator).length;x++) 
		if(jQuery(".red:eq("+x+")",jRotator).hasClass('selected')) {break;}
	var videoId = rotatorData[x]['video-id'];
	jQuery("#neulion-video").css("display","block");
    neulionPlayer(videoId);
    jQuery("#close-player").show();
}
	
function rotStopVideo() {
	if(jQuery("#jtvshlembed").length>0) {
		try {document.getElementById('jtvshlembed').pauseVideo();}
		catch(ex) {/*setTimeout("rotStopVideo()",1000);*/}
	}
	jQuery("#close-player").hide();
	jQuery("#neulion-video").hide();
	jQuery("#jtvshlAdContainer").hide();
}
/******************************************************************************
	END rotator
******************************************************************************/

/******************************************************************************
	START top-rated-photos
******************************************************************************/
jQuery(document).ready(function () {
	jPhotos = jQuery("#top-rated-photos");
	if(jPhotos)
		buildTopRatedPhotos();
		
	jQuery(".control-left",jPhotos).click(function () {
		for(var x=0;x<jQuery(".top .image",jPhotos).length;x++) {
			if(jQuery(".top .image:eq("+x+")",jPhotos).hasClass("active")) {
				if(x==0)
					selectTopRatedPhotosItem(jQuery(".top .image",jPhotos).length - 1);
				else
					selectTopRatedPhotosItem(x - 1);
				break;	
			}	
		}
	});
	jQuery(".control-right",jPhotos).click(function () {	
		for(var x=0;x<jQuery(".top .image",jPhotos).length;x++) {
			if(jQuery(".top .image:eq("+x+")",jPhotos).hasClass("active")) {
				if(x==(jQuery(".top .image",jPhotos).length-1))
					selectTopRatedPhotosItem(0);
				else
					selectTopRatedPhotosItem(x + 1);
				break;	
			}	
		}
	});
	jQuery(".bottom .image",jPhotos).click(function () {
		jQuery(".bottom .image",jPhotos).removeClass("active");
		jQuery(this).addClass("active");
		for(var x=0;x<jQuery(".top .image",jPhotos).length;x++) {
			if(jQuery(".bottom .image:eq("+x+")",jPhotos).hasClass("active")) {
				selectTopRatedPhotosItem(x);
				break;	
			}	
		}
	});
});
	
function buildTopRatedPhotos() {
	selectTopRatedPhotosItem(0);

	jQuery(jPhotos).animate({
		height:"360px",
		opacity:"1"	
	},1000);
}

function selectTopRatedPhotosItem(num) {
	jQuery(".bottom .image",jPhotos).removeClass('active');
	jQuery(".bottom .image:eq("+num+")",jPhotos).addClass('active');	
	
	jQuery(".top .image",jPhotos).removeClass('active');
	jQuery(".top .image:eq("+num+")",jPhotos).addClass('active');	
	
	var title = jQuery(".top .data .title:eq("+num+")",jPhotos).html();
	var link = jQuery(".top .data .gallery-link:eq("+num+")",jPhotos).html();
	var buyPhoto = jQuery(".top .data .buy-photo:eq("+num+")",jPhotos).html();
	var content = title + " | " + link;
	if(buyPhoto>'')
		content = content + " | " + buyPhoto;
	jQuery(".top .info",jPhotos).html(content);
}






/******************************************************************************
	END top-rated-photos
******************************************************************************/

















