/*
 * 1. load all js libs
 * 2. load player template with links to client css, js, html
 */

//namespace Axisto
if(typeof(axisto) == 'undefined')
    axisto = {};

//namespace
axisto = (function() {
    // private fields
	var appPath = "";
	var isInited = false;
	var params = null;
	var playerPath = "";
	var version = 22;
    
	//get window protocol
	var protocol = 'http:';
    try{
    	if(window.location.protocol == 'https:'){
    		protocol = 'https:';
    	}
    }
    catch(ex)
	{}
	
    // private members
    function getScriptPath(script) {
		var j = -1;
		var path = null;
		var url = null;
		var scripts = document.getElementsByTagName("script");
        for (var i = 0; scripts.length; i++) {
			j = scripts[i].src.indexOf(script) || -1;
			if(j > -1){
				path = scripts[i].src.substring(0, j);
				url = scripts[i].src;
				break;
			}
        }
		return {path: path, url: url};
    }
	
	function getURLParams(url)
	{
		if(url == null) return null;
		var arr = [];
		
		if (url.indexOf("?") > -1)
		{
			var params = url.split("?")[1];
			var param = params.split("&");
		
			for (var i = 0; i < param.length; ++i)
			{
				var name = param[i].split("=")[0];
				var value = param[i].split("=")[1];
				arr[name] = value;
			}
		}
		return arr;
	}

    function isScriptLoaded(script) {
        var isLoaded = false;
        if (script == null || script.types == null)
            return isLoaded;

        for (var i = 0; i < script.types.length; i++) {
            try {
                if (eval(script.types[i]) == 'undefined') {
                    isLoaded = false;
                    break;
                }
            }
            catch (ex) {
            }
        }
        return isLoaded;
    }

    function parseVersion(version) {
        var v = { major: 0, minor: 0, release: 0, value: 0 };
        if (typeof (version) == 'string' && version.length > 0) {
            var r = version.split('.');
            if (version.length > 2) {
                v.major = parseInt(r[0]);
                v.minor = parseInt(r[1]);
                v.release = parseInt(r[2]);
                v.value = parseInt(r[0]) * 100000 + parseInt(r[1]) * 1000 + parseInt(r[2])
            }

        }
        return v;
    }

    // Everything returned in the object literal is public, but can access the
    // members in the closure created above.
    return {
        //public fields
        // public members
        init: function(script) {
            //get app path
            var o = getScriptPath(script);
			appPath = o.path;
			
			//get params
			params = getURLParams(o.url);
			
			if(params["player"] == null){
				params["player"] = "./";
			}
			
			isInited = true;
        },
        isEmbedded: function(){
            return (params["player"] != "./");
        },
		getAppPath: function(){
			return appPath;
		},
		getConfig: function(){
			return params;
		},
		getProtocol: function(){
			return protocol;
		},
        load: function(file, path, browser, version) {
            if(file == null) return;

			//add path
			var url = (path == null)?appPath+file:path+file;
					
			//add version to avoid caching
			if(url.indexOf("?") < 0){
				url += "?v="+version;
			}
			
			if(file.indexOf(".js") > -1){
				//brute force, safari can't do dynamic element creation
            	document.write(unescape('%3Cscript src="' + url + '" type="text/javascript"%3E%3C/script%3E'));
			}
			else if(file.indexOf(".css") > -1){
			    // IE Specific CSS
                if (browser == 'IE' && version == 'all') {
			        document.write(unescape('%3C%21--%5Bif%20IE%5D%3E%3Clink rel="stylesheet" href="' + url + '" media="screen" type="text/css"%3E%3C/link%3E%3C%21%5Bendif%5D--%3E'));
			    } else if (browser == 'IE' && version == '6') { // IE Version Specific CSS
			        document.write(unescape('%3C%21--%5Bif%20IE%206%5D%3E%3Clink rel="stylesheet" href="' + url + '" media="screen" type="text/css"%3E%3C/link%3E%3C%21%5Bendif%5D--%3E'));
			    } else if (browser == 'IE' && version == '7') {
			        document.write(unescape('%3C%21--%5Bif%20IE%207%5D%3E%3Clink rel="stylesheet" href="' + url + '" media="screen" type="text/css"%3E%3C/link%3E%3C%21%5Bendif%5D--%3E'));
			    } else if (browser == 'IE' && version == '8') {
			        document.write(unescape('%3C%21--%5Bif%20IE%208%5D%3E%3Clink rel="stylesheet" href="' + url + '" media="screen" type="text/css"%3E%3C/link%3E%3C%21%5Bendif%5D--%3E'));
			    } else {
			        document.write(unescape('%3Clink rel="stylesheet" href="' + url + '" media="screen" type="text/css"%3E%3C/link%3E'));
			    }
			}
        }
    };
})();

//init
axisto.init("axp/axp.js");

//get player config
var playerPath = axisto.getConfig().player;

//load loader
axisto.load("stylesheets/player.scaffold/loader.axisto.css", playerPath);
document.write(unescape('%3Cdiv id="axp-player-view-loader" class="loading"%3E%3Cdiv class="axp-wrapper"%3E%3Cdiv class="logo"%3E%3C/div%3E%3Cdiv class="progress"%3E%3C/div%3E%3C/div%3E%3C/div%3E'));
//load player

if(axisto.isEmbedded()){
	axisto.load("data/player.view.js", playerPath);
	axisto.load("data/player.js?v="+new Date().getTime(), playerPath);
}
else{
	axisto.load("player.js?v="+new Date().getTime(), "./data/");
}

//load libs
//jquery, must load first before other libs to avoid conflict. noConflict is set in jquery.ext.js to $jq
axisto.load("jquery/jquery-1.3.2.js");
axisto.load("jquery/ui/jquery-ui-1.7.2.min.js");

// axisto.load("jquery/ui.core.js");
// axisto.load("jquery/ui.resizable.js");
// axisto.load("jquery/ui.accordion.js");
// axisto.load("jquery/ui.dialog.js");
// axisto.load("jquery/ui.slider.js");
// axisto.load("jquery/ui.tabs.js");
// axisto.load("jquery/ui.tabs.ext.js");

// Custom Jquery UI
axisto.load("axp/jquery.player.js");
axisto.load("axp/jquery.player.controls.js");
axisto.load("axp/jquery.player.chapters.js");
axisto.load("axp/jquery.player.slides.js");
axisto.load("axp/jquery.ext.js");

// JQuery Plugins
axisto.load("jquery/plugins/jquery.validate.js");
axisto.load("jquery/plugins/jquery.scrollTo-min.js");
axisto.load("jquery/plugins/jquery.countdown.min.js");
axisto.load("jquery/plugins/jquery.enumerable.js");
axisto.load("jquery/plugins/jquery.tools.min.js"); // Includes only scrollable. Should convert to use all tools
axisto.load("jquery/plugins/jquery.thickbox-3.1.js");

// axisto.load("jquery/jquery.validate.js");
// axisto.load("jquery/jquery.scrollTo-min.js");
// axisto.load("jquery/jquery.countdown.min.js");
// axisto.load("jquery/jquery.enumerable.js");
// axisto.load("jquery/jquery.scrollable-1.0.2.js");

//prototype
axisto.load("prototype/prototype.js");
axisto.load("prototype/prototype-plugin-0.5.js");
axisto.load("prototype/event-dispatcher-1.js");

//scriptaculous
axisto.load("scriptaculous/scriptaculous.js");
axisto.load("scriptaculous/effects.js");
axisto.load("scriptaculous/controls.js");
axisto.load("scriptaculous/slider.js");

//other
axisto.load("swfobject/swfobject.js");
axisto.load("jw/wmv/silverlight.js");
axisto.load("jw/wmv/wmvplayer.js");

//axp
axisto.load("axp/axlib.js");
axisto.load("axp/positionbar.js");
axisto.load("axp/axplayer-wm.js");
axisto.load("axp/axplayer-wm-jw.js");
axisto.load("axp/axplayer-fmp-jw.js");
axisto.load("axp/axplayer-fmp.js");
axisto.load("axp/axplayer-rm.js");
axisto.load("axp/axsubtitles.js");
axisto.load("axp/axslides-3.js");
axisto.load("axp/tracker.js");
axisto.load("axp/mediatracker.js");
axisto.load("axp/axplayer-controller.js");
axisto.load("ga.js", ((axisto.getProtocol() == "https:") ? "https://ssl." : "http://www.") + "google-analytics.com/");

//init player
axisto.load("axp/axplayer.js");
