// General JavaScript for Vintage Wedding Hire //

// Convert Hex Pairs to 
$fromHex = function(str,b){
	b = (b) ? b : 8;
	var hi = String("0123456789ABCDEF").split('');
	var hh = new Array();
	for (i in hi) {hh[hi[i]] = i;}
	str = str.replace(/[^0-9A-F]/gi,'');
	str = str.toUpperCase();
	str = (str.length%2) ? '0'+str : str;
	var deca = new Array();
	for (i = 0; i < str.length; i+=(b/4)) {
		var word = 0;
		for (j = 0; j < b/4; j++) {
			word = (word<<4)|(hh[str.charAt(i+j)]);
		}
		deca.push(word);
	}
	return deca;
}

// Deobfuscate Email Address
function remail(s,text){
	var a = $fromHex(s);
	var e = '';
	for (i in a) {e+=String.fromCharCode(a[i]);}
	if (typeof(text) != 'string') text = e;
	document.write('<a class="mailto" href="mailto:' + e + '">' + text + '</a>');
}

// Element Shortcut //
$get = function(id){return document.getElementById(id);}

// Add event listener workaround for IE //
$listener = function (o,e,f) {
	if (document.addEventListener) {
		o.addEventListener(e,f,false);	// Normal //
	} else {
		o.attachEvent('on'+e,f);		// IE //
	}
}

$parsequery = function(loc) {
	loc = (loc) ? loc : String(window.location);
	var queryobj = {};
	if (loc.indexOf('?') > -1) {
		var querystr = decodeURI(loc.substr(loc.search(/\?/)+1));
		var pairs = querystr.split("&");
		for (i in pairs) {
			var nameval = pairs[i].split("=");
			queryobj[nameval[0]] = nameval[1];
		}
	}
	return queryobj;
}

// Pre-load images //
var preload = new Array(
	'images/VWH-Badge-Icon-On.jpg',
	'images/VWH-Button-Navigation-On.jpg',
	'images/VWH-Navigation-On.jpg'
);
for (var i = 0; i < preload.length; i++) {
	new Image().src = preload[i];
}
