YAHOO.namespace('gen.video');

var Dom		= YAHOO.util.Dom,
    Event	= YAHOO.util.Event;

YAHOO.gen.video.player = 
{
	container: 'player-container',

	root: 'video-gallery',
	
	reloadPage: true,

	sfwObject: {},

	currentThumbnail: {},
	
	beforeInitEvent: new YAHOO.util.CustomEvent('onBeforeInit', this),
	
	afterInitEvent: new YAHOO.util.CustomEvent('onAfterInit', this),


	/**
	 * Get items within child node img
	 */
	getItems: function(el)
	{
    return true; // crutch :) 
    var oFirstChild = el.firstChild;
		if (oFirstChild) {
			if (1 != oFirstChild.nodeType) {
				var oNextSibling = oFirstChild.nextSibling;
				if (oNextSibling && 1 == oNextSibling.nodeType) {
					oFirstChild = oNextSibling;
				}
			}
			if ('IMG' == oFirstChild.tagName.toUpperCase()) {
				return true;
			}
		}
		return false;
	},
	
	getItemByUrl: function(url) {
        var len = this.items.length;
        for (var i = 0; i < len; i++) {
            if (-1 != this.items[i].href.lastIndexOf(url)) {
                return this.items[i];
            }
        }
        return false;
	},

    show: function(e)
	{
    var target;
		if ('object' === typeof e) {
			target = Event.getTarget(e);
			if ('A' != target.tagName.toUpperCase()) {
                target = target.parentNode;
			}
			if (!this.reloadPage) {
			    Event.preventDefault(e);
			}
		} else if ('string' === typeof e) {
            target = this.getItemByUrl(e);
		} else {
            target = this.items[0];
		}
		if (target) {
    		var link = target.href;
    		var matches = link.match(/(\/[^\/]+\/)video\-([^\/]+)\.html/);
			//alert(matches);
    		if (matches) {
    			Dom.removeClass(this.currentThumbnail, 'sel');
    			this.currentThumbnail = target.parentNode;
    			Dom.addClass(this.currentThumbnail, 'sel');
    			
    			this.sfwObject.addVariable('path', matches[1]);
    			this.sfwObject.addVariable('key', 'video' + matches[2]);
    			this.sfwObject.write(this.container);
    		}
		}
    },

	init: function()
    {
        this.beforeInitEvent.fire();

        this.sfwObject = new SWFObject('/customfiles/Flash/player.swf', 'videoplayer', '580', '452', '8', '#ffffff');
		this.sfwObject.addParam('quality', 'high');
		this.sfwObject.addParam('menu', 'false');
		this.sfwObject.addParam('wmode', 'transparent');
		this.sfwObject.write(this.container);

        this.items = Dom.getElementsBy(this.getItems, 'a', this.root);
        Event.addListener(this.items, 'click', this.show, this, true);
        
        this.afterInitEvent.fire();
    }
}

Event.onAvailable(YAHOO.gen.video.player.container, YAHOO.gen.video.player.init, YAHOO.gen.video.player, true);