menu = Class.create();
menu.prototype = {
  getPage:	'',
  loadcount: '',
  loaded: new Array(),
  checking: false,
  checkInterval: '',
  splashLoaded: false,
  clickdisabled: false,
  colorbg: '',
  subMenuMO: false,
  subMenuInterval: '',
  sendTo: '',
  
  // Constructor
  initialize: function(menu_items)
  {
     //TODO: generate menu automatically from array
     
		//Wait for window to load
	    Event.observe(window, 'load', function() {
	    
	     // Are we loading another page?
	     if ((location.hash != "")&&(location.hash != "#"))
	     {
	     	// Loading specified page first
	     	// Move content_target off screen and hide
	     	$('content_target').style.left = '-5000px';
	     	
	     	// Load content
	     	menu.click(location.hash.substr(1),'#fff');
	     }
	      else
	     {
			// Loading splash
			//start checking if the splash image is loaded
	     	document.images[0].addEventListener('load',menu.splashloaded(),false);
	     }
		});
  },
  
  fade: function(type, highlighted)
  {
  	if (type == 'in')
  	{
  		//Fade all menu items, except highlighted
  		if (highlighted != 'home')	{	new Effect.Opacity($('home'), { from: 1.0, to: 0.5, duration: 0.2 });	}
  		if (highlighted != 'exhi')	{	new Effect.Opacity($('exhi'), { from: 1.0, to: 0.5, duration: 0.2 });	}
  		if (highlighted != 'inta')	{	new Effect.Opacity($('inta'), { from: 1.0, to: 0.5, duration: 0.2 });	}
  		if (highlighted != 'info')	{	new Effect.Opacity($('info'), { from: 1.0, to: 0.5, duration: 0.2 });	}
  		if (highlighted != 'past')	{	new Effect.Opacity($('past'), { from: 1.0, to: 0.5, duration: 0.2 });	}	
  	}
  	 else
  	{
  		// Restore all menus
  		if (highlighted != 'home')	{	new Effect.Opacity($('home'), { from: 0.5, to: 1.0, duration: 0.2 });	}
  		if (highlighted != 'exhi')	{	new Effect.Opacity($('exhi'), { from: 0.5, to: 1.0, duration: 0.2 });	}
  		if (highlighted != 'inta')	{	new Effect.Opacity($('inta'), { from: 0.5, to: 1.0, duration: 0.2 });	}
  		if (highlighted != 'info')	{	new Effect.Opacity($('info'), { from: 0.5, to: 1.0, duration: 0.2 });	}
  		if (highlighted != 'past')	{	new Effect.Opacity($('past'), { from: 0.5, to: 1.0, duration: 0.2 });	}
  	}
  },
  
  click: function(section, color)
  {
  	var color = (color == null) ? "#fff" : color; //default colour to white
  	if (!this.clickdisabled)
  	{
  		//disable reaction to clicks for a while.
  		this.clickdisabled = true;
  		
  		//set the hash for the page for bookmarking purposes
  		location.hash = section;
  		
		switch(section)
		{
		case 'home':
		  this.loadElement('home', color);
		  break;    
		case 'exhi':
		  this.loadElement('exhi', color);
		  break;
		case 'inta':
		  this.loadElement('inta', color);
		  break;   
		case 'info':
		  this.loadElement('info', color);
		  break;   
		case 'past':
		  this.loadElement('past', color);
		  break;   
		  
		//hat pages  
		case 'chronometer':
		  this.loadElement('chronometer', color);
		  break;  
		   
		case 'tansihat':
		  this.loadElement('tansihat', color);
		  break;   

		case 'velocity':
		  this.loadElement('velocity', color);
		  break;   

		case 'resolution':
		  this.loadElement('resolution', color);
		  break;   

		case 'ranchers':
		  this.loadElement('ranchers', color);
		  break;   
		  
		case 'deception':
		  this.loadElement('deception', color);
		  break;   
		  
		case 'further':
		  this.loadElement('further', color);
		  break;  
		  
		//contact pages
		case 'contact-paul':
		  this.loadElement('contact-paul', color);
		  break;  
		  
		case 'contact-':
		  this.loadElement('contact-', color);
		  break;  
		  
		case 'contact-finch':
		  this.loadElement('contact-finch', color);
		  break;  
		  
		case 'contact-robert':
		  this.loadElement('contact-robert', color);
		  break;  
		  
		case 'contact-ross':
		  this.loadElement('contact-ross', color);
		  break;  
		  
		case 'contact-mike':
		  this.loadElement('contact-mike', color);
		  break;  
		  
		  
		case 'contact-jaz':
		  this.loadElement('contact-jaz', color);
		  break;  
		  
		case 'contact-mccann':
		  this.loadElement('contact-mccann', color);
		  break;  

		case 'contact-kev':
		  this.loadElement('contact-kev', color);
		  break;  

		case 'contact-jen':
		  this.loadElement('contact-jen', color);
		  break;  

		case 'contact-steve':
		  this.loadElement('contact-steve', color);
		  break;  

		case 'contact-simon':
		  this.loadElement('contact-simon', color);
		  break;  

		case 'contact-baris':
		  this.loadElement('contact-baris', color);
		  break;  
		  
		  
		default:
		  alert('The page you are looking for does not exist.\nClick OK to go to the homepage.');
		  this.loadElement('home', '#000');
		  break;
		}
	}
  },
  
  enableclick: function()
  {
  	//wait a wee while
  	setTimeout("menu.clickdisabled = false;", 100);

  },
  
  loadElement: function(what,color)
  {
  	// changes display and performs AJAX call
  	
  	//set and reset global variables
  	this.getPage = what;
  	this.loaded = new Array();
  	this.checking = false;
  	this.colorbg = color;
  	
  	// fade loader into view
  	new Effect.Opacity($('loading_1'), { from: 0, to: 1.0, duration: 0.5 });
  	
  	//move current content off screen and fade out
  	new Effect.Move($('content_target'), {mode:'absolute',x:-500, duration: 0.5});
  	new Effect.Opacity($('content_target'), { from: 1.0, to: 0, duration: 0.5, afterFinish: this.showContent });
  	
  },
  
  showContent: function()
  {
  	//replace content
   
	new Ajax.Request('index.php',
	  {
	    method:'get',
	    parameters: {page: menu.getPage},
	    onSuccess: function(transport){
	      var response = transport.responseText || "no response text";
	      
	      $('content_target').innerHTML = response;
	      
	      //examine new content for images and wait for them to load
		  //add a listener for each image on the page, but not the loader at the end
		  menu.setlc(document.images);
		  		  
		  //if there are images, deal with them, if not then simply load content
		  if (document.images.length != 4)
		  {
			  for (i=0;i<(document.images.length-4);i++)
			  {
			  	if(document.images[i].addEventListener)
			  	{
			  		document.images[i].addEventListener('load',menu.imageloaded,false);
			  	}
			  	 else
			  	{
			  		document.images[i].attachEvent('onload',menu.imageloaded);
			  	}
			  }
		  }
		   else
		  {
		  	menu.loaded = new Array('');
		  	menu.loadcount = 1;
		  	menu.checklc();
		  }
	    },
	    onFailure: function(){
	    		
	    		$('loading_1').innerHTML = '<img src="images/structure/error.gif" />Oops. Something went wrong.';
	    		new Effect.Highlight($('loading_1'), { startcolor: '#FF6347' });
	    		
	    }
	  });
  },
  
  
  setlc: function(count)
  {
  	this.loadcount = document.images.length - 4;
  },
  
  imageloaded: function(element)
  {
	//add to loaded array
	menu.array_push('loaded');
	//start checking if not already started
	if (!menu.checking)
	{
		menu.checking = true;
		menu.checkInterval = setInterval(menu.checklc,1000);
  	}
  },
  
  checklc: function()
  {
	//compare loadcount to loaded
	if (menu.loadcount == menu.loaded.length)
	{
		//all images loaded, stop checking
		clearInterval(menu.checkInterval);
		
		//remove listeners
	    for (i=0;i<(document.images.length-1);i++)
	    {
			if(document.images[i].removeEventListener)
			{
				document.images[i].removeEventListener('load',menu.imageloaded,false);
			}
			 else
			{
				document.images[i].detachEvent('onload',menu.imageloaded);
			}
		}
		
		//and load content!

		//rebuild lightbox list
		//lightbox.updateImageList();
		
		//fade loader out
		new Effect.Opacity($('loading_1'), { from: 1.0, to: 0, duration: 0.5 });
		  	
		//move content on screen
		new Effect.Move($('content_target'), {mode:'absolute',x:0, duration: 0.5});
		new Effect.Opacity($('content_target'), { from: 0, to: 1.0, duration: 0.5, afterFinish: menu.enableclick });
	  	if(document.body.morph)
	  	{
	  		document.body.morph('background:' + menu.colorbg + ';', {duration: 0.2 });
	  	}
	  	 else
	  	{
	  		document.body.style.background = menu.colorbg;
	  	}

	}
  },
  
  array_push:function(pushwhat)
  {
  	nextid = this.loaded.length
	this.loaded[nextid] = pushwhat;
  },
  
  splashloaded:function()
  {
   	new Effect.Opacity($('loading_1'), { from: 1.0, to: 0, duration: 0.5 });
  	new Effect.Opacity($('content_target'), { from: 0, to: 1.0, duration: 0.5 });
  },
  
  
  
  
  showMug: function(obj,name)
  {
  	//show face
  	new Effect.Opacity(obj, { from: 0, to: 1, duration: 0.2 });
  	
  	//show name
  	$('name_catcher').innerHTML = ' // ' + name;
  },
  
  hideMug: function(obj)
  {
  	new Effect.Opacity(obj, { from: 1, to: 0, duration: 0.2 });
  	$('name_catcher').innerHTML = '';
  },
  
  subMenu: function(showhide)
  {
  	if (showhide == 'show')
  	{
  		//show the submenu
  		this.subMenuMO = true;
  		setTimeout("$('submenu').morph('height:160px;bottom:20px;',{duration:0.2});",500);
  	}
  	 else
  	{
  		//hide the submenu
  		this.subMenuMO = false;
  		this.subMenuInterval = setInterval(menu.hideSubmenu,500);

  	}
  },
  
  hideSubmenu: function()
  {
  	if (menu.subMenuMO == false)
  	{
  		//clear to close the menu
  	  	$('submenu').morph('height:1px;',{duration:0.2});
  		$('submenu').morph('bottom:0px;',{duration:0.2});
  		//stop interval
  		clearInterval(menu.subMenuInterval);
		
  	}
  },
  
  forceHide: function()
  {
  	//hide the menu after 3 seconds if not disturbed by other factors
  	//setTimeout("if ()",1000);
  },
  
  sendEmail: function(to)
  {
  	//set and reset global variables
  	this.checking = false;
  	this.sendTo = to;
  	
  	// fade loader into view
  	new Effect.Opacity($('loading_1'), { from: 0, to: 1.0, duration: 0.5 });
  	
  	//move current content off screen and fade out
  	new Effect.Move($('content_target'), {mode:'absolute',x:-500, duration: 0.5});
  	new Effect.Opacity($('content_target'), { from: 1.0, to: 0, duration: 0.5, afterFinish: menu.sendEmail2});
  },
  
  
  sendEmail2: function()
  {
	var from_name  = $('fromName').value;
	var from_email = $('fromEmail').value;
	var subject_  = $('subject').value;
	var body_     = $('body').value;
	
	new Ajax.Request('index.php',
	  {
	    method:'post',
	    parameters: {sendto: menu.sendTo, fromName: from_name, fromEmail: from_email, subject: subject_, body: body_},
	    onSuccess: function(transport){
	      var response = transport.responseText || "no response text";
	      
	      $('content_target').innerHTML = response;
		  	menu.loaded = new Array('');
		  	menu.loadcount = 1;
		  	menu.checklc();
	    },
	    onFailure: function(){
	    		
	    		$('loading_1').innerHTML = '<img src="images/structure/error.gif" />Oops. Something went wrong.';
	    		new Effect.Highlight($('loading_1'), { startcolor: '#FF6347' });
	    		
	    }
	  });
  }
  
}

var menu = new menu('');
