// TODO: Make this Prototype.[Browser || OS].iPhone?
$IPHONE = navigator.userAgent.toLowerCase().indexOf('iphone') != -1;

document.observe('dom:loaded', function(){
	if(Prototype.Browser.IE){
		ieDomLoader();
	}else{
		domLoader();
	}
});

function ieDomLoader(){
	$('local_demo').style.display = 'none';
	$('vimeo_demo').style.display = 'block';
	$$('#navigation li a').each(function(link,index){
		link.href = link.getAttribute('rel');
		link.setAttribute('target', 'winenotes');
	});	
}

function domLoader(){
	$$('#navigation li a').each(function(link,index){
		var hashName = link.hash.replace('#', '');
		link.onclick = function(){
			loadVideoForTab(hashName);
			return false;
		}.bind(this);
	});
	// Let iPhones use the links.
	// Browsers should embed the videos.
	if(!$IPHONE){
		$$('#video_links li a').each(function(link, i){
			link.onclick = function(){
				playVideo(this.href);
				return false;
			};
		});
	}
}

// http://developer.apple.com/documentation/QuickTime/Conceptual/QTScripting_JavaScript/bQTScripting_JavaScri_Document/QuickTimeandJavaScri.html#//apple_ref/doc/uid/TP40001526-CH001-DontLinkElementID_17
document.observe('qt_pause', function(){
	stopVideo();
});

function loadVideoForTab(tabName){
	stopVideo();
	$$('#video_links li.current').each(function(li, index){
		li.removeClassName('current');
	});
	$('video_'+tabName).addClassName('current');
}

function playVideo(fileName){
	$('iphone_frame').addClassName('playing');
	$('iphone_frame').removeClassName('stopped');
	var embedHTML = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" \
	         codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=7,2,1,0" \
	         width="320" height="480" type="video/quicktime" id="movie1" \
	         style="behavior:url(#qt_event_source);"> \
	  <param name="src" value="'+fileName+'" />   \
	  <param name="postdomevents" value="true" /> \
		<param name="controller" value="false" />   \
	  <embed src="'+fileName+'" width="320" height="480" id="movie_embed1" name="movie1" postdomevents="true" controller="false"></embed> \
	</object>';	
	$('video_playback_frame').innerHTML = embedHTML;
}

function stopVideo(){
	$('video_playback_frame').innerHTML = "";
	$('iphone_frame').addClassName('stopped');
	$('iphone_frame').removeClassName('playing');
}