

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

var preClicked = gup( 'page' );
if (preClicked == '')
	preClicked = 0;
var menuIsBusy=0;
var ajaxIsBusy = 0;

window.addEvent('domready', function(){
	var szNormal = 30, szSmall  = 25, szFull   = 300, szSemi = 50;
 
	var kwicks = $$("#kwicks .kwick");
	var fx = new Fx.Elements(kwicks, {wait: false, duration: 700, transition: Fx.Transitions.Back.easeOut});
	kwicks.each(function(kwick, i) {
		kwick.addEvent("click", function(event) {
			url = kwick.getProperty('href');
			menuIsBusy=1;
			var o = {};
			o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
			kwicks.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("width").toInt();
					if(w != szSmall) o[j] = {width: [w, szSmall]};
				}
			});
			navigate(i,url);
			fx.start(o).chain(function(){
				menuIsBusy = 0;
				if (ajaxIsBusy == 0 && menuIsBusy == 0) {
					fadeMainIn();
				}
			})
			return false;
		});
		
	} );
	var temp = preClicked + 1;	
	
});


function navigate(i,url) {
		document["logo"].src = "/assets/img/logoanim.gif";
		
		startSlideshow(i);
	
		stopSlideshow(preClicked);		
		preClicked = i;
		getText(url);
		setTitle(i);
	
	
}

function fadeMainIn() {	
	var fx = new Fx.Styles($('main'), {duration:500, wait:false});
	fx.start({
		'opacity': 1
	});
	var fx = new Fx.Styles($('title'), {duration:500, wait:false});
	fx.start({
		'opacity': 1
	}).chain(function(){
		navigationFinished();		   
	});
	
}

function AJAXFinished() {
	ajaxIsBusy = 0;
	if (ajaxIsBusy == 0 && menuIsBusy == 0) {
		fadeMainIn();
	}
}

function navigationFinished() {
	document["logo"].src = "/assets/img/logo.gif";
}

function getText(url) {
	//fadeMainOut();
	
	var fx = new Fx.Styles($('main'), {duration:500, wait:false});
	
	ajaxIsBusy = 1;
	fx.start({
		'opacity': 0
	}).chain(function(){
		new Ajax(url, {
			method: 'get',
			update: $('mainText'),
			evalScripts: true,
			onComplete: AJAXFinished,
			onFailure: AJAXFinished	
		}).request();
	});
}

function setTitle(i) {
	titles = new Array('Welcome', 'Philospophy', 'Store', 'Coffee House', 'Premises', 'Tapas', 'Hampers');
	var fx = new Fx.Styles($('title'), {duration:500, wait:false});
	fx.start({
		'opacity': 0
	}).chain(function(){
		$('title').setHTML(titles[i]);
		this.start();
	});
}
