// JavaScript Document



var gallery_timer = false;



function gallery(container, thumb_container){

this.gallery_images= new Array();
this.gallery_state= 0;
this.gallery_offset= -1 ;
this.current_container = false;
this.backward_timout = false;
this.forward_timout = false;
this.img_timout = false;
this.current_image_info="";
	

	this.init = function(){
		
	var image_panel = $(container) ; 
	var img_array = this.gallery_images ;
	
	// load images into array
	
	var imgs = image_panel ? image_panel.select('.image_placeholder') : new Array() ;
	var i=0;
	

	imgs.each(function(n) {
		
		var img = n.select('img').first();
		
	
		var to_load= new Image();
		
		to_load.onload= function(img){
			
			var current_image = this;
			
			var chunks = current_image.src.split(/\//);
		
			var img_id= chunks[chunks.length-1].replace(/\.[a-z]+$/,'');
			
			var current_container = $(img_id);
			
			if(current_container) current_container.addClassName('loaded');
			
						
			}
		
		var chunks = img.src.split(/\//);
		
		var img_id= chunks[chunks.length-1].replace(/\.[a-z]+$/,'');
		
		to_load.src= img.src;	
					
		img_array[i] = n ;
		
		i++;
			
			});
	
	
		
		
		var thumb_panel = $(thumb_container) ; 
		
		var thumbs = thumb_panel ? thumb_panel.select('.image_placeholder') : new Array();
				
		thumbs.each(function(n) {
			
			var img = n.select('img').first();
			
		
			var to_load= new Image();
			
			to_load.onload= function(img){
				
				var current_image = this;
				
				var chunks = current_image.src.split(/\//);
			
				var img_id= chunks[chunks.length-1].replace(/\.[a-z]+$/,'');
				
				var current_container = $(img_id);
				
				if(current_container) current_container.addClassName('loaded');
				
							
				}
			
			var chunks = img.src.split(/\//);
			
			var img_id= chunks[chunks.length-1].replace(/\.[a-z]+$/,'');
			
			to_load.src= img.src;	
		
					
				});
			
		
		
		}// end init
	

	
	this.move_forward = function(offset){
		
		var last_offset = this.gallery_offset;
		
		var current_image = this.gallery_images[last_offset];
		var next_image = false; 
		
		if( typeof offset !=='undefined'  ){
		
		next_image =  this.gallery_images[offset]  ;
		
		this.gallery_offset= offset ;
		
			}else{
		
		next_image = last_offset+1 <  this.gallery_images.length ? this.gallery_images[last_offset+1] : this.gallery_images[0] ;
		
		if( last_offset+1 < this.gallery_images.length ) this.gallery_offset++; else  this.gallery_offset=  0  ;
		
			}
		
				
			if( current_image && !current_image.hasClassName('hidden') ) current_image.addClassName('hidden');
			if( next_image && next_image.hasClassName('hidden') ) next_image.removeClassName('hidden');
			
			if( current_image )  Effect.Fade(current_image.id);
			
			if( this.img_timout ){
				clearTimeout (this.img_timout);
			}
			
			if( next_image ) this.img_timout = setTimeout( 'Effect.Appear(\''+next_image.id+'\');', 1000 );
			
						 
		}
	
	
	this.move_backward = function(){
		
		var current_offset = this.gallery_offset;
		var current_image = this.gallery_images[current_offset];
		var previous_image = current_offset-1 > 0 ? this.gallery_images[current_offset-1] : this.gallery_images[this.gallery_images.length-1] ;
		
		if( this.gallery_offset-1 > 0 ) this.gallery_offset--; else  this.gallery_offset= this.gallery_images.length -1 ;
		
					
			if( current_image && !current_image.hasClassName('hidden') ) current_image.addClassName('hidden');
			if( previous_image && previous_image.hasClassName('hidden') ) previous_image.removeClassName('hidden');
			
			if( current_image )  Effect.Fade(current_image.id);
			
			if( this.img_timout ){
				clearTimeout (this.img_timout);
			}
			
			if( previous_image ) this.img_timout = setTimeout( 'Effect.Appear(\''+previous_image.id+'\');', 1000 );
						
		}
		
		


}// gallery object

function move_to(offset){
	
	setTimeout( 'gallery.move_forward('+offset+')', 200 ) ;
	
	}


function next_image(){
	
	setTimeout( 'gallery.move_forward()', 200 );
	
	}



function previous_image(){
	
	setTimeout( 'gallery.move_backward()', 200 ) ;
	
	}


function toggle_loop_state( controller ){


if( gallery.gallery_state == 0 ){
	
	controller.innerHTML = "<img src=\"images/btn_stop.jpg\" width=\"30\" height=\"30\" border=\"0\">" ;
	
	gallery.gallery_state = 1 ; 
	
	loop();
	
	}else{ 
	
	controller.innerHTML = "<img src=\"images/btn_play.jpg\" width=\"30\" height=\"30\" border=\"0\">" ;
	
	gallery.gallery_state= 0;
	
	}
	
	
	}// end toggle_loop_state



function loop(){
	
if( gallery_timer ){
			clearTimeout (gallery_timer);
		}

if(gallery && gallery.gallery_state == 1) gallery.move_forward();
		
if( gallery.gallery_state == 1) this.gallery_timer = setTimeout( 'loop()', 5000 ); 
	
	}
	


function switch_page(controller,direction){
	
	controller= $(controller);
	
	if(direction == 'next'){
		
			var current_page= $$('.thumb_page.selected').first();
			var next_page = current_page.next('.thumb_page') ? current_page.next('.thumb_page') : current_page.previous('.thumb_page.first') ;
			
			if( next_page.hasClassName('last')){
				
				 controller.removeClassName('next');	
			 
				 controller.addClassName('previous');	
				
				}else if(next_page.hasClassName('first')){
					
				 controller.removeClassName('previous');	
			 
				 controller.addClassName('next');	
					
					
					}
			
			current_page.removeClassName('selected');
							
			next_page.addClassName('selected');
			
						
		}else if(direction == 'previous'){
			
			var current_page= $$('.thumb_page.selected').first();
			var next_page = current_page.previous('.thumb_page') ? current_page.previous('.thumb_page') : current_page.next('.thumb_page.last') ;
			
			current_page.removeClassName('selected');
			next_page.addClassName('selected');
			
			
		}
	
	
}// end switch page




function prepare_validation(){

	var forms= $$('FORM.need_validation'); 

	forms.each(function(n) {
	  	
	 n.writeAttribute('onsubmit','return check_fields(this);');
	  
	  
	});


}



function check_fields(form){
	
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	
	var current_form = $(form);
	
	var required_elements= current_form.select('.required'); 
	
	var errors = 0 ; 
	
	required_elements.each(function(n) {
	  	
		var type =  new String( n.readAttribute('class') );
		
		
		
			if(type.include('text') ){
				
				var value = n.value;
					
					
					
					if(  value == "" ){ 
						
						var t =  n.previous('label');
						
						if(!IE6){
						
						if(t) t.addClassName('validation_error');
																		
						n.addClassName('validation_error');
						
						n.writeAttribute('onfocus','clear_error(this)');
						
						} else{
							
							window.alert('Please fill the '+n.title);
							
							}
						
						errors++ ; 
						
						} 
						
						
				
					if( type.include('mail') ){
						
						var emailFilter=/^.+@.+\..{2,}$/;
						var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	
						
						if(!(emailFilter.test(value))|| value.match(illegalChars)){ 
							
							if(!IE6){
							
							var t =  n.previous('label');
							
							if(t) t.addClassName('validation_error');
							
							n.addClassName('validation_error');
							
							n.writeAttribute('onfocus','clear_error(this)');
							
							errors++ ; 
							
								} else{
								
								window.alert('Please enter a valid mail address ');
								
								}
							
						
							}// end match mail
				
						}
					
					
				
				}
	  
	  	
	});
	
	return (errors > 0 ? false : true ); 

}




function clear_error(element){
	
	var current_element = $(element);
	
	current_element.removeClassName('validation_error');
	
	current_element.previous('label').removeClassName('validation_error');
	
	current_element.removeAttribute('onfocus');

}



function toggle_button_class(control,status){
	
	var controler= $(control);
	
	if(controler && status=='hover'){ controler.addClassName('button_hover'); }else if(controler &&  status==''){ controler.removeClassName('button_hover'); }
	
	}