/*
 * File dependency: swfobject.js
 */

includeJS('/pub-share/js/swfobject.js');

function includeJS(scriptPath) {
	document.write('<script type="text/javascript" src="' + scriptPath + '" language="javascript"></script>');
	return false;
}


/*
 * Variable dependency: var currentTeam in library.js
 * Initialize video player, hide it, show first photo
 */

var Gallery = Class.create();
Gallery.prototype = {
	initialize: function() {
		this.Urls = new Array();
		this.initVideos();
		this.currentVideo = 0;
		this.menuItems = [ $('videoPlayImg_0'),$('videoPlayImg_1'),$('videoPlayImg_2'),$('videoPlayImg_3'),$('videoPlayImg_4'),$('videoPlayImg_5'),$('videoPlayImg_6'),$('videoPlayImg_7'),$('videoPlayImg_8') ];
	},
	
	handleMenuMouseOver: function(imageNum, imageName) {
		$('videoPlayImg_' + imageNum).src = '/hypeman/images/' + imageName + '_on.gif';
	},
	
	handleMenuMouseOut: function(imageNum, imageName) {
		if(this.currentVideo != imageNum)
		{
			$('videoPlayImg_' + imageNum).src = '/hypeman/images/' + imageName + '.gif';
		}
	},
	
	turnOffUnselectedMenuItems: function() {
		this.menuItems.each(function(item, index) {
			if(index != this.currentVideo) {
				var imageName = item.src;
				imageNum = imageName.split('video_part')[1].substr(0,1);
				item.src = '/hypeman/images/video_part' + imageNum + '.gif';
				var foo = "foo";
			}
		}.bind(this));
	},
			
	// *** videos ***
	
	initVideos: function() {

		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN1.flv');    
		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN2.flv');
		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN3.flv');
		
		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN4.flv');
		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN5.flv');
		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN6.flv');
		
		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN7.flv');
		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN8.flv');
		this.Urls.push('http://a230.g.akamai.net/7/230/83646/v001/toyota.download.akamai.com/83646/scion/media/video/hypeman/HYPEMAN9.flv');
		
		this.createVideoPlayer(this.Urls[0]);
		
	},
	// create video player
	// http://www.jeroenwijering.com/?item=JW_FLV_Media_Player
	createVideoPlayer: function(videoPlaylist) {
		var so = new SWFObject('swf/mediaplayer.swf', 'galleryVideoPlayer', '372', '258', '8');
		so.addParam('allowscriptaccess', 'always');
		so.addParam('allowfullscreen', 'false');
		so.addParam('flashvars','&file=' + this.Urls[0] + '&bufferlength=9&stretching=fit&skin=/hypeman/swf/playerSkin.swf&');
		so.addParam('wmode', 'opaque');
		so.addVariable('file', videoPlaylist);
		so.addVariable('width', '484');
		so.addVariable('height', '325');
		so.addVariable('showicons', 'false');
		so.addVariable('usefullscreen','false');	
		so.write('video_holder');
		//$('video_holder').style.visibility = 'hidden';
	},
	
	/** video player utilities **/
	sendEvent: function(swf,typ,prm) { 
		if($(swf)) {
			try {
				var player = $(swf);
				player.sendEvent(typ,prm); 
			} catch(e) {
				// swf probably hasn't been init'd yet
				new PeriodicalExecuter(function(pe) {
					try {
						var player = $(swf);
						player.sendEvent(typ,prm); 
						pe.stop();
					} catch(e) {
						// try again later
						//alert('failed in PE - error is e: ' + e.message);
					}
				}.bind(this), 2);
			}
		}
	},
	getUpdate: function(typ,pr1,pr2,swf) {
		// do nothing for now
	},
	loadFile: function(swf,obj) { 
		if($(swf)) {
			try {
				this.sendEvent("LOAD",obj);
			} catch(e) {
				// do nothing ... swf probably hasn't been init'd yet
				alert('loadFile failed: ' + e.message);
			}
		}
	},
	playVideo: function(number) { 
		this.sendEvent('galleryVideoPlayer','STOP');
		this.sendEvent('galleryVideoPlayer', 'LOAD', {file:this.Urls[number]});
		this.sendEvent('galleryVideoPlayer','PLAY', 0);
		this.currentVideo = number;
		this.turnOffUnselectedMenuItems();
	}
}




