
// template utility function
function template(tpl, opt) {
	var out = tpl;
	if (opt) {
		for (var key in opt) {
			out = out.replace(new RegExp('<%' + key + '%>', 'g'), opt[key]);
		}
	}
	//console.log(out);
	return out;
}

function parseQueryString(q) {
	var o = {};
	var pairs = q.split('&');
	for (var i = 0; i < pairs.length; i++) {
		var psplit = pairs[i].split('=');
		o[decodeURIComponent(psplit[0])] = decodeURIComponent(psplit[1]);
	}
	return o;
}

function popUp(src, opts) {
	var opts = opts || {};
	var name = opts.name || '_blank';
	var params = [];
	// numbered and boolean parameters
	var numParams = ['top', 'left', 'width', 'height'];
	var boolParams = [
		'dependent', 'directories', 'fullscreen', 
		'location', 'menubar', 'resizable', 
		'scrollbars', 'status', 'toolbar'
	];
	// run through the parameters
	for (var i = 0; i < numParams.length; i++) {
		if (opts[numParams[i]]) { params.push(numParams[i] + '=' + opts[numParams[i]]); }
	}
	for (var j = 0; j < boolParams.length; j++) {
		if (opts[boolParams[i]]) { params.push(boolParams[i] + '=yes'); }
	}
	var w = window.open(src, name, params.join(','));
	// focus if specified
	if (opts.focus) { w.focus(); }
	// return the window
	return w;
}

jQuery(function(){

	/* ****************************** MENUS ************************************** */

	// controls the submenus  
	// we need the immediate descendant selector here
	var MenuLIs = jQuery('#TopMenu > ul > li');
	MenuLIs.bind('mouseenter', function(){
		var realTarget = jQuery(this);
		
		// fade in the child of the target
		realTarget.children('ul').fadeIn('fast');
		return false;
	})
	.bind('mouseleave', function(){
		var realTarget = jQuery(this);
		realTarget.children('ul').fadeOut('slow');
		return false;
	});
	MenuLIs.children('a').click(function(){ return false; });

	// we need the third descendant selector here
	var MenuLIThirds = jQuery('#TopMenu > ul > li > ul > li');
		
	MenuLIThirds.bind('mouseenter', function(){
		var realTarget = jQuery(this);
		var ParentWidth = realTarget.outerWidth() + 2;

		// fade in the child of the target
		realTarget.children('ul').css('left', ParentWidth + 'px');
		realTarget.children('ul').fadeIn('fast');
		// return false;
	})
	.bind('mouseleave', function(){
		var realTarget = jQuery(this);
		realTarget.children('ul').fadeOut('slow');
		// return false;
	});
	
	/* ******************************* OFFICES ***************************************** */
	
	var OfficesContainer = jQuery('#Offices');
	var Offices = OfficesContainer.find('li');
	var OfficesSelect = jQuery('<select><option value=""> - Select an Office Location - </option></select>').insertBefore(OfficesContainer.find('ul'));
	
	// changes the office shown
	function changeOffice(id) {
		Offices.hide();
		jQuery('#' + id).show();
		//alert(id);
	}
	
	// populate select box
	Offices.each(function(){
		var tgt = jQuery(this);
		var id = tgt.attr('id');
		var name = tgt.find('.office-name').text();
		name = name.substr(0, name.length - 1);
		OfficesSelect.append('<option value="' + id + '">' + name + '</option>');
	});
	
	// select box functionality
	OfficesSelect.change(function(){
		if (OfficesSelect.val() !== "") {
		changeOffice(OfficesSelect.val());
		}
	});
	
	// show the first menu
	//var toShow = Offices.attr('id'); // first office listed, by default
	var toShow = 'Officeefc1a411-1321-0e36-ba2d-cfada4f77c46'; // Cheyenne
	changeOffice(toShow);
	
});


