/* FUNCTION DECLARATION
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/

function init() {   
	menu();
	//showHeading2(); //Replaced by Ruby conditional
	//maintenance(); //uncomment to take the site down for maintenance (this is a ridiculous method but XL has no other options)
};


function maintenance() {
  var l = parseUri(location.href);
  if (l.path != '/admin' && l.path != '/session/new' && l.path !='/maintenance') { 
    var x = readCookie('session_id')
    if (x != '96ffad495a16af235a66c76beb24086b') { //update this to your current session variable
    	window.location = 'http://www.artizenrenovations.com/maintenance';
    }
  }
}

/* http://www.quirksmode.org/js/cookies.html */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}



/* Sets the down state for menu items
*
* Requires: Prototype, parseUri 1.2.2
* Usage Note: 
* Main Menu id attribute must = 'mainMenu-'+ sectionName 
* Sub-Menu  id attribute must = 'subMenu-' + pageName 
*/
function menu() {
    //l.path returns "/sectionName" or "/sectionName/pageName"
    var l = parseUri(location.href);
        t = l.path, 
	    a = t.split('/'),
    	s = (t == '/') ?  'mainMenu-home' : 'mainMenu-'+a[1],
		p = 'subMenu-'+a[2];
	
	/*	Sets mainMenu selected state*/
	if ($(s)) { $(s).toggleClassName("selected"); }

	/*	Sets subMenu selected state*/
	if ($(p)) { $(p).toggleClassName("selected"); }
};

/* Query string sets visibility of H2 element
*
* Requires: Prototype, parseUri 1.2.2
*/
function showHeading2() {
    var l = parseUri(location.href);
    if (l.queryKey.h2 == '1') { $('pageHeading').toggleClassName("hide"); }
};



/* Enables and shows a disabled and hidden field and vice versa
* 
* Requires: Prototype
*
* Usage: 
* onclick="enableField('fieldName', [0 | 1] );" 
* 0 to hide and disable, 1 to show and enable
*/
function enableField(f, d) {
    if ($(f).disabled && d == 1) {
      $(f).appear();
      $(f).enable();
    } else {
      $(f).hide();
      $(f).disable();
    }
};



/*
* http://blog.stevenlevithan.com/archives/parseuri
* http://stevenlevithan.com/demo/parseuri/js/
*
* parseUri 1.2.2
* (c) Steven Levithan <stevenlevithan.com>
* MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
