/*
$(function(){
	utils = new Utils;
});
*/
//window.onunload = function(){ }
//window.onbeforeunload = function(){ }

Utils = function(){
	var s = location.href;
	//	this.baseURL = (typeof(baseURL) != 'undefined') ? baseURL : s.slice(0, (s.indexOf('www')+3));
	this.indexPage = '';
	var port = "";
	if (location.href.indexOf(":") != -1) {
		port = ":"+location.port;
	}
	this.baseURL = (typeof(baseURL) != 'undefined') ? baseURL : location.protocol+'//'+location.hostname+port+'/index.php';
	//	this.indexPage = '/index.php';
	this.imageBaseURL = this.baseURL+ '/lib/images/';
	$.ajaxSetup( { global: false, dataType: 'html', type:'GET' } ); 
}

Utils.prototype = {
	index:function(arr,el){
		for (var i = 0; i < arr.length; i++) {
			if (arr[i] == el) return i;
		}
		return -1;
	},

	getQueryValue:function( key ){
	  var query = this.getQueryBuffer();
  	var vars = query.split("&");
	  for (var i=0;i < vars.length;i++) {
  	  var pair = vars[i].split("=");
	    if (key == pair[0]) {
  	    return pair[1];
	    }
  	} 
	 	return null;
	},

	location:function( url ){
		(location.href.assign) ? location.href.assign(url) : location.href = url;
	},

	getMovie:function ( movieName ){
		// IE and Netscape refer to the movie object differently.
		
		// This function returns the appropriate syntax depending on the browser.
		if (navigator.appName.indexOf ("Microsoft") !=-1) {
			return window[movieName];
		}	else {
			return document[movieName];
		}
	},

	setFlashVar:function( movie, name, val ){
		var movie = (typeof movie == 'string') ? this.getMovie(movie) : movie
		if(movie != null && movie.SetVariable) 	movie.SetVariable( name,val);
	},

	getURLWithQueryString:function(pairs, baseURL){
		var baseURL = baseURL || null;
		if(!baseURL) baseURL = this.baseURL;
		if(typeof pairs == 'string') return baseURL + '?' +pairs;

		for(var i = 0; i < pairs.length; i++){
			if(i == 0){
				if(baseURL.indexOf('?') < 0 ) baseURL += '?' + pairs[i][0] + '=' + pairs[i][1];
				else  baseURL += '&' + pairs[i][0] + '=' + pairs[i][1];
			}	else baseURL += '&' + pairs[i][0] + '=' + pairs[i][1];
		}
		return baseURL;
	},
	
	getQueryStringAsArray:function(url){
		var url = url || null;
		var qString;
		if(url) { 
			qString = (url.indexOf('?') > -1 ) ? url.slice(url.indexOf('?')+1) :  '';
		}else{
			qString = this.getQueryBuffer();
		}		

		var pairs = [];
		if(qString.length < 1) return pairs;
  	var vars = qString.split("&");
	  for (var i=0;i < vars.length;i++) {
  	  var pair = vars[i].split("=");
			pairs.push(pair);
  	} 
		return pairs;
	},

	getParamsAsArray:function(params){
  	var vars = params.split("&");
		var pairs = [];
	  for (var i=0;i < vars.length;i++) {
  	  var pair = vars[i].split("=");
			pairs.push(pair);
  	} 
		return pairs;
	},

	getArrayAsParams:function(arr){
		var params = '';
		for(var i = 0; i < arr.length; i++){
			params += arr[i][0]+ '=' +arr[i][1]
			if(i < arr.length - 1) params += '&';
		}
		return params;	
	},

	setKeyValueToQueryString:function(key,value,url){
		var pairs = (url) ? this.getQueryStringAsArray(url) : this.getQueryStringAsArray();
		var exists = this.getQueryValue(key);
		var url = url || null;
		if(exists != null){
			for(var i = 0; i < pairs.length; i++){
				if(pairs[i][0] == key) {
					if(value)	pairs[i][1] = value;
					else pairs.remove(pairs[i]);
				}
			}
		}else{
			pairs.push([key,value]);
		}
		this.queryBuffer = this.getArrayAsParams(pairs);
		return this.queryBuffer;
	},

	getQueryBuffer:function(){
		return (this.queryBuffer.indexOf('?') > -1 ) ? this.queryBuffer.substring(1) : this.queryBuffer;
	},

	unique:function(a){
		tmp = new Array(0);
		for(i=0; i < a.length;i++){
			if(!contains(tmp, a[i])){
				tmp.length+=1;
				tmp[tmp.length-1]=a[i];
			}
		}
		return tmp;	
	},
  
	stringToInitialCap:function(str){
		str.toLowerCase();
		var initial = str.slice(0,1);
		var last = str.slice(1);
	 return initial.toUpperCase() + last;
	},
	
	getCookie:function(cookieName) {
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
	
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );
	
	
			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
			// if the extracted name matches passed check_name
			if ( cookie_name == cookieName )
			{
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )
		{
			return null;
		}
	}
}	

jQuery.prototype.closest = function( selector ) {
	var pos = jQuery.expr.match.POS.test( selector ) ? jQuery(selector) : null;

	return this.map(function(){
		var cur = this;
		while ( cur && cur.ownerDocument ) {
			if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) )
				return cur;
			cur = cur.parentNode;
		}
	});
}

Array.prototype.contains = function(el){
	for (var i = 0; i < this.length; i++) {
		if (this[i] == el) return true;
	}
	return false;
}

Array.prototype.indexOf = function(el){
	for (var i = 0; i < this.length; i++) {
		if (this[i] == el) return i;
	}
	return -1;
}

Array.prototype.remove = function(el){
	var i = 0;
	while (i < this.length) {
		if (this[i] == el) {
			this.splice(i, 1);
		} else {
				i++;
		}
	}
}

