/*
-----------------------------------------------
MonkeyHouseMusic.com
Script: monkeyUtil.js
Author:Steve Van Etten
Created: 
----------------------------------------------- */

monkeyUtil = {
	init:function() {
		monkeyUtil.mailtoFix('REMOVETHISBEFORESENDING');
		monkeyUtil.preparePopups();
		monkeyUtil.autoPopulate('input.populate');
		if ($jq('.jcarousel-item').length) {
			$jq('.jcarousel-item').equalizeCols();
		}
	},
	mailtoFix:function(stringToRemove) {
		var links = document.getElementsByTagName('a');
		var removeText = new RegExp(stringToRemove);
		for (var i = 0; i < links.length; i++) {
			if (links[i].href.indexOf('mailto:') != -1) {
				links[i].href = links[i].href.replace(removeText, '');
				links[i].firstChild.nodeValue = links[i].firstChild.nodeValue.replace(removeText, '');
				links[i].firstChild.nodeValue = links[i].firstChild.nodeValue.replace(/mailto:/, '');
			}
		}
	},
	popUp:function(winURL, name, parameters) {
		window.open(winURL, name, parameters);
	},
	preparePopups:function() {
		if (!document.getElementsByTagName) return false;
		var lnks = document.getElementsByTagName("a");
		for (var i=0; i<lnks.length; i++) {
			if (lnks[i].className == "popup") {
				lnks[i].title+= " (opens in a new window)";
				lnks[i].onclick = function() {
					monkeyUtil.popUp(this.href, "popup", "width=320,height=480");
					return false;
				}
			}
			else if (lnks[i].className == "external") {
				lnks[i].title+= " (opens in a new window)";
				lnks[i].onclick = function() {
					monkeyUtil.popUp(this.href, "external", "");
					return false;
				}
			}
			else if (lnks[i].href != null && lnks[i].href.indexOf('.pdf') != -1) {
				lnks[i].title += " (opens in a new window)";
				lnks[i].onclick = function() {
					monkeyUtil.popUp(this.href, "pdf", "");
					return false;
				}
			}
		}
	},
	trimString:function(str) {
		return str.replace(/^\s*\n*\r*|\s*\n*\r*$/g,'');
	},
	fadeUp:function(element, red, green, blue) {
		if (element.fade) {
			clearTimeout(element.fade);
		}
		element.style.backgroundColor = 'rgb('+red+','+green+','+blue+')';
		if (red == 255 && green == 255 && blue == 255) {
			return;
		}
		var newred = red + Math.ceil((255-red)/10);
		var newgreen = green + Math.ceil((255-green)/10);
		var newblue = blue + Math.ceil((255-blue)/10);
		var repeat = function() {
			monkeyUtil.fadeUp(element, newred, newgreen, newblue);
		}
		element.fade = setTimeout(repeat, 100);
	},
	autoPopulate:function(input_sel) {	
		$jq(input_sel).each(function() {
			var populate_text = $jq('label[for="' + $jq(this).attr('id') + '"]').text();
			if (populate_text) {
				$jq(this).val(populate_text).data('populate_text', populate_text);				
				$jq(this).focus(function() {
					if ($jq(this).val() == $jq(this).data('populate_text')) {
						$jq(this).val('');
					}
				});
				$jq(this).blur(function() {
					if ($jq(this).val() == '') {
						$jq(this).val($jq(this).data('populate_text'));
					}
				});
			}
		});
	}	
}


$jq(document).ready(function() {
monkeyUtil.init();
});


