/**************************************************************/
/*************************** WIDGETS **************************/
/**************************************************************/


/********************** BASE WIDGET *********************************/
MTVM.widgets.BASE = function(){
	
	this.list = document.createElement('div');
	
	this.headerObj = $J("<h4></h4>").html("List").addClass("titleSmall grey");
	this.footerObj = $J("<div></div>").html("");
	
	this.__loadData	= function(options)
	{ 
		var options = options || {};
		var url = ('url' in options) ? options.url : '';
		var ajaxType = ('ajaxType' in options) ? options.ajaxType : 'GET';
		var data = ('data' in options) ? options.data : '';
		var dataType = ('dataType' in options) ? options.dataType : 'xml';
		var timeout = ('timeout' in options) ? options.timeout : 20000;
		var successHandler = ('successHandler' in options) ? options.successHandler : this.___successHandler;
		var errorHandler = ('errorHandler' in options) ? options.errorHandler : this.___errorHandler;
		
		try{
			$J.ajax({
				url: url,
				type: ajaxType,
				data: data,
				dataType: dataType,
				timeout: timeout,
				error: function(){ errorHandler(); },
				success: function(data) { successHandler(data); }
			});
		}
		catch(e){ MTVM.logger.log("BASE widget __loadData failed: " + e, 2); }
	}
		this.___errorHandler = function(){
			MTVM.logger.log('BASE WIDGET: Error loading XML document', 2);
		}
		this.___successHandler = function(data){
			//MTVM.logger.log(data);
		}
	
	this.__clearListHtml = function()
	{
		var l = $J(this.list);
		l.empty();
	}
	
	this.__buildListHtmlHeader = function(headerObj)
	{
		var l = $J(this.list);
		if (headerObj==null){
			l.prepend(this.headerObj);
		} else {
			if (typeof headerObj == 'object') l.prepend(headerObj); else l.prepend($J(headerObj));
		}
	}

	this.__buildListHtmlFooter = function(footerObj)
	{
		var l = $J(this.list);
		if (footerObj==null){
			l.append(this.footerObj);
		} else {
			if (typeof footerObj == 'object') l.append(footerObj); else l.append($J(footerObj));
		}
	}
	
	this.__convertToFriendlyUrl = function(artistAlias, videoId, videoTitle)
	{
			var urlPath = "";
			try{
				videoTitle = videoTitle.replace(/[^\w]/g, "_");
				videoTitle = videoTitle.replace(/_+/g, "_");
				videoTitle = videoTitle.toLowerCase();
				
				urlPath = "/"+artistAlias+"/videos/"+videoId+"/"+videoTitle;
			}
			catch(e)
			{
				MTVM.logger.log("Base widget: convertToFriendlyUrl failed " + e);
			}
			
			return urlPath;
	}

	this.__resetWidgetDiv = function(divId)
	{
		var cleansed = 0
		if(document.getElementById(divId) != null)
		{		
			document.getElementById(divId).innerHTML = "";
			cleansed = 1;
		}
		return cleansed;
	}
}
/************************************************************************/
/********************** END BASE WIDGET *********************************/
/************************************************************************/


/************************************************************************/
/******************* GET HD CONCERT/ PLAYLIST WIDGET  *******************/
/************************************************************************/
MTVM.widgets.getHDConcert = function(containerDivId, options)
{	
	var ptr = this; // self pointer	
	var myDataservice = '/sitewide/dataservices/playlistXML/';
	this.jsonData = null;
	this.playlistID = ('playlistID' in options) ? options.playlistID : ''; // playlistID id to construct playlistID mgid	
	this.containerDivId = containerDivId;
	this.options = options || {};	
	var ajaxParams = (this.playlistID != null) ? "playlistID="+this.playlistID+"&showComments=false" : "showComments=false";
	var emptyArrows = '<img src="/img/spacer.gif" width="18" height="27" border="0" />';
	
	this.size = 0;
	this.carousel = {}
	
	var headerObj = "";
	var footerObj = "";
	
	this.__buildHtmlList = function(artistAlias, videoId, videoTitle, artistName)
	{	
		var l = $J(this.list);
		var videoUrl = this.__convertToFriendlyUrl(artistAlias, videoId, videoTitle);
		var html ="";
		var playlistAddBtn = "";
		var tempNode = "";
		var customHtml ="";
		imagePath = "http://mtvmusic.mtvnimages.com/uri/"+MTVM.config.mgidPrefix+videoId+"?width=140&amp;height=105";

		videoUrl = "/"+MTVM.lang.urls.artist+videoUrl;
		
		if(typeof MTVN_PL != "undefined"){
			playlistAddBtn = "<div class=\"right\">"+MTVN_PL.AddButtonManager.renderButton(MTVM.config.mgidPrefix+videoId)+"</div><div class=\"clear\"></div>";
		}		
		
		try{
			customHtml += "<li class=\"hd-festivals\">"+
				"	<div>"+
				"	<div class=\"videoLinkContainer\"><a href=\""+videoUrl+"\" " +
				"onclick=\"document.location='"+videoUrl+"';\" "+
				"onmouseout=\"MTVM.services.ui.thumbFlyover.proc(this, event);\" " +
				"onmouseover=\"MTVM.services.ui.thumbFlyover.proc(this, event);\">"+
				"		<div class=\"videoLinkThumb\"><img height=\"105\" border=\"0\" width=\"140\" onerror=\"this.src='/sitewide/img/misc/metaSeperator.gif';\" alt=\"\" src=\""+imagePath+"\"/></div>"+
				"		<div class=\"videoLinkThumbBg\" style=\"display: none;\"><img height=\"105\" border=\"0\" width=\"140\" src=\"/sitewide/img/spacer.gif\"/></div>"+
				"		<div class=\"videoLinkPlay\" style=\"display: none;\"><img border=\"0\" class=\"playIcon\" src=\"/sitewide/img/spacer.gif\" alt=\"\"/></div></a>"+
				"	</div>"+
				"</div>"+
				"<div>"+
				"	<div class=\"lightGrey txtMed\" style=\"margin-top:3px;\"><a class=\"blue\" href=\""+videoUrl+"\"><strong>"+videoTitle+"</strong></a><br/>By <a href=\"/artist/"+artistAlias+"\" class=\"blue\">"+artistName+"</a></div>"+
				"</div>"+
			"</li>";

			if($J('#'+this.containerDivId+' ul').length == 0 )
			{	
				tempNode = $J('<ul></ul>').html("");
				tempNode.appendTo(this.list);
			}
			
			containerNode = $J('#'+ptr.containerDivId+' ul');
			tempNode = customHtml;
			containerHtml = containerNode[0].innerHTML + tempNode
			containerNode[0].innerHTML = containerHtml;

		}
		catch(e)
		{
			MTVM.logger.log("MTVM.widgets.getHDConcert widget build html list failed " + 
				artistAlias+ " - " + videoId+ " - " + videoTitle+ " - " + imagePath+ " - " + artistName + e, 2);
		}

    	try{
    		MTVM.util.generateThumbOverlay(videoId, VideoThumbOverlays);
    	}catch(e){
    		MTVM.logger.log("MTVM.widgets.getHDConcert widget generate overlay failed " + e, 2);
    	}

	}
	
	this.processData= function(json)
	{	
		var json = eval(json);
		ptr.jsonData = json;
		
		try{

			if(json.videos.length>0)
			{
				MTVM.logger.log("MTVM.widgets.getHDConcert widget:  processing JSON");
				
				for(var i=0;  i< json.videos.length; i++)
				{
					ptr.__buildHtmlList(json.videos[i].artistAlias, json.videos[i].videoID, json.videos[i].title, json.videos[i].artist);
				}
				
				ptr.size = json.videos.length;
				var widthOfList = $J("li.hd-festivals").length * 164;
				//console.log(widthOfList);
				
				ptr.carousel = new MTVM.services.ui.carousel("hdPerformances", {itemwidth:164, containerwidth: 984, size: ptr.size, leftTriggerID: 'hdperf-carousel-left-arrow', rightTriggerID: 'hdperf-carousel-right-arrow'});
				
				if (ptr.size <= 6){
					$J("#hdperf-carousel-left-arrow").html(emptyArrows);
					$J("#hdperf-carousel-right-arrow").html(emptyArrows);
				}
			}
		}
		catch(e){ MTVM.logger.log("MTVM.widgets.getHDConcert widget: processData failed " + e, 2); }
	}
	
	this.init = function(containerDivId){
		this.__clearListHtml();
		var l = this.list;
		var processData = this.processData;
		$J(l).appendTo("#"+containerDivId);		
		this.__loadData({url:myDataservice,data:ajaxParams,successHandler:processData, dataType: 'json'})
	}
	
	this.init(containerDivId);
}
MTVM.widgets.getHDConcert.prototype = new MTVM.widgets.BASE; // inherits

/************************************************************************/
/******************* END HD CONCERT/ GET PLAYLIST WIDGET *******\********/
/************************************************************************/




/************************************************************************/
/******************* MORE BY ARTIST  ***************************/
/************************************************************************/
MTVM.widgets.moreVideosByArtist = function(containerDivId, options)
{	
	var ptr = this; // self pointer	
	var myDataservice = '/sitewide/dataservices/artistInfo/getArtistVideos/';
	this.jsonData = null;
	this.maxItemsToShow = 6;
	this.totalShownItems = 0;	
	this.shownVids = new Array();
	this.videoId = ('videoId' in options) ? options.videoId : ''; // video id to construct video mgid
	var videoData = ('videoData' in options) ? options.videoData : '&targetSearch=all'; // params to pass in the ajax
	this.containerDivId = containerDivId;
	this.options = options || {};	
	var ajaxParams = (this.videoId != null) ? "artistAlias="+MTVM.contextValues.artistAlias+"&showComments=false" : "showComments=off";
	
	ajaxParams += videoData;
	
	var headerObj = '<h4 class="titleSmall grey">'+MTVM.lang.video.moreFrom+'</h4>';
	var footerObj = $J("<div></div>").html("<br/>").addClass("clear");
	
	
	this.__canBeAdded = function(randomIndex)
	{
		var result = "";
		var alreadyInArray =false;

		if(randomIndex != null && ptr.jsonData.videos[randomIndex].videoId != MTVM.contextValues.vid)//check if video id is the current one playing
		{
			for(var i=0; i<ptr.shownVids.length; i++) //check if video id already in shownVids array
			{		
				if(ptr.shownVids[i].videoId == ptr.jsonData.videos[randomIndex].videoId){alreadyInArray = true;}				
			}			
			
			if( alreadyInArray == false  && ptr.shownVids.length < ptr.maxItemsToShow)//check if already at max num of videos
			{
				ptr.shownVids.push(ptr.jsonData.videos[randomIndex])
				result = true;
			}
			else{result = false;}
		}else{result = false;}
		
		return result;
	}
	
	this.__buildHtmlList = function(artistAlias, videoId, videoTitle, imagePath, artistName)
	{	
		var l = $J(this.list);
		var videoUrl = this.__convertToFriendlyUrl(artistAlias, videoId, videoTitle);
		var html ="";
		var playlistAddBtn = "";
		var tempNode = "";
		var containerNode ="";
		var containerHtml ="";
		var customHtml ="";
		imagePath = "http://mtvmusic.mtvnimages.com/uri/"+MTVM.config.mgidPrefix+videoId+"?width=140&amp;height=105";

		videoUrl = "/"+MTVM.lang.urls.artist+videoUrl;
		
		if(typeof MTVN_PL != "undefined"){
			playlistAddBtn = "<div class=\"right\">"+MTVN_PL.AddButtonManager.renderButton(MTVM.config.mgidPrefix+videoId)+"</div><div class=\"clear\"></div>";
		}		
		
		try{

			customHtml = "<div class=\"videoLinkContainer\">"+
				"<a onmouseover=\"MTVM.services.ui.thumbFlyover.proc(this, event);\""+
				"    onmouseout=\"MTVM.services.ui.thumbFlyover.proc(this, event);\""+
				"    onclick=\"document.location='"+videoUrl+"';\""+
				"    href=\""+videoUrl+"\">"+
				"<div class=\"thumbOverlay"+videoId+"\" class=\"franchiseVideoImage\"></div>"+
				"<div class=\"videoLinkThumb\"><img src=\""+imagePath+"\" alt=\"\" border=\"0\" width=\"140\" height=\"105\" onError=\"this.src='/img/misc/metaSeperator.gif';\"></div>"+
				"<div class=\"videoLinkThumbBg\"><img src=\"/img/spacer.gif\" border=\"0\" width=\"140\" height=\"105\"></div>"+
				"<div class=\"videoLinkPlay\"><img border=\"0\" alt=\"\" src=\"/img/spacer.gif\" class=\"playIcon\"></div></a></div>"+											
				"<div class=\"link\"><div class=\"left\"><a class=\"blue\" href=\""+videoUrl+"\">"+videoTitle+"</a></div>"+playlistAddBtn+"</div>"+
			"</div>";
	
	
			
			if($J('#'+this.containerDivId+' ul').length == 0 )
			{	
				tempNode = $J('<ul></ul>').html("");
				tempNode.appendTo(this.list);
			}
	
			if(
					(ptr.shownVids.length != ptr.maxItemsToShow && ptr.jsonData.videos.length >= ptr.maxItemsToShow) ||
					(ptr.shownVids.length != ptr.jsonData.videos.length && ptr.jsonData.videos.length < ptr.maxItemsToShow)
			){
				containerNode = $J('#'+ptr.containerDivId+' ul');
				tempNode = "<li>"+customHtml+"</li>";
				containerHtml = containerNode[0].innerHTML + tempNode
				containerNode[0].innerHTML = containerHtml;
			}else{
				containerNode = $J('#'+ptr.containerDivId+' ul');
				tempNode = "<li class=\"last\">"+customHtml+"</li><div class=\"clear\"></div>";
				containerHtml = containerNode[0].innerHTML + tempNode
				containerNode[0].innerHTML = containerHtml;
			}
		}
		catch(e)
		{
			MTVM.logger.log("MTVM.widgets.moreVideosByArtist widget build html list failed " + 
						artistAlias+ " - " + videoId+ " - " + videoTitle+ " - " + imagePath+ " - " + artistName +	
						e, 2);
		}

    	try{
    		MTVM.util.generateThumbOverlay(videoId, VideoThumbOverlays);
    	}catch(e){
    		MTVM.logger.log("MTVM.widgets.moreVideosByArtist widget generate overlay failed " + e, 2);
    	}

	}
	
	this.processData= function(json)
	{	
		var json = eval(json);
		ptr.jsonData = json;
		var videoTitle	= ""; 
		var artistName  = "";
		var artistAlias = "";
		var videoId		= null;		
		var contentUri	= null;
		var imagePath	= null;		
		var videoUrl    = null;
		var artistUrl	= "";
		var randomizedNum = null;	
			
		
		try{

				if(json.videos.length>0)
				{
					MTVM.logger.log("MTVM.widgets.moreVideosByArtist widget:  processing JSON");
					videoTitle	= null; 
					artistName  = json.videos[0].artistName;
					artistAlias = json.videos[0].videoArtistAlias;
					videoId		= null;		
					contentUri	= null;
					imagePath	= null;		
					videoUrl    = null;
					artistUrl	= json.videos[0].artistUrl;
					randomizedNum = null;

					if(ptr.jsonData.videos.length > ptr.maxItemsToShow)
					{
						headerObj = "<h4 class=\"titleSmall grey\">"+MTVM.lang.video.moreFrom+" "+artistName+" <span style=\"margin:0 0 0 13px;\"><a class=\"red\" style=\"font-weight:normal;\" href=\"/"+MTVM.lang.urls.artist+"/"+artistAlias+"\">"+MTVM.lang.video.seeAllFromArtist+"</a></span>"+"</h4>";
					}
					else
					{
						headerObj = "<h4 class=\"titleSmall grey\">"+MTVM.lang.video.moreFrom+" "+artistName+"</h4>";
					}			

					ptr.__buildListHtmlHeader(headerObj);

					
					for(var i=0;  ptr.shownVids.length< ptr.maxItemsToShow && i<40; i++)
					{
						randomizedNum = MTVM.util.randomize(json.videos.length);
						if(ptr.__canBeAdded(randomizedNum))
						{	
							if(json.videos[randomizedNum].videoThumb == null || json.videos[randomizedNum].videoThumb == ""){
								imagePath = "/sitewide/img/default/140-105.jpg";
							}
							
							ptr.__buildHtmlList(json.videos[randomizedNum].videoArtistAlias, json.videos[randomizedNum].videoId, json.videos[randomizedNum].videoTitle, json.videos[randomizedNum].videoThumb, json.videos[randomizedNum].videoUrl);
						}	
					}
					
					ptr.__buildListHtmlFooter(footerObj);

					
				}
				
				
			if(ptr.shownVids.length == 0)
			{
				document.getElementById(ptr.containerDivId).style.display="none";
			}
			else
			{
				document.getElementById(ptr.containerDivId).style.display="block";
			}
		}
		catch(e){ MTVM.logger.log("MTVM.widgets.moreVideosByArtist widget: processData failed " + e, 2); }
	}
	
	this.init = function(containerDivId){
		this.__clearListHtml();
		var l = this.list;
		var processData = this.processData;
		$J(l).appendTo("#"+containerDivId);		
		this.__loadData({url:myDataservice,data:ajaxParams,successHandler:processData, dataType: 'json'})
	}
	
	this.init(containerDivId);
}
MTVM.widgets.moreVideosByArtist.prototype = new MTVM.widgets.BASE; // inherits


/************************************************************************/
/******************* END MORE BY ARTIST **************\********/
/************************************************************************/




/************************************************************************/
/*******************  MORE HD VIDS     ***************************/
/************************************************************************/
MTVM.widgets.moreHDVideos = function(containerDivId, options){
	
	var ptr = this; // self pointer
	
	var recsDataservice = '/sitewide/dataservices/relatedVideos/hd/';
	this.maxItemsToShow = 6;
	this.totalShownItems = 0;
	this.videoId = ('videoId' in options) ? options.videoId : ''; // video id to construct video mgid
	var videoData = ('videoData' in options) ? options.videoData : '&targetSearch=all'; // params to pass in the ajax
	this.containerDivId = containerDivId;
	this.options = options || {};
	var ajaxParams = (this.videoId != null) ? 'showComments=off&uri='+MTVM.config.mgidPrefix+this.videoId : 'showComments=off';
	ajaxParams += videoData;
	
	var headerObj = '<h4 class="titleSmall grey">'+MTVM.lang.video.moreHDVideos+'</h4>';
					//
	var footerObj = $J("<div></div>").html("<br/>").addClass("clear");
	
	this.__canBeAdded = function(videoId)
	{
		var result = false;		
		if(videoId != null && videoId != this.videoId)//check if video id is the current one playing
		{
			if(this.totalShownItems < this.maxItemsToShow)//check if already at max num of videos
			{
				this.totalShownItems++
				result = true;
			}
		}
		return result;	
	}
	
	this.__buildHtmlList = function(artistAlias, videoId, videoTitle, imagePath, artistName)
	{	
		var l = $J(this.list);
		var videoUrl = this.__convertToFriendlyUrl(artistAlias, videoId, videoTitle);
		var html ="";
		var playlistAddBtn = "";
		var tempNode = "";
		var containerNode ="";
		var containerHtml ="";
		var customHtml ="";
		imagePath = "http://mtvmusic.mtvnimages.com/uri/"+MTVM.config.mgidPrefix+videoId+"?width=140&amp;height=105";

		videoUrl = "/"+MTVM.lang.urls.artist+videoUrl;
		
		if(typeof MTVN_PL != "undefined"){
			playlistAddBtn = "<div class=\"right\">"+MTVN_PL.AddButtonManager.renderButton(MTVM.config.mgidPrefix+videoId)+"</div><div class=\"clear\"></div>";
		}		
		
		try{			
			customHtml = "<div class=\"videoLinkContainer\">"+
			"<a onmouseover=\"MTVM.services.ui.thumbFlyover.proc(this, event);\""+
			"    onmouseout=\"MTVM.services.ui.thumbFlyover.proc(this, event);\""+
			"    onclick=\"document.location='"+videoUrl+"';\""+
			"    href=\""+videoUrl+"\">"+
			"<div class=\"thumbOverlay"+videoId+"\" class=\"franchiseVideoImage\"></div>"+
			"<div class=\"videoLinkThumb\"><img src=\""+imagePath+"\" alt=\"\" border=\"0\" width=\"140\" height=\"105\" onError=\"this.src='/img/misc/metaSeperator.gif';\"></div>"+
			"<div class=\"videoLinkThumbBg\"><img src=\"/img/spacer.gif\" border=\"0\" width=\"140\" height=\"105\"></div>"+
			"<div class=\"videoLinkPlay\"><img border=\"0\" alt=\"\" src=\"/img/spacer.gif\" class=\"playIcon\"></div></a></div>"+
			"<div class=\"link\"><div><a class=\"blue bold\" href=\""+videoUrl+"\">"+videoTitle+"</a></div><div class=\"left\"><span class=\"lightGrey \">"+MTVM.lang.playlist.by+" </span><a href=\"/"+MTVM.lang.urls.artist+"/"+artistAlias+"\" class=\"blue\">"+artistName+"</a></div>"+
			playlistAddBtn+"</div>"+
			"</div>";
			
			
			
			if($J('#'+this.containerDivId+' ul').length == 0 )
			{	
				tempNode = $J('<ul></ul>').html("");
				tempNode.appendTo(this.list);
			}
			
			
			
			
			if(this.totalShownItems != this.maxItemsToShow){
				containerNode = $J('#'+this.containerDivId+' ul');
				tempNode = "<li>"+customHtml+"</li>";
				containerHtml = containerNode[0].innerHTML + tempNode
				containerNode[0].innerHTML = containerHtml;
			}else{
				containerNode = $J('#'+this.containerDivId+' ul');
				tempNode = "<li class=\"last\">"+customHtml+"</li><div class=\"clear\"></div>";
				containerHtml = containerNode[0].innerHTML + tempNode
				containerNode[0].innerHTML = containerHtml;
			}
		}
		catch(e)
		{
			MTVM.logger.log("MTVM.widgets.moreHDVideos widget build html list failed " + 
						artistAlias+ " - " + videoId+ " - " + videoTitle+ " - " + imagePath+ " - " + artistName +	
						e, 2);
		}

    	try{
    		MTVM.util.generateThumbOverlay(videoId, VideoThumbOverlays);
    	}catch(e){
    		MTVM.logger.log("MTVM.widgets.moreHDVideos widget generate overlay failed " + e, 2);
    	}

	}
	
	this.processData= function(json)
	{	
		var hdVideos = new Array();
		var imagePath = "";	
		
		try{									
			ptr.__buildListHtmlHeader(headerObj);
			
			if(typeof json.solr != "undefined")
			{
				for(var i = 0; i<json.solr.videos.length; i++)
				{
						
					if(json.solr.videos[i].videoType_facet=="Music Video" && hdVideos.length < ptr.maxItemsToShow)
					{
						hdVideos.push(json.solr.videos[i]);
					}
				}
			}
			
			//hit aux videos if not enough vids available
			if(hdVideos.length<ptr.maxItemsToShow)
			{
				if(typeof json.aux != "undefined")
				{
					for(var i = 0; i<json.aux.videos.length; i++)
					{
						if(json.aux.videos[i].videoType_facet=="Music Video" && hdVideos.length < ptr.maxItemsToShow)
						{
							hdVideos.push(json.aux.videos[i]);
						}
					}				
				}
			}	
			
			//loop thru hdVideos
			for(var i=0; i<hdVideos.length; i++)
			{
				imagePath = "http://mtvmusic.mtvnimages.com/uri/"+MTVM.config.mgidPrefix+hdVideos[i].id+"?width=140&amp;height=105";

				if(ptr.__canBeAdded(hdVideos[i].id))
				{	
					ptr.__buildHtmlList(hdVideos[i].artistAlias_s, hdVideos[i].id, hdVideos[i].title_t, imagePath, hdVideos[i].artist_t);
				}				
			}
			
				
			ptr.__buildListHtmlFooter(footerObj);
				
			if(ptr.totalShownItems == 0)
			{
				document.getElementById(ptr.containerDivId).style.display="none";
			}
			else
			{
				document.getElementById(ptr.containerDivId).style.display="block";
			}
		}
		catch(e){ MTVM.logger.log("MTVM.widgets.moreHDVideos widget: processData failed " + e, 2); }
	}
	
	this.init = function(containerDivId){
		this.__clearListHtml();
		var l = this.list;
		var processData = this.processData;
		$J(l).appendTo("#"+containerDivId);		
		this.__loadData({url:recsDataservice,data:ajaxParams,successHandler:processData, dataType: 'json'})
	}
	
	this.init(containerDivId);
}
MTVM.widgets.moreHDVideos.prototype = new MTVM.widgets.BASE; // inherits

/************************************************************************/
/******************* END MORE HD VIDS   **********************/
/************************************************************************/






/************************************************************************/
/******************* RELATED VIDEOS BY ARTIST ***************************/
/************************************************************************/
MTVM.widgets.relatedVideosByArtist = function(containerDivId, options){
	
	var ptr = this; // self pointer
	
	var recsDataservice = '/sitewide/dataservices/relatedVideos/';
	this.maxItemsToShow = 6;
	this.totalShownItems = 0;
	this.videoId = ('videoId' in options) ? options.videoId : ''; // video id to construct video mgid
	var videoData = ('videoData' in options) ? options.videoData : '&targetSearch=all'; // params to pass in the ajax
	this.containerDivId = containerDivId;
	this.options = options || {};
	var ajaxParams = (this.videoId != null) ? 'showComments=off&uri='+MTVM.config.mgidPrefix+this.videoId : 'showComments=off';
	ajaxParams += videoData;
	
	var headerObj = '<h4 class="titleSmall grey">'+MTVM.lang.video.relatedVideos+'</h4>';
					//$J("<h4></h4>").html("Related Videos").addClass("titleSmall grey"); //Both works
	var footerObj = $J("<div></div>").html("<br/>").addClass("clear");
	
	this.__canBeAdded = function(videoId)
	{
		var result = false;		
		if(videoId != null && videoId != this.videoId)//check if video id is the current one playing
		{
			if(this.totalShownItems < this.maxItemsToShow)//check if already at max num of videos
			{
				this.totalShownItems++
				result = true;
			}
		}
		return result;	
	}
	
	this.__buildHtmlList = function(artistAlias, videoId, videoTitle, imagePath, artistName)
	{	
		var l = $J(this.list);
		var videoUrl = this.__convertToFriendlyUrl(artistAlias, videoId, videoTitle);
		var html ="";
		var playlistAddBtn = "";
		var tempNode = "";
		var containerNode ="";
		var containerHtml ="";
		var customHtml ="";
		imagePath = "http://mtvmusic.mtvnimages.com/uri/"+MTVM.config.mgidPrefix+videoId+"?width=140&amp;height=105";

		videoUrl = "/"+MTVM.lang.urls.artist+videoUrl;
		
		if(typeof MTVN_PL != "undefined"){
			playlistAddBtn = "<div class=\"right\">"+MTVN_PL.AddButtonManager.renderButton(MTVM.config.mgidPrefix+videoId)+"</div><div class=\"clear\"></div>";
		}		
		
		try{			
			customHtml = "<div class=\"videoLinkContainer\">"+
			"<a onmouseover=\"MTVM.services.ui.thumbFlyover.proc(this, event);\""+
			"    onmouseout=\"MTVM.services.ui.thumbFlyover.proc(this, event);\""+
			"    onclick=\"document.location='"+videoUrl+"';\""+
			"    href=\""+videoUrl+"\">"+
			"<div class=\"thumbOverlay"+videoId+"\" class=\"franchiseVideoImage\"></div>"+
			"<div class=\"videoLinkThumb\"><img src=\""+imagePath+"\" alt=\"\" border=\"0\" width=\"140\" height=\"105\" onError=\"this.src='/img/misc/metaSeperator.gif';\"></div>"+
			"<div class=\"videoLinkThumbBg\"><img src=\"/img/spacer.gif\" border=\"0\" width=\"140\" height=\"105\"></div>"+
			"<div class=\"videoLinkPlay\"><img border=\"0\" alt=\"\" src=\"/img/spacer.gif\" class=\"playIcon\"></div></a></div>"+
			"<div class=\"link\"><div><a class=\"blue bold\" href=\""+videoUrl+"\">"+videoTitle+"</a></div><div class=\"left\"><span class=\"lightGrey \">"+MTVM.lang.playlist.by+" </span><a href=\"/"+MTVM.lang.urls.artist+"/"+artistAlias+"\" class=\"blue\">"+artistName+"</a></div>"+
			playlistAddBtn+"</div>"+
			"</div>";
			
			
			
			if($J('#'+this.containerDivId+' ul').length == 0 )
			{	
				tempNode = $J('<ul></ul>').html("");
				tempNode.appendTo(this.list);
			}
			
			
			
			
			if(this.totalShownItems != this.maxItemsToShow){
				containerNode = $J('#'+this.containerDivId+' ul');
				tempNode = "<li>"+customHtml+"</li>";
				containerHtml = containerNode[0].innerHTML + tempNode
				containerNode[0].innerHTML = containerHtml;
			}else{
				containerNode = $J('#'+this.containerDivId+' ul');
				tempNode = "<li class=\"last\">"+customHtml+"</li><div class=\"clear\"></div>";
				containerHtml = containerNode[0].innerHTML + tempNode
				containerNode[0].innerHTML = containerHtml;
			}
		}
		catch(e)
		{
			MTVM.logger.log("MTVM.widgets.relatedVideosByArtist widget build html list failed " + 
						artistAlias+ " - " + videoId+ " - " + videoTitle+ " - " + imagePath+ " - " + artistName +	
						e, 2);
		}

    	try{
    		MTVM.util.generateThumbOverlay(videoId, VideoThumbOverlays);
    	}catch(e){
    		MTVM.logger.log("MTVM.widgets.relatedVideosByArtist widget generate overlay failed " + e, 2);
    	}

	}
	
	this.processData= function(json)
	{	
		var relatedVideos = new Array();
		var imagePath = "";	
		
		try{									
			ptr.__buildListHtmlHeader(headerObj);
			
			if(typeof json.solr != "undefined")
			{
				for(var i = 0; i<json.solr.videos.length; i++)
				{
						
					if(json.solr.videos[i].videoType_facet=="Music Video" && relatedVideos.length < ptr.maxItemsToShow)
					{
						relatedVideos.push(json.solr.videos[i]);
					}
				}
			}
			
			//hit aux videos if not enough vids available
			if(relatedVideos.length<ptr.maxItemsToShow)
			{
				if(typeof json.aux != "undefined")
				{
					for(var i = 0; i<json.aux.videos.length; i++)
					{
						if(json.aux.videos[i].videoType_facet=="Music Video" && relatedVideos.length < ptr.maxItemsToShow)
						{
							relatedVideos.push(json.aux.videos[i]);
						}
					}				
				}
			}	
			
			//loop thru relatedVideos
			for(var i=0; i<relatedVideos.length; i++)
			{
				imagePath = "http://mtvmusic.mtvnimages.com/uri/"+MTVM.config.mgidPrefix+relatedVideos[i].id+"?width=140&amp;height=105";

				if(ptr.__canBeAdded(relatedVideos[i].id))
				{	
					ptr.__buildHtmlList(relatedVideos[i].artistAlias_s, relatedVideos[i].id, relatedVideos[i].title_t, imagePath, relatedVideos[i].artist_t);
				}				
			}
			
				
			ptr.__buildListHtmlFooter(footerObj);
				
			if(ptr.totalShownItems == 0)
			{
				document.getElementById(ptr.containerDivId).style.display="none";
			}
			else
			{
				document.getElementById(ptr.containerDivId).style.display="block";
			}
		}
		catch(e){ MTVM.logger.log("MTVM.widgets.relatedVideosByArtist widget: processData failed " + e, 2); }
	}
	
	this.init = function(containerDivId){
		this.__clearListHtml();
		var l = this.list;
		var processData = this.processData;
		$J(l).appendTo("#"+containerDivId);		
		this.__loadData({url:recsDataservice,data:ajaxParams,successHandler:processData, dataType: 'json'})
	}
	
	this.init(containerDivId);
}
MTVM.widgets.relatedVideosByArtist.prototype = new MTVM.widgets.BASE; // inherits


/************************************************************************/
/******************* END RELATED VIDEOS BY ARTIST **************\********/
/************************************************************************/





/************************************************************************/
/******************* PLAYLISTS CONTAINING THIS VIDEO*********************/
/************************************************************************/
MTVM.widgets.playlistsContainingVideo = function(containerDivId, options){
	
	var ptr = this; // self pointer

	var dataserviceUrl = '/sitewide/dataservices/playlistsContainingVideo/?showComments=off&akamaized=y&';
	this.maxItemsToShow = 6;
	this.totalShownItems = 0;
	this.itemId = ('itemId' in options) ? options.itemId : ''; // video id to construct video mgid
	this.containerDivId = containerDivId;
	this.options = options || {};	
	
	var ajaxParams = (this.itemId != null) ? 'contentURI='+MTVM.config.mgidPrefix+this.itemId : '';
	
	var headerObj = '<h4 class="titleSmall grey">'+MTVM.lang.playlist.containingVideo+'</h4>';
					//
	var footerObj = $J("<div></div>").html("<br/>").addClass("clear");
	
	
	
	
	this.__canBeAdded = function()
	{
		var result = false;		

		if(this.totalShownItems < this.maxItemsToShow)//check if already at max num of Items
		{
			this.totalShownItems++
			result = true;
		}
		return result;	
	}
	
	this.__buildHtmlList = function(userFluxId, itemMgid, itemTitle, imagePath, userName)
	{
		var l = $J(this.list);
		var itemUrl = "/playlists/"+userFluxId+"/"+itemMgid;
		var html ="";
		var playlistAddBtn = "";
		var customHtml = "";
		var tempNode = "";
		var containerNode ="";
		var containerHtml ="";
		var customHtml ="";
		
		try{

			customHtml = "<div class=\"playlistThumbBlock\"><div class=\"videoLinkContainer\">"+
			"<a onmouseover=\"MTVM.services.ui.thumbFlyover.proc(this, event);\""+
			"    onmouseout=\"MTVM.services.ui.thumbFlyover.proc(this, event);\""+
			"    onclick=\"document.location='"+itemUrl+"';\""+
			"    href=\""+itemUrl+"\">"+
			"<div id=\"thumbOverlay"+itemMgid+"\" class=\"thumbOverlay"+itemMgid+"\"></div>"+
			"<div class=\"videoLinkThumb\"><img src=\""+imagePath+"\" alt=\"\" border=\"0\" width=\"127\" height=\"105\" onError=\"this.src='/img/misc/metaSeperator.gif';\"></div>"+
			"<div class=\"videoLinkThumbBg\"><img src=\"/img/spacer.gif\" border=\"0\" width=\"127\" height=\"96\"></div>"+
			"<div class=\"videoLinkPlay\"><img border=\"0\" alt=\"\" src=\"/img/spacer.gif\" class=\"playIconPlaylist\"></div></a></div></div>"+											
			"<div class=\"link\"><div class=\"left\">" +			
			"<a class=\"blue\" href=\""+itemUrl+"\">"+itemTitle+"</a><br/><span class=\"lightGrey \">"+MTVM.lang.playlist.by+" </span><a href=\"/playlists/"+userFluxId+"\" class=\"blue\">"+userName+"</a>"+
			"</div>"+playlistAddBtn+"</div>";
			
			
			if($J('#'+this.containerDivId+' ul').length == 0 )
			{				
				$J('<ul></ul>').html("").appendTo(this.list);
			}
			

			
			if(this.totalShownItems != this.maxItemsToShow){
				containerNode = $J('#'+this.containerDivId+' ul');
				tempNode = "<li>"+customHtml+"</li>";
				containerHtml = containerNode[0].innerHTML + tempNode
				containerNode[0].innerHTML = containerHtml;
			}else{
				containerNode = $J('#'+this.containerDivId+' ul');
				tempNode = "<li class=\"last\">"+customHtml+"</li><div class=\"clear\"></div>";
				containerHtml = containerNode[0].innerHTML + tempNode
				containerNode[0].innerHTML = containerHtml;
			}
			
			
			try{
        		MTVM.util.generateThumbOverlay(itemMgid, VideoThumbOverlays);
        	}catch(e){
        		//console.log(e);
        	}
			
			
		}
		catch(e)
		{
			MTVM.logger.log("MTVM.widgets.playlistsContainingVideo: widget build html list failed " + e, 2);
		}
	}
	
	this.processData = function(json)
	{	
		var playlistName			= "";
		var playlistThumb			= "";
		var playlistCreateDate		= "";
		var playlistMgid			= "";
		var userFluxId				= "";				
		var userDisplayName			= "";
		var userThumb				= "";

		try{			
			if(json.playlists.length>0)
			{
				ptr.__buildListHtmlHeader(headerObj);			
			
				for(var i=json.playlists.length-1; i>=0 && ptr.totalShownItems <= ptr.maxItemsToShow ; i--){					
					playlistName			= json.playlists[i].name;
					playlistThumb			= json.playlists[i].thumbnail;
					playlistCreateDate		= json.playlists[i].createDate;
					playlistMgid			= json.playlists[i].contentURI;
					userFluxId				= json.playlists[i].userUcid;				
					userDisplayName			= json.playlists[i].userDisplayName;
					userThumb				= json.playlists[i].userThumbnail;
					
					if(ptr.__canBeAdded())
					{	
	
						ptr.__buildHtmlList(userFluxId, playlistMgid, playlistName, playlistThumb, userDisplayName);
					}
				}
	
				
				ptr.__buildListHtmlFooter(footerObj);
			}
			
			if(ptr.totalShownItems == 0)
			{
				
				document.getElementById(ptr.containerDivId).style.display="none";
			}
			else
			{
				document.getElementById(ptr.containerDivId).style.display="block";
			}


		}
		catch(e){ MTVM.logger.log("MTVM.widgets.playlistsContainingVideo: processData failed " + e); }
	}
	
	this.init = function(containerDivId){
		
		var l = this.list;
		this.__clearListHtml();
		var processData = this.processData;
		$J(l).appendTo("#"+containerDivId);
		
		this.__loadData({url:dataserviceUrl,data:ajaxParams,successHandler:processData, dataType:'json'})
	}
	
	this.init(containerDivId);
}
MTVM.widgets.playlistsContainingVideo.prototype = new MTVM.widgets.BASE; // inherits






/************************************************************************/
/******************* DISCOVER AND DOWNLOAD *********************/
/************************************************************************/
MTVM.widgets.discoverAndDownload = function(containerDivId, options){

	this._dndPopup_ 			= [];
	this._dndSongTitle_ 		= [];
	this._dndThumbnail_ 		= [];	
	this.totalShownItems		= 0;
	this.containerDivId 		= "";	
	this.maxItemsToShow = 5;
	this.totalShownItems = 0;	
	this.containerDivId = containerDivId;
	this.options = options || {};		

	var ptr = this; // self pointer	
	var ajaxParams ="";		
	var dataserviceUrl 		= '/sitewide/dataservices/discoverAndDownload/?showComments=off&v=true';	
	var headerObj = '<h4 class="titleSmall grey">Discover and Download</h4><div>Get free mp3s from these new and emerging artists</div>';					
	var footerObj = '<div class="right"><a href="http://www.mtv.com/music/discover_download/" target="_blank" class="red">More Discover & Download</a><br/><br/></div><div class="clear"></div>';
	
	
	this.__canBeAdded = function()
	{
		var result = false;		
		try{
		
			if(this.totalShownItems < this.maxItemsToShow)//check if already at max num of Items
			{
				this.totalShownItems++
				result = true;
			}
		}
		catch(e)
		{
			MTVM.logger.log("MTVM.widgets.discoverAndDownload: canBeAdded Failed " +e);
		}
		return result;	
	}
	
	this.__buildHtmlList = function(artistArray)
	{
		var l = $J(this.list);
		var customHtml = "";

		
		try{

			if($J('#'+this.containerDivId+' ul').length == 0 )
			{				
				$J('<ul></ul>').html("").appendTo(this.list);
			}

			
			for(var i=0; i<artistArray.length; i++)			
			{		
				this.totalShownItems = this.totalShownItems+1;	
				customHtml	= "<div class=\"left thumb\"><a href=\"javascript:_void();\" onclick=\""+artistArray[i].onclick+"\"><img width=\"70\" height=\"52\" src=\""+artistArray[i].thumbnail+"\"  /></a></div>"+
								"<div class=\"left link\"><a class=\"blue\" href=\"javascript:_void();\" onclick=\""+artistArray[i].onclick+"\">"+artistArray[i].title+"</a>"+
								"</div>"+
								"<div class=\"clear\"></div>";							
							
				$J('<li></li>').html(customHtml).appendTo('#'+this.containerDivId+' ul');
			}
			
		}
		catch(e)
		{
			MTVM.logger.log("MTVM.widgets.discoverAndDownload: widget build html list failed " + e, 2);
		}
	}
    
    
	this.processData = function(data)
	{	
		var title ="";
		var thumbnail = "";
		var onclick ="";
		var artistArray = new Array();
		
		eval(data); //contains var feed and var mtv
		
		try{			 						
			ptr.__buildListHtmlHeader(headerObj);
			
			for(var i=0; i<feed.length; i++)
			{
				title = feed[i].title;
				thumbnail = feed[i].thumbnail;

				for(var j=0; j<mtv.length; j++)
				{
					if(title.indexOf(mtv[j].artistName) >-1)
					{
						onclick = mtv[j].onclick;
					}
				}
				
				artistArray.push(
						{
							title:title,
							thumbnail:thumbnail,
							onclick:onclick
						}
				)
				
			}
			
			ptr.__buildHtmlList(artistArray); 
			ptr.__buildListHtmlFooter(footerObj);
			
			if(ptr.totalShownItems == 0)				
			{document.getElementById(ptr.containerDivId).style.display="none";}
			else
			{document.getElementById(ptr.containerDivId).style.display="block";}			
		}
		catch(e){ MTVM.logger.log("MTVM.widgets.discoverAndDownload: processData failed " + e); }
	}
	
	this.init = function(containerDivId)
	{	
		var l = this.list;		
		var processData = this.processData;
		
		this.containerDivId = containerDivId;		
		this.__clearListHtml();
		
		$J(l).appendTo("#"+this.containerDivId);
		
		this.__loadData({url:dataserviceUrl,data:ajaxParams,successHandler:processData, dataType:'html'})
	}
	
	this.init(containerDivId);
}
MTVM.widgets.discoverAndDownload.prototype = new MTVM.widgets.BASE; // inherits