/*************************************************************************
* global-functions.js
* A collection of small functions used throughout the site.
*
* Usage:
*
* Dependencies: 		Mootools 1.2.1 Core
*
* Author: 				Tri Doan
*
* v.1 (11/18/2008) 	- 	Initial release of Game Media Section
*
*
* PLEASE KEEP FUNCTION SECTIONS IN ALPHABETICAL ORDER
*	-Game Media Section
*	-IE Only
*	-Onload Event
*	-Page ID insertion
*	-Parent Sniffer
*
**************************************************************************/

/**************************************************************************
*  GAME MEDIA SECTION
*
*  Description:	A collection of functions to handle the screenshots/videos
*				for the game detail pages.  Not enough time to convert these
*				into OO classes.  Maybe later?
*
*  To Do:		Object Orientify this bad boy
***************************************************************************/
// Some Global Vars:
var ssContainerID = $('screenshotInner');			// container ID for image
var ssImageID = $('screenshotImage');				// image ID

var vidContainerID = $('videoInner');				// container ID for video

function displaySS(theScreenshot) {
	if(stopFlashVideo()) {
		vidContainerID.set('styles',{'display':'none'});
		ssContainerID.set('styles',{'display':'inline'});
		ssImageID.set('src',theScreenshot);
	}
}
function startVideo(vidPath,vidID) {
	if($('video_player')){
		stopFlashVideo();
		$('video_player').playVideo(vidPath);
	}
	if($(vidID)){
		if (Browser.Engine.trident) {
			var temp = $$('.assetList object');
		} else {
			var temp = $$('.assetList embed');
		}
		for(i=0;i<temp.length;i++){
			stopFlashVideo(temp[i].id);
		}
		$(vidID).playVideo(vidPath);
		//window.setTimeout(function(){ $(vidID).stopVideo() },1500);
	}
}
function displayVideo(vidPath, vidWidth, vidHeight, isPortrait, assetMode, vidParent) {
	var playerHeight = 296;
	var playerWidth = 322;

	if(isPortrait =='true') {
		playerHeight = 392;
		playerWidth = 240;
	}
	if(assetMode) {
		if($(vidParent)){
			$(vidParent).set('html', '<br /><br /><p style="cursor:pointer;">Click to start video.</p><br /><br />');
		}
		$$('#'+vidParent+' p')[0].addEvent('click', function(){
		    var vidID = vidParent+'_video_player';
			var playerObject = new SWFObject(flashPlayerFile, vidID, playerWidth, playerHeight, '8', '#000000' );
			playerObject.addParam( 'wmode', 'transparent' );
			playerObject.addParam( 'scale', 'exactfit' );
			playerObject.addParam( 'allowScriptAccess', 'always' );
			playerObject.addVariable('skin_path', flashSkinPath);
			playerObject.write(vidParent);

			var vidURL = generateExternalEmbedCode(vidWidth,vidHeight,vidPath);
			$(vidParent+'_url').set('value',vidURL);

			window.setTimeout(function(){ startVideo(vidPath,vidID) },500);
		});

	} else {
		ssContainerID.set('styles',{'display':'none'});
		vidContainerID.set('styles',{'display':'inline'});

		var playerObject = new SWFObject(flashPlayerFile, 'video_player', playerWidth, playerHeight, '8', '#000000' );
		playerObject.addParam( 'wmode', 'transparent' );
		playerObject.addParam( 'scale', 'exactfit' );
		playerObject.addParam( 'allowScriptAccess', 'always' );
		playerObject.addVariable('skin_path', flashSkinPath);
		playerObject.write('videoHolder');

		generateExternalEmbedCode(vidWidth,vidHeight,vidPath);

		window.setTimeout(function(){ startVideo(vidPath) },250);

	}
}
function generateExternalEmbedCode(vidWidth, vidHeight, vidPath){
	var re = new RegExp('\/([0-9]+)\.Par');
	var m = re.exec(vidPath);
	var gameID = m[1];

	var currUrl = (location.href).split ('/');

	var rootURL = "http://www.n-gage.com";
	var vidHeight = vidHeight - 40;
	var embedCode = "<object width='" + vidWidth + "' height='" + vidHeight + "'>" +
					"<param value='lt' name='salign'></param>" +
					"<param value='high' name='quality'></param>" +
					"<param value='exactfit' name='scale'></param>" +
					"<param name='wmode' value='transparent'></param>" +
					"<param name='movie' value='"+rootURL+"/"+currUrl[3]+"/images/showroom/flvplay.swf'></param>" +
					"<param name='FlashVars' value='&streamName=" +
						rootURL + vidPath + "&skinName="+ rootURL +
						"/"+currUrl[3]+"/images/showroom/flvskin&autoPlay=true&autoRewind=true'></param>" +
					"<embed width='" + vidWidth + "' height='" + vidHeight +
						"'flashvars='&streamName=" + rootURL + vidPath +
						"&autoPlay=false&autoRewind=true&skinName=" + rootURL +
						"/"+currUrl[3]+"/images/showroom/flvskin.swf' quality='high' scale='noscale' salign='LT' " +
						"type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' " +
						"src='" + rootURL + "/"+currUrl[3]+"/images/showroom/flvplay.swf' wmode='transparent'></embed>" +
					"</object><br />" +
					"<a href='" + rootURL + "'>N-Gage</a>: " +
					"<a href='" + rootURL + "/"+currUrl[3]+"/ngage/web/g0/en/showroom/gamedetail."+ gameID +".GameDetail.html' style='cursor:pointer; text-decoration:none;'>Learn more</a>";

	if($('videoURL')) {
		$('videoURL').set('value',embedCode);
	} else {
		return embedCode;
	}
}

function stopFlashVideo(vidID) {
	if($('video_player')) {
		if ($('video_player').stopVideo) $('video_player').stopVideo();
	} else if($(vidID)){
		if ($(vidID).stopVideo) $(vidID).stopVideo();
	}
	return true;
}
function selectInput(inputField) {
	var inputID = inputField.id;
	$(inputID).focus();
	$(inputID).select();
}

/**************************************************************************
*  Parent Sniffer
*
*  Description:	These 2 functions detects when mouse leaves the parent of the
*				target el, counteracts the bubbling effect
*
*  Uses:		Game Showroom Page
*  To Do:		none;
***************************************************************************/
function mouseLeaves (element, evt) {
	if (typeof evt.toElement != 'undefined' && evt.toElement && typeof element.contains != 'undefined') {
		return !element.contains(evt.toElement);
	}
	else if (typeof evt.relatedTarget != 'undefined' && evt.relatedTarget) {
		return !contains(element, evt.relatedTarget);
	}
}

function contains (container, containee) {
	while (containee) {
		if (container == containee) {
			return true;
		}
		containee = containee.parentNode;
	}
	return false;
}