// == FOLDER STRUCTURE ==
var folder = {
	JS: 'js/',
	XML: 'xml/',
	FLASH: 'flash/',
	IMAGES: 'images/',
	MEDIA: 'media/'
};

// ===== Load media data then play first video ==================================================================
window.addEvent('domready',function(){
	if(!$('videoPlayer')) return;

	// ===== FLASH: Video Module ================================================
	var so = new SWFObject(folder.FLASH+'videoModule.swf', 'oeVideoModule', '100%', '100%', '8', '#000000');
	if(so.installedVer.major >= 7){
		so.useExpressInstall(folder.FLASH+'expressinstall.swf');
		so.addParam('allowScriptAccess','always');
		so.addParam('wmode','transparent');
		so.write("videoPlayer");
	} else {
		$('videoPlayer').set('html','<div class="upgrade"><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></div>');	
	}
	
	// ===== XML: Read the Media List of Videos/Descriptions ======================
	$$('li.menuparent a').addEvent('click',function(e){ new Event(e).stop(); });
		
	var aMedia = []; // Media Container
	var mediaRequest = new Request({
		url: folder.XML + 'mediaList.xml', 
		method: 'get',
		onSuccess: function(txt,xml) {			
			var media = xml.getElementsByTagName('media');

			for(var x = 0, xl = media.length; x < xl; x++){
				// ===== Convert XML To Object =====
				var oMedia = {};				
				oMedia.asset = {};
				oMedia.id = media[x].getAttribute('id').trim();				
				oMedia.title = media[x].getElementsByTagName('description')[0].getAttribute('title').trim();
				//IE needs different node caller
				if(Browser.Engine.trident){
					oMedia.description = {
						short: (media[x].getElementsByTagName('short-description')[0] != null)? media[x].getElementsByTagName('short-description')[0].text.trim() : null,
						long: (media[x].getElementsByTagName('long-description')[0] != null)? media[x].getElementsByTagName('long-description')[0].text.trim() : null
					};
				}else{
					oMedia.description = {
						short: (media[x].getElementsByTagName('short-description')[0] != null)? media[x].getElementsByTagName('short-description')[0].textContent.trim() : null,
						long: (media[x].getElementsByTagName('long-description')[0] != null)? media[x].getElementsByTagName('long-description')[0].textContent.trim() : null
					};
				}
				var assets = media[x].getElementsByTagName('asset');
				for(var a = 0, al = assets.length; a < al; a++){
					switch(assets[a].getAttribute("type")){
						case "thumbnail" : oMedia.asset.thumbnail = assets[a].firstChild.nodeValue.trim(); break;
						case "video_still" : oMedia.asset.videostill = assets[a].firstChild.nodeValue.trim(); break;
						case "video" : oMedia.asset.video = assets[a].firstChild.nodeValue.trim(); break;
						case "href" : oMedia.asset.href = assets[a].firstChild.nodeValue.trim(); break;
					};
				};
				aMedia.include(oMedia);
				
				// ===== Attach Events to menu items to play video =====
				
				$(aMedia[x].id).setProperty('rel','media'+x).addEvent('click',function(e){
				   
					new Event(e).stop();
					var media = aMedia[this.getProperty('rel').replace(/\D+/g,'').toInt()];
					if($defined(media.asset.video) && !$defined(media.asset.thumbnail)){
						// == VIDEO ==
						$('photoViewer').addClass('hide');
						$('videoPlayer').removeClass('hide');
						$E('.videoDescription').empty();
						if($defined(media.description.short)){
							$E('.videoDescription').set('html',media.description.short);
						}
						(function(){ document.getElementById('oeVideoModule').loadMedia(media.asset.videostill,media.asset.video); }).delay(250);
						
					} else if(!$defined(media.asset.video) && $defined(media.asset.thumbnail)){
						// == PHOTO ==
						$('videoPlayer').addClass('hide');
						$('photoViewer').removeClass('hide').set('html','<div class="img"><a href="'+ media.asset.href +'"><img src="'+ media.asset.thumbnail +'" title="'+ media.title +'" /></a></div>');
						var img = $E('#photoViewer img');
						img.setOpacity(0.01).addEvent('load',function(){
							this.setStyles({
								'top': (174-this.getCoordinates().height/2)+'px',
								'left':(234-this.getCoordinates().width/2)+'px',
								'opacity':1
							});
						});
						$E('.videoDescription').empty();
						if($defined(media.description.short)){
							$E('.videoDescription').set('html',media.description.short);
						}
					}
				});
			}
		},
		onFailure: function() {
			//$('result').set('text', 'The request failed.');
		}
	}).send();
});
window.addEvent('load',function(){
	// ===== AutoPlay introductory video =====
	if(document.getElementById('videoPlayer')){		
		(function(){ document.getElementById('oeVideoModule').loadMedia(folder.MEDIA + 'photos/clear.gif',folder.MEDIA + 'videos/introduction_ETC.flv'); }).delay(250);
	};
});
// ===== Fix issues for any IE < v7 =========================================================================
function fixPNG(){
	$$('img[src$=png]').each(function(el){
		var coord = el.getCoordinates();
		el.setStyles({
			width: coord.width,
			height: coord.height,
			filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + el.src + '", sizingMethod="crop")'
			//visibility: 'hidden'
		});
		el.src = folder.IMAGES + 'clear.gif';
	});
};

function fixHover(){
	$$('.primary-nav li').each(function(menu){
		if(menu.hasClass('menuparent')){
			menu.onmouseover = function() { this.className += " over"; this.style.zIndex=200; }
			menu.onmouseout = function() { this.className = "menuparent"; this.style.zIndex=100; }
		}
	});
};

window.addEvent('domready',function(){
	if(Browser.Engine.trident4){
		fixPNG();
		fixHover();
	};
});

// ====================================================================================
function $E(selector, filter){
	return ($(filter) || document).getElement(selector);
};
String.extend({
	trim: function(){
		return this.replace(/^\s*|\s*$/g, "");
	}
});