/**
 * @author jordand
 */


if(typeof NICK == "undefined" || !NICK) var NICK = {};
NICK.namespace("showsNav");

$(document).ready(function() {
	
	$(document).bind("authStatus loggedIn", function(response){
		NICK.showsNav.getFavorites();
	});

	$(document).bind("loggedOut", function (){
		NICK.club.favorites.resetFavoriteButtonText();
	});
	
	//specials nav selected states	
	if (window.location.pathname.indexOf("specials/") !== -1) {
		if ($("#nav").length > 0) {
			NICK.showsNav.setSpecialsNav();
		}		
	}	

	
/*
 * Added for KCA Show nav pulled from poll tool
 */	
	if($('#nav-east')){NICK.showsNav.setKCANav();}
	
 	if (KIDS.get("site") != "TeenNick") {
		NICK.showsNav.getFavorites();
	}
 	
});


NICK.showsNav.showSubnav = function(divID) {
	$("#show-bar sub").css("display","none");
	$("#"+divID).css("display","block");
	$("#"+divID).css("z-index","200");
};
NICK.showsNav.hideSubnav = function(divID) {
	$("#"+divID).css("display","none");
};

NICK.showsNav.getFavorites = function(section) {
	
	if (typeof NICK.login != "undefined" && NICK.login.isLoggedIn()) {
		var nick_url = NICK.utils.getNickDomain();
		NICK.request.doRequest({
			dataType: "jsonp",
			url: "http://" + NICK.utils.getNickDomain() + "/sbcom/data/profile/nick/shows_paginated.jhtml",
			data: {username: NICK.login.getNickName(), start:'1', max:'100'},
			onSuccess: function(response){
				var currentShow = NICK.get("fccRelatedShow");
				for (var favShow in response.list) {
					var favShow = response.list[favShow];
					matchShow = favShow.fccRelatedShow;
					matchShow = matchShow.split(":");
					matchShow = matchShow[matchShow.length-1];
					NICK.utils.doLog("favShow: "+matchShow);
					if(currentShow == matchShow){
						NICK.utils.doLog("match favShow: "+matchShow);
						$("#become-a-fan").addClass("isFan");
						$("#become-a-fan a").html($("#become-a-fan a").html().replace('Become a',"You are a") );
						break;
					}
				}
			}
		});
	}
};


/*
 * Added for KCA Show nav pulled from poll tool
 */
NICK.showsNav.setKCANav = function(){
	var tz = NICK.utils.getTimezoneKCA();
	if(tz == 'west'){
		$('#nav-east').hide();
		$('#nav-west').show();
	}else{
		$('#nav-east').show();
		$('#nav-west').hide();		
	}
	
	var navItems = $('#nav-' + tz + ' td').size();
	$('#nav-' + tz + ' a').each(
			function(){
				var thisLink = $(this).attr("href").substring(0, $(this).attr("href").length - 1);
				NICK.utils.doLog(thisLink);
				if( window.location.pathname.indexOf(thisLink) !== -1 ){
					if(thisLink == '/kids-choice-awards'){
						if($("body").attr("id") == 'kca-home'){
							$(this).addClass('currentPage');
						}
					}else{
						$(this).addClass('currentPage');
					}
				}
	});

};
 
 NICK.showsNav.setSpecialsNav = function() {
	 var selectedSpecialButtons = 0;
	 $('#nav li a').each(
		function(){
			var thisLink = $(this).attr("href").substring(0, $(this).attr("href").length - 1);
			if( window.location.pathname.indexOf(thisLink) !== -1 ){
				$(this).parent().addClass('selected');
			} else {
				if (window.location.pathname.indexOf("/characters/") !== -1 && thisLink.indexOf("/characters/") !== -1){
					$(this).parent().addClass('selected');
				}
			}
		});	 

	 $('#nav li.selected').each(function(){selectedSpecialButtons++;});	 
	 
	 if (selectedSpecialButtons > 1) {
		 //clear the home nav selection, we have more then one selected button
		 $('#nav li').first().removeClass('selected');
	 }
	 
 };




