var timer,updateDiv;
var base = "http://www.ceo-evenementen.nl/";
//var base = "http://localhost/ceo_v2/";

Event.observe(window, 'load', function() {

	// observe the left click of the gallery
	Event.observe('goLeft', 'click', function(){project('left');});
	// observe the right click of the gallery
	Event.observe('goRight', 'click', function(){project('right');});
	
	slideShow(2);
});

function slideShow(imgId){
	
	if(imgId == 8){
		imgId = 1;
	}
	// determine the div to update
	if($('two').style.display == "none"){
		var updateDiv = "two";
	}else{
		var updateDiv = "one";
	}
	
	var img = base+"images/imp_"+ (imgId+1) +".jpg";
	preloadImages(img);
	
	new Ajax.Updater(
		updateDiv,
		base+"pages/slideshow.php",
		{
			method: 'post',
			parameters: 'imgid='+imgId,
			requestHeaders: ['Expires', 'Thu, 17 May 2001 10:17:17 GMT', 'Cache-Control', 'no-cache, must-revalidate', 'Pragma', 'no-cache'],
			onComplete: function (){
			
				if(updateDiv == "two"){
					updateDiv = "one";
					new Effect.Appear($("two"), {delay: 5.0,duration: 0.8 });		
				}else{
					updateDiv = "two";
					new Effect.Appear($("one"), {delay: 5.0,duration: 0.0 });
					new Effect.Fade($("two"), {delay: 5.0,duration: 0.8 });
				}
				
				imgId++;
				setTimeout("slideShow("+imgId+")",8000);
				
			}
		});
}




function preloadImages() {
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

var clicking = function(observing) {
	if(observing == 'observe'){
		// observe the clicks in the gallery
		Event.observe('goLeft', 'click', function(){project('left');});
		Event.observe('goRight', 'click', function(){project('right');});	
	}else{
		Event.stopObserving('goLeft', 'click');
		Event.stopObserving('goRight', 'click');
	}
}



var clicks = 0;
function project(direction){
	
	// stop the clicking observe
	clicking('stop');
	// get positions of hor_scroll	
	var minLeft 	= parseFloat($('hor_scroll').style.left);
	var projects 	= $$("#hor_scroll a").length;// 9
	var maxClicks 	= projects - 3; // =6
	var maxRight	= (projects * -222) +220;

	// to the left
	if(direction == 'left'){
			
		if(clicks > 0){
			clicks --;
			//var imgId	= "br"+clicks;
			//alert("removing "+imgId);
			
			new Effect.Move ('hor_scroll',{ y: 0, x: 220 , duration:0.5, mode: 'relative', afterFinish: function() {
		   		//re-observe the gallery clicks
				clicking('observe');
		   		}
			});
			//$(imgId).remove();
			//var w		= $('hor_scroll').getWidth(); 
			//var nw		= w-220;
			//$('hor_scroll').setStyle({
			//	width: nw+'px'
			//});
		}else{
		//	alert(clicks);
		
			// $('hor_scroll') relative position
			var offset 	= $('hor_scroll').positionedOffset();
			var w		= $('hor_scroll').getWidth(); 
			var nw		= w+220;
			var ofs 	= offset[0] - 220;
			//alert(offset[0]);
			// return to last
			if(offset[0] == 220){
				new Effect.Move ('hor_scroll',{ y: 0, x: -1980 , duration:1.0, mode: 'relative', afterFinish: function() {
			   		//re-observe the gallery clicks
					clicking('observe');
			   		}
				});
			}else{
		
				// insert last button in front
				var imgId	= "bl"+clicks;
				//var btn 	= new Element('a', { href: link, id:imgId, style: 'display:none' }).update("<img src='http://localhost/ceo_v2/images/banner10.jpg' alt='new added'>\n");
				//insert item
				//$('hor_scroll').insert({ 'top' : btn } );
			

				// set new 'hor_scroll' position because of shift
				//$('hor_scroll').setStyle({
				//	left: ofs+'px'//,
					//width: nw+'px'
				//});
				//$(imgId).setStyle({
				//	display: 'inline'
				//})
				//move backwards
				new Effect.Move ('hor_scroll',{ y: 0, x: 220 , duration:0.5, mode: 'relative', afterFinish: function() {
			   		//re-observe the gallery clicks
					clicking('observe');
			   		}
				});
			}
			
		}
	//
	}else{
		if(clicks < maxClicks){
			// $('hor_scroll') relative position
			var offset 	= $('hor_scroll').positionedOffset();
			var w		= $('hor_scroll').getWidth(); 
			var nw		= w+220;
			var ofs 	= offset[0] - 220;
		
			// return to last
			if(offset[0] == -1980){
				new Effect.Move ('hor_scroll',{ y: 0, x: 1980 , duration:1.0, mode: 'relative', afterFinish: function() {
			   		//re-observe the gallery clicks
					clicking('observe');
			   		}
				});
			}else{
				new Effect.Move ('hor_scroll',{ y: 0, x: -220 , duration:0.5, mode: 'relative', afterFinish: function() {
			   		//re-observe the gallery clicks
					clicking('observe');
			   		}
				});
			}
			
			/* inserting images
			var imgId	= "br"+clicks;
			//alert("adding "+imgId);
			if(clicks == 0){
				var base	= $('hor_scroll').down();
				var link	= base.href;
				var img		= $('hor_scroll').down(1).src;
			}else{
				var base	= $('hor_scroll').down().next(clicks-1);
				var link 	= base.href;
				var img		= base.down().src;
			}

			var w		= $('hor_scroll').getWidth(); 
			var nw		= w+220;
			$('hor_scroll').setStyle({
				width: nw+'px'
			});

			var btn 	= new Element('a', { href: link, id:imgId }).update("<img src='" +img +"' alt='new added'>\n");
			$('hor_scroll').insert(btn);
			//alert(imgId);
			//alert(clicks);
			
			clicks ++;
			
			new Effect.Move ('hor_scroll',{ y: 0, x: -220 , duration:0.5, mode: 'relative'});
			*/
		}
	}
}


function sendForm(){
	var form_values = $('aanmelding').serialize(true);
	var missed;
	
	var naam 	= form_values['naam'];
	var email 	= form_values['email'];
	if(naam == ''){
		$('naam').addClassName('missed');
		missed = true;
	}else{
		$('naam').removeClassName('missed');
	}
	
	if(email == ''){
		$('email').addClassName('missed');
		missed = true;
	}else{
		$('email').removeClassName('missed');
	}

	if(missed == true){
		return false;
	}
	
	
	new Ajax.Updater(
		'news',
		base+'pages/aanmelding_nieuwsbrief.php',
		{
			method: 'post',
			parameters: form_values,
			requestHeaders: ['Expires', 'Thu, 17 May 2001 10:17:17 GMT', 'Cache-Control', 'no-cache, must-revalidate', 'Pragma', 'no-cache'],
			onComplete: function () {
				$('news').update("<span style='font-size:11px;'><strong>Bedankt voor uw aanmelding!</strong><br /><br />Een bevestiging gestuurd naar: <a href='mailto:"+email+"'>"+email+"</a></span>");
			}
		});
}

function formval(){
	new Effect.Appear( $('indicator'),{duration:0.2} );
	var form_values = $('infoAanvraag').serialize(true);
	var missed;
	
	var naam 	= form_values['naam2'];
	var email 	= form_values['email2'];
	var tel 	= form_values['telefoon'];
	var pers 	= form_values['personen'];
	
	if(naam == ''){
		$('naam2').addClassName('missed');
		missed = true;
	}else{
		$('naam2').removeClassName('missed');
	}
	
	if(email == ''){
		$('email2').addClassName('missed');
		missed = true;
	}else{
		$('email2').removeClassName('missed');
	}
	
	if(tel == ''){
		$('telefoon').addClassName('missed');
		missed = true;
	}else{
		$('telefoon').removeClassName('missed');
	}
	
	if(pers == ''){
		$('personen').addClassName('missed');
		missed = true;
	}else{
		$('personen').removeClassName('missed');
	}

	if(missed == true){
		$('msg').update("U heeft een aantal verplichte velden niet ingevuld. Om de informatieaanvraag <br />te voltooien, dienen deze velden ingevuld te zijn.");
		new Effect.Fade( $('indicator'),{duration:0.2} );
		return false;
	}
	
	new Ajax.Updater(
		'',
		base+'pages/maildata.php',
		{
			method: 'post',
			parameters: form_values,
			requestHeaders: ['Expires', 'Thu, 17 May 2001 10:17:17 GMT', 'Cache-Control', 'no-cache, must-revalidate', 'Pragma', 'no-cache'],
			onComplete: function () {
				window.location = "http://www.c-e-o.nl/?msg=1";
			}
		});
}