﻿/** 
*
* @projectDescription 	Global Application File helper class
*
* @author	Tom Newton Tom.Newton@rosetta.com
* @version	1.2b 
* 
* @returns application object
* @constructor
* @namespace
=====================================================*/
var ROSETTA = ROSETTA || {};
ROSETTA.application = function() {
    /* properties 
    =================================================================*/
    this.YUIPath = "includes/js/libs/yui/"; // path to YUI library files (build dir)

    /* API Loader Object
    =================================================================*/
    var ajaxlibs = {};
    ajaxlibs.baseURL = "/"; // relative or abs url to libraries
    ajaxlibs.loadObjects = []; //array of modules objects added with each successful .load statement
    ajaxlibs.onLoadCallback = {}; //empty onload callback function placeholder

    // libraries array
    ajaxlibs.modules = new Array(); //external modules array for ajax loader
    ajaxlibs.modules["yuiloader"] = { name: "yuiloader", type: "script", path: "/style%20library/scripts/libs/yui/yuiloader/yuiloader-min.js", forceInsert: false };
    ajaxlibs.modules["prototype"] = { name: "prototype", type: "script", path: ajaxlibs.baseURL + "prototype/1.6.0.2/prototype-1.6.0.2.js", forceInsert: true };
    ajaxlibs.modules["scriptaculous"] = { name: "scriptaculous", type: "script", path: ajaxlibs.baseURL + "scriptaculous/1.8.1/src/scriptaculous.js", forceInsert: true };
    ajaxlibs.modules["modals"] = { name: "modals", type: "script", path: "/includes/js/classes/modals.js" };
    ajaxlibs.modules["validation"] = { name: "validation", type: "script", path: "/includes/js/classes/validation.js" };
    ajaxlibs.modules.modals.files = []; //additional required files as files obj in parent obj


    // loads a specified module[m] into document head, fire onloadcallback when all modules are ready
    ajaxlibs.loadLibrary = function(m) {
        var obj = ajaxlibs.modules[m];
        if (obj) {
            if (obj.path) { ajaxlibs.loadObjects.push(obj); }
            if (obj.files) {
                for (x in obj.files) {
                    ajaxlibs.loadObjects.push(obj.files[x]);
                }
            }
        }
    }
    ajaxlibs.doLoading = function() {
        // load the first object
        util.loadFile(ajaxlibs.loadObjects[0].type, ajaxlibs.loadObjects[0].path, null, function() {
            ajaxlibs.loadNext();
        }, ajaxlibs.loadObjects[0].forceInsert);
    }
    ajaxlibs.loadNext = function() {
        ajaxlibs.loadObjects.splice(0, 1); // remove object previously loaded
        if (ajaxlibs.loadObjects.length == 0) {
            util.addOnloadEvent(ajaxlibs.onLoadCallback); //all modules loaded and ready, do onload
        } else {
            ajaxlibs.doLoading(); //load the next module
        }
    }
    ajaxlibs.setOnLoadCallback = function(f) {
        //set the private callback property
        ajaxlibs.onLoadCallback = f;
        if (ajaxlibs.loadObjects.length == 0) {
            util.addOnloadEvent(f);
        } else {
            ajaxlibs.doLoading(); // load all specified modules and fire onload after all are loaded
        }
    }
    ajaxlibs.addModule = function(obj) {
        ajaxlibs.modules[obj.name] = { name: obj.name, type: obj.type, path: obj.path };
    }

    // Expose ajaxlibs methods
    this.setOnLoadCallback = ajaxlibs.setOnLoadCallback;
    this.loadLibrary = ajaxlibs.loadLibrary;
    this.addModule = ajaxlibs.addModule;

    /* Utility object 
    =================================================================*/
    var util = {};
    //executes a passed function[i] when the dom is ready
    util.addOnloadEvent = function(i) {
        var u = navigator.userAgent.toLowerCase();
        var ie = /*@cc_on!@*/false;
        if (/webkit/.test(u)) {
            // safari
            timeout = setTimeout(function() {
                if (document.readyState == "loaded" || document.readyState == "complete") {
                    i();
                } else {
                    setTimeout(arguments.callee, 10);
                }
            }, 10);
        } else if ((/mozilla/.test(u) && !/(compatible)/.test(u)) || (/opera/.test(u))) {
            // opera/moz
            document.addEventListener("DOMContentLoaded", i, false);
        } else if (ie) {
            // IE
            (function() {
                var tempNode = document.createElement('document:ready');
                try {
                    tempNode.doScroll('left');
                    i();
                    tempNode = null;
                } catch (e) {
                    setTimeout(arguments.callee, 0);
                }
            })();
        } else {
            window.onload = i;
        }
    }
    // File Loader - loads file of [type] into document head 
    util.loadFile = function(type, url, id, callback, bruteForce) {
        if (bruteForce) { //brute force insert method
            switch (type) {
                case "script":
                    document.write('<script type="text/javascript" src="' + url + '"><\/script>');
                    if (callback) { callback() };
                    break;
                case "link":
                    document.write('<link rel="stylesheet" href="' + url + '" type="text/css"><\/script>');
                    if (callback) { callback() };
                    break;
            }
        } else { //DOM insert method
            var e = document.createElement(type);
            switch (type) {
                case "script":
                    e.src = url;
                    e.type = "text/javascript";
                    break;
                case "link":
                    e.rel = "stylesheet";
                    e.href = url;
                    e.type = "text/css";
                    break;
            }
            if (id) { e.id = id; }
            if (callback) { //execute callback if specified when content is ready
                if (typeof (e.onreadystatechange) == 'undefined') { // W3C
                    e.onload = function() { this.onload = null; callback(); };
                } else { // IE
                    e.onreadystatechange = function() { if (this.readyState != 'loaded' && this.readyState != 'complete') return; this.onreadystatechange = null; callback(); }; // Unset onreadystatechange, leaks mem in IE
                }
            }
            document.getElementsByTagName("head")[0].appendChild(e);
        }
    }

    /* Expose Util Object Properties/Methods */
    this.util = {};
    this.util.addOnloadEvent = util.addOnloadEvent; //self explanatory
    this.util.loadFile = util.loadFile;
}


/* EXTERNAL CALLS FOR EXPANDING VIDEO FLASH */
function expandFlash() {
    document.getElementById('flashcallout_container').className = "expanded";
    document.getElementById('flashcallout').className = "expanded";
}
function collapseFlash() {
    document.getElementById('flashcallout').className = "collapsed";
}
