
/**
 * flow.playlist 0.10. Flowplayer playlist script
 * 
 * http://flowplayer.org/tools/flow-playlist.html
 *
 * Copyright (c) 2008 Tero Piirainen (tero@flowplayer.org)
 *
 * Released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * >> Basically you can do anything you want but leave this header as is <<
 *
 * Version: 0.10 - 05/19/2008
 */ 

(function($) {		
 //function isIE(){  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);}
 function isIE(){  return /msie/i.test(navigator.userAgent);}
 function isEarlyIE(){
	var ua = window.navigator.userAgent
	var msie = ua.indexOf ( "MSIE " )
	if ( msie > 0 )      // If Internet Explorer, return version number
		return parseInt (ua.substring (msie+5, ua.indexOf (".", msie ))) < 7
	else                 // If another browser, return 0
		return 0
}
	
	// plugin initialization
	$.fn.extend({
		playlist: function(params, config, opts) { 			
			return this.each(function() {
				new playlist($(this), params, config, opts);
			});
		}		
	});
					
			
	function playlist(root, params, config, playlistOpts) {

		var player = null;
		var lastImage = '';
		var currentDate;
		var lastPlay = 0;

		var opts = {
			playingClass: 'playing',
			pausedClass: 'paused',
			player: '#player',
			loop:false
		}
		
		opts = $.extend(opts, playlistOpts); 		
		
		config = config || {};
		if (typeof params == 'string') params = {src:params};
		 
		if (!$(opts.player).length) {
			alert("flow.playlist not configured properly\nnonexisting element " + opts.player);
			return;
		}
		
		var items = root.children();
		if (items.is(".__scrollable")) items = root.children().children();
		
		items.mouseover(function(event) {	
			var el = $(this);
			currentDate = new Date();
			// When the list auto scrolls don't display a picture
			if (currentDate.getTime() - lastPlay > 2000) {
				thePage=unescape(el.attr("href"));
				thePage=thePage.substr(0,thePage.lastIndexOf('.'));
				thePage=thePage+'.jpg';
				if (lastImage != thePage) {
					lastImage = thePage;
					if (!isEarlyIE()) {
						document.images['overlayimage'].src=thePage;
						document.getElementById('overlay').style.display="block";
					}
				}
			}
		});	
		
		items.mouseout(function(event) {	
			var el = $(this);
			var child = 0;
			var to_element = event.relatedTarget || event.toElement;
			while (to_element) {
				if (to_element == this) {
					child = 1;
					break;
				}
				to_element = to_element.parentNode;
			}
			if (!child) {
				lastImage = '';
				document.getElementById('overlay').style.display="none";
			}
		});	

		items.click(function(event) {	
			
			var el = $(this);

			document.getElementById('overlay').style.display="none";
			currentDate = new Date();
			lastPlay = currentDate.getTime();
			
			// toggle play pause action
			if (player && el.hasClass(opts.playingClass)) {
				if (player.getIsPaused()) player.DoPlay();
				else player.Pause();
				return false;
			}			
			
			// toggle playing state
			el.parent().find("." + opts.playingClass)
				.removeClass(opts.playingClass)
				.removeClass(opts.pausedClass)
			;
				
			el.addClass(opts.playingClass);
			
			config.videoFile = el.attr("href");
				// possible fallback
			if (!flashembed.isSupported([9,115])) {
				config.videoFile = config.videoFile.substring(0, config.videoFile.lastIndexOf(".") + 1) + 'flv';				
			}	
			
			if (player == null) {
				player = flashembed($(opts.player)[0], params, {config:config}); 
				if (!isIE()) {

				   document.getElementById('overlay').style.top="-120px";
				   document.getElementById("iframe").style.display="block";
				}
				
			} else {
				player.setConfig(config);
			} 
	
			// setup callback methods

			window.onClipDone = function() {
				el.removeClass(opts.playingClass).removeClass(opts.pausedClass);
				// move to next entry if it exists
				if (el.next().length) el.next().click();
				
				// else reset player				
				else {
					if (opts.loop) {
						items.eq(0).click();
						
					} else {
						player.DoStop();
						player.Seek(0);
					}
				}
					
				
				// omit player's default behaviour (since version 2.2)
				return false;
			}	  			
	
			window.onStreamNotFound = function() {
			   alert('Video file not found');
			}
	
			window.onPause = function() {
				if (el.hasClass(opts.playingClass)) el.addClass(opts.pausedClass);	
			}
	
			window.onResume = function() {
				el.removeClass(opts.pausedClass);	
			}	
			
			// disable default behaviour
			return false;			
			
		});	
		
		// clicking on the player clicks on the first playlist entry
		$(opts.player).click(function(event) {
			event.preventDefault();
			items.eq(0).click();		
		});			
			
	}
	

})(jQuery);


