/**
* Namespace constructor for the mds object. Oh and browser
* detection agent cause it needed to be outside.
* @ constructor
*/
var mds = window.mds || {};
new function() {
	var b = navigator.userAgent.toLowerCase();
	mds.browser = {
		safari: /webkit/.test(b),
		opera: /opera/.test(b),
		msie: /msie/.test(b) && !/opera/.test(b),
		mozilla: /mozilla/.test(b) && !/compatible/.test(b)
	};
};

/**
* Utilities for use by documentation code, and the world in general. 
* @ class
*/
mds.utils = {
	
	/**
    * Grab host from the URL string
    * @return {string} host 
    */
	getHost:function (){
		var loc = window.location.href;
		var prot = loc.split(":");
		var reg = new RegExp("^(http:\/\/)?([^\/]+)");
		var bob = reg.exec(loc);
		return bob[0].replace(prot[0]+"://","");		
	},
	
	/**
    * Grab GET variables from the URL string
    * @return {array} variables key/value 
    */
	getUrlArgs:function (){
		var args = {};
		var loc = window.location.href;
		var q = loc.indexOf("?");
		if (q==-1){
			return false;
		}
		loc = loc.substring(q+1);
		var pairs = loc.split("&");
		for (var i=0; i<pairs.length;i++){
			var keyval = pairs[i].split("=");
			args[keyval[0]] = unescape(keyval[1]);
		}
		return args;
	},
	
	/**
	* Get the browser size
	* @return {array} w/h
	*/
	getPageSize:function(){
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		arrayPageSize = new Array(w,h);
		return arrayPageSize;
	}
}

/**
* Programs run by the fornt of the house.
* @ class
*/
mds.front = {
	
	fieldFocus:function(f) {
		f.value="";
	},
	
	fieldBlur:function(f,n) {
		if(f.value==""){
			f.value=n;
		}
	},
	
	/**
	* After fighting the 1 pixel shoft for too long, I have given up. Time for JS to fix it for me
	*/
	stretchPage:function(){
		var btm = jQuery("#footanchor");
		var pos = btm.position();
		var pge = mds.utils.getPageSize();		
		
		var val = pos.top;
		if(val < pge[1]){
			var sze = pge[1] - val - 16;
			btm.css({"height" : sze + "px"});
		}
	},
	
	/**
    * Toggle the Search Box
    * @return {array} variables key/value 
    */
	toggleSearch:function(li) {
		if(mds.vars.frontSearchState == false){
			document.getElementById('searchicon').src="/wp-content/themes/version2-1/images/v2_search_on.png";
			jQuery("#topsearch").toggle("slide", { direction: "up" }, 500, function(){
				document.forms["searchform"].elements["s"].focus();
			});	
			mds.vars.frontSearchState = true;
		}else{
			document.getElementById('searchicon').src="/wp-content/themes/version2-1/images/v2_search_off.png";
			jQuery("#topsearch").toggle("slide", { direction: "up" }, 500);
			mds.vars.frontSearchState = false;			
		}
	},
	
	/**
	 * Zebra stripe any tables you see of the specific classes	
	 */
	zebraStripe:function(){
		var r=0;
		var tbls = document.getElementsByTagName('table');
		for (var i=0; i < tbls.length; i++) {
			r=0;
			if(tbls[i].className == "page-tabledetails"){
				var gimme = tbls[i].getElementsByTagName("tr");
				for(z=0;z<gimme.length;z++){
					if(gimme[z].parentNode.parentNode == tbls[i]){
						if(gimme[z].style.display != "none"){
							gimme[z].className = (r++ & 1) ? '' : 'altbg';
						}
					}
				}
			}
		}
	}
	
}

/**
* A lot of stuff runs automatically. By keeping enable flags in here, we allow
* doc authors to keep stuff from running. Just change the values to false in your
* actual doc files.
* @ class
*/
mds.vars = {
	
	frontSearchState:false,	
	buildTopNavigation:true,
	buildTOC:true,
	addSwatches:true,
	autoIndent:true,
	TopNavigation:{}
	
}