/*---------------------------------------------------------*\
|		harley.helper.js - JavaScript Helper collection		|
|		   v5.2, October 2011 by Harley H. Puthuff			|
\*---------------------------------------------------------*/

/*
 * Useful Constants:
 */
var NOTFOUND	=	-1;			// not found condition

/*
 * Method for simple Array objects to find items
 *	P1 = item to find
 *	returns the index (0-n) or -1 if not found
 */
Array.prototype.find = function(lookfor) {
	for (var ix=0; ix<this.length; ++ix)
		if (this[ix]==lookfor) return ix;
	return NOTFOUND;
	}

/*
 * Method for String objects to capitalize each word
 *	returns properly capitalized string
 */
String.prototype.capitalize = function() {
	return this.replace(
		/(^|\s)([a-z])/g,
		function(m,p1,p2){
			return p1+p2.toUpperCase()
			}
		);
	}

/*
 * Method for String objects to render Skype-proof
 *	returns skype-proof string for links, etc.
 */
String.prototype.skypeProof = function() {
	var half = Math.floor(this.length/2);
	return this.substr(0,half)+"<span style='display:none'>_</span>"+this.substr(half);
	}

/*
 * Clone an object using deep copy
 *	P1 = ref. to object to clone
 *	returns an object
 */
function clone(obj) {
	return $.extend(true,{},obj);
	}

/*
 * Detect and return correct timezone offset (w/o DST)
 *	(courtesty of Michael Khalili)
 *	returns a +/- offset from GMT
 */
function TimezoneDetect() {
    var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear());
    var intOffset = 10000; //set initial offset high
    var intMonth;
    var intHoursUtc;
    var intHours;
    var intDaysMultiplyBy;
    //go through each month to find the lowest offset to account for DST
    for (intMonth=0; intMonth < 12; intMonth++) {
        //go to the next month
        dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);
        //To ignore daylight saving time look for the lowest offset.
        //Since, during DST, the clock moves forward, it'll be a bigger number.
        if (intOffset > (dtDate.getTimezoneOffset() * (-1))) {
            intOffset = (dtDate.getTimezoneOffset() * (-1));
			}
		}
    return intOffset;
	}

/*
 * Generic function to handle small pop-up boxes
 *	Note:	1) links for popups should have: rel="pop-up"
 *			2) after page loading: $('a[rel="pop-up"]).click(invokePopUp);
 */
function invokePopUp() {
	var popup = {width: 430,height: 260};
	var win = {left: 0,top: 0};
	if ($.browser.msie)
		{win.left = window.screenLeft; win.top = window.screenTop;}
	else
		{win.left = window.screenX; win.top = window.screenY;}
	var offset = $(this).offset();
	offset.left = win.left + offset.left;
	offset.top = win.top + offset.top;
	var features=
		"height="+popup.height+","+
		"width="+popup.width+","+
		"left="+offset.left+","+
		"top="+offset.top;
	window.open(this.href,'Popup',features,true);
	return false;
	}

/*
 * Breakout a thing using a popup message
 */
function breakout(thing) {
	var msg = "Breakout:\r\r";
	switch (typeof thing) {
		case 'undefined':
			msg += "[Undefined]\r";
			break;
		case null:
			msg += "[NULL]\r";
			break;
		case 'boolean':
			msg += ("[Boolean] "+thing+"\r");
			break;
		case 'number':
			msg += ("[Number] "+thing+"\r");
			break;
		case 'string':
			msg += ("[String] "+thing+"\r");
			break;
		case 'function':
			msg += ("[Function]\r");
			break;
		case 'object':
			msg += ("[Object]\r");
			for (p in thing) msg += (" '"+p+"': "+thing[p]+"\r");
			break;
		default:
			msg += ("[UNKNOWN TYPE]\r");
			break;
		}
	alert(msg);
	}

