﻿
//
// Modification Log:
// Date 	Initial Description
// 26 May 2008	TKO	Created by Tanny O'Haley

/*global addEvent, escape, unescape */

var domReadyEvent = {
    name: "domReadyEvent",
    // Array of DOMContentLoaded event handlers.
    events: {},
    domReadyID: 1,
    bDone: false,
    DOMContentLoadedCustom: null,

    // Function that adds DOMContentLoaded listeners to the array.
    add: function(handler) {
        // Assign each event handler a unique ID. If the handler has an ID, it
        // has already been added to the events object or been run.
        if (!handler.$$domReadyID) {
            handler.$$domReadyID = this.domReadyID++;

            // If the DOMContentLoaded event has happened, run the function.
            if (this.bDone) {
                handler();
            }

            // store the event handler in the hash table
            this.events[handler.$$domReadyID] = handler;
        }
    },

    remove: function(handler) {
        // Delete the event handler from the hash table
        if (handler.$$domReadyID) {
            delete this.events[handler.$$domReadyID];
        }
    },

    // Function to process the DOMContentLoaded events array.
    run: function() {
        // quit if this function has already been called
        if (this.bDone) {
            return;
        }

        // Flag this function so we don't do the same thing twice
        this.bDone = true;

        // iterates through array of registered functions
        for (var i in this.events) {
            this.events[i]();
            
        }
    },

    schedule: function() {
        // Quit if the init function has already been called
        if (this.bDone) {
            return;
        }

        // First, check for Safari or KHTML.
        if (/KHTML|WebKit/i.test(navigator.userAgent)) {
            if (/loaded|complete/.test(document.readyState)) {
                this.run();
            } else {
                // Not ready yet, wait a little more.
                setTimeout(this.name + ".schedule()", 100);
            }
        } else if (document.getElementById("__ie_onload")) {
            // Second, check for IE.
            return true;
        }

        // Check for custom developer provided function.
        if (typeof this.DOMContentLoadedCustom === "function") {
            //if DOM methods are supported, and the body element exists
            //(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1] 
            //in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
            if (typeof document.getElementsByTagName !== 'undefined' && (document.getElementsByTagName('body')[0] !== null || document.body !== null)) {
                // Call custom function.
                if (this.DOMContentLoadedCustom()) {
                    this.run();
                } else {
                    // Not ready yet, wait a little more.
                    setTimeout(this.name + ".schedule()", 250);
                }
            }
        }

        return true;
    },

    init: function() {
        // If addEventListener supports the DOMContentLoaded event.
        if (document.addEventListener) {
            document.addEventListener("DOMContentLoaded", function() { domReadyEvent.run(); }, false);
        }

        // Schedule to run the init function.
        setTimeout("domReadyEvent.schedule()", 100);

        function run() {
            domReadyEvent.run();
        }

        // Just in case window.onload happens first, add it to onload using an available method.
        if (typeof addEvent !== "undefined") {
            addEvent(window, "load", run);
        } else if (document.addEventListener) {
            document.addEventListener("load", run, false);
        } else if (typeof window.onload === "function") {
            var oldonload = window.onload;
            window.onload = function() {
                domReadyEvent.run();
                oldonload();
            };
        } else {
            window.onload = run;
        }


        /* for Internet Explorer */
        /*@cc_on
        @if (@_win32 || @_win64)
        document.write("<script id=__ie_onload defer src=\"//:\"><\/script>");
        var script = document.getElementById("__ie_onload");
        script.onreadystatechange = function() {
            if (this.readyState == "complete") {
                domReadyEvent.run(); // call the onload handler
            }
        };
        @end
        @*/
    }
};

var domReady = function(handler) { domReadyEvent.add(handler); };
domReadyEvent.init();


window.$txlFlvPlayers = new Array();
window.$txlFlvPlayerCurrent = null;

var TxlFlvPlayer = function (oDiv, cTopUrl) {
    this.TopUrl = cTopUrl,
    this.tAutoplay = '',
   this.tUrl = '',
   this.tThumbnail = '',
   this.tTitle = '',
   this.tclrTitle = '',
   this.tclrBottom = '',
   this.tclrTop = '',
   this.tclrIcon = '',
   this.tclrBigIcon = '',
   this.tWidth = '',
   this.tHeight = '',
   this.oHtmlWrapper = null,
   this.IsInstantiated = false,

    this.init = function () {

        // get object
        switch (typeof (oDiv)) {
            case 'object':
                {
                    this.oHtmlWrapper = oDiv;
                    break;
                }
            case 'string':
                {
                    this.oHtmlWrapper = document.getElementById(oDiv);
                    break;
                }
        }

        if (!this.oHtmlWrapper) {
            alert('Wrapper is not found');
        }

        // get attributes
        if (this.oHtmlWrapper.getAttribute('tAutoplay')) {
            this.tAutoplay = (this.oHtmlWrapper.getAttribute('tAutoplay') == 1);
        } else {
            this.tAutoplay = false;
        }
        this.tUrl = this.oHtmlWrapper.getAttribute('tUrl');
        this.tThumbnail = this.oHtmlWrapper.getAttribute('tThumbnail');
        this.tTitle = this.oHtmlWrapper.getAttribute('tTitle');
        this.tclrTitle = this.toHexFormat(this.oHtmlWrapper.getAttribute('tclrTitle'));
        this.tclrBottom = this.toHexFormat(this.oHtmlWrapper.getAttribute('tclrBottom'));
        this.tclrTop = this.toHexFormat(this.oHtmlWrapper.getAttribute('tclrTop'));
        this.tclrIcon = this.toHexFormat(this.oHtmlWrapper.getAttribute('tclrIcon'));
        this.tclrBigIcon = this.toHexFormat(this.oHtmlWrapper.getAttribute('tclrBigIcon'));
        this.tWidth = this.oHtmlWrapper.getAttribute('tWidth');
        this.tHeight = this.oHtmlWrapper.getAttribute('tHeight');

        //TxlFlvPlayerManager.prototype.addEventListener(this.oHtmlWrapper, 'click', function() { alert('tut'); });
        this.draw();

    },

    this.toHexFormat = function (cVal) {
        if (cVal != null && cVal.length > 0) {
            return '0x' + cVal.replace('#', '');
        }
        return '';
    },
    this.Play = function () {
        alert('Not implemented');
    }
    this.Stop = function () {
        var oMovie = getFlashMovie('vms' + this.oHtmlWrapper.id);
        oMovie.flvStop();
    }

    this.draw = function () {

        if (this.GetFlashMajorVersion() < 10) {
            this.showNotSupportedMessage();
            return;
        }
        var cVars = '';
        cVars += (this.oHtmlWrapper.id) ? '&htmlName=' + this.oHtmlWrapper.id : '';
        cVars += (this.tAutoplay) ? '&Autoplay=true' : '';
        cVars += (this.tUrl) ? '&Url=' + (this.TopUrl + this.tUrl) : '';
        cVars += (this.tThumbnail) ? '&Thumbnail=' + (this.TopUrl + this.tThumbnail) : '';
        cVars += (this.tTitle) ? '&Title=' + this.tTitle : '';
        cVars += (this.tclrTitle) ? '&clrTitle=' + this.tclrTitle : '';
        cVars += (this.tclrBottom) ? '&clrBottom=' + this.tclrBottom : '';
        cVars += (this.tclrTop) ? '&clrTop=' + this.tclrTop : '';
        cVars += (this.tclrIcon) ? '&clrIcon=' + this.tclrIcon : '';
        cVars += (this.tclrBigIcon) ? '&clrBigIcon=' + this.tclrBigIcon : '';
        cVars += (this.tWidth) ? '&Width=' + this.tWidth : '';
        cVars += (this.tHeight) ? '&Height=' + this.tHeight : '';

        var player = this.TopUrl + 'TxlFlvPlayer.swf';
        var cRes = '';

        //cRes += ' <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="' + this.tWidth + '" HEIGHT="' + this.tHeight + '" id="vms' + this.oHtmlWrapper.id + '">'
        //+ '<PARAM NAME="movie" VALUE="'+player+'" /><PARAM NAME="quality" VALUE="high" /><PARAM NAME="allowScriptAccess" VALUE="always" /><PARAM NAME="allowFullScreen" VALUE="true" /><PARAM NAME="wmode" VALUE="transparent" /><PARAM NAME="FlashVars" VALUE="' + cVars + '"/>';
        // + '<EMBED href="TxlFlvPlayer.swf" quality=high bgcolor=#FFFFFF WIDTH="' + this.tWidth + '" HEIGHT="' + this.tHeight + '" NAME="vms' + this.oHtmlWrapper.id + '"'
        //+ ' ALIGN="" TYPE="application/x-shockwave-flash"  FlashVars="' + cVars + '" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT> ';

        cRes += '<embed src="'+player+'" quality="high" width="' + this.tWidth + '" height="' + this.tHeight + '" id="1vms' + this.oHtmlWrapper.id + '" name="vms' + this.oHtmlWrapper.id + '"' +
            'allowScriptAccess="sameDomain" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" FlashVars="' + cVars + '"' +
            'type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />';
        this.oHtmlWrapper.innerHTML = cRes;

        this.IsInstantiated = true;
    },

    this.showNotSupportedMessage = function () {
        this.oHtmlWrapper.innerHTML = '<a href="http://get.adobe.com/flashplayer/"><img src="http://www.adobe.com/misc/images/160x41_get_flashplayer.gif" border="0"/></a>';
    },
    this.GetFlashMajorVersion = function () {
        // Required 10
        try {
            try {
                // avoid fp6 minor version lookup issues
                // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
                var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
                try { axo.AllowScriptAccess = 'always'; }
                catch (e) { return 6; }
            } catch (e) { }
            return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1].split(',')[0];
        } catch (e) {
            try {
                if (navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) {
                    return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1].split(',')[0];
                }
            } catch (e) { }
        }
        return 10;
    }

    return this.init();
}

var TxlFlvPlayerManager = function() {
    this.TopUrl = '',
    this.init = function() {

        // get top url
        //this.TopUrl = window.location.protocol + '//' + window.location.host + '/';
        this.TopUrl = '/';
    
        //if (window.location.hostname=='localhost') {
        //    this.TopUrl += window.location.pathname.split('/')[1] + '/';
        //}


        
        // fill array
        var aDivs = document.getElementsByTagName("div");
        for (var i = 0; i < aDivs.length; i++) {
            if (aDivs[i].className.toLowerCase() == 'txlflvplayer') {
                aDivs[i].id = 'TxlFlvPlayer' + i;
                window.$txlFlvPlayers.push(new TxlFlvPlayer(aDivs[i], this.TopUrl));
            }
        }

    }
    return this.init();
}
TxlFlvPlayerManager.prototype.addEventListener = function(Obj, cEvent, oHandler) {
    if (window.addEventListener) {
        Obj.addEventListener(cEvent, oHandler, false);
    } else {
        Obj.setCapture();
        Obj.attachEvent("on" + cEvent, oHandler);
    }
}
TxlFlvPlayerManager.prototype.StopMovie = function(cID) {
    for (var i = 0; i < window.$txlFlvPlayers.length; i++) {
        if (window.$txlFlvPlayers[i].oHtmlWrapper.id != cID) {
            window.$txlFlvPlayers[i].Stop();
        }
    }

}

/*  FLASH COMMUNICATION */
function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}
function flvPlay(cID) {
    TxlFlvPlayerManager.prototype.StopMovie(cID);
}

domReady(function() { var aa = new TxlFlvPlayerManager(); });


