/*
 * 3. VIDEO THUMBPAGE FUNCTIONS
 * (c) 2011
 *
 *
 * functions for creating and using thumbnails ONLY
 * see "betapetrol_video_page.js" for video functions
 * create
 * page
 *   display_thumbnails()
 * mouse
 *   get_event_vars(e)
 *   thumb_over(e), etc
 *
 * NOTE:
 * Classes start with "video", ie "videoThumbImage"
 * IDs start with "thumb" and end with number, ie "thumbImage6"
 *
 *
*/ 
	// global thumb vars
	var colorWhite = 'rgba(255,255,255, 1);';
	var colorBlack = 'rgba(000,000,000, 1);';
	var colorGreen = 'rgba(153,255,000, 1);';
	//var videoPath = "videos/thumbnails/";
	var numPages=0;
	var numThumbs=0;
	var thumbsArray = new Array();
	var disposableArray = new Array();
	var thumbSlotsArray = new Array();
	//
	var maxThumbsPerPage=16; // 12		
	var thumbsLeft;
	var thumbsMade=0;
	var thumbSlots=0;
	var thumbdelaytotaltime=0;
	var thumbsOnFirstPage=0;
	var curQuad=0;		
	var tCounter=0;
	//
	if (!imgW) { imgW=213; imgH=120; }
	var bigImgW = 426;
	var bigImgH = 240;
	

// ===================
// DISPLAY THUMBS

	//
	// ***
	function display_all_thumbnails(videosDisplayArray) {
		numThumbs = maxThumbsPerPage;		
		thumbsLeft = numVideos;
		curPage = 0; // reset		
		// 1) 
		videosArray = videosDisplayArray;
		make_thumbs_array();
		// 2) 
		calculate_thumb_pages(); // count thumbs etc
		build_thumb_pages(); // add tpages to stage
		add_thumpage_bgimage();
		// 3) 
		add_all_thumbs();
		thumbs_ready();
	}
	// ***
	//
	
	// 1. 
	function make_thumbs_array() {
		var tempSpots = new Array();
		var tempFeats = new Array();
		// make reel thumbs
		tempReel = new Object();
		tempReel.num=tempFeats.length;
		tempReel.client="Beta Petrol";
		tempReel.thumb="thumb-reel.jpg";
		tempReel.type="reel";
		tempReel.featured=1;
		tempReel.category = currentSubPage;
		if (currentSubPage=="licensing") { //tempReel.category="licensing";
			tempReel.title = "Music Licensing Reel"; 
			tempReel.reelid = "licensing";
			tempFeats.push(tempReel); // add to beginning
			//tempSpots.splice(0,1,tempReel); // add to beginning
		} else if (currentSubPage=="original") { //tempReel.category="original";
			tempReel.title = "Original Music Reel"; 
			tempReel.reelid = "original";
			tempFeats.push(tempReel); // add to beginning
			/*//
			var tempReel2 = jQuery.extend({}, tempReel); // copy entire object
			tempReel2.title = "Sound Design Reel"; 
			tempReel2.reelid = "sounddesign";
			tempFeats.push(tempReel2); // add to beginning
			*/
		}		
		//
		for (t=0; t<videosArray.length; t++) {
			var thisThumb = videosArray[t];
			if (thisThumb.featured==1) {
				tempFeats.push(thisThumb);
				//trace("- Feat: "+thisThumb.title);
				//disposableArray.splice(t,1); // delete
				//disposableArray.unshift(thisThumb); // inesrt at beginning
			} else { //if (thisThumb.visible==1) {				
				tempSpots.push(thisThumb);
			}
		} // end for
		//
		var numFeats = tempFeats.length;
		var ezmode=true;
		// EZ mode: all feats at beginning
		if (ezmode) { thumbsArray = tempFeats.concat(tempSpots); } 
		// PRO mode: feats at first2
		else { 
			trace("# featured spots: "+numFeats);
			if (numFeats<=2) {
				trace("# EZ mode");
				thumbsArray = tempFeats.concat(tempSpots);
			} else { 
				trace("# PRO mode");
				tempSpots.splice(0,2, tempFeats[0],tempFeats[1]); // add 2 to beginning
				for (f=0; f<tempFeats.length; f++) {
				
				}
				thumbsArray = tempSpots;
			}
		
		}
		// renumber
		for (var n=0; n<thumbsArray.length; n++) { thumbsArray[n].num=n; }
	}
		
	// ===
	// 2A)
	function calculate_thumb_pages() {
		// calculate # of pages
		if (thumbsLeft>maxThumbsPerPage) { numPages=Math.ceil(thumbsLeft/maxThumbsPerPage); }
		if (numPages==0) { numPages=1; }
		// recalculate # of pages
		if (numPages>1) { numPages=Math.ceil(thumbsLeft/maxThumbsPerPage); }
		// done.
		trace("- We'll need "+numPages+" pages to accomodate "+thumbsLeft+" thumbs");  
		thumbSlots = numVideos; 
		if (currentSubPage=="licensing" || currentSubPage=="original") { thumbSlots++; }
		// find featured 4x
		var featCount=0;
		for (spot in thumbsArray) {
			if (thumbsArray[spot].featured==1) { featCount++; }
		}
		if (featCount>0) {
			thumbSlots += (featCount * 3); // increment slot count for each feat spot
			if (thumbSlots>maxThumbsPerPage) { numPages=Math.ceil(thumbSlots/maxThumbsPerPage); }
			trace("- There are "+featCount+" featured, bringing the new total to "+ thumbSlots +" slots in " +numPages+" pages.");
		}
		//
	}
	
	// 2B)
	function build_thumb_pages() {
		// create thumbpages (runs only once if only 1 thumbpage)
		for (p=1; p<=numPages; p++) {
		   curPage=p;
		   var curPageHandle = create_thumbpage(curPage);
		   $(curPageHandle).css({ "left":(curPage-1)*1000+"px" }); // position
		   $('.thumbPagesDiv').append( curPageHandle ); // add to screen
			//trace("- Built thumbpage #"+p);
		}
		//trace("- Built "+curPage+" thumbpages");
	}
	function create_thumbpage(num) {
		var newThumbPage = document.createElement('div'); // in memory, not on page	
		newThumbPage.setAttribute( "class","thumbPage" ); 	
		newThumbPage.setAttribute( "id","thumbPage"+num );
		return newThumbPage;
	}	
	
	// 2C)
	function add_thumpage_bgimage() {
		var bgimg = document.createElement('img');
		$(bgimg).attr({"src":"images/thumbs-bg-grid.png"}).addClass("thumbPageBG");
		$('#thumbPage1').append(bgimg).show();
	}
	
	
	// ===	
	
	// 3A)
	function add_all_thumbs() {		
		var countThumbsThisPage=0;
		curPage=1; // reset
		thumbsOnFirstPage=0; // reset
		//
		for (t=0; t<thumbsArray.length; t++) {
			var thisThumb = thumbsArray[t];	//
			if (thisThumb.featured==1) { countThumbsThisPage+=4; } else { countThumbsThisPage+=1; }
			var newThumb = build_a_thumbnail( thisThumb ); // CREATE THUMB
			$('#thumbPage'+curPage).append( newThumb ); // ADD TO PAGE
			if (countThumbsThisPage>=maxThumbsPerPage) { curPage++; countThumbsThisPage=0; } // go to next page
			if (curPage==1) { thumbsOnFirstPage++; }
		}
		// ---
		// fill in remaining slots with blanks
		var countEmptyThumbSlots = (maxThumbsPerPage - countThumbsThisPage);
		var startIndex = thumbsArray.length;
		for (var e=0; e<countEmptyThumbSlots; e++) {
			var blankThumb = build_blank_thumbnail( (startIndex + e) ); // create blank
			$('#thumbPage'+curPage).append( blankThumb );
		}
	}
		
	// 3B) 
	function thumbs_ready() {
		$('.thumbText').css("left","-500px"); // all		
		curPage=1; // reset		
		// animate
		for (t=0; t<=thumbsOnFirstPage; t++) {
			var delayTime= (t*100);
			var animTime = 250;
			if ( $('#thumb'+t).width() > 400) { 
				//$('#thumb'+t).stop(true,true).hide().delay(t*150).fadeIn(500);
				//delayTime = t*250;
				if (t==0) { delayTime=0; }
				else if (t==1) { delayTime=400; }
				else if (t==2) { delayTime=500; }
				else if (t==3) { delayTime=600; }
				$("#thumbImage"+t+", #thumbImage"+t+"_BW").css({"left":"-500px"}).delay(delayTime).animate({"left":"0px"}, animTime);
			} else { 
				delayTime += 200;
				$("#thumbImage"+t+", #thumbImage"+t+"_BW").css({"left":"-500px"}).delay(delayTime).animate({"left":"0px"}, animTime);
			}
			trace("- "+t+": "+delayTime);
		}
		//
		setTimeout(manage_nav_menu_nextprev, 500); 
		$('.PrevNextDiv').hover(arrowsOver, arrowsOut).css({"opacity":"0.5"});
		$('.PrevNextDiv').click(arrowsClick);
		//
		setTimeout(activate_thumbs, (thumbsOnFirstPage*250) );
	}
	
	function activate_thumbs() {
		$('.thumbnailDiv').hover(thumb_over, thumb_out).click(thumb_click).css({"cursor":"pointer"});	
		//
	}
	

// ========================================
// MENU NAV ITEMS

	// METHODS

	function videoCloseClick() {
		close_video(); // local
	}
	function videoCloseOver(e) {
		$(e.target).addClass("hilite");
	}	
	function videoCloseOut(e) {
		$(e.target).removeClass("hilite");
	}
	
	function vpanel_click_link(e) {
		var thisNum = e.target.num;
		var winURL = videosArray[thisNum].link;
		var winTitle = videosArray[thisNum].client;
		var newWindow = window.open(winURL, winTitle);	
	}

	


// ========================================
// NEXT PREV

	function arrowsOver(e) {
		$(this).css({"opacity":"1"});
	}
	function arrowsOut(e) {
		$(this).css({"opacity":"0.5"});
	}
	function arrowsClick(e) {
		var me = $(this).attr("id");
		if (me == "nextArrow") { goto_next_thumbpage(); }
		else if (me == "prevArrow") { goto_prev_thumbpage(); }
	}	

	function manage_nav_menu_nextprev() {
		clear_nav_menu();
		if (numPages>1) { 
			if (curPage<numPages && curPage<=1) { // show next only
				$('#prevArrow').hide();
				$('#nextArrow').show();
				$('#nextArrowImg').stop(true,true).css({"position":"absolute","left":"-50px"}).delay(150).animate({"left":"0px"},250);
				//nav_menu_add_current();
			}
			else if (curPage<numPages && curPage>1) { // show next and prev
				$('#prevArrow').show();
				$('#nextArrow').show();
				//nav_menu_add_current();
			}
			else if (curPage==numPages) { // show prev
				$('#prevArrow').show();
				$('#prevArrowImg').stop(true,true).css({"position":"absolute","left":"50px"}).delay(150).animate({"left":"0px"},250);
				$('#nextArrow').hide();
				//nav_menu_add_current();
			}
		}
		// activate
		$('.navItemVideo').hover(menu_over, menu_out); // all
	}
	
	
// METHODS
	
	var pageSpeed = 250;
	var pageWidth = 1000;

	function goto_next_thumbpage() {
		var cur = curPage;
		var speed=pageSpeed;
		if (curPage < numPages) {
			var nextPage = "thumbPage"+(curPage + 1).toString();
			curPage++;
			var toleft = ((curPage*pageWidth) - pageWidth);
			$('.thumbPagesDiv').animate({ "left":"-"+toleft+"px" }, speed);
			manage_nav_menu_nextprev();
		}
	}
	
	function goto_prev_thumbpage() {
		var cur = curPage;
		var speed=pageSpeed;
		//		
		if (curPage>1) {
			curPage--;
			var toleft = ((curPage*pageWidth) - pageWidth) * -1;
			$('.thumbPagesDiv').animate({ "left":""+toleft+"px" }, speed);
			manage_nav_menu_nextprev();
		}
	}

// ===

	function thumbpage_hello(str) {
		trace("Hello, "+str);
	}

