var _functions = {
	/*
	 * Merge a Hash 
	 *
	 */
	mergeObject: function(a, b) {
		for (prop in a) { 
		   if (prop in b) { continue; }
		   b[prop] = a[prop];
		}
		return b;
	},
	/*
	 * Make query string from Hash/Array
	 *
	 */
	arrayToQueryString: function(array, name) {
		var string = "";
		for (i = 0; i < array.length; i++) {
			string += name +'[]='+ array[i] +'&';
		}
		return string.replace(/&$/, '');
	},
	/*
	 * Merge two query strings together
	 *
	 */
	mergeQueryString: function(a, b) {
		return a +'&'+ b;
	},
	/*
	 * Locate DOM ancestor by class name
	 *
	 */
	locateAncestorByClass: function(elementID, classID) {
		var a = elementID.ancestors();
		for (i = 0; i < a.length; i++) {
			if (a[i].className == classID) {
				return a[i];
			}
		}
	},
	/*
	 * Locate DOM ancestor by ID
	 *
	 */
	locateAncestorByID: function(elementID, id) {
		var a = elementID.ancestors();
		for (i = 0; i < a.length; i++) {
			if (a[i].id == id) {
				return a[i];
			}
		}
	},
	setBackgroundColor: function(e,c) {
		$(e).style.backgroundColor = c;
	},
	addBookmark: function() {
		if(document.all)
		window.external.AddFavorite(location.href, 'Unlock iPhone for $19.95 - unlockediphones.com');
		else if(window.sidebar)window.sidebar.addPanel
		('Unlock iPhone for $19.95 - unlockediphones.com',location.href,'');
	}
};
var hpClick = {
	element: null,
	lastClick: 0,
	baseID: 0,
	sfText: "",
	quads: [],
	pages: [],
	toggle: function(target) {
		hpClick.menus = $$("div#ibuy div.hbuy");
		hpClick.quads = $$("div#imenu div.htitle div.himage");
		hpClick.baseID = target.id.replace(/^.*-/, "");
		hpClick.quads.each(function(e) {
			e.hide();
		});
		if (hpClick.lastClick != 0) {
			$('f-'+ hpClick.lastClick).className = 'htitle';
		}
		hpClick.menus.each(function(e) {
			e.hide();
			if (e.id == 'q-'+ hpClick.baseID) {
				e.show();
				$('f-'+hpClick.baseID).className = 'htitle-selected';
				hpClick.lastClick = hpClick.baseID;
			}
		});
	}
};

Event.observe(document, 'click', function(event) {
	var target = event.element();
	var id = target.id;
	var hpq = _functions.locateAncestorByClass(target,'htitle');
	if(hpq) {
		hpClick.toggle(hpq);
	}

	if (target.className == 'shutlink') {
		var toggleDiv = 'ff-'+ id.replace(/^[WM]-(2G|3G|3Gs)-(.*)$/, "$1");
		$(toggleDiv).update("<div class='load'><h3>Loading...</h3><img src='/images/load.gif'></div>");
		new Ajax.Request('/getform/'+ id, {
			method: 'get',
			onSuccess: function(transport) {
				$(toggleDiv).update(transport.responseText);
			}
		});
	}

	if (target.id == 'BM') {
		_functions.addBookmark();
	}
	if (target.id == 'EF1') {
		Element.toggle('EF');
	}

	if (target.id == 'EFs') {
		if ( $('sft').value == "" || $('sff').value == "" || $('sfn').value == "") {
			alert('Please fill out all fields to send your invitation.');
			return false;
		}

		$('EFl').show();
		new Ajax.Request('/send_friend/', {
			parameters: { name: $('sfn').value, to: $('sft').value, from: $('sff').value },
			onSuccess: function(transport) {
				$('EFl').hide();
				alert('Your invitation has been sent to '+ $('sft').value +'.');
				$('sft').value = "";
				$('sff').value = "";
				$('EF').hide();
			}
		});		
	}
});

function memberUpgrade(f) {
	document.location.href = '/members/?download='+ f;
}
