NICK.namespace("homepage");	

$(document).ready(function(){	
		var toutsPerColumn = 5;
		var totalGameTouts = $(".hpGameList li").length -1;
		var gamesToShow = NICK.utils.getNoRepeatRandoms(toutsPerColumn,totalGameTouts);
		$(".hpGameList li").each(function (i) {
		    //i = i+1;
			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").length -1;
		var videosToShow = NICK.utils.getNoRepeatRandoms(toutsPerColumn,totalVideoTouts);
		$(".hpVidList li").each(function (i) {
		    //i = i+1;
			currLi = $(this);
			currLi.hide();
		    jQuery.each(videosToShow, function() {
			    
				if(this == i){
					currLi.show();
					//NICK.utils.doLog("true " + i);
				}else{
					//currLi.hide();
				}
		      });
		});	

		NICK.homepage.loadShoutbox('video');
		NICK.homepage.loadShoutbox('game');
		NICK.homepage.initVideoPlayIcon();
		NICK.homepage.getWhatsOn();
	});	


NICK.homepage.loadShoutbox = function(type,start,stop,imgpath) {

	var limit=20;
	var start 	= start ? start : null;
	var stop 	= stop ? stop : limit;
	var comments = null;
	
	var commentCall = '/api/comments/1.0/search?parentSiteAlias=site-nicktoons&parentContentSuperType='+type+'s';
	if (start) commentCall += '&start='+start;
	if (stop) commentCall += '&rows='+stop;
	
	$.ajax({
		type:"get",
		url:commentCall,
		dataType:"json",
		success: function(c){ NICK.homepage.displayShoutbox('shoutbox',c,limit,type);},
		error: function(m) {return 'no comments'}
	})
	
	if (comments) return comments;
}

NICK.homepage.displayShoutbox = function(type,json_comments,l,itemType){

	var comlist ='';

	if (json_comments.response.numFound > 0) {
		if(json_comments.response.numFound >= 19){
			var topNum = 19;
		}else{
			var topNum = json_comments.response.numFound;
		}
		var commentsToShow = NICK.utils.getNoRepeatRandoms(3,topNum);	
		
			//for (q = 0; q < l; q++) {
			jQuery.each(commentsToShow, function(q) {
				NICK.utils.doLog("this: " + this);
				NICK.utils.doLog("docs"+ json_comments.response.docs[this]);
				if(json_comments.response.docs[this] != undefined ||json_comments.response.docs[this] !=null){
				current_type = json_comments.response.docs[this].parentContentSuperType_t;
				current_com = json_comments.response.docs[this];
				thumbnail = (current_com.wideThumbnail_t) ? NICK.utils.getImage(current_com.wideThumbnail_t[0],25):'';
				comlist += NICK.homepage.buildCommentLI(
								type, 
								current_com.memberName_t, 
								current_com.comment_t, 
								thumbnail,
								current_com.parentTitle_t, 
								current_com.parentUrlAlias_t,
								current_type
							);
				}
			});
		} else {
				// if no shoutbox data, pull the shoutbox
				//$("#game-comments").hide();
				return false;
		}
	
		if(itemType == "game"){
			$("#game-comments").html(comlist);
		}else{
			$("#video-comments").html(comlist);
		}
		return false;
}

NICK.homepage.buildCommentLI=function(type,username,usercomment,s_image,s_gametitle,s_gamelink,contenttype) {

				var shoutlink = NICK.homepage.buildShoutLink(contenttype,s_gamelink);
				if(String(usercomment).length > 80){
					var newText = String(usercomment).slice(0,80);
					newText += '...';
				}else{
					var newText = usercomment;
				}
				var licontent = '<li class="clearfix">'+
									//<img src="/assets/homepage/placeholder_avatar.jpg" />
				'<div class="name-container username">'+username+'</div>'+
									'<div class="bubble">'+
										
										'<span class="title"><a href="'+shoutlink+'">'+s_gametitle+'</a></span>'+
										'<p class="comment">'+newText+'</p>' +
									'</div>'+
								'</li>';
	return licontent;
}

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 nick_url = NICK.utils.getNickDomain();
	var dataURL = "http://"+ nick_url + "/sbcom/data/json/whats_on.jhtml?channelID=37";
	 //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]);
			}
		}				
	});
}

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);		
	}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=37";
	
	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-6) >= 0){
						whatsOn = schedData[i-6];
					}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));
	
	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){
	/*
	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] + '" width="66" height="37"/>');
			}else{
				//$("#whatsOnIcon").remove();
				$("#current-tv-show-title span").css({'padding-left' : '10px'});
			}
		},
		onFail: function(errors) {
			for(var error in errors) {
				NICK.utils.doLog("JSON Response: Error: "+error+" - "+errors[error]);
			}
		}				
	});	*/
	
}
