﻿/**
 * @author pop webdev [cn]
 * @version: 0.2.
 * @classDescription: Detail Video Updater
 * @dependencies: Prototype v1.6.1.
 */
// start DetailVideoUpdater
var DetailVideoUpdater = Class.create ({
	initialize: function(flash,links,options,flashOptions)
	{
		this.flash = flash;
		this.flashID = this.flash.identify();
		this.links = links;
		this.length = this.links.size();
		this.container = this.flash.up();
		this.options = Object.extend({
			initialIndex: 0,
			activeClassName: 'active'
		}, options || {});
		this.flashOptions = Object.extend({
			stageWidth: '480',
			stageHeight: '390',
			videoWidth: '480',
			videoHeight: '360',
			bgcolor: '#ffffff',
			version: '9.0.0',
			swfPath: 'videoplayer.swf'
		}, flashOptions || {});
		if (this.options.initialIndex >= this.length)
		{
			this.options.initialIndex = 0;
		}
		this.currentItem = this.options.initialIndex;

		var boundClickHandler = this.__Click.bindAsEventListener(this);
		this.links.invoke('observe', 'click', boundClickHandler);

		// set initial position
		this.SwapVideo(this.currentItem);
		
	},
	__Click: function(e)
	{
		e.stop();
		var element = e.findElement('a');
		for (var i=0; i<this.length; i++)
		{
			if (this.links[i] == element)
			{
				this.SwapVideo(i);
				// broadcast custom event
				this.container.fire('updater:triggered', {index: i});
				break;
			}
		}
	},
	SwapVideo: function(index)
	{
		this.currentItem = index;
		//update detail img
		var lnkRel = this.links[index].rel.split(';');
		var videoType = lnkRel[0];
		var videoID = lnkRel[1];
		this.FlashInit(videoType,videoID);

		//highlight link
        var activeClassName = this.options.activeClassName;
        this.links.each(function(el, index) {
			el.up().removeClassName(activeClassName);
		}); // un-highlight all links parents
		this.links[index].up().addClassName(activeClassName); // highlight specific link parent
	},
    FlashInit: function(videoType,videoID)
    {
		if (videoType == 'youtube')
		{
			this.PlayYouTube(videoID);
		}
		else if (videoType == 'vimeo')
		{
			this.PlayVimeo(videoID);
		}
		else {
			this.ClosePopover();
		}
    },
    PlayYouTube: function(videoID)
    {
	    var flashvars = {
			autoPlayBool: 'false',
			loopBool: 'false',
			playerPath: 'http://www.youtube.com/apiplayer?version=3',
			videoWidth: this.flashOptions.videoWidth,
			videoHeight: this.flashOptions.videoHeight,
			videoID: videoID
		};
	    var params = {
			allowFullScreen: 'true',
			allowScriptAccess: 'always',
			menu: 'true',
			autoplay: 'false',
			loop: 'false',
			quality: 'high',
	    	salign: 't',
	    	scale: 'exactfit',
	    	wmode: 'opaque'
	    };
		var attributes = {
			bgcolor: this.flashOptions.bgcolor,
	    	id: this.flashID
		};
		swfobject.embedSWF(this.flashOptions.swfPath, this.flashID, this.flashOptions.stageWidth, this.flashOptions.stageHeight, this.flashOptions.version, false, flashvars, params, attributes);
    },
    PlayVimeo: function(videoID)
    {
	    var flashvars = {
			clip_id: videoID,
			autoplay: 0,
			fullscreen: 1,
			js_api: 1,
			show_byline: 0,
			show_portrait: 0,
			show_title: 0,
			js_onLoad: 'vimeo_player_loaded',
			js_swf_id: 'moogaloop'
		};
	    var params = {
			allowFullScreen: 'true',
			allowScriptAccess: 'always',
			autoplay: 'false',
	    	wmode: 'opaque'
	    };
		var attributes = {
			bgcolor: this.flashOptions.bgcolor,
	    	id: this.flashID
		};
		swfobject.embedSWF('http://vimeo.com/moogaloop.swf', this.flashID, this.flashOptions.stageWidth, this.flashOptions.stageHeight, this.flashOptions.version, false, flashvars, params, attributes);
    }
});
// end DetailVideoUpdater

