/*jslint laxbreak: true, eqeqeq: true, browser: true, undef: true */
/*extern $, $$, Class, Options, Element, Request, Accordion, Browser, Swiff, Cookie */
/*extern Uplayer, trace */
/*global DR, ROOT, API_URL */

/**
 * @author peter
 */
if (!DR) { DR = {}; }
if (!DR.Ung ) { DR.Ung = {}; }

(function () {

function getHeaderHeight () {
    return $$('#globalWrapper,.headerWrapper,.navigationWrapper').getHeight().sum();
}
	
DR.Ung.PiratFlash = new Class({
    Implements: [Options],

    options: {
        container: 'container_swf',
        playlist: 'playlist',
        flash_id: 'pirat_swf'
    },

    swf: {},

	isResizing: 0,
	
	initialize : function(options){

        this.setOptions(options);
        this.attachPlaylist();

        this.mode = $(this.options.container).get('class');

        if (Browser.Engine.gecko || Browser.Engine.webkit) {
            this.getHTMLFromElement = function (el) {
                return '<div id="container_swf" class="' + el.get('class') + '">' + el.get('html') + '</div>';
            };
        }

        this.minHeight = 850;
        if (this.mode === 'page_shows') {
            this.minHeight = Math.ceil($$('li[class^=show]').length / 3) * 200 + 200;
        } else if (this.mode === 'page_start') {
            this.minHeight = 1000 + Math.ceil($$('#tagCloud li').get('text').join('').length / 100) + 40;
        } else if (this.mode === 'page_clip') {
            this.minHeight = 1100;
        } else if (this.mode === 'page_category' || this.mode === 'page_search') {
            this.minHeight = 1300;
        } else if (this.mode === 'page_show') {
            this.minHeight = 1000;
        }
		
	    this.embedFlash();
	    
	    window.DEBUG_PIRAT = this;

	},

    // Method: attachPlaylist
    //      Attaches eventhandler to the playlist element, triggering display of flash playlist
    attachPlaylist : function () {
        $(this.options.playlist)
            .addEvent('mouseover', function () {
                //$$('div.page').setStyle('z-index', over ? -1 : 1);
                Swiff.remote(this.swf.object, 'showPlaylist');
                //trace('mouseover playlist');
            }.bind(this));
            
    },
    
	resizeElements : function(){

        var headerHeight = getHeaderHeight();

        var styles = {
			width: '100%',
			height: [$(document).getScrollHeight() - headerHeight, this.minHeight].max(),
			left: 0,
			top: headerHeight
        };

        $(this.options.container).setStyles(styles);

	},

	getBottom: function(){
		var headerHeight  = getHeaderHeight();
		
		var b = $(window).getSize().y + window.getScroll().y - headerHeight - 7;
		return b;
	},
	
	done:function(){
		this.resizeElements();
		$(window).addEvent("resize", function(){
			if (this.isResizing < 1) {
				this.isResizing++; //recursion flag
				this.resizeElements();
				this.isResizing--;
			}
		}.bind(this));
	},
	
	flashOver:function(over){
        $$('div.page').setStyle('z-index', over ? -1 : 1);
	},
	
	showPlaylist:function(show){
		$$("#playlist").setStyle("visibility", show ? "visible" : "hidden");
	},

	
	embedFlash: function(){

        var container = $(this.options.container);

		if (document.location.href.indexOf("?noflash") === -1) {

			var content = this.getHTMLFromElement(container);

			this.swf = new Swiff(ROOT + 'flash/pirat.swf', {
				id: "pirat_swf",
				container: container,
				width: "100%",
				height: "100%",
				params: {
					quality: "autolow",
					menu: "false",
					wmode: "transparent",
					allowScriptAccess: "always",
					scale: "noscale"
				},
				vars: {
					mode: this.mode,
					content: content,
					login: false,
                    apiurl: API_URL
				}
				,callBacks: {
					done:this.done.bind(this),
					showPlaylist:this.showPlaylist.bind(this),
					flashOver:this.flashOver.bind(this),
					getBottom:this.getBottom.bind(this),
					login:DR.Ung.LoginForm.show,
					logout:DR.Ung.Logout
				}
			});
		}
		
		this.resizeElements();
	},

    // Method: getHTMLFromElement
    //      return the html represented by an element. This is only usable for IE and Opera, for other browsers use el.get('html')
    getHTMLFromElement: function (el, tag) {

        var children = el.getChildren();

        if (tag) {
            var html = '<' + el.get('tag')
                + (el.get('id') ? ' id="' + el.get('id') + '"' : "")
                + (el.get('class') ? ' class="' + el.get('class') + '"' : "")
				+ (el.get('href') ? ' href="' + el.get('href') + '"' : "")
				+ (el.get('title') ? ' title="' + el.get('title') + '"' : "")
				+ (el.get('rel') ? ' rel="' + el.get('rel') + '"' : "")
                + '>';
        } else {
            html = '<div id="container_swf" class="' + el.get('class') + '">';
        }

        if (!children.length) {
            html += el.get('text');
        } else {
            children.forEach( function (x) { html += this.getHTMLFromElement(x, true); }.bind(this) );
        }

        if (tag) {
            html += '</' + el.get('tag') + ' >';
        } else {
            html += '</div>';
        }

        return html;
    }
});

})();


