/*
	/static/scripts/gg_lib.js
	provides core function library for gogamecocks.com
	DO NOT put '$(document).ready(function(){});' content here
	(c) 2010, The State media Company
	Chris Hessert <chris.hessert@gmail.com>
	updated:  2010-08-20
*/

/* @minify true */

/*  declarations  */
var vmixProxyUrl = 'http://www.gogamecocks.com/cgi-bin/vmix_proxy.php';  // imports VMIX account settings (production)


/*  functions  */

// returns a proxied url for data connections
function buildApiUrl(api, action, params) {
	var myApi = '?api=' + api;
	var myAction = '&action=' + action;
	var myParams = '&params=' + escape(params);
	return vmixProxyUrl + myApi + myAction + myParams;
}

// parses mysql datetime string and returns javascript Date object
function mysqlTimeStampToDate(timestamp) {	
	//input has to be in this format: 2007-06-05 15:26:02
	var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
	var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
	return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}

// returns url parameters from an array as a string formatted for data proxy calls
function paramsToString(array) {
	var myString = new String();
	for ( var i = 0; i < array.length; i++ ) {
		myString += array[i] + ";";  // add array elements separated by semi-colons
	}
	return myString;
}

// splits a URL query string into key/value pairs
function parseUrl() {
	if ( !location.hash ) {
		return false;
	}
	var hash = location.hash.substring(1);
	var pieces = hash.split('&');
	var pairs = new Array();
	var out = {};
	for ( i in pieces ) {
		pairs = pieces[i].split('=');
		eval('out.' + pairs[0] + ' = "' + pairs[1] + '"');
	}
	return out;
}

// returns a string stripped of all weird characters and extra white space (for searches)
function safeSearchString(string) {
	if (string) {
		string = string.replace(/\W/g, " ");  // replace weird characters with a blank space
		string = string.replace(/\s+/g, " ");  // strip extra whitespace
		string = string.replace(/^\s+|\s+$/g, "");  // strip leading and trailing whitespace
		return string;
	}
}

// switches display elements on/off
function toggleDisplay(element, style) {
	switch( $("#" + element).css("display") ) {
		case "none":
			$("#" + element).css("display", style);
			break;
		default:
			$("#" + element).css("display", "none");
			break;
	}
}

// prepends a blank space to values less than 10 (to maintain alignment)
function toSingleDigit(i) {
	if (i < 10) {
		i = "&nbsp;" + i;
	}
	return i;
}

// renders a string in title or proper case
function toTitleCase(str) {
    return str.replace(/\w\S*/g,  function(txt) {
		return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
	});
}

// returns a string trimmed to a designated length with '...' at the end
function trim(str, strMax){
	if (str.length > strMax){
		return str.substr(0, strMax) + '...';
	} else {
		return str;
	}
}

// prepends "0" to values less than 10
function toTwoDigit(i) {
	if (i < 10) {
		i = "0" + i;
	}
	return i;
}

/*  cookie handlers  */
// sets a cookie
function setCookie() {
	if (arguments.length < 2) {
		return;
	}
	var name = arguments[0];
	deleteCookie(name);
	var value = arguments[1];
	var days = 0;
	if (arguments.length > 2) {
		days = parseInt(arguments[2]);
	}
	var exp = '';
	if (days > 0) {
		var now = new Date();
		expiry = now.getTime() + (days * 24 * 60 * 60 * 1000);
		now.setTime(expiry);
		exp = '; expires=' + now.toGMTString();
	}
	document.cookie = name + "=" + escape(String(value)) + '; path=/' + exp;
}

// retrieves cookie values
function getCookie(name) {
	var cookiecontent = new String();
	if (document.cookie.length > 0) {
		var cookiename = name+ '=';
		var cookiebegin = document.cookie.indexOf(cookiename);
		var cookieend = 0;
		if (cookiebegin > -1) {
			cookiebegin += cookiename.length;
			cookieend = document.cookie.indexOf(";",cookiebegin);
			if (cookieend < cookiebegin) {
				cookieend = document.cookie.length;
			}
			cookiecontent = document.cookie.substring(cookiebegin,cookieend);
		}
	}
	return unescape(cookiecontent);
}

// eats cookies for breakfast
function deleteCookie(name, path, domain) {
	if (getCookie(name)) document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
