﻿/** 
*
* @projectDescription 	Global Application File
*
* @author	Tom Newton tnewton@rosettamarketing.com
* @version	1.1b 
* 
* @returns application object
* @constructor
* @namespace
=====================================================*/
var ROSETTA = ROSETTA ? ROSETTA : new Object();
ROSETTA.application = function(){
    /* Configuration object 
    =================================================================*/
    var config = new Object();
    this.config = config; 
    
    /* API Loader Object
    =================================================================*/
    var ajaxlibs = new Object();
    ajaxlibs.baseURL = "http://scripts.rosettamarketing.com/js/";
    ajaxlibs.loadObjects = new Array(); //array of modules objects added with each successful .load statement
    ajaxlibs.onLoadCallback = {}; //empty onload callback function placeholder

    ajaxlibs.modules = new Array(); //external modules array for ajax loader
    ajaxlibs.modules["yuiloader"] = {name: "yuiloader", type: "script", path: "/includes/js/libs/yui/yuiloader/yuiloader-beta-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["validation"] = {name: "selector", type: "script", path: "/includes/js/libs/yui/selector/selector-min.js"};

    ajaxlibs.modules.modals.files = new Array(); //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.load = 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.load;
    this.addModule = ajaxlibs.addModule;
    
	/* Utility object 
	=================================================================*/
	var util = new Object();
	//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;
        }
    }
    
    
	util.addListener = function(o,e,c){
	    //[o]bject target [e]vent [c]allback
	    if (o.addEventListener){
          o.addEventListener(e, c, false); 
        } else if (o.attachEvent){
          o.attachEvent(e, c);
        } else {
          o.e = c();
        }
	}
	util.removeListener = function(o,e,c){
	    //[o]bject [e]vent [c]allback
	    if (o.removeEventListener){
          o.removeEventListener(e, c, false); 
        } else if (o.detachEvent){
          o.detachEvent(e, c);
        } else {
          o.e = c();
        }
	}
	// 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);
       }
    }
    /* Get Base URL */
    util.getBaseURL = function() {
        var a = location.href.split("/");
        var b = location.href.slice(0,location.href.indexOf(a[3]));
        return b;
    }
    /* Get IE version */
    util.getIEVersion = function() {
        var ua = window.navigator.userAgent
        var msie = ua.indexOf ( "MSIE " )

        if (msie > 0){      // If Internet Explorer, return version number
            return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
        }else{                 // If another browser, return 0
            return 0
        }
    }
    //trimSpaces trims leading and trailing spaces only
    util.trimSpaces = function(strText) { 
        // this will get rid of leading spaces 
        while (strText.substring(0,1) == ' ') 
            strText = strText.substring(1, strText.length);

        // this will get rid of trailing spaces 
        while (strText.substring(strText.length-1,strText.length) == ' ')
            strText = strText.substring(0, strText.length-1);

       return strText;
    }
    //stripSpaces trims ALL spaces
    util.stripSpaces = function(tmpString) {
        var returnVal = (tmpString.replace(/^\s+/,'')).replace(/\W+$/,'');
        return returnVal;
    } 
    util.anchor = function (fx, fy) {
        var x = fx;
        var y = fy;
        this.setAnchor = function(nx, ny){
            x = nx;
            y = ny;
        }
        this.getPosition = function(rx, ry){
            var returnObj = new Object();
            returnObj.x = rx - x;
            returnObj.y = ry - y;
            return returnObj;
        }
    } 
    
     /* Expose Util Object Properties/Methods */
    this.util = new Object();
    this.util.addOnloadEvent = util.addOnloadEvent; //self explanatory
    this.util.loadFile = util.loadFile; //loads script or css file into document w/ optional callback
    this.util.getBaseURL = util.getBaseURL; //returns root url of site
    this.util.getIEVersion = util.getIEVersion;
    this.util.trimSpaces = util.trimSpaces;
    this.util.stripSpaces = util.stripSpaces;
    this.util.anchor = util.anchor;
}

// *************************************************************
//NEW UTILS obj will replace the internals above in next release
// *************************************************************
ROSETTA.utils = function(){}
//trimSpaces trims leading and trailing spaces only
ROSETTA.utils.prototype.trimSpaces = function(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}
//stripSpaces trims ALL spaces
ROSETTA.utils.prototype.stripSpaces = function(tmpString) {
    var returnVal = (tmpString.replace(/^\s+/,'')).replace(/\W+$/,'');
    return returnVal;
} 