﻿/// <reference path="../External/jquery-1.3.2-vsdoc.js" />

if (typeof (window.Agility) == "undefined") {
    window.Agility = {};
}

(function(Agility, $) {
    Agility.RegisterNamespace = function(space) {
        /// <summary>
        /// Register a javascript namespace.
        /// </summary>
        /// <param name="space" type="String">The namespace (dot-separated) to register.</param>
        /// <returns type="object">Object representing the created namespace.</returns>

        var spaces = space.split("."),
            root = window;

        for (var i = 0; i < spaces.length; i++) {
            if (typeof (root[spaces[i]]) == "undefined") {
                root = root[spaces[i]] = {};
            } else {
                root = root[spaces[i]];
            }
        }

        return root;
    }
    
    var _sessionLevelCacheKey = null;
    Agility.SessionLevelCacheKey = function() {
		/// <summary>
		/// gets a string that is based on the date and time that this page was loaded.  Use this for Client Templating with urls.
		/// </summary>
		
		if (_sessionLevelCacheKey == null) {
			_sessionLevelCacheKey = (new Date()).toString("yyyyMMddHHmmss");
		}
		return _sessionLevelCacheKey;
		
    };

    Agility.ResolveUrl = function(url) {
        /// <summary>
        /// Resolve "~/" to the application's base url. *Requires that global var "Edentity_BaseUrl" has been set.
        /// </summary>
        /// <param name="url" type="String">The URL whose path to resolve.</param>
        /// <returns type="string">String representing the resolved URL.</returns>

        var baseUrl = window.Agility_BaseUrl || "/";
        return url.replace(/^~\//, baseUrl);
    }
    
    
    var _unique = {};
    var _uniqueIndex = 0;
    Agility.UniqueID = function(prePend){
		/// <summary>
        /// Build a Unique ID (unique to this instance).
        /// </summary>
        /// <param name="prePend" type="String">The value to prepend to the Unique portion.</param>
                        
        if (prePend == undefined || prePend == null || prePend == "") {
			return _uniqueIndex++;
        } else {
			if (_unique[prePend] == undefined) _unique[prePend] = 0;
			return prePend + _unique[prePend]++;
        }				
	}
	
	Agility.CloneObject = function (what, serializationType) {
		/// <summary>
        /// Clones an object to preserve the original value.
        /// </summary>
        /// <param name="what" type="object">The object to clone.</param>
        
        if (serializationType != undefined && serializationType != null && serializationType != "") {
			this.__type = serializationType;	
        }
        
		for (i in what) {
			if (what[i] == null) {
				this[i] = null;
			} else {
			 
				if (typeof what[i] == 'object') {
					this[i] = new Agility.CloneObject(what[i]);
				} else {
					var val = what[i];				
					this[i] = val;				
				}
			}
		}
	}


	
	Agility.QueryString = function(name, url) {
		/// <summary>
        /// Gets a variable from the query string.  
        /// </summary>
        /// <param name="name" type="String">QueryString variable to retrieve.</param>
        /// <param name="url" type="String">(Optional) the url to take the querystring from.  If this is not present, the current url is used.</param>
		
		if (url == undefined || url == null || url == "") {
			url = location.href;
		}
		
		var index = url.indexOf("?");
		if (index < 1 && index == url.length - 2) return null;
		if (url.indexOf("#") != -1) {
			url = url.substring(0, url.indexOf("#"));
		}
		
		var qstr = url.substring(index + 1, url.length);
        
        var ary1 = qstr.split("&");
        var retValue = null;
        
        jQuery.each(ary1, function(index, q) {
			var ary2 = q.split("=");
			if (ary2.length == 2) {
				if (unescape(ary2[0]).toLowerCase() == name.toLowerCase()) {
				
					retValue = ary2[1];					
					retValue = retValue.replace(/\+/g, "%20");
					retValue = unescape(retValue);
					return false;
				}
			}
        });
        
        return retValue;
        
	}
	
	Agility.ToJSONDate = function(dateObj) {
		/// <summary>
        /// Converts a Date object to a JSON string, and it will account for the timezone offset (\/Date(000000000000)\/). 
        /// </summary>
        /// <param name="dateObj" type="Date">Date object to convert.</param>
				
		dateObj.setMinutes(dateObj.getMinutes() + dateObj.getTimezoneOffset());
				
		var dtStr = dateObj.getFullYear() + "-" + Agility.PadLeft(dateObj.getMonth() + 1, "0") + "-" + Agility.PadLeft(dateObj.getDate() + 1, "0") + " " + Agility.PadLeft(dateObj.getHours(), "0") + ":" + Agility.PadLeft(dateObj.getMinutes(), "0") + ":" + Agility.PadLeft(dateObj.getSeconds(), dateObj.getMilliseconds(), "0");
		
		return "\/Date("+ utcMilli +")\/";			
	}
	
	Agility.PadLeft = function(str, padChar) {
		
		str = str + "";
		while (str.length < padChar) {
			str = padChar + str;
		}
		
		return str;
	}
	
	Agility.GetFlashVersion = function() {
		
	   var flashVersion = 0;
	   
		if (window.ActiveXObject) {   
			//ie
			var control = null;   
			try {   
				control = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');   
				if (control) {					
					var version = control.GetVariable('$version').substring(4);   
					version = version.split(',');   
					flashVersion = parseInt(version[0]);
				}   
			} catch (e) {  				
			}   
			
		} else { 
	   
			//non ie
			if (navigator.plugins != null) {
				if (navigator.plugins.length > 0) {
					var flashPlugin = navigator.plugins['Shockwave Flash']; 
						if (typeof flashPlugin == 'object') {
						for (i=25;i>0;i--) {
							if (flashPlugin.description.indexOf(i+'.') != -1){ 
								flashVersion = i;
							}
						}
					}
				}
			}
		}
		
		return flashVersion;
	}

	
	

	

	

})(Agility, jQuery);



