// Initaliaze the menu
function initMedidMenu() { mmenu = new MedidMenu('medid_menu',false); }
Event.observe(window, 'load', initMedidMenu, false);

// Initalize the carousel
function initCarousel() {
  carousel = new Carousel('forreal-carousel', 
     { animHandler:animHandler, 
       animParameters:{duration:0.5}, 
       nextElementID:'next-arrow', 
       prevElementID:'prev-arrow', 
       size:31}
  ) 
}

function animHandler(carouselID, status, direction) {
   var region = $(carouselID).down(".carousel-clip-region")

   if (status == "before") {
      Effect.Fade(region, {to: 0.3, queue: { position:'end', scope: "carousel" }, duration: 0.1})
   }

   if (status == "after") {
      Effect.Fade(region, {to: 1, queue: { position:'end', scope: "carousel" }, duration: 0.1})
   }
}


/*
 *	<div class="presenter" id="presenter">
 *		<img id="presenter-img" src="/images/empty.gif" alt="" />
 *		<div class="transbox">
 *			<p id="presenter-txt">Tekst voor plaatje</p>
 *		</div>
 *		<div id="presenter-navigator">
 *			<a href="#" id="presenter-left">»</a>
 *			<a href="#" id="presenter-right">«</a>
 *			<span id="presenter-counter">1 / 5</span>
 *		</div>
 *	</div>
 *
 */

var Presenter = Class.create()

Presenter.prototype = {
	initialize: function(container,img_data,is_static) {
		// dom ids
		this.container   = $(container)
		this.p_img       = $('presenter-img')
		this.p_txt       = $('presenter-txt')
		this.p_prev      = $('presenter-prev')
		this.p_next      = $('presenter-next')
		this.p_counter   = $('presenter-counter')
		this.p_nav       = $('presenter-navigator')
		this.p_images    = $('presenter-images')
		
		// img data for the slideshow
		this.img_data    = img_data
		
		// what kind of slideshow
		this.is_static = ( is_static == true ? true : false )
		
		// positions
		this.currIndex   = 0
		this.prevIndex   = 0
		this.maxIndex    = this.img_data.length-1
		
		// abort, this is too small
		if ( this.img_data.length == 0 ) {
			this.img_data [ { src: 'images/empty.gif', title: 'Kies plaatjes voor uw slide show'} ]
			this.p_nav.setStyle({visibility: "hidden"})
			return false
		}
		
		// only one image, hide the navigator
		if ( this.maxIndex == 0 ) {
			this.p_nav.setStyle({visibility: "hidden"})
		}
		// else we need some event bindings: uber!
		else {
			this.prevImgX = this.prevImg.bindAsEventListener(this)
			this.nextImgX = this.nextImg.bindAsEventListener(this)
		
			Event.observe(this.p_prev,'click',this.prevImgX)
			Event.observe(this.p_next,'click',this.nextImgX)
      }		

      // load first image then preload rest
     	this.preLoadImg();
	},
	
	preLoadImg: function() {
		// preload the slideshow, don't start at zero, we'll let the browser handle that
		if ( this.is_static == true ) {
			this.setImg();
		   for ( var i=1; i<this.img_data.length; i++ )	{
			   var img = new Image();
			   img.src = this.img_data[i]['src']
		   }
		} 
		// built the HTML
		else {
			for ( var i=0; i<this.img_data.length; i++ ) {
				var img = document.createElement('img')
				img.setAttribute("src",   this.img_data[i]['src'])
				img.setAttribute("title", this.img_data[i]['title'] ? this.img_data[i]['title'] : this.img_data[i]['src'])
				img.setAttribute("alt",   this.img_data[i]['src'])
				var li = document.createElement("li")
				li.setAttribute("id", "slide-" + i )
				if ( i!=0 ) {
					// li.setStyle doesn't work in IE <why?>
					// li.setStyle({display: "none"})
					li.style.display = 'none'
				}
				li.appendChild(img)
				this.p_images.appendChild(li)
			}
			this.setTxt();
		}
	},
	
	setImg: function() {
		if ( this.is_static == true ) {
			this.setImgStatic();
		} else {
			this.setImgDynamic();
		}
	},
	
	setTxt: function() {
		this.p_txt.innerHTML = this.img_data[this.currIndex]['title'] ?
		   this.img_data[this.currIndex]['title'] :
		   this.img_data[this.currIndex]['src']
		this.p_counter.innerHTML = this.currIndex+1 + '/' + this.img_data.length
	},
	
	setImgDynamic: function() {
		if ( this.prevIndex != this.currIndex ) {
	      Effect.Fade("slide-"+this.prevIndex)
      }
      Effect.Appear("slide-"+this.currIndex)
		this.setTxt();
	},
	
	setImgStatic: function() {
		this.p_img.setAttribute("src",   this.img_data[this.currIndex]['src'])
		this.p_img.setAttribute("title", this.img_data[this.currIndex]['title'])
		this.p_img.setAttribute("alt",   this.img_data[this.currIndex]['src'])
		this.setTxt();
	},
	
	nextImg: function(event) {
		this.prevIndex = this.currIndex
	   if ( this.currIndex < this.maxIndex )	{
		   ++this.currIndex
		} else if ( this.currIndex == this.maxIndex ) {
			this.currIndex = 0
		}
		this.setImg();
	},
	
	prevImg: function(event) {
		this.prevIndex = this.currIndex
		if ( this.currIndex == 0 ) {
			this.currIndex = this.maxIndex
		} else if ( this.currIndex > 0 ) {
			--this.currIndex
		}
		this.setImg();
	}

}



var Contact = Class.create();

Contact.prototype = {
   initialize: function() {
      this.email_match = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
      this.is_error = 0
   },
   
   /* -------------------------------------------------------------------------- */
   check: function() {
   // #ff9999
      if (! form_fields) {
         alert("sorry no form fields initialized, we are going to quit")
         return false
      } else {
         this.is_error = 0
      }

      for ( var i=0; i<form_fields.length; i++ ) {
         if (  ( $(form_fields[i][1]) == 1 ) && ( $F(form_fields[i][0]) == '' ) ) {
            this.blink_field(form_fields[i][0]);
            this.is_error = 1;
         } else {
            $(form_fields[i][0]).style.backgroundColor = 'white';
         }
      }

      if ( this.is_error == 1 ) {
         this.init_error_message()
         return false;
      } else {
         return true;
      }
   },
   /* -------------------------------------------------------------------------- */


   /* -------------------------------------------------------------------------- */
   blink_field: function(field) {
      if ( !$(field) ) {
         alert(field + " does not exist, please update your code.")
         return
      }
      $(field).style.backgroundColor = '#ebcacb';
      $(field).onclick = function() { this.style.backgroundColor = 'transparent' };
      new Effect.Appear(field);
   },
   /* -------------------------------------------------------------------------- */

   /* -------------------------------------------------------------------------- */
   init_error_message: function() {
      var message = 'The form below contains errors, correct them and try again!';
      $('error_msg').innerHTML = message;
      new Effect.Appear('error_msg');
   }
   /* -------------------------------------------------------------------------- */
   
}
function initContact() { contact = new Contact(); }

