//******************************************************************************************//
//     	 			   File: 	html_javascript
//
//			 		Version:	1.0 
//					   Date:	Thu Jun 8 2006
//				  Copyright:	¨2006 PureBlend New Media Design Group <info@pureblend.com>
//
//******************************************************************************************//
var d = document;
var arrSubMenuItems;
var activeMenu;
var boolShowBuyBtn;
function OpenPopupWindow(aURL,aWidth,aHeight) {

	// mozilla & safari DOM
	theParentTop 		= parent.screenY;
	theParentLeft 		= parent.screenX;
	theParentHeight 	= parent.innerHeight;
	theParentWidth 		= parent.innerWidth;
	
	// winIE DOM
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
		theParentTop 		= parent.screenTop - 60; // cheat the top a bit
		theParentLeft 		= parent.screenLeft;
		theParentHeight 	= document.body.offsetHeight;
		theParentWidth 		= document.body.offsetWidth;
	}
	
	// common code
	theTop 				= Math.round((theParentHeight / 2) - (aHeight / 2) + theParentTop);
	theLeft				= Math.round((theParentWidth  / 2) - (aWidth  / 2) + theParentLeft);
	//windowName_suffix  	= Math.round(Math.random()*4);
	
	theWindow = window.open(aURL, 'popWindow', 'width=' + aWidth + ',height=' + aHeight + ',top=' + theTop + ',left=' + theLeft + ',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes');
}

function OpenSlideShowWindow(anID) {
	theURL = "/slideshow.php?resID=" + anID;
	if (arguments.length > 1) {
		theURL += '&idf='+arguments[1]+'&tab='+arguments[2];
	} 
	OpenPopupWindow(theURL,1000,820);
}

function OpenSlideShowImage(path,caption,height,width) {
	theURL = "/slideshow.php?path=" + path + "&caption=" + caption + "&height=" + height + "&width=" + width;
	OpenPopupWindow(theURL,1000,820);
}


function populateMenu(arrMenuItems, strMenuID, boolShowBuyBtn) {
	var theMenu			= d.getElementById(strMenuID);
	theMenu.innerHTML 	= '';
	
	if (theMenu && typeof arrMenuItems != 'undefined') {
		var menuLength			= arrMenuItems.length;
		var itemWidth			= 85;
		var availColumns		= 5;
		var colCount			= 1;
		var theStyle			= '';
		for (i = 0; i < menuLength; i++) {
			
			var theMenuItem 	= d.createElement('li');
			var theId		 	= getTokenizedSegControlId(arrMenuItems[i]);//'segControl_'+arrMenuItems[i].replace(/\s/g, '_').toLowerCase();
			if (d.getElementById(theId)) {
				theId 			= theId+i;
			} 
			
			
			if (colCount > availColumns) {
				// new row.
				colCount = 0;
			} 
			
			theMenuItem.setAttribute('id', theId);
			theMenuItem.setAttribute('onclick', 'setSegmentControl(this);');
			theMenuItem.innerHTML	= arrMenuItems[i];
			
			
			theMenu.appendChild(theMenuItem);
			
			if (theMenuItem.clientWidth > itemWidth) {
				theStyle += 'width:'+theMenuItem.clientWidth+'px;';
			} 
			
			if (menuLength < 6) {
				theStyle += 'width:auto;';
				
			} 
			
			if (theStyle != '') {
				theMenuItem.setAttribute('style', theStyle);
			} 
			colCount++;
		}
		
	} 
	
	if (boolShowBuyBtn) {
		var theBuyButton 	= d.createElement('a');
		theBuyButton.setAttribute('href', '/Buy/MovieSlate');
		theBuyButton.setAttribute('id', 'buyNowButton_top');
		theBuyButton.setAttribute('class', 'buttonStdBlue');
		theBuyButton.innerHTML = 'Buy Now';
		theMenu.parentNode.appendChild(theBuyButton);
	} 
	
	
}
function startup() {
	
	(activeMenu)
		setCurrentMenuItem(activeMenu);
	if (typeof arrSubMenuItems != 'undefined') {
		setCurrentMenuItem(activeSubMenu);
	} 

	var videos	= d.getElementsByTagName('div');
	//  && videos[i].hasChildNodes() == false
	if (typeof videos == 'object') {
		for (i = 0; i < videos.length; i++) {
			//alert(videos[i].className);
			if (videos[i].className.indexOf('video_poster') > -1 && videos[i].hasChildNodes() == false) {
				videos[i].click();
				
			} 
		}
	} 
}

function setCurrentMenuItem(str_id) {
	if (str_id != null && str_id != '') {
		var tok_id		= getTokenizedSegControlId(str_id);

		var theTokIDTarget	= d.getElementById(tok_id);

		if (theTokIDTarget != null) {
			theTokIDTarget.className = 'selected';
			var sitemap 	= d.getElementById('sitemap');
			var theLinks	= sitemap.getElementsByTagName('a');
			var test 		= '';
			for (i = 0; i < theLinks.length; i++) {
				var linkTok	= getTokenizedSegControlId(theLinks[i].pathname.substring(1));
				
				//test += 'tok_id: '+tok_id+", linkTok: "+linkTok+"\n";
				if (linkTok != 'undefined' && linkTok == tok_id) {
					theLinks[i].className = 'selected';
				} 
				
			}
			//alert(test);
		} 
		
		
	} 
}
function getTokenizedSegControlId(str) {
	if (str) {
		var theToken = 'segControl_'+str.replace(/\s/g, '_').replace(/\//g, '_');
		return theToken;
	} 
}

function setSegmentControl(obj) {
	//{{{
	var theSegmentedControl 	= obj.parentNode;//d.getElementById('segmentedControl_Features');
	
	if (theSegmentedControl && obj) {
		var theSegments				= theSegmentedControl.getElementsByTagName('li');
		var theSection 				= obj.id.replace(/segcontrol\_/i, '');// obj.wholeText.toLowerCase().replace(/\s/g, '_');
		
		d.location.href 				= '/'+theSection+'/';
		
	} 
	
	//}}}
}


function setSlideshowControl(id) {
	//{{{
	d.location.href 				= '/slideshow.php?resID='+id;
	//}}}
}

arrow_left_gen_ot 		= new Image();
arrow_left_gen_ot.src 	= "/images/arrows/arrow_left_gray_ot.gif";
arrow_left_gen_ov 		= new Image();
arrow_left_gen_ov.src 	= "/images/arrows/arrow_left_blue_ov.gif";

arrow_left_ov	 		= new Image();
arrow_left_ov.src 		= "/images/arrows/arrow_left_ov.png";
arrow_left_ot 			= new Image();
arrow_left_ot.src 		= "/images/arrows/arrow_left_ot.png";

arrow_right_ov	 		= new Image();
arrow_right_ov.src 		= "/images/arrows/arrow_right_ov.png";
arrow_right_ot 			= new Image();
arrow_right_ot.src 		= "/images/arrows/arrow_right_ot.png";

function playInlineVideo(posterContainerObj, str_extra_cssClassName) {
	//alert('Loading posterContainerObj.className');
	var thePoster					= posterContainerObj;
	thePoster.className				= 'video_poster_active '+str_extra_cssClassName;
	thePoster.style.backgroundImage = '';
	var theVideo					= thePoster.getElementsByTagName('video')[0];
	//theVideo.style.visibility		= '';
	var count = 0;
	while(theVideo.readyState != 4 && count < 10) {
		try {
			var fullScreen = theVideo.webkitSupportsFullscreen;
			theVideo.play();
		} catch(e) {
			thePoster.childNodes[0].style.zIndex = -1;
			thePoster.childNodes[1].style.zIndex = -1;
		}
		count++;
	}
	
}

function videoLoadedMetadata() {
}

function videoFailed(e) {
	// video playback failed - show a message saying why
	switch (e.target.error.code) {
		case e.target.error.MEDIA_ERR_ABORTED:
			alert('You aborted the video playback.');
			break;
		case e.target.error.MEDIA_ERR_NETWORK:
			alert('A network error caused the video download to fail part-way.');
			break;
		case e.target.error.MEDIA_ERR_DECODE:
			alert('The video playback was aborted due to a corruption problem or because the video used features your browser did not support.');
			break;
		case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
			alert('The video could not be loaded, either because the server or network failed or because the format is not supported.');
			break;
		default:
			alert('An unknown error occurred.');
			break;
	}
}
	

function drawMovieContainer(objCaller, arrMovieSrc, strPosterPath, intWidth, intHeight, strCssStyle) {
	/*
	<div class="video_poster" style="width:480px;height:351px;border:1px solid #888;margin-left:0px;" onclick="playInlineVideo(this, 'float_right');">
		<video width="480" height="265" preload="none" controls="controls" >
			Your browser does not support HTML5.
			<source src="http://s3.amazonaws.com/insidemoves/interview_077f7.mp4" type="video/mp4" />
			<source src="http://s3.amazonaws.com/insidemoves/interview.webm" type="video/webm" />
		</video>
	</div>
	*/
	
	
	var posterDiv				= objCaller;//
	
	posterDiv.setAttribute('class',  'video_poster');
	posterDiv.setAttribute('style',  strCssStyle);
	posterDiv.style.width		= intWidth;
	posterDiv.style.height		= intHeight;
	
	
	var theVideo				= d.createElement('video');
	
	for (i = 0; i < arrMovieSrc.length; i++) {
		var aVideoSrc				= d.createElement('source');
		aVideoSrc.setAttribute('src',  arrMovieSrc[i][0]);
		aVideoSrc.setAttribute('type',  arrMovieSrc[i][1]);
		aVideoSrc.setAttribute('width',  intWidth);
		aVideoSrc.setAttribute('height',  intHeight);
		aVideoSrc.setAttribute('preload',  'none');
		aVideoSrc.setAttribute('controls',  'controls');
		aVideoSrc.setAttribute( 'autoplay',   false);
		theVideo.appendChild(aVideoSrc);
	}
	posterDiv.appendChild(theVideo);
	
	//posterDiv.onfocus 			= playInlineVideo(posterDiv, '');
	addEvent(posterDiv, 'click', function() { playInlineVideo(posterDiv, '') });
}

function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}

function playGifAnimation(src) {
	var path		= '/art/anims/'+src+'.gif';
	var theImage	= d.getElementById('anims_'+src);
	theImage.src 	= path;
}



function ShowAnObject(elt) {
	elt.style.display = "block";
}

function HideAnObject(elt) {
	elt.style.display = "none";
}

function openPhoneNav() {
	//{{{
	var phoneCover			= d.getElementById('phoneCover');
	var theSelect 			= d.getElementById('phoneCover_select');

	if (!theSelect) {
		
		var theSelect 			= d.createElement('select');
		theSelect.id			= 'phoneCover_select';
		theSelect.style.width	= '1px';
		theSelect.style.opacity	= 0;
		theSelect.onchange		= function(e) {
			d.location = '/'+this.options[this.options.selectedIndex].value;
		}
		
		var subMenus			= arrSubMenuItems[activeMenu];
		subMenus.unshift('Please Choose&hellip;');
		subMenus.push('Buy from iTunes');
		var subMenusLength		= subMenus.length;
		
		for (i = 0; i < subMenusLength; i++) {
			var anOption 			= d.createElement('option');
			anOption.value 			= subMenus[i].replace(/\s/g, '_');
			anOption.innerHTML		= subMenus[i];
			if (activeSubMenu == subMenus[i]) {
				anOption.selected 	= true;
			} 
			theSelect.appendChild(anOption);
		}
		phoneCover.appendChild(theSelect);
	} 
	setTimeout(theSelect.focus(), 100);
	//}}}
}

