// JavaScript Document
var _LB = null;
var _LB_HOME = null;
var _PAGECONTENT;
var _PAGECLASS;
var _CURPAGE;
var _PAGE_TOGGLE = false;
var _PAGEBACKUP;
var arrImages = Array();
function loadPage(url) {
	if (!_PAGE_TOGGLE) {
		_PAGE_TOGGLE = true;

		// get center
		var bodyWidth = document.viewport.getWidth();
		var loadWidth = 32;
		var loadOffset = (bodyWidth - loadWidth) / 2;
		$('loader').setStyle({ left: loadOffset + 'px', top: '200px' });
		z = new Effect.Appear('loader', { duration: 0.5, from: 0.0, to: 1.0, queue: 'end' });

		pageDestruct();

		_PAGEBACKUP = _PAGECLASS;
		_PAGECLASS = url.replace(/\.php/,'');
		var d = new Date();
		r = setTimeout("sendRequest('"+url+"', '"+d+"')", 500);
	}
}
function sendRequest(url, d) {

	var ajaxPage = new Ajax.Request('http://www.lickerish.ca/'+url+'?content-only=true&'+d, {
		method: 'get',
		onSuccess: function(transport) {
			var htmlDoc = transport.responseText;
			a = new Effect.Fade('loader', { duration: 0.5, from: 1.0, to: 0.0, queue: 'end' }); 
			x = new Effect.Pulsate($('copy'), { pulses: 1, duration: 2.0, queue: 'end' });
			_PAGECONTENT = htmlDoc;
			y = setTimeout('updateText()', 1500);
		},
		onFailure: function(transport) {
			alert('error');
			a = new Effect.Fade('loader', { duration: 0.5, from: 1.0, to: 0.0, queue: 'end' }); 
			_PAGECLASS = _PAGEBACKUP;
			updateText()
		}

	});
}
function updateText(txt) {

	$('copy').innerHTML = _PAGECONTENT;
	$('copy').className = '';
	$('copy').className = _PAGECLASS;
	pageConstr(_PAGECLASS);
	_PAGE_TOGGLE = false;
}

function pageDestruct() {
	switch(_CURPAGE) {
		case 'index':
			break;
		case 'gallery':
//			arrImages = Array();
//			window.onresize = null;
//			_LB = null;
			break;
		case 'contact':
			GUnload();
			break;
	}
}
function previewUpdatePosition(w) {
	var bodyWidth = document.viewport.getWidth();
	var imgWidth = w;
	var imgOffset = (bodyWidth - imgWidth) / 2;
	$('preview-container').setStyle({ left: imgOffset + 'px' });

	if (w > 333) {
		$('preview-container').setStyle({ top: '75px' });
	}
	else {
		$('preview-container').setStyle({ top: '5px' });
	}
}
function pageConstr(page) {
	_CURPAGE = page;
	switch(page) {
		case 'index':
		case '':
			if (_LB == null) {
				_LB = new Lightbox();
			}
			break;
		case 'gallery':
			if (_LB == null) {
				_LB = new Lightbox();
			}
			break;
		case 'contact':
			if (GBrowserIsCompatible()) {
				var map = new GMap2(document.getElementById("map"));
				map.setCenter(new GLatLng(49.278669, -123.127813), 15);

				var lickerishIcon = new GIcon(G_DEFAULT_ICON);
				lickerishIcon.image = "http://www.lickerish.ca/images/lickerish-icon.png";
				gMarker = new GMarker(new GLatLng(49.278669, -123.128213), { icon: lickerishIcon });
				map.addOverlay(gMarker);

			}
			break;
		case 'reservation':
			Event.observe('rsvp-request', 'click', function(e) {
				var req = ''; 
				$('rsvp-request').disabled = true; 
				$$('div#rsvp-form input').each(function(i) {
					req += encodeURI(i.getAttribute('id')) + '=' + encodeURI(i.value) + '&';
				});
				req += encodeURI('Special Requests') + '=' + encodeURI($('Special Requests').value);

				var ajaxPage = new Ajax.Request('http://www.lickerish.ca/process.php?'+req, {
					method: 'get',
					onSuccess: function(transport) {
						if (transport.responseText == 'true') {
							alert('Thanks for your request! You will be contacted soon to verify your reservation.');
						}
						else {
							alert('Whoops! There was a problem making your reservation. If this persists, please contact us at 604.696.0725 and we will be happy to make your reservation.');
						}
					},
					onFailure: function(transport) {
						alert('Whoops! There was a problem making your reservation. If this persists, please contact us at 604.696.0725 and we will be happy to make your reservation.');
					}

				});
			});
			break;
		case 'menu':
			_SP_TOGGLE = false;
			Event.observe('specials','click', showSpecials);
			Event.observe('close', 'click', hideSpecials); 
			$('specials').setStyle({ cursor: 'pointer' });

			browser = navigator.appName;
			if (browser.match('Microsoft')) {
				$('close').setStyle({ margin: '0 0 0 110px' });
			}
//			(navigator.appName.match('Microsoft') != null) ? $('close').setStyle({ margin-left: '110px' }) : '';

			break;
	}
}
function showSpecials() {
	z = new Effect.Fade('spbackground', { duration: 0.5, from: 1.0, to: 0.0, queue: 'end' });
	y = new Effect.Morph('specials', { style: { width: '300px' }, queue: 'end' });
	$('spdata').appear({ duration: 0.5, from: 0.0, to: 1.0, queue: 'end' });
	Event.stopObserving('specials','click',showSpecials);
	$('specials').setStyle({ cursor: 'default' });
}
function hideSpecials() {
	x = new Effect.Fade('spdata', { duration: 0.5, from: 1.0, to: 0.0, queue: 'end' });
	w = new Effect.Morph('specials', { style: { width: '50px' }, queue: 'end' });
	$('spbackground').appear({ duration: 0.5, from: 0.0, to: 1.0, queue: 'end' });
	t = setTimeout('attachSpecials()', 1500);
}
function attachSpecials() {
	Event.observe('specials','click', showSpecials);
	$('specials').setStyle({ cursor: 'pointer' });
}