/// <reference path="../jquery/jquery-1.5.js" />
(function (window) {
    var lightplayer = {
        version: "0.1",
        register: function (path) {
            var parts = path.split('.');
            var root = window;
            for (var i = 0; i < parts.length; i++) {
                var part = parts[i];
                var ns = root[part];
                if (!ns) {
                    ns = root[part] = {};
                    ns.namespace = true;
                    ns.typeName = parts.slice(0, i + 1).join('.');
                }
                root = ns;
            }
        },
        defaultVideoWidth: 500,
        defaultVideoHeight: 300,
        defaultAudioWidth: 400,
        defaultAudioHeight: 30,
        options: {
            poster: "",
            surface: "Classic",
            theme: "default",
            sources: undefined,
            tracks: undefined,
            preload: 'none',
            loop: false,
            autoPlay: false,
            autoBuffer: false,
            //idleHideControls: false,
            currentMeidaIndex: 0,
            startVolume: .1,
            saveVolume: true,
            useSavedVolume: true,
            startMuted: false,
            saveMuted: true,
            useSavedMuted: true,
            enableAutosize: false,
            features: ['overlay', 'spinner', 'play', 'progress', 'time', 'track', 'volume', 'fullscreen']
        },
        platform: {
            isiPad: (window.navigator.userAgent.toLowerCase().match(/iPad/i) !== null),
            isiPhone: (window.navigator.userAgent.toLowerCase().match(/iPhone/i) !== null),
            isAndroid: (window.navigator.userAgent.toLowerCase().match(/Android/i) !== null),
            isIE: (window.navigator.appName.indexOf("Microsoft") != -1),
            isChrome: (window.navigator.userAgent.toLowerCase().match(/Chrome/gi) !== null)
        },
        create: function (selector, options) {
            options = jQuery.extend({}, lightplayer.options, options);
            var players = [];
            var $selector = jQuery(selector);
            for (var i = 0; i < $selector.length; i++) {
                var player = new lightplayer.MediaPlayer(jQuery.extend({ element: $selector[i] }, options));
                if (typeof player.ready == 'function') {
                    player.ready();
                }
                players.push(player);
            }
            return players;
        },
        utilies: {
            encodeUrl: function (url) {
                return encodeURIComponent(url); //.replace(/\?/gi,'%3F').replace(/=/gi,'%3D').replace(/&/gi,'%26');
            },
            escapeHTML: function (s) {
                return s.split('&').join('&amp;').split('<').join('&lt;').split('"').join('&quot;');
            },
            absolutizeUrl: function (url) {
                var el = document.createElement('div');
                el.innerHTML = '<a href="' + this.escapeHTML(url) + '">x</a>';
                return el.firstChild.href;
            },
            getScriptPath: function (scriptNames) {
                var 
			        i = 0,
			        j,
			        path = '',
			        name = '',
			        script,
			        scripts = document.getElementsByTagName('script');

                for (; i < scripts.length; i++) {
                    script = scripts[i].src;
                    for (j = 0; j < scriptNames.length; j++) {
                        name = scriptNames[j];
                        if (script.indexOf(name) > -1) {
                            path = script.substring(0, script.indexOf(name));
                            break;
                        }
                    }
                    if (path !== '') {
                        break;
                    }
                }
                return path;
            },
            secondsToTimeCode: function (seconds) {
                seconds = Math.round(seconds);
                var minutes = Math.floor(seconds / 60);
                minutes = (minutes >= 10) ? minutes : "0" + minutes;
                seconds = Math.floor(seconds % 60);
                seconds = (seconds >= 10) ? seconds : "0" + seconds;
                return minutes + ":" + seconds;
            },
            getExtension: function (file) {
                return file.substring(file.lastIndexOf(".") + 1);
            },
            absolutizeUrl: function (url) {
                var el = document.createElement('div');
                el.innerHTML = '<a href="' + this.escapeHTML(url) + '">x</a>';
                return el.firstChild.href;
            }
        }
    };
    window.lightplayer = lightplayer;
    lightplayer.options.flashElement = lightplayer.utilies.getScriptPath(['lightplayer.js', 'lightplayer.min.js']) + 'flash/FlashElement.swf';
    lightplayer.options.silverlightElement = lightplayer.utilies.getScriptPath(['lightplayer.js', 'lightplayer.min.js']) + 'silverlight/SilverlightElement.swf';
})(window);
