MTV.Backchannel = new function(){
	var tzCookieName = "bczone";
	
	this.init = function(){
		// initialize timezone dropdowns
		$j(".bc-timezone select").each(function(i){
			$j(this).change(MTV.Backchannel.selectTimeZone);
		});
		
		// handle cookied timezone value
		var tzCookieVal = MTV.Utils.Cookies.read(tzCookieName);
		$j(".bc-timezone select option").each(function(i){
			if(this.value == tzCookieVal) this.selected = true;
			else this.selected = false;
		});
		$j(".bc-timezone select").change();
		
		MTV.Backchannel.loadGameTimeChecker();
		MTV.Backchannel.handleUserInfo();
	}

	this.playerSearch = function(){
		//alert('hello');
		var container = $j("#searchFormResults");
		var params = {};
		params.q = document.searchUser.q.value;
		var waitImg = $j("<img/>").attr("src", "/content/ontv/backchannel/images/waitImg.gif").attr("style", "margin-left:110px;");
		if(container.is("*")){
			container.empty().append(waitImg);
			$j.get("/ontv/backchannel/show/ajax/player_search.jhtml", params, function(data){
				container.empty().append(data);
			});
		}
	}
	
	this.selectTimeZone = function(){
		var tz = this.value;
		
		// update all dropdowns
		$j(".bc-timezone select option").each(function(i){
			if(this.value == tz) this.selected = true;
			else this.selected = false;
		});
		
		// handle main module on homepage
		var tzMainId = "timezoneMain" + tz;
		$j("#mdlMain ol").each(function(i){
			if(tzMainId == this.id) $j(this).removeClass("hide");
			else $j(this).addClass("hide");
		});
		
		// handle Next Game module wherever
		var tzMainId = "timezoneNext" + tz;
		$j("#mdlNext table").each(function(i){
			if(tzMainId == this.id) $j(this).removeClass("hide");
			else $j(this).addClass("hide");
		});
		
		// handle table on homepage
		var tzTableId = "timezoneTable" + tz;
		$j("#mdlTable table").each(function(i){
			if(tzTableId == this.id) $j(this).removeClass("hide");
			else $j(this).addClass("hide");
		});
		
		MTV.Utils.Cookies.create(tzCookieName, tz, 365);
	}
	
	this.loadGameTimeChecker = function(){
		var gameTimeChecker = $j('<div id="gameTimeChecker"></div>').css({position:"absolute", top:"0", left:"-999em"});
		$j("body").append(gameTimeChecker);
		
		var configUrl;
		var pathname = window.location.pathname;
		configUrl = "http://backchannel.mtv.com/feeds/playerTime";
		//if(window.location.href.indexOf("x-d") > 0) configUrl = "http://localhost/servertime.php?bingo=1232643000"; //launch day
		if(window.location.href.indexOf("x-jd") > 0) configUrl = "http://backchannel.mtv-d.mtvi.com/feeds/playerTime";
		if(window.location.href.indexOf("v-jd") > 0) configUrl = "http://backchannel.mtv-d.mtvi.com/feeds/playerTime";
		if(window.location.href.indexOf("-jq") > 0) configUrl = "http://backchannel.mtv-q.mtvi.com/feeds/playerTime";
		//alert(configUrl);
		var gameTimeCheckerSwf = new SWFObject("/ontv/backchannel/components/checkGameTime.7sec.swf","gameTimeCheckerSwf", "2", "2", "8", "#cccccc");
		gameTimeCheckerSwf.addVariable("CONFIG_URL", configUrl);
		gameTimeCheckerSwf.write("gameTimeChecker");
	}
	
	this.handlePlayNow = function(nextGameTime){
		var containers = $j(".ngt" + nextGameTime);
		var button = $j("<img/>").attr("src", "/content/ontv/backchannel/real_world_brooklyn/images/play-now.gif");
		var buttonLink = $j("<a/>").attr("href", "playgame.jhtml").append(button);
		containers.empty().append(buttonLink);
	}
	
	this.handlePlayNoMore = function(nextGameTime){
		var containers = $j(".ngt" + nextGameTime);
		var button = $j("<img/>").attr("src", "/content/ontv/backchannel/real_world_brooklyn/images/game-closed.gif");
		containers.empty().append(button);
	}
	
	this.handleUserInfo = function(){
		var params = {};
		if(Flux.Context._isCommunityMember){
			params.ucid = Flux.Context._loggedUserUcid;
			var profileNavItem = $j("#n-bc-profile a");
			var profileLink = profileNavItem.attr("href") + "?ucid=" + Flux.Context._loggedUserUcid;
			profileNavItem.attr("href", profileLink);
		}

		var container = $j("#user");
		if(container.is("*")){			
			$j.get("/ontv/backchannel/ajax/user.jhtml", params, function(data){
				container.append(data);
			});
		}
	}
}

var changeToPlayButton = MTV.Backchannel.handlePlayNow;
var removePlayButton = MTV.Backchannel.handlePlayNoMore;

MTV.initArray.push(MTV.Backchannel.init);