// process the click - by Bramus! - http://www.bram.us/
var dubaiOnTheMove	= {
	
	creditsLeft		: 0,		// Don't even think of changing this through the URL bar ... it won't work!
	omarsLeft		: 0,		// Don't even think of changing this through the URL bar ... it won't work!
	
	doClick		: function(whereTo, prizeId, el) {
		
					if ((dubaiOnTheMove.creditsLeft > 0) && (dubaiOnTheMove.omarsLeft > 0)) {
					
						// subtract a credit
							dubaiOnTheMove.creditsLeft		-= 1;
							dubaiOnTheMove.omarsLeft		-= 1;
							
						// set creditsLeft in html
							$('creditsLeft').innerHTML		= dubaiOnTheMove.creditsLeft;
							$('omarsLeft').innerHTML		= dubaiOnTheMove.omarsLeft;
							
						// process the click
						
							// CRUD, now they don't like the nifty ajax call anymore :(
							/*
							new Ajax.Updater (
								'prize' + prizeId, 
								'index.php?p=/vote/', 
								{	 
									onLoading	: function(req) {
										// Give notice that vote is being cast
										Element.update(
											$('prize' + prizeId), 
											"<img src=\"images/spinner.gif\" alt=\"\" title=\"\" /> Processing your click, please wait"
										);

									},
									
									onComplete	: function(req) {								
										// update voteform
										Element.update(
											$('prize' + prizeId), 
											req.responseText
										);
									},
									
									parameters	: 'id=' + prizeId,
									
									method		: 'post',
									
									asynchronous: true
								}
							);	
							*/
							
							el.onclick = function() { return false; }
							
							new Ajax.Request(
								'index.php?p=/vote/', 
								{
									method:'post', 
									postBody: "id="+ prizeId,
									onSuccess:	function(t) {
										window.location.replace(whereTo + "/playandwin/" + prizeId + "/" + (trimmer.trim(t.responseText)));
									}
								}
							);
					} else {
						alert("You don't have any credits or omars left");
					}
					
					return false;
	},
	
	setLang		: function (lang, whereto) {
										
					new Ajax.Request(
						'index.php?p=/setlang/', 
						{
							method:'post', 
							postBody: "lang="+ lang +"&whereto=" + whereto,
							onSuccess:	function(t) {
								// fake reload, cos otherwise POST would be redone (if there was any at all)
								window.location	= window.location.href;
							}
						}
					);
	},
	
	gameWon		: function(userId, whereTo, prizeId) {
					new Ajax.Request(
						'index.php?p=/setGameWon/', 
						{
							method:'post', 
							postBody: "id="+ userId,
							onSuccess:	function(t) {
								new Ajax.Request(
									'index.php?p=/vote/', 
									{
										method:'post', 
										postBody: "id="+ prizeId,
										onSuccess:	function(t) {
											window.location.replace(whereTo + "/playandwin/" + prizeId + "/" + (trimmer.trim(t.responseText)));
										}
									}
								);
							}
						}
					);
	}
}

// trimmer - by Bramus! - http://www.bram.us/
var trimmer		= {
	LTrim		: function (value) {
					var re = /\s*((\S+\s*)*)/;
					return value.replace(re, "$1");	
	},

	RTrim		: function (value) {
					var re = /((\s*\S+)*)\s*/;
					return value.replace(re, "$1");
	},
	
	trim		: function (value) {
					return trimmer.LTrim(trimmer.RTrim(value));
	}
}

// displays the messageoverlay - by Bramus! - http://www.bram.us/
var messageOverlay	= {		
	overlay			: null,		
	overlaycontent	: null,		
	closeimage		: '',		
	loading			: "<img src=\"images/bar.gif\" alt=\"\" title=\"\" width=\"220\" height=\"19\" style=\"display: block; position: absolute; left: 50%; top: 50%; margin-left: -110px; margin-top: -10px;\" />",		
	_before			: "",
	_after			: "",		
	
	showMessage		: function(content, height) {
						if (messageOverlay.overlay && messageOverlay.overlaycontent) {
							if ((content == "") || (content == undefined)) {
								messageOverlay.overlaycontent.innerHTML		= messageOverlay.closeimage + messageOverlay.loading;
							} else {
								messageOverlay.overlaycontent.innerHTML		= messageOverlay.closeimage + messageOverlay._before + content + messageOverlay._after;
							}
							messageOverlay.overlay.style.display		= 'block';
							messageOverlay.overlaycontent.style.display	= 'block';
							//Event.observe('btnClose', 'click', messageOverlay.observeBtnClose );
							messageOverlay.overlaycontent.style.height	= (height || 200) + 'px';
						}
	},
	
	updateMessage	: function(content, height) {
						if (messageOverlay.overlay && messageOverlay.overlaycontent) {
							if ((content == "") || (content == undefined)) {
								messageOverlay.overlaycontent.innerHTML		= messageOverlay.closeimage + messageOverlay.loading;
							} else {
								messageOverlay.overlaycontent.innerHTML		= messageOverlay.closeimage + messageOverlay._before + content + messageOverlay._after;
							}
							//Event.observe('btnClose', 'click', messageOverlay.observeBtnClose );
							messageOverlay.overlaycontent.style.height	= (height || 200) + 'px';								
						}
	},
	
	hideMessage		: function() {
						if (messageOverlay.overlay && messageOverlay.overlaycontent) {
							messageOverlay.overlaycontent.innerHTML		= "";
							messageOverlay.overlay.style.display		= 'none';
							messageOverlay.overlaycontent.style.display	= 'none';
						}			
	},
	
	observeBtnClose	: function() {
						messageOverlay.hideMessage();
						if ($('btnClose')) { Event.stopObserving('btnClose','click', messageOverlay.observeBtnClose); }
	},
	
	init			: function() {
						messageOverlay.overlay			= $('overlay');
						messageOverlay.overlaycontent	= $('overlaycontent');
	}

}
