/*
	VideoData()
	Creates an Array named VideoData.videoList which contains all the video data from a collection.
	
	requires jquery
	
	required 
	VideoData.init(p_showID, p_freeWheelSectionID, p_pageUrl);

	optional
	VideoPresentation.js  
*/

function VideoData(){

}

VideoData.environment				= "www";																							//defaulted to production 
VideoData.params 					= null;

VideoData.showID					= "";
VideoData.tveID						= "";
VideoData.freeWheelSectionID 		= "";
VideoData.pageUrl 					= "";

VideoData.regEx						= "";
VideoData.regExReplace				= "";

VideoData.resultsLimit				= "0";
VideoData.configURL					= "";
VideoData.tveContentURL				= "";
VideoData.tveMostRecentURL			= "";
	
VideoData.episodesByCategoryPath 	= "";
VideoData.episodesByShowPath		= "";
VideoData.videoPlayListPath 		= "";
VideoData.ratingsPath 				= "";
VideoData.timeStampNextGen			= "";

VideoData.videoList 				= new Array(0);
VideoData.tveVideoList 				= new Array(0);

VideoData.init = function(p_showID, p_freeWheelSectionID, p_pageUrl){

	VideoData.environment			= VideoData.checkForStage();
	VideoData.params 				= VideoData.getQueryData();

	var sStr = "http://" + VideoData.environment + ".cartoonnetwork.com/cnvideosvc2/staged/CN2.staging.configuration.xml?r="  + VideoData.getRandomNumber();
	var pStr = "http://www.cartoonnetwork.com/video/staged/CN2.configuration.xml?r" + VideoData.getRandomNumber();

	VideoData.tveContentURL 	= "http://" + VideoData.environment + ".cartoonnetwork.com/cntv/mvpd/services/getCollectionByContentId.do?offset=0&limit=200&id=";
	VideoData.tveMostRecentURL 	= "http://" + VideoData.environment + ".cartoonnetwork.com/cntv/mvpd/services/getAllEpisodes.do?offset=0&limit=4&dateInteval=20";
	
	VideoData.configURL				= VideoData.environment == "www" ? pStr : sStr;

	VideoData.regEx					= VideoData.environment == "www" ? /cnvideosvc2.cartoonnetwork.com/ : /ctn\dcma\d.\d\d\d\d/;
	VideoData.regExReplace			= VideoData.environment + ".cartoonnetwork.com/cnvideosvc2";
	
	VideoData.showID				= VideoData.params.showID != null ? VideoData.params.showID : p_showID;
	VideoData.freeWheelSectionID 	= VideoData.params.showID != null ? VideoData.params.fwSectionID : p_freeWheelSectionID;		
	VideoData.pageUrl 				= p_pageUrl;	
	
	VideoData.loadConfigData();
}

VideoData.loadDataError = function(p_request, p_error){
	alert("error" + p_request + " : " + p_error);
}

VideoData.loadConfigDataSuccess = function(p_data){
	
	VideoData.timeStampNextGen			= jQuery(p_data).find('timestamp').attr('value');
	VideoData.resultsLimit				= jQuery(p_data).find('services').children('episodesByShow').attr('limit');
	
	VideoData.episodesByShowPath		= jQuery(p_data).find('services').children('episodesByShow').attr('url');
	VideoData.episodesByShowPath 		= VideoData.episodesByShowPath.replace(VideoData.regEx, VideoData.regExReplace);		//update domain for ajax
	
	VideoData.episodesByCategoryPath	= jQuery(p_data).find('services').children('episodesByCategory').attr('url');
	VideoData.episodesByCategoryPath 	= VideoData.episodesByCategoryPath.replace(VideoData.regEx, VideoData.regExReplace);	//update domain for ajax
	
	VideoData.videoPlayListPath			= jQuery(p_data).find('services').children('getVideoPlaylist').attr('url');
	VideoData.videoPlayListPath 		= VideoData.videoPlayListPath.replace(VideoData.regEx, VideoData.regExReplace);			//update domain for ajax
	VideoData.videoPlayListPath 		= VideoData.videoPlayListPath + "&r=" + VideoData.timeStampNextGen;
	
	VideoData.ratingsPath				= jQuery(p_data).find('services').children('rankEpisode').attr('url');
	VideoData.ratingsPath 				= VideoData.ratingsPath.replace(VideoData.regEx, VideoData.regExReplace);				//update domain for ajax
	
	VideoData.loadShowData();
}

VideoData.loadShowDataSuccess = function(p_data){
	jQuery(p_data).find('episode').each(function() {
		
		var newEP = new VideoData.episodeData(	jQuery(this).attr("id"), 
												jQuery(this).attr("thumbnailUrl"), 
												VideoData.doReplaceSpecialChar(jQuery(this).attr("title")), 
												VideoData.doReplaceSpecialChar(jQuery(this).children("description").text()), 
												jQuery(this).attr("contentTags"), 
												jQuery(this).attr("episodeType"),
												jQuery(this).attr("collectionTitle"), 
												jQuery(this).attr("ranking"), 
												jQuery(this).attr("collectionId"), 
												jQuery(this).attr("epiSeasonNumber"), 
												jQuery(this).attr("epiSeasonName"), 
												jQuery(this).attr("epiRatingImageUrl"), 
												jQuery(this).attr("numberOfViews"), 
												jQuery(this).attr("epiOverlayURL"), 
												jQuery(this).attr("embed"),
												jQuery(this).attr("launchDate"));
		
		VideoData.videoList.push(newEP);

		jQuery(this).find("segments").children("segment").each(function() {
			VideoData.addSegmentData(jQuery(this).attr("episodeId"), jQuery(this).attr("id"), jQuery(this).attr("duration"), jQuery(this).attr("thumbnailUrl"));
		});
	});
	
	
	VideoData.loadTVEIDData();
	
	if(typeof(VideoPresentation) == "function"){ 
		VideoPresentation.onDataReady(); 
	}
}

VideoData.getVideoList = function(){

	return VideoData.videoList; 
}

VideoData.loadConfigData = function(){

	jQuery.ajax({type: "GET", url: VideoData.configURL, dataType: "xml", error: function (request, error) {VideoData.loadDataError(request, error)}, success: function(data) {VideoData.loadConfigDataSuccess(data)}});
}

VideoData.loadShowData = function(){

	var sURL = VideoData.episodesByShowPath  + "&id=" + VideoData.showID + "&limit=" + VideoData.resultsLimit + "&offset=0&r=" + VideoData.timeStampNextGen;
	jQuery.ajax({type: "GET", url: sURL, dataType: "xml", error: function (request, error) {VideoData.loadDataError(request, error)}, success: function(data) {VideoData.loadShowDataSuccess(data)}});
}




//////////////////////////////TVE /////////////////////////////////////////////////



	
VideoData.loadTVEIDData = function(){

	if(typeof(tvePropertyID) != "undefined"){
		VideoData.tveID = tvePropertyID == null ? "" : tvePropertyID;
		
		if(VideoData.tveID == ""){
			VideoData.loadTVEMostRecentData();
		}else{
			VideoData.loadTVEContentData();
		}	
	}
}
	
VideoData.loadTVEContentData = function(){

	var sURL = VideoData.tveContentURL + VideoData.tveID;
	jQuery.ajax({type: "GET", url: sURL, dataType: "xml", error: function (request, error) {VideoData.loadTVEError(request, error)}, success: function(data) {VideoData.loadTVEContentDataSuccess(data)}});
}

VideoData.loadTVEContentDataSuccess = function(p_data){

	jQuery(p_data).find('episode').each(function() {

		var newEP = new VideoData.tveData(jQuery(this).attr("id"), jQuery(this).children('thumbnailUrl').first().text(), jQuery(this).children('episodeTitle').first().text(), true);
		VideoData.tveVideoList.push(newEP);
	});

	if(VideoData.tveVideoList.length < 4){
		VideoData.loadTVEMostRecentData();		
	}else{
		if(typeof(VideoPresentation) == "function"){
			VideoPresentation.onTVEDataReady(VideoData.tveVideoList);
		}
	}
}

VideoData.loadTVEError = function(p_request, p_error){

	VideoData.loadTVEMostRecentData();
}

VideoData.loadTVEMostRecentData = function(){

	var sURL = VideoData.tveMostRecentURL;
	jQuery.ajax({type: "GET", url: sURL, dataType: "xml", error: function (request, error) {VideoData.loadTVEMostRecentDataError(request, error)}, success: function(data) {VideoData.loadTVEMostRecentDataSuccess(data)}});
}

VideoData.loadTVEMostRecentDataSuccess = function(p_data){

	jQuery(p_data).find('episode').each(function() {

		var newEP = new VideoData.tveData(jQuery(this).attr("id"), jQuery(this).children('thumbnailUrl').first().text(), jQuery(this).children('episodeTitle').first().text(), true);
		VideoData.tveVideoList.push(newEP);
	});

	if(VideoData.tveVideoList.length < 4){
		VideoData.addFourClips();
	}else{
		if(typeof(VideoPresentation) == "function"){
			VideoPresentation.onTVEDataReady(VideoData.tveVideoList);
		}
	}
}

VideoData.loadTVEMostRecentDataError = function(p_request, p_error){
	
	if(VideoData.tveVideoList.length < 4){
		VideoData.addFourClips();
	}else{
		if(typeof(VideoPresentation) == "function"){
			VideoPresentation.onTVEDataReady(VideoData.tveVideoList);
		}
	}
}

VideoData.addFourClips = function(){
	
	for(var x=0; x <= VideoData.VideoList.length; x++){
		if(VideoData.VideoList[x].episodeType.indexOf("CLI")){
			
			var newEP = new VideoData.tveData(VideoData.VideoList[x].id, VideoData.VideoList[x].thumb, VideoData.VideoList[x].title, false);
			VideoData.tveVideoList.push(newEP);
			
			if(VideoData.tveVideoList.length >=4){
				break;
			}
		}
	}
	
	if(typeof(VideoPresentation) == "function"){
		VideoPresentation.onTVEDataReady(VideoData.tveVideoList);
	}
}


//////////////////////////////TVE /////////////////////////////////////////////////




VideoData.episodeData = function(p_id, p_thumb, p_title, p_description, p_morebutton, p_episodeType, p_collectionTitle, p_ranking, p_collectionId, p_epiSeasonNumber, p_epiSeasonName, p_epiRatingImage, p_numberOfViews, p_epiOverlayURL, p_embed, p_launchDate){
	
	if(p_ranking > 5){
		p_ranking = 5;
	}
	
	this.id 				= p_id;
	this.thumb 				= p_thumb;
	this.title 				= p_title;
	this.description 		= p_description;
	this.morebutton			= p_morebutton;
	this.episodeType 		= p_episodeType;
	this.collectionTitle	= p_collectionTitle;
	this.ranking			= p_ranking;
	this.collectionId		= p_collectionId;
	this.epiSeasonNumber	= p_epiSeasonNumber;
	this.epiSeasonName		= p_epiSeasonName;
	this.epiRatingImage		= p_epiRatingImage;
	this.epiOverlayURL		= p_epiOverlayURL;
	this.numberOfViews		= p_numberOfViews;
	this.embed				= (p_embed == "Y") ? true : false;
	this.launchDate			= p_launchDate;

	this.segment 			= new Array(0);
	this.duration 			= new Array(0);
	this.thumbnailUrl 		= new Array(0);
}

VideoData.seasonData = function(p_seasonNumber, p_seasonName){
	
	this.seasonNumber 		= p_seasonNumber;
	this.seasonName			= p_seasonName;

}

VideoData.addSegmentData = function(p_epId, p_id, p_duration, p_thumbURL){
		
	for(var i=0; i <= VideoData.videoList.length - 1; i++){
		if(VideoData.videoList[i].id == p_epId){
			VideoData.videoList[i].segment.push(p_id);
			VideoData.videoList[i].duration.push(p_duration);
			VideoData.videoList[i].thumbnailUrl.push(p_thumbURL);
			break;
		}
	}
}

VideoData.tveData = function(p_episodeId, p_thumbnailUrl, p_title, p_isTVE){
	
	this.episodeId 		= p_episodeId;
	this.thumbnailUrl	= p_thumbnailUrl;
	this.title 			= p_title;
	this.isTVE			= p_isTVE;
}

VideoData.checkForStage = function (){

	var	s = "www";
	var dmArr = new Array();
	dmArr[0] = "staging.cartoonnetwork.com";
	dmArr[1] = "gcstage.cartoonnetwork.com";
	dmArr[2] = "bgcstage.cartoonnetwork.com";
	dmArr[3] = "swgcstage.cartoonnetwork.com";
	dmArr[4] = "mixitstaging.cartoonnetwork.com";
	
	for(var i=0; i <= dmArr.length-1; i++){
		if(document.URL.indexOf(dmArr[i]) > -1){
			s = "staging"; 
		}
	}
	
	return s;
}

VideoData.getQueryData = function(){

	var vals = location.search.substring(1, location.search.length).split("&");
	var val;
	var i = vals.length;
	var result = {};

	while(i--)
	{
		val = vals[i].split("=");
		result[val[0]] = val[1];
	}
	
	return result;
}

VideoData.doReplaceSpecialChar = function(p_str){

	var str_regEx 		= /\Wamp\Wamp\W/;
	var str_replace		= "&";
	var str_VidCopy		= p_str;
	
	while(str_VidCopy.indexOf("&amp;amp;") > -1){
		str_VidCopy = str_VidCopy.replace(str_regEx, str_replace);
	}
	
	str_regEx 		= /\Wamp\W\W39\W/;
	str_replace		= "'";
	
	while(str_VidCopy.indexOf("&amp;#39;") > -1){
		str_VidCopy = str_VidCopy.replace(str_regEx, str_replace);
	}
	
	str_regEx 		= /\Wamp\Wquot\W/;
	str_replace		= "&quot;"
	
	while(str_VidCopy.indexOf("&amp;quot;") > -1){
		str_VidCopy = str_VidCopy.replace(str_regEx, str_replace);
	}
	
	return str_VidCopy;
}

VideoData.getRandomNumber = function(){

	var randomNumber;
	
	var m   	= new Date().getMonth() + 1;
	var d  		= new Date().getDate();
	var yr  	= new Date().getFullYear();
	var h 	 	= new Date().getHours();
	var min 	= new Date().getMinutes();
	var newMin 	= "00";
	
	if(min >= 0 && min <= 15){
		newMin = "00";
	}
	
	if(min >= 16 && min <= 30){
		newMin = "15";
	}
	
	if(min >= 31 && min <= 45){
		newMin = "30";
	}
	
	if(min >= 46 && min <= 59){
		newMin = "45";
	}
	
	randomNumber = m + "" + d + "" + yr + "" + h + "" + newMin;
	
	return randomNumber;        	
}

VideoData.sortVideoList = function(p_sortOn){
	
}

VideoData.printVideoList = function(){
	
	//output for testing
	jQuery("<div></div>").html("VideoData.resultsLimit " + VideoData.resultsLimit).appendTo(jQuery('body'));
	jQuery("<div></div>").html("VideoData.episodesByCategoryPath " + VideoData.episodesByCategoryPath).appendTo(jQuery('body'));
	jQuery("<div></div>").html("VideoData.episodesByShowPath " + VideoData.episodesByShowPath).appendTo(jQuery('body'));
	jQuery("<div></div>").html("VideoData.videoPlayListPath " + VideoData.videoPlayListPath).appendTo(jQuery('body'));
	jQuery("<div></div>").html("VideoData.ratingsPath " + VideoData.ratingsPath).appendTo(jQuery('body'));
	jQuery("<div></div>").html("VideoData.timeStampNextGen " + VideoData.timeStampNextGen).appendTo(jQuery('body'));
	jQuery("<div></div>").html("<BR>").appendTo(jQuery('body'));
	jQuery("<div></div>").html("Loaded " + VideoData.configURL).appendTo(jQuery('body'));
	jQuery("<div></div>").html("Loaded " + VideoData.episodesByShowPath  + "&id=" + VideoData.showID + "&limit=" + VideoData.resultsLimit + "&offset=0&r=" + VideoData.timeStampNextGen).appendTo(jQuery('body'));
	jQuery("<div></div>").html("Loaded " + VideoData.episodesByCategoryPath  + "&id=multiCat&filterByCollectionId=" + VideoData.showID + "&limit=" + VideoData.resultsLimit + "&offset=0&r=" + VideoData.timeStampNextGen).appendTo(jQuery('body'));
	jQuery("<div></div>").html("<BR>").appendTo(jQuery('body'));
	
	for(var i=0; i <= VideoData.videoList.length -1; i++){
	
		jQuery("<div></div>").html("<BR>").appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].id = " 				+  VideoData.videoList[i].id				).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].thumb = " 			+  VideoData.videoList[i].thumb				).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].title = " 			+  VideoData.videoList[i].title				).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].description  = " 		+  VideoData.videoList[i].description 		).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].morebutton = " 		+  VideoData.videoList[i].morebutton		).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].episodeType = " 		+  VideoData.videoList[i].episodeType		).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].collectionTitle = " 	+  VideoData.videoList[i].collectionTitle	).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].ranking = " 			+  VideoData.videoList[i].ranking			).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].collectionId = " 		+  VideoData.videoList[i].collectionId		).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].epiSeasonNumber = " 	+  VideoData.videoList[i].epiSeasonNumber	).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].epiSeasonName = " 	+  VideoData.videoList[i].epiSeasonName		).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].epiRatingImage = " 	+  VideoData.videoList[i].epiRatingImage	).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].epiOverlayURL = " 	+  VideoData.videoList[i].epiOverlayURL		).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].numberOfViews = " 	+  VideoData.videoList[i].numberOfViews		).appendTo(jQuery('body'));
		jQuery("<div></div>").html("VideoData.videoList["+i+"].embed = " 			+  VideoData.videoList[i].embed				).appendTo(jQuery('body'));
	
		for(var n=0; n <= VideoData.videoList[i].segment.length -1; n++){
			jQuery("<div></div>").html("&nbsp;&nbsp;VideoData.videoList["+i+"].segment["+n+"] = " 		+  VideoData.videoList[i].segment[n]		).appendTo(jQuery('body'));
			jQuery("<div></div>").html("&nbsp;&nbsp;VideoData.videoList["+i+"].duration["+n+"] = " 		+  VideoData.videoList[i].duration[n]		).appendTo(jQuery('body'));
			jQuery("<div></div>").html("&nbsp;&nbsp;VideoData.videoList["+i+"].thumbnailUrl["+n+"] = " 	+  VideoData.videoList[i].thumbnailUrl[n]	).appendTo(jQuery('body'));
		}			 
		jQuery("<div></div>").html("<BR>").appendTo(jQuery('body'));
	}
}


