// ===== MooTools =========================
var imagePath = "";
var activeAXIS = "";
var scrollBars = "";

// ===== Load media data then play first video ==================================================================
window.addEvent('domready',function(){
	if(!$('videoPlayer')) return;

	// ===== FLASH: Video Module ================================================
	var so = new SWFObject('flash/videoModule.swf', 'oeVideoModule', '100%', '100%', '8', '#000000');
	if(so.installedVer.major >= 7){
		so.useExpressInstall('flash/expressinstall.swf');
		so.addParam('allowScriptAccess','always');
		so.write("videoPlayer");
	} else {
		$('videoPlayer').set('html','<h2>Please <a href="http://www.adobe.com/products/flashplayer/" target="blank">upgrade your Flash Player</a><br /> to experience this site to its fullest.</h2>');	
	}
	if($('scrollContent')){
		scrollBars = new ScrollBar('scrollingArea', 'scrollContainer', 'scrollbar', 'track', 'thumb', 'arrowUp', 'arrowDown');
		new Drag('scrollContainer', {
			'modifiers': {y: 'height', x: 'width'},
			'limit': { x:[100], y:[50] },			
			onDrag: function(){
				scrollBars.update();
			}
		});
	}
	// ===== Load the PlayList ==================================================
	if($('singleVideoItem')){
		window.addEvent('domready',function(){
			var media = {
				videostill: $('singleVideoItem').getElement('.video_still').get('text'),
				video: $('singleVideoItem').getElement('.video').get('text')
			};
			if($('oeVideoModule')){
				(function(){ document.getElementById('oeVideoModule').loadMedia(media.videostill,media.video); }).delay(250);
			}
		});
	} else {
		var playList = ($(document.body).hasClass('index'))? $('videoItems') : $('scrollContent');
		var oMedia = {}; // Media Container
		var activeVideo = "";
		
		var fid = jQuery('#ctl00_uxMainContent_uxParam').attr('value');
		
		var mediaRequest = new Request({
			url:'/photovideoactionpage.aspx',
			data: "fid=" + fid, 
			method: 'get',
			onSuccess: function(txt,xml) {
				var media = xml.getElementsByTagName('media');
				oMedia.total = media.length;
				var ul = new Element('ul');
				for(var x=0,xl=media.length;x<xl;x++){
					// ===== Convert XML To Object =====
					oMedia[x] = {
						id: media[x].getAttribute('id').trim(),
						asset: {
							thumbnail: media[x].getElementsByTagName('asset')[0].firstChild.nodeValue.trim(),
							videostill: media[x].getElementsByTagName('asset')[1].firstChild.nodeValue.trim(),
							video: media[x].getElementsByTagName('asset')[2].firstChild.nodeValue.trim(),
							href: media[x].getElementsByTagName('asset')[3].firstChild.nodeValue.trim()
						},
						title: media[x].getElementsByTagName('description')[0].getAttribute('title').trim(),
						description: {
							short: media[x].getElementsByTagName('short-description')[0].firstChild.nodeValue.trim(),
							long: media[x].getElementsByTagName('long-description')[0].firstChild.nodeValue.trim()
						}
					};
					// ===== Create PlayList =====
					// <li><a href="#"><img src="images/photo_videoItem1.jpg" width="54" height="36" alt="" /><span><b>Teaser 1 (2:56)</b><br />In rutrum ac</a></span></li>
					ul.adopt(new Element('li').adopt(
						new Element('a',{
							'nVideo': x,
							'events': {
								'click': function(){
									// ===== Set Active Video Button =====
									if(activeVideo != "") activeVideo.removeClass('active');
									activeVideo = this.getParent();
									activeVideo.addClass('active');
									// ===== Activate Selected Video =====
									var media = oMedia[this.getAttribute('nVideo')];
									if($('videoDescription')){
										$('videoDescription').empty().set('html','<h2>'+media.title+'</h2>', media.description.long);
									};
									if($('oeVideoModule')){
										document.getElementById('oeVideoModule').loadMedia(media.asset.videostill,media.asset.video);
									}
								}
							}
						}).adopt(new Element('img',{'src':oMedia[x].asset.thumbnail,'width':54,'height':36,'alt':''})
						).adopt(new Element('span').set('html','<b>'+oMedia[x].title+'</b>'))
					));
				};
				playList.empty().adopt(ul);
				if($('scrollContent')){
					scrollBars.update();
				}
				// Delay 'fireEvent' to allow flash module to instantiate;
				(function(){ playList.getElements("li")[randomRange(0, oMedia.total-1)].getElement('a').fireEvent('click') }).delay(125);
			},
			onFailure: function() {
				//$('result').set('text', 'The request failed.');
			}
		}).send();
	};

});

// ============================================
String.extend({
	trim: function(){
		return this.replace(/^\s*|\s*$/g, "");
	}
});
function randomRange(lower, upper) {
	var range = upper-lower+1; 
	var l = ("" + range).length; 
	var randomNumber = (Math.floor(Math.random() * Math.pow(10,l)) % range) + parseInt(lower); 
	return randomNumber; 
}
