/*
 * VIDEO DATA FUNCTIONS
 * (c) 2011
 * code for video data
 * - get from db
 * - filter
 * - etc
*/

	var videoPath = "videos/";
	var categories = new Array();
	//var videoLinksArray = new Array();
	//var currentDataObject = new Object();


// ============
// CREDITS

	function toggle_credits() {
		$('.videoCredits').stop();
		var currPos = $('.videoCredits').position();
		//trace(currPos.left);
		//if (currPos.left > -500) { 
		if (currPos.left != -5) { 
			$('.videoCredits').animate({ "left":"-5px" }, 250).show();
		} else { 
			$('.videoCredits').animate({ "left":"-900px" }, 500);
		}
	}	  					  
	
	function make_credits(dataObject) {
		var countLines=0;
		//
		var creditsDiv = document.createElement('div'); // in memory, not on page			
		creditsDiv.setAttribute('class', 'videoCredits'); //$(creditsDiv).attr({ "class":"videoCredits" });
		//
		for (var prop in dataObject) {
			var credit = jQuery.trim( dataObject[prop] ); // val
			if (credit) {
				if (prop=="client") { 
					$(creditsDiv).append( make_credit(prop, credit, "creditLarge") ); countLines++; }
				else if (prop=="title" || prop=="artist" || prop=="song" || prop=="song_name") { 
					$(creditsDiv).append( make_credit(prop, credit, "creditMedium") ); countLines++; }
				else if (prop=="rolehead" || prop=="role" || prop=="linkblurb" || prop=="song_remix") { 
					$(creditsDiv).append( make_credit(prop, credit, "creditSmall") ); countLines++; 
				}	
				else if (prop=="blurb") { 
					$(creditsDiv).append( make_credit(prop, credit, "creditTiny") ); countLines++; 
				}	
				if (prop=="website" && dataObject.linkblurb) { $(creditsDiv).append( make_credit(prop, "Website Above", "creditSmall") ); countLines++; }	
				//if (prop=="website" && dataObject.linkblurb) { $(creditsDiv).append( make_credit(prop, credit, "creditSmall") ); countLines++; }	
			}
		}
		//
		$('.videoPlayAreaDiv').append(creditsDiv); // on top
		//var newy = (countLines * -10) + 310;
		var newy = ( (5 - countLines) * 30 ) + 375;
		$(creditsDiv).css({ "top":newy, "left":"-900px" });
	}
	
	function make_credit(creditType, creditText, creditClass) { 
		var newCreditSpan = document.createElement('span');
		newCreditSpan.setAttribute('class', "allCredits "+creditClass); 
		newCreditSpan.innerHTML = creditText + " <br/>";
		if (creditType=="title" || creditType=="song" || creditType=="song_name") newCreditSpan.innerHTML= "'"+creditText + "' <br/>"; // overwrite
		return newCreditSpan;
	}
	
	
// ===

	function category_str_to_id(categoryStr) {
		if (categoryStr=="licensing") { categoryID = 1; }
		else if (categoryStr=="original") { categoryID = 2; }
		if (categoryStr=="campaign") { categoryID = 3; }
		// 
		return categoryID;
	}


// ======================
// VIDEOS VIA DATABASE

	function get_all_videos_from_db(callback_function) {
		$.ajax({
		 	dataType: "json",
			url: "js/get_all_from_db.php", //?act=getall
			data: "&act=getall",
			success: function( data, textStatus, jqXHR ) {
				//console.log(data);
				// done
				eval(callback_function)(data); //window[callback_function](videoArray);
			}, // end success	
		}); // end ajax
	}

	function get_video_links_from_db(dataObject) {
		$.ajax({
		 	dataType: "json",
			url: "js/get_all_from_db.php", 
			data: "&act=getlinksfor&vid="+dataObject.videoID,
			success: function( data, textStatus, jqXHR ) {
				var videoLinksArray = data;				
				// done
				finish_nav_menu_player(videoLinksArray, dataObject)
			}, // end success	
		}); // end ajax
	}
	
	function get_featurepage_from_db(callback_function) {
		$.ajax({
		 	dataType: "json",
			url: "js/get_all_from_db.php", 
			data: "&act=getfeats",
			success: function( data, textStatus, jqXHR ) {
				// done
				//finish_featurepage(data);
				process_featurepage_data(data, callback_function);
				//eval(callback_function)(data); //window[callback_function](videoArray);
			}, // end success	
		}); // end ajax
	}
	
	function get_reelinfo_from_db(reelName, callback_function) {
		//console.log("Getting from db reel "+reelName);
		$.ajax({
		 	dataType: "json",
			url: "js/get_all_from_db.php", 
			data: "&act=getreelinfo&rn="+reelName,
			success: function( data, textStatus, jqXHR ) {
				eval(callback_function)(data); //window[callback_function](videoArray);
			}, // end success	
			error: function( jqXHR, errorType, excobj ) {
				trace("!!! ERROR "+errorType);
				console.log(errorType);
			}
		}); // end ajax
	}
	
	function get_playlist_from_db(reelID, callback_function) {
		$.ajax({
		 	dataType: "json",
			url: "js/get_all_from_db.php", 
			data: "&act=getplaylist&rid="+reelID,
			success: function( data, textStatus, jqXHR ) {
				eval(callback_function)(data); //window[callback_function](videoArray);
			} // end success	
		}); // end ajax
	}

	function get_category_names_from_db() {
		$.ajax({
		 	dataType: "json",
			url: "js/get_all_from_db.php", 
			data: "&act=getcatnames",
			success: function( data, textStatus, jqXHR ) {
				// done
				for (cat in data) {
					categories.push(cat); // global
				}
			}, // end success	
		}); // end ajax
	}
	
	/*
	function db_success(videoArray) {
		trace("DB success");
		trace("> " + videoArray);
		trace("> " + videoArray[0].client);
	}
	*/
// ======================
	// FILTER SORT

	function filter_videos_to_visible(videoArray) {
		for (n=0; n<videoArray.length; n++) {
			if (videoArray[n].visible != 1) { videoArray.splice(n,1); } 
		}
		// reset array index number
		//for (n=0; n<videoArray.length; n++) {
		//	videoArray[n].num=n; // reset num
		//}
		//
		return videoArray;
	}

	function filter_array_for_website_display(fvideoArray) {
		var countVideos=0;
		var visibleArray=[];
		// remove spots set not to display
		for (n=0; n<fvideoArray.length; n++) {
			//if (videoArray[n].visible != 1) { videoArray.splice(n,1); } 
			if (fvideoArray[n].visible == 1) { visibleArray.push(fvideoArray[n]); } 
		}
		// reset some data
		for (n=0; n<visibleArray.length; n++) {
			if (visibleArray[n].volume==undefined) visibleArray[n].volume = 5; // catch !vol 
			if (visibleArray[n].featured != 1) visibleArray[n].featured = 0; // catch !feat 
			visibleArray[n].num=n; // reset num
			visibleArray[n].dbid=visibleArray[n].videoID; 
		}
		//
		return visibleArray;
	}
	
			
	function filter_videos_to_category(category) {
		videosArray = new Array(); // clear
		numVideos=0; // reset
		
		var categoryID = category_str_to_id(category);
		
		// get all
		if (!category || category==0) { 
			for (n in masterVideosArray) {
				videosArray.push(masterVideosArray[n]); 
				numVideos++;
			 }
		// get specific category
		} else {
		   for (n in masterVideosArray) {
				if (masterVideosArray[n].category_id == categoryID && masterVideosArray[n].visible==1) { 
					videosArray.push(masterVideosArray[n]); 
					numVideos++;
			 	}
		   } 
			trace("> Found "+numVideos+" in category "+category);
		}	
		//
		// now reset index nums
		trace("> Filtered "+numVideos + " videos out of "+totalVideos);
		for (n=0; n<videosArray.length; n++) { 
			videosArray[n].num=n; 
			//trace(videosArray[n].num + ": " +videosArray[n].title );
		}
		//
	} // end func
	


	function lookup_video_data_by_id(allVideosArray, idToLookup) {	
		//trace("- looking up "+fileToLookup);
		var videoData = new Object(); 
		/*// hand-code intro file
		if (fileToLookup == 1) {
			videoData.length=3;
			videoData.client = "BetaPetrol";
			videoData.title = "Title Card";
			videoData.video_id = fileToLookup;
		} else { 
		*/
			$.each(allVideosArray, function(i, obj) {
				//trace(" "+i+") #"+obj.videoID + " ~ " + idToLookup);
				if (obj.videoID == idToLookup) { videoData = obj; }	//trace("> MATCHED <");
			});
		
		//
		return videoData;
	}
	
	function process_featurepage_data(getVideosArray, callback_function) {
		var featuredIDArray = getVideosArray;
		numVideos = featuredIDArray.length;
		currentVideoNum=0;	// reset
		trace("> found "+numVideos+" videos in feat list.");
		//
		var featuredArray = new Array();	
		for (n=0; n<numVideos; n++) {
			var thisVideoData = lookup_video_data_by_id(masterVideosArray, featuredIDArray[n].video_id);
			if (thisVideoData.client) { // verify
				featuredArray.push( thisVideoData );
			}
		}	
		trace("> put "+featuredArray.length+" videos in feat array.");
		//
		eval(callback_function)(featuredArray); // return
	}

// ======================
// VIDEOS VIA XML

  /*
	* master function
	* gets all xml nodes and creates an array of data objects
	* executes custom callback function on success
	*/
	function get_all_videos_from_xml(xmlfilepath, callback_function) {
		var videoArray = new Array();
		var countVideos=0;
		$.ajax({
			type: "GET",
			url: xmlfilepath,
			dataType: "xml",
			cache:false,
			success: function(xml) {
				$(xml).find("item").each(function(i,node) {							 
					countVideos++;
					var videoData = new Object(); 
					$.each(node.childNodes, function(k,v) { 
						if (v.nodeType == 1) {
							var nodeVal = jQuery.trim( $(v).text() );
							if (nodeVal==" " || nodeVal=="NO") nodeVal=""; // null
							videoData[v.nodeName] =nodeVal;
							//videoData[v.nodeName] = $(v).text();
						}
					});
					videoData.num = countVideos;
					videoArray.push(videoData);
				}); // end (xml).find.each
			//
			// extra processing:
			for (var spot in videoArray) { 
				//if (videoArray[spot].featured==" " || videoArray[spot].featured=="NO") videoArray[spot].featured = ""; 
				if (!videoArray[spot].volume) videoArray[spot].volume = 5; 
			} //trace(videoArray[spot].num+": "+videoArray[spot].title); }
			//
			eval(callback_function)(videoArray); //window[callback_function](videoArray);
			}, // end success	
		}); // end ajax
	}
  /*
	*
	*/
	

		
	/* * * * * * * * * *
	* master function
	* reads xml, gets all video filenames and puts them into blank dataObjects and puts those into an array
	* returns array via callback function
	*/	
	function get_all_filenames_from_xml(xmlfilepath, callback_function) {
		var videoFilenameArray = new Array();
		$.ajax({
			type: "GET",
			url: xmlfilepath,
			dataType: "xml",
			cache:false,
			success: function(xml) {
				$(xml).find("item").each(function(i,node) {	
					var videoData = new Object(); 
			 		//$('.debug').append(node.nodeName + " " + node.childNodes[0].nodeValue +": <br>");
					if (node.nodeName == "item") videoData.video = node.childNodes[0].nodeValue;
					videoFilenameArray.push(videoData);
				}); // end (xml).find.each
			eval(callback_function)(videoFilenameArray);
			}, // end success	
		}); // end ajax
	}

  /*
   * finds xml block that matches filename
	* returns xml block as dataObject 
	*/
	function lookup_video_data_by_filename(allVideosArray, fileToLookup) {	
		var videoData = new Object(); 
		// hand-code intro file
		if (fileToLookup == "betapetrol_logo.mp4") {
			videoData.length=3;
			videoData.client = "BetaPetrol";
			videoData.title = "Title Card";
			videoData.video = fileToLookup;
		} else { 
			$.each(allVideosArray, function(i,xmlBlock) {
				if (xmlBlock.video == fileToLookup) { videoData = xmlBlock; }	
				//trace(xmlBlock.video);
			});
		} 
		//
		//if (!videoData.video) videoData.video="not found";
		return videoData;
	}
	
	function filter_xml_array_for_website_display(videoArray, callback_function) {
		var countVideos=0;
		for (n=0; n<videoArray.length; n++) {
			// remove spots set not to display
			var viz = videoArray[n].visible;
			if (!viz || viz=="NO" || viz==false || viz==0) { videoArray.splice(n,1); } // remove
		}
		// reset number and normalize volume
		for (n=0; n<videoArray.length; n++) {
			videoArray[n].num=n;
			if (!videoArray[n].volume) videoArray[n].volume = 5;
			if (videoArray[n].featured!=1) videoArray[n].featured = 0;
		}
		//
		return videoArray;
		//eval(callback_function)(videoArray); //window[callback_function](videoArray);
	}
	

