/* 
 *
 * Author: Adam Legge
 * 
 * Description: 
 * 
 * Note 1: This file requires JQuery to be already be loaded.
 *
 * Note 2: jQuery's CSS selectors don't play well with special characters, that's why you'll notice the use of the
 * filter method. This simply calls a function to test the equality of certain attributes instead of using the
 * typical $('element[name=value]') syntax but achieves the same goal.
 *
 */
 
$(document).ready
(
	function()
	{

		//
		css_browser_selector(navigator.userAgent);
		
	}
);

/*
 *
 * Misc.
 *
 */

//Retrieves each of the name/value pairs provided in the current URL).
function getURLParams()
{
	//Set up the variables.
	var url_params = new Array();

	//Check if the url contains both url and hash parameters.
	if(window.location.search.indexOf('?') != -1 && window.location.hash != "")
	{
		//Parses the url parameter portion of the string.
		var url_params_string = window.location.search.substring(window.location.search.indexOf('?')+1);
		var url_params = splitParamsString(url_params_string);

		//Parses the hash parameter portion of the string.
		var hash_params_string = window.location.hash.substring(window.location.hash.indexOf('?')+1);
		var hash_params = splitParamsString(hash_params_string);
		
		//Loop over each of the hash parameters, checking if they also exist as url parameters or not.
		for(i=0; i < hash_params.length ; i++ )
		{
			//If the hash parameter in question doesn't exist in the url parameters yet then we add it.
			if(url_params[hash_params[i]] == undefined)
			{
				url_params.push(hash_params[i])
				url_params[hash_params[i]] = hash_params[hash_params[i]]
			}
			
			//The hash parameter takes precedence and overwrites the url parameter of the same name.
			url_params[hash_params[i]] = hash_params[hash_params[i]];
		}
	}
	//If there is only one type of parameters then we don't have to worry about conflicts and just parse the string as is.
	else
	{
		url_params_string = window.location.href.substring(window.location.href.indexOf('?')+1);
		url_params = splitParamsString(url_params_string);
	}
	
	return url_params;
};

function splitParamsString(string)
{
	params = new Array();

	//Loop over the given string, assigning each name/value pair to a new slot in the associative array.
	var pairs = string.split("&");
	for( i=0; i < pairs.length ; i++ )
	{
		var current_pair = pairs[i];
		
		var name = decodeURI(current_pair.substring(0,current_pair.indexOf('=')));
		var value = decodeURI(current_pair.substring(current_pair.indexOf('=')+1));
		
		params.push(name)
		params[name] = value;
	}
	
	return params
};

/*
CSS Browser Selector v0.4.0 (Nov 02, 2010)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u)
{
	var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;
};

//
function disableTextSelect($element)
{
	//Firefox
	if($.browser.mozilla)
	{
		$element.css('MozUserSelect','none');
	}
	//IE
	else if($.browser.msie)
	{
		$element.bind('selectstart',function(){return false;});
	}
	//Opera, etc.
	else
	{
		$element.mousedown(function(){return false;});
	}
};

//
function centerElement($element)
{
	$element.css("position","absolute"); 
    $element.css("top", (($(window).height() - $element.outerHeight()) / 2) + $(window).scrollTop() + "px"); 
    $element.css("left", (($(window).width() - $element.outerWidth()) / 2) + $(window).scrollLeft() + "px"); 
};

//
String.prototype.trim = function()
{
	return this.replace(/^[\s]+/,"").replace(/[\s]+$/,"")
};

//
String.prototype.titleize = function()
{
	return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){return p1+p2.toUpperCase();});  
};
