NICK.namespace("homepage");	

$(document).ready(function(){	
		var toutsPerColumn = 6;
		var totalGameTouts = $(".hpGameList li").size() - 1;
		var gamesToShow = NICK.utils.getNoRepeatRandoms(toutsPerColumn,totalGameTouts);
		$(".hpGameList li").each(function (i) {
			currLi = $(this);
			currLi.hide();
		    jQuery.each(gamesToShow, function() {
			    
				if(this == i){
					currLi.show();
					//NICK.utils.doLog("true " + i);
				}else{
					//currLi.hide();
				}
		      });
		});
		
		var totalVideoTouts = $(".hpVidList li").size() - 1;
		var videosToShow = NICK.utils.getNoRepeatRandoms(toutsPerColumn,totalVideoTouts);
		$(".hpVidList li").each(function (i) {
			currLi = $(this);
			currLi.hide();
		    jQuery.each(videosToShow, function() {
			    
				if(this == i){
					currLi.show();
					//NICK.utils.doLog("true " + i);
				}else{
					//currLi.hide();
				}
		      });
		});
		
		try{
			NickComments.setAvatarSize( 100, 80 );
			NickComments.setCollectionHTML( 'UserGameComments', {
				parentContentSuperType: 'games',
				titles: true,
				start: 0,
				rows: 3
			});
			NickComments.setCollectionHTML( 'UserVideoComments', {
				parentContentSuperType: 'videos',
				titles: true,
				start: 0,
				rows: 3
			});
		}catch(error){
			NICK.utils.doLog("Comments Catch:: "+error);
		  }
		/* Disabled this functionality for the static icon*/
		/*NICK.homepage.initVideoPlayIcon();*/
		NICK.homepage.getWhatsOn();
	});	

NICK.homepage.buildShoutLink= function(ctype,link) {
	var l = "";
	
	if (ctype=='video')  l = '/videos/clip/'+link+'.html'; 
	if (ctype=='game')  l = '/games/'+link+'.html'; 
	if (ctype=='playlist')  l = '/videos/playlist/'+link+'.html'; 

	return l;
}

NICK.homepage.getWhatsOn = function(){
	var channel_id = NICK.config.CHANNEL_ID;
	if ( $("#current-tv-show").size() == 0 ) {
		if( $("#current-tv-show-nickatnite").size() == 0){
			return false;
		}else{
			/* get current time to determine what schedule to display on nickatnite */
			var showtime = new Date();
			var utc = showtime.getTime() + (showtime.getTimezoneOffset() * 60000);
			
			var newDate = new Date(utc - (3600000*4));
			
			if((newDate.getHours() > 19) || (newDate.getHours() < 7)){
				channel_id = '41';
			}else{
				channel_id = '11';
			}
		}
	}
	
	var nick_url = NICK.utils.getNickDomain();
	var dataURL = "http://"+ nick_url + "/sbcom/data/json/whats_on.jhtml?channelID=" + channel_id;
	 //var dataURL = "http://" + nick_url + "/sbcom/data/json/broadcast_day_listing.jhtml?channelID=11";

	NICK.request.doRequest({
		dataType:"jsonp",
		url: dataURL,
		onSuccess: function(response) {	
			NICK.homepage.displayWhatsOn(response.data);
		},
		onFail: function(errors) {
			for(var error in errors) {
				NICK.utils.doLog("JSON Response: Error: "+error+" - "+errors[error]);
				$("#current-tv-show").remove();
			}
		}				
	});
}

NICK.homepage.initVideoPlayIcon = function() {
	/*$(".hpVidList .with-nick-tooltip")
		.unbind("mouseenter", NICK.homepage.addVideoPlayIcon)
		.unbind("mouseleave", NICK.homepage.removeVideoPlayIcon)
		.bind("mouseenter", NICK.homepage.addVideoPlayIcon)
		.bind("mouseleave", NICK.homepage.removeVideoPlayIcon);
	*/
}

NICK.homepage.addVideoPlayIcon = function(j) {
	var icon = $("<div />").attr("id", "video-overlay-icon").appendTo($(this).children("a:first"));
	if ( $(this).parent().parent().parent().hasClass("thumbnail-touts") ) {
		icon.addClass("large-icon");
	}
	var img = $(this).find("img");
	icon.css({
		top: Math.round(img.height() / 2) - Math.round(icon.height() / 2) + "px",
		left: Math.round($(this).width() / 2) - Math.round(icon.width() / 2) - 70 + "px"
	});
};

NICK.homepage.removeVideoPlayIcon = function() { $("#video-overlay-icon").remove(); }

NICK.homepage.displayWhatsOn = function(schedData){
	var timeZone = NICK.utils.getTimezone();
	var whatsOn = schedData[0];
	var showTime = new Date(Date.parse(whatsOn.CalendarAirTime));
	if(timeZone == "east"){	
		var woHtml = NICK.homepage.getWhatsOnHTML(whatsOn);
		$("#current-tv-show-title").html(woHtml);
		//NICK.utils.doLog(NICK.homepage.getWhatsOnImage);
		NICK.homepage.getWhatsOnImage(whatsOn.seriesId,whatsOn.seriesName);		
	}else{
		NICK.homepage.whatsOnWest(showTime);
	}
}

NICK.homepage.whatsOnWest = function(eastTime){
	NICK.homepage.eastTime = eastTime;
	var nick_url = NICK.utils.getNickDomain();
	var dataURL = "http://" + nick_url + "/sbcom/data/json/broadcast_day_listing.jhtml?channelID=" + NICK.config.CHANNEL_ID;
	
	NICK.request.doRequest({
		dataType:"jsonp",
		url: dataURL,
		onSuccess: function(response) {
			NICK.homepage.displayWest(response.data);
		},
		onFail: function(errors) {
			for(var error in errors) {
				NICK.utils.doLog("JSON Response: Error: "+error+" - "+errors[error]);
			}
		}				
	});	
}

NICK.homepage.displayWest = function(schedData){
	var whatsOn;
		jQuery.each(schedData, function(i) {
			
			var showTime = new Date(Date.parse(schedData[i].CalendarAirTime));
			
			if(NICK.homepage.eastTime.getHours() == showTime.getHours()){
				if(NICK.homepage.eastTime.getMinutes() == showTime.getMinutes()){
					if((i) >= 0){
						whatsOn = schedData[i];
					}else{
						whatsOn = null;
					}
						
				}
			}
			
		});
		
		if(whatsOn != null){
			var woHtml = NICK.homepage.getWhatsOnHTML(whatsOn);
			$("#current-tv-show-title").html(woHtml);
			//NICK.utils.doLog(NICK.homepage.getWhatsOnImage);
			NICK.homepage.getWhatsOnImage(whatsOn.seriesID);
		}else{
			var woHtml = '<span class="title">Tune In Tomorrow!</span>';
		}
		
		$("#current-tv-show-title").html(woHtml);
}

NICK.homepage.getWhatsOnHTML = function(whatsOn){
	var showTime = new Date(Date.parse(whatsOn.CalendarAirTime));
	if(showTime == 'Invalid Date'){$("#current-tv-show").remove(); return false;}

	
	var showName = unescape(whatsOn.seriesName).replace(/\+/g, " ");
	var a_p = "";
	
	var curr_hour = showTime.getHours();
	var curr_min = showTime.getMinutes();

	if (curr_hour < 12){
	   a_p = "AM";
	}else{
	   a_p = "PM";
	}
	if (curr_hour == 0){
	   curr_hour = 12;
	}
	if (curr_hour > 12){
	   curr_hour = curr_hour - 12;
	}
	
	
	if(curr_min == 0){
		curr_min = '00';
	}
		
	var woHtml = '<span class="title">' + curr_hour + ':' + curr_min + a_p + ' | ' + showName + '</span>';
	
	return woHtml;
}

NICK.homepage.getWhatsOnImage = function(seriesId,seriesName){
	var nick_url = NICK.utils.getNickDomain();
	var dataURL = "http://"+ nick_url + "/sbcom/data/scenic/show_series_data.jhtml";
	var channelId = 11;
	var key =  "showThumb_" + seriesId;
	//NICK.utils.doLog(key);
	
	NICK.request.doRequest({
		dataType:"jsonp",
		url: dataURL,
		onSuccess: function(response) {
		//NICK.utils.doLog(response.data[key]);
			if(response.data[key] != "" && response.data[key] != null){
				$("#current-tv-show-title").prepend('<img src="' + response.data[key] + '" alt="' + seriesName + '" width="66" height="37"/>');
			}
		},
		onFail: function(errors) {
			for(var error in errors) {
				NICK.utils.doLog("JSON Response: Error: "+error+" - "+errors[error]);
			}
		}				
	});	
	
}

