window.addEvent('domready', function()
{
	
	function slide_in(tab_name,end_pos)
	{
			var document_size = $(tab_name).getPosition();
			var start_x = document_size.x;
			
			var tab_out = new Fx.Morph($(tab_name), {duration: 1000, transition: Fx.Transitions.linear});
			tab_out.start({ 'left': [start_x,end_pos] });
	
	}
	
	function slide_out(tab_name,end_pos)
	{
		var document_size = $(tab_name).getPosition();
		var start_x = document_size.x;
		
		var tab_out = new Fx.Morph($(tab_name), {duration: 1000, transition: Fx.Transitions.linear});
		tab_out.start({ 'left': [start_x,end_pos] });	
	}
	
	
	$('total_fav_items').load('fav_items.php');						
	
	$('about_page').addEvents({
		
		'click': function() 
		{
			$('about_text').load('about.htm');
			$('about_text').setStyle('top','25px');
		
				$('load_content').fade('hide');
				$('image_content').fade('hide');
			
				$('fade_background').fade(0.2,1);
			slide_in('advertising_tab_content','-1055');
			$('advertising_tab').setProperty('title','1');	
			
			slide_in('information_tab_content','-1055');
			$('information_tab').setProperty('title','1');
			
			slide_in('illustration_tab_content','-1055');
			$('illustration_tab').setProperty('title','1');
			
			slide_in('imi_tab_content','-1055');
			$('imi_tab').setProperty('title','1');
			
			slide_in('typographic_tab_content','-1055');
			$('typographic_tab').setProperty('title','1');
			
			
		}
	});
	
	
	$('fav_page').addEvents({
		
		'click': function() 
		{
			slide_in('advertising_tab_content','-1055');
			$('advertising_tab').setProperty('title','1');	
			
			slide_in('information_tab_content','-1055');
			$('information_tab').setProperty('title','1');
			
			slide_in('illustration_tab_content','-1055');
			$('illustration_tab').setProperty('title','1');
			
			slide_in('imi_tab_content','-1055');
			$('imi_tab').setProperty('title','1');
			
			slide_in('typographic_tab_content','-1055');
			$('typographic_tab').setProperty('title','1');
			
				$('fade_background').fade(0.2);
			
			
			var req = new Request.HTML({url:'favourites.php', method:'get',
								   
			onComplete: function(html) {
				
				$('load_content').fade('hide'); 
				$('load_content').fade('in');
				$('load_content').set('html', '');	
				$('load_content').adopt(html);
				load_from_fav();	
					
				$('image_content').fade('hide');
				$('image_content').set('html', '');	
				$('image_content').setStyles({
								top: 0,
								width:0,
								height:0, 
								'margin-left': 'auto',
								'margin-right':'auto'
							});	
				$('wrapper').fade('show');
				
				$('status_message').fade('hide');
$('send_email').addEvents({
							
'click': function() 
{ 
				
	var email_address = $('email_address').get('value'); 	
				 
	var myRequest = new Request
	({ 
			method: 'get', 
			url: 'es.php',
			data: 'email_address='+email_address
	}).send();
			$('status_message').fade('hide');
			$('status_message').set('html', 'Work been sent');
			$('status_message').fade('in');

	}
	});	
			},
			onFailure: function() {
				$('load_content').set('text', 'The request failed.');
			}
			});
			
			req.send();	 
		}
	 });
	
	
	$('advertising_tab').addEvents({
		
		'click': function() 
		{
			var status =  this.get('title');
			if(status ==2)
			{
				slide_in('advertising_tab_content','-1055');
				this.setProperty('title','1');	
					
			}
			
			if(status ==1)
			{
				slide_out('advertising_tab_content',0);
				this.setProperty('title','2');				
			}
		 }
	
	});

		
	
	$('information_tab').addEvents({
		
		'click': function() 
		{
			var status =  this.get('title');
			if(status ==2)
			{
				slide_in('information_tab_content','-1055');
				this.setProperty('title','1');				
			}
			
			if(status ==1)
			{
				slide_out('information_tab_content',0);
				this.setProperty('title','2');				
			}
		 }
		
	});
	
	$('illustration_tab').addEvents({
		
		'click': function() 
		{
			var status =  this.get('title');
			if(status ==2)
			{
				slide_in('illustration_tab_content','-1055');
				this.setProperty('title','1');				
			}
			
			if(status ==1)
			{
				slide_out('illustration_tab_content',0);
				this.setProperty('title','2');				
			}
		 }
	});
	
	$('imi_tab').addEvents({
		
		'click': function() 
		{
			var status =  this.get('title');
			if(status ==2)
			{
				slide_in('imi_tab_content','-1055');
				this.setProperty('title','1');				
			}
			
			if(status ==1)
			{
				slide_out('imi_tab_content',0);
				this.setProperty('title','2');				
			}
		 }
		
	});
	
	$('typographic_tab').addEvents({
		
		'click': function() 
		{
			var status =  this.get('title');
			if(status ==2)
			{
				slide_in('typographic_tab_content','-1055');
				this.setProperty('title','1');				
			}
			
			if(status ==1)
			{
				slide_out('typographic_tab_content',0);
				this.setProperty('title','2');				
			}
		 }
	});
	
			// functions
				function load_from_fav()
			{
				$(document.body).getElements('a.view_img_fav').addEvents({
					'click': function() 
					{
							var url = this.get('href');
						
							var url_array = new Array();
							
							url_array = url.split('/');
							
							var page =  url_array[0];
							var section = url_array[1];
							var sub_section = url_array[2];
							var page_data = url_array[3];
							var page_img = url_array[4];
	  					
						slide_out(sub_section+'_tab_content',-1030);
						$(sub_section+'_tab').setProperty('title','1');
				
						var req1 = new Request.HTML({url:'student.php?id='+page_data+'&c='+sub_section, method:'get',
									   
						onComplete: function(html) {
							$('load_content').fade('hide');
							$('load_content').fade('in');
							$('load_content').set('html', '');	
							$('load_content').adopt(html);
							scan_student_images();
						},
						 onFailure: function() {
							$('load_content').set('text', 'The request failed.');
						}
						}).send();
					
						
						var req = new Request.HTML({url:'image_load.php?id='+this.get('rel'), method:'get',
								   
						onComplete: function(html) {
							$('image_content').fade('hide');
							$('image_content').fade('in');
							$('image_content').set('html', '');	
							$('image_content').adopt(html);
							$('wrapper').fade(0.2);
							resize_window();
							close_image();
							manage_fav();
						},
						 onFailure: function() {
							$('image_content').set('text', 'The request failed.');
						}
						}).send();
						 
					}
				});
			}
			
			
			
			function scan_student_images()
			{
				$(document.body).getElements('a.view_img').addEvents({
					'click': function() 
					{
						var req = new Request.HTML({url:'image_load.php?id='+this.get('rel'), method:'get',
								   
						onComplete: function(html) {
							$('image_content').fade('hide');
							$('image_content').fade('in');
							$('image_content').set('html', '');	
							$('image_content').adopt(html);
							$('wrapper').fade(0.2);
							resize_window();
							close_image();
							manage_fav();
						},
						 onFailure: function() {
							$('image_content').set('text', 'The request failed.');
						}
						}).send();
						 
					}
				});
			}
			
			function close_image()
			{
				$('close_image').addEvents({
					'click': function() 
					{
							$('image_content').fade('out');
							$('image_content').set('html', '');	
											
							$('wrapper').fade(0.2,1);
							$('image_content').setStyles({
								top: 0,
								width:0,
								height:0, 
								'margin-left': 'auto',
								'margin-right':'auto'
							});	
							scan_student_images();
					}
				});
			}
			
			
			function manage_fav()
			{
					$('fav_icon').addEvents({
					'click': function() 
					{
						var fav = this.get('rel');
						var action = this.get('class');
						$('fav_icon').fade('out'); 
						
						if(action == 'del_fav')
						{
							var myRequest = new Request({method: 'get', url: 'fav_del.php'});
							myRequest.send('fav='+fav);
							myRequest.addEvents({'complete': function() 
								{
									$('total_fav_items').fade(0,1);
									$('total_fav_items').set('text','');
									
									$('fav_icon').fade('in');				
									$('fav_icon').setProperties({
										src: 'images/plus.png',
										alt: 'Add to favourites',
										'class': 'add_fav'
									});
									
									$('total_fav_items').load('fav_items.php');
									
								}
							});
								
						}
							else
						{
							var myRequest = new Request({method: 'get', url: 'fav_add.php'});
							myRequest.send('fav='+fav);
							myRequest.addEvents({'complete': function() 
								{
									$('total_fav_items').fade(0,1);
									$('total_fav_items').set('text','');
									
									$('fav_icon').fade('in');				
									$('fav_icon').setProperties({
										src: 'images/minus.png',
										alt: 'Remove from favourites',
										'class': 'del_fav'
									});
									
									$('total_fav_items').load('fav_items.php');
									
								}
							});
							
						}
					}
				});
			}
			
			function resize_window()
			{
				var imagesize = $('loaded_image').getSize();
				var image_height = imagesize.y;
				var image_width = imagesize.x; 
				$('image_content').setStyles({
					top: 20,
					width:image_width,
					height:image_height, 
					'margin-left': 'auto',
					'margin-right':'auto'
				});	
			}
	
	
	
			// student clicking
			$(document.body).getElements('a.click_link').addEvents({
			'click': function() 
			{
				
				$('fade_background').fade(0.2);
				
				$('load_content').fade('out');
				
				slide_in('advertising_tab_content','-1055');
				slide_in('information_tab_content','-1055');
				slide_in('illustration_tab_content','-1055');
				slide_in('imi_tab_content','-1055');
				slide_in('typographic_tab_content','-1055');
				
				slide_out(this.get('rel'),'-1030');
				
				
				 	
				var tab = this.get('rel').replace('_content','');
				
				var cat = this.get('rel').replace('_tab_content','');
				
				$(tab).setProperty('title','1');
				
				var req = new Request.HTML({url:'student.php?id='+this.get('title')+'&c='+cat, method:'get',
								   
					onComplete: function(html) {
						$('load_content').fade('hide');
						$('load_content').fade('in');
						$('load_content').set('html', '');	
						$('load_content').adopt(html);
						scan_student_images();
					},
					 onFailure: function() {
						$('load_content').set('text', 'The request failed.');
					}
					}).send();
				}
			  
			});
	
	
			
	// set up a check if they link to the page
	var page_var = location.hash.replace(/^#\/\//,'');
	
	var url_array = new Array();
	url_array = page_var.split('/');
	
	var page =  url_array[0];
	var section = url_array[1];
	var sub_section = url_array[2];
	var page_data = url_array[3];
	var page_img = url_array[4];
	 	
		
		if(section=='about')
		{
			$('about_text').load('about.htm');
			$('about_text').setStyle('top','25px');
				slide_in('advertising_tab_content','-1055');
			$('advertising_tab').setProperty('title','1');	
			
			slide_in('information_tab_content','-1055');
			$('information_tab').setProperty('title','1');
			
			slide_in('illustration_tab_content','-1055');
			$('illustration_tab').setProperty('title','1');
			
			slide_in('imi_tab_content','-1055');
			$('imi_tab').setProperty('title','1');
			
			slide_in('typographic_tab_content','-1055');
			$('typographic_tab').setProperty('title','1');
			
				//$('fade_background').fade(0);
				$('load_content').fade('hide');
				$('image_content').fade('hide');
		}
		if(section =='favourites')
		{
			slide_in('advertising_tab_content','-1055');
			$('advertising_tab').setProperty('title','1');	
			
			slide_in('information_tab_content','-1055');
			$('information_tab').setProperty('title','1');
			
			slide_in('illustration_tab_content','-1055');
			$('illustration_tab').setProperty('title','1');
			
			slide_in('imi_tab_content','-1055');
			$('imi_tab').setProperty('title','1');
			
			slide_in('typographic_tab_content','-1055');
			$('typographic_tab').setProperty('title','1');
			
				$('fade_background').fade(0.2);
			
			var req = new Request.HTML({url:'favourites.php', method:'get',
								   
			onComplete: function(html) {
				
				$('load_content').fade('hide'); 
				$('load_content').fade('in');
				$('load_content').set('html', '');	
				$('load_content').adopt(html);
				load_from_fav();		
				
				$('image_content').fade('hide');
				$('image_content').set('html', '');	
				$('image_content').setStyles({
								top: 0,
								width:0,
								height:0, 
								'margin-left': 'auto',
								'margin-right':'auto'
							});	
				$('wrapper').fade('show');
				
				$('status_message').fade('hide');
$('send_email').addEvents({
							
'click': function() 
{ 
				
	var email_address = $('email_address').get('value'); 	
				 
	var myRequest = new Request
	({ 
			method: 'get', 
			url: 'es.php',
			data: 'email_address='+email_address
	}).send();
			$('status_message').fade('hide');
			$('status_message').set('html', 'Work been sent');
			$('status_message').fade('in');

	}
	});	
			},
			onFailure: function() {
				$('load_content').set('text', 'The request failed.');
			}
			});
			
			req.send();	
		}
		
		if(typeof(page_data) != 'undefined')
		{
			var req = new Request.HTML({url:'student.php?id='+page_data, method:'get',
								   
			onComplete: function(html) {
				
				$('load_content').fade('hide');
				$('load_content').fade('in');
				$('load_content').set('html', '');	
				$('load_content').adopt(html);
				scan_student_images();		
			},
			onFailure: function() {
				$('load_content').set('text', 'The request failed.');
			}
		});
				
		req.send();	 
		slide_in(sub_section +'_tab_content','-1030');
 
		$(sub_section+'_tab').setProperty('title','1');
		
		$('fade_background').fade(0.2);
		
		
		
		
	}
	
		if(typeof(page_img) != 'undefined')
		{
			 
			$('wrapper').fade(0.2);
				
			$('image_content').fade('hide');
				
			var req = new Request.HTML({url:'image_load.php?id='+page_img, method:'get',
								   
			onComplete: function(html) {
				$('image_content').fade('in');
				$('image_content').set('html', '');	
				$('image_content').adopt(html);
				resize_window();
				close_image();
				manage_fav();
			},
			onFailure: function() {
				$('image_content').set('text', 'The request failed.');
			}
				
			}).send();
				
			scan_student_images();	
		} 
	
});

