var Projects = new Class(
{
	initialize: function(mcLocations)
	{
		//mcLocations is an Array that contains the urls of the slideshow movieclips
		this.projectSliders = new Array();
		this.nProjects = 0;
		this.projectList = new Fx.Slide('projectlist', {duration: 150, mode: 'horizontal'});
		//create the projects
		this.createProjects(mcLocations.length);
		//create the slideshow
		this.theSlideShow = new FlashSliders(mcLocations);
	},
	
	createProjects: function(amount)
	{	    
		for (i=1; i<=amount; i++)
		{
			//add the slide function to each project,
			//and add the link to each link
			this.projectSliders[i] = new Fx.Slide('p'+i, {duration: 500});
			$('l'+i).onclick = this.flipProject.bindAsEventListener($('l'+i),[this,i]);
			
			//hide projects except the first	
			if (i!=1)
			{
				this.projectSliders[i].hide();
			}
			
			this.nProjects++;
		}
	},

	showProjectList: function(t,pClicked)
	{
		t.projectList.slideIn();
		t.projectSliders[pClicked].removeEvent('onComplete',t.showProjectList);
	},
	
	flipProject: function(e,t,pClicked)
	{
		//use bindAsEventListener to attach this function to an event
		//e = the onClick Event
		//t points to an instance of the Projects class
		//pClicked represents the number of the clicked project
		e = new Event(e);
		
		//prevent mouseClicks, hide the projectList.
		//The project will fire the slideIn event later on.
		t.projectList.slideOut();
		
		for (i=1; i<=t.nProjects; i++)
		{
			if (i==pClicked)
			{
				t.projectSliders[i].addEvent('onComplete', t.showProjectList.pass([t, i]));
				
				//show the clicked project
				t.projectSliders[i].slideIn();
				//highlight
				if (!$('l'+i).hasClass('content_activelink'))
				{
					$('l'+i).addClass('content_activelink');
				}
				//change the flash slideshow movieclip
				t.theSlideShow.replaceSlideShow(i-1);
				
				//alert(t.theSlideShow);
			}
			else
			{
				//hide the project
				t.projectSliders[i].slideOut();
				//remove highlight
				if ($('l'+i).hasClass('content_activelink'))
				{
					$('l'+i).removeClass('content_activelink');
				}
			}		
		}	
		
		//prevent further handling of the onclick 
		//that is, prevent that the browser goes to href
		e.stop();
	}

}); /* end of class Projects */


var FlashSliders = new Class(
{
	initialize: function(ar)
	{
		//ar should be a array that contains the locations of the swf movies (in string format)
		this.fsLocations = ar;
		this.fsContainer = $('slideshow');
	},
	
	replaceSlideShow: function(nr)
	{
		this.fsContainer.setHTML(this.buildString(this.fsLocations[nr]));
	},
	
	buildString: function(file)
	{
		s = '';
		s = s + '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="602" height="385" title="Slideshow">';
		s = s + '<param name="movie" value="' + file + '" />';
		s = s + '<param name="quality" value="high" /><param name="wmode" value="transparent">';
		s = s + '<embed src="' + file + '" width="602" height="385" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" wmode="transparent"></embed></object>';
		return s;
	}
}); /* end of class FlashSliders */

var FormHandler = new Class(
{
	initialize: function()
	{
		$('name').addEvent('blur', this.onBlur.pass([$('name'), 'name']));
		$('name').addEvent('focus', this.onFocus.pass([$('name'), 'name']));
		$('email').addEvent('blur', this.onBlur.pass([$('email'), 'email']));
		$('email').addEvent('focus', this.onFocus.pass([$('email'), 'email']));
		$('message').addEvent('blur', this.onBlur.pass([$('message'), 'message']));
		$('message').addEvent('focus', this.onFocus.pass([$('message'), 'message']));
		//ajax handler
		$('formEnquiry').addEvent('submit', this.submitForm.bindWithEvent($('formEnquiry'),[this]));		
	},
	
	onBlur: function(t,defValue)
	{
		//if empty, insert defValue
		if (t.value.trim()=='') t.value=defValue;
	},
	
	onFocus: function(t,defValue)
	{
		//if defValue remove defValue
		if (t.value==defValue) t.value = '';
	},
	
	resetForm: function()
	{
		//put the defaultvalues back in the form
		$('name').value = 'name';
		$('email').value = 'email';
		$('message').value = 'message';
	},
	
	onFormComplete: function(t)
	{
		//called on completion of the ajax call
		$('contact_confirm').removeClass('ajax-loading');
		
		s = $('contact_confirm').getText();
		
		//if the first char of the response is #, the form is sent succesfully
		//used this method to make the form post more unobstrusive
		if (s.charAt(0) == '#')
		{
			//succes!, remove #
			s = s.substr(1);

			//clear the form
			t.resetForm();					
		}
		
		//add return link
		s = s + '<br /><br /><a href="#" id="formbacklink">Go back to the form</a>';
		$('contact_confirm').setHTML(s);
		
		//attach an eventhandler to the backlink
		$('formbacklink').addEvent('click', function(e){
			e = new Event(e);
			
			//show the form and hide the ajaxresult layer
			$('formEnquiry').setOpacity(1);
			$('contact_confirm').setOpacity(0);
						
			//prevent the click event
			e.stop();
		});		
		
	},
	
	submitForm:function(e,t)
	{
		//handles the submit event of the form
		
		//Prevent the submit event
		new Event(e).stop();
		
		//attach the loading indicator class
		$('contact_confirm').empty().addClass('ajax-loading');		
		
		//hide the form and show the ajaxresult layer
		$('formEnquiry').setOpacity(0);
		$('contact_confirm').setOpacity(1);
  
		//send takes care of encoding and returns the Ajax instance.
		//onComplete removes the spinner from the log.
		//update represents the html element (div) to insert the data into
		this.send({
			update: $('contact_confirm'),
			onComplete: t.onFormComplete.pass([t])
		});		
	}

}); /* end of class FormHandler */

