$(function() {
	// external links
	$("a[rel=external]").attr("target", "_blank");

	// links to other sites
	var host = document.location.protocol + "//" + document.location.host;
	$("a[href^=http]").each(function() {
		if (this.href.indexOf(host) !== 0) {
			$(this).attr("target", "_blank");
		};
	});
	// PDFs 
	$("a[href$=\".pdf\"]").attr("target", "_blank");
	
	// Placeholders
	$("input[type=text][title]").each(function() {
		$(this).data("placeholder", this.title).bind("focus", function() {
			if (this.value == $(this).data("placeholder")) {
				this.value = "";
			}
			$(this).removeClass("default");
		}).bind("blur", function() {
			if (this.value == "" || this.value == $(this).data("placeholder")) {
				this.value = $(this).data("placeholder");
				$(this).addClass("default");
			};
		}).attr("title", "").trigger("blur").closest("form").bind("submit", function(evt) {
			$(this).find("input[type=text][title], textarea[title]").each(function() {
				$(this).triggerHandler("focus");
			});
		});
	});
	$("input").each(function() {
		$(this).addClass('input_' + $(this).attr("type"));
	});
});

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


/**
 * Read the JavaScript cookies tutorial at:
 *   http://www.netspade.com/articles/javascript/cookies.xml
 */

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


/*
	Time and string functions
*/

function strTruncate(str, len, filler) {
	len = (len === undefined) ? 100 : len;
	filler = (filler === undefined) ? "..." : filler;
	return str.length > len ? str.substring(0, len) + filler : str;
}
String.prototype.truncate = function(len, filler) {
	return strTruncate(this, len, filler);
};
function str_pad (input, pad_length, pad_string, pad_type) {
    // Returns input string padded on the left or right to specified length with pad_string  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/str_pad
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + namespaced by: Michael White (http://getsprink.com)
    // +      input by: Marco van Oort
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
    // *     returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
    // *     example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
    // *     returns 2: '------Kevin van Zonneveld-----'
    var half = '', pad_to_go;

    var str_pad_repeater = function (s, len) {
        var collect = '', i;

        while (collect.length < len) {collect += s;}
        collect = collect.substr(0,len);

        return collect;
    };

    input += '';
    pad_string = pad_string !== undefined ? pad_string : ' ';

    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }

    return input;
}
function getDays(month, year) {
	var days = 0;
	if (month%2==1) {
		days = 31;
	} else if (month==2) {
		if (year%4==0 && year%100!=0) {
			days = 29;
		} else {
			days = 28;
		}
	} else {
		days = 30;
	}
	return days;
}

function timeSinceNow(date, now) {
	now = (now !== undefined && now instanceof Date) ? now : new Date();
	var difference = now - date;
	var returnVal = "";
	
	// date is in the future
	if (difference < 0) {
		// seconds 
		if (difference > 1000 * 60 * -1) {
			returnVal = "Less than a minute from now";
		// minutes
		} else if (difference > 1000 * 60 * 60 * -1) {
			var minutes = Math.floor(difference * -1 / (1000 * 60));
			returnVal = minutes + " minute" + (minutes > 1 ? "s" : "") + " from now";
		// hours
		} else if (difference > 1000 * 60 * 60 * 24 * -1) {
			var hours = Math.floor(difference * -1 / (1000 * 60 * 60));
			returnVal = hours + " hour" + (hours > 1 ? "s" : "") + " from now";
		// days
		} else if (difference > 1000 * 60 * 60 * 24 * getDays(now.getMonth() + 1, now.getFullYear()) * -1) {
			var days = Math.floor(difference * -1 / (1000 * 60 * 60 * 24));
			returnVal = days + " day" + (days > 1 ? "s" : "") + " from now";
		// months
		} else if (difference > 1000 * 60 * 60 * 24 * 365 * -1) {
			var months = Math.floor(difference * -1 / (1000 * 60 * 60 * 24 * 365) * 12);
			returnVal = months + " month" + (months > 1 ? "s" : "") + " from now";
		// years
		} else if (difference <= 1000 * 60 * 60 * 24 * 365 * -1) {
			var years = Math.floor(difference * -1 / (1000 * 60 * 60 * 24 * 365));
			returnVal = years + " year" + (years > 1 ? "s" : "") + " from now";
		}
		
	// date is in the past
	} else {
		// seconds 
		if (difference < 1000 * 60) {
			returnVal = "Less than a minute ago";
		// minutes
		} else if (difference < 1000 * 60 * 60) {
			var minutes = Math.floor(difference / (1000 * 60));
			returnVal = minutes + " minute" + (minutes > 1 ? "s" : "") + " ago";
		// hours
		} else if (difference < 1000 * 60 * 60 * 24) {
			var hours = Math.floor(difference / (1000 * 60 * 60));
			returnVal = hours + " hour" + (hours > 1 ? "s" : "") + " ago";
		// days
		} else if (difference < 1000 * 60 * 60 * 24 * getDays(now.getMonth() + 1, now.getFullYear())) {
			var days = Math.floor(difference / (1000 * 60 * 60 * 24));
			returnVal = days + " day" + (days > 1 ? "s" : "") + " ago";
		// months
		} else if (difference < 1000 * 60 * 60 * 24 * 365) {
			var months = Math.floor(difference / (1000 * 60 * 60 * 24 * 365) * 12);
			returnVal = months + " month" + (months > 1 ? "s" : "") + " ago";
		// years
		} else if (difference >= 1000 * 60 * 60 * 24 * 365) {
			var years = Math.floor(difference / (1000 * 60 * 60 * 24 * 365));
			returnVal = years + " year" + (years > 1 ? "s" : "") + " ago";
		}
	}
	return returnVal;
}


function number_format(number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/number_format
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +   improved by: davook
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Jay Klehr
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *    example 10: number_format('1.20', 2);
    // *    returns 10: '1.20'
    // *    example 11: number_format('1.20', 4);
    // *    returns 11: '1.2000'
    // *    example 12: number_format('1.2000', 3);
    // *    returns 12: '1.200'
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

