if (top != self) { top.location = location; }

var CanvasMenu = Class.create()

CanvasMenu.prototype = {
	initialize: function() {
	   this.menu = $('submenu')
	   if ( ! this.menu ) {
		   return false
	   }
	   this.doors = this.menu.getElementsByClassName('door')

	   this.menu_width = 468
		this.door_width = 90 // 4px border + 90px width
	
	   this.door_border_width = 4
	
		// this.y = 44  // kleine kolommen
		// this.z = 296 // grote kolommen
		
		this.y = 45  // kleine kolommen
		this.z = 260 // grote kolommen
		
		
		this.debug_application = false
	
	
	   this.reset();
	
	   if ( submenu_pos >= 0 ) {
		   this.loadSubMenu(submenu_pos,menu_id)		
	   }

	}, 
	
	reset: function() {
		this.BuildStates(0)
		var extra_width_last = this.menu_width - this.doors.length * ( this.door_width + this.door_border_width )
		for ( var i=0; i<this.states.length; i++ ) {
			if ( $('link_id_'+i) ) {
				var mleft = ( i == 0 ) ? 0 :  i*(this.door_width+this.door_border_width) 
				// content with
				var cwidth = ( i == this.doors.length-1 ) ? (this.door_width+this.door_border_width+extra_width_last) : this.menu_width-mleft

				Element.setStyle($('link_id_'+i).parentNode.parentNode, {
					borderLeft: this.door_border_width + 'px solid #fff',
					width: cwidth + 'px',
					marginLeft: mleft+'px',
				   zIndex: i+10,
				   left: '',
				   top: '',
				   right: '',
				   bottom: ''
				})		
			} else {
				this.debug("wtf? " + i)
			}
		}
	}, 
	
	loadSubMenu: function(seq,w_id) {
		new Ajax.Updater(
			'submenu_'+seq, 
			'/content/submenu/?id='+w_id+'&selected_id='+website_id, {
				asynchronous:true, 
				evalScripts:true, 
				onFailure: function(request) {
					alert('pagina bestaat niet') 
				}, 
				onLoading: function(request){
					canvas_menu.setState(seq)
				}, 
				onComplete: function(request){
					if ( $('submenu_'+seq).getElementsByTagName("li").length > 0 ) {
						new Effect.BlindDown('submenu_'+seq)
					}
				}
			}); 
			return false;
	},
	
	
	setState: function(j) {
	   this.BuildStates(j)
	   for ( var i=0; i< this.states.length-1; i++ ) {
		   if ( ! $('link_id_' + i) ) {
			   break
			}
		   var container = $('link_id_'+i).parentNode.parentNode
		   var xpos = this.getPos(i)['x']
		   var wpos = this.getPos(i)['w']
			$('submenu_'+i).hide()
		   new Effect.Parallel(
			   [ new Effect.Move(container,{ sync: true, x: xpos, mode: 'relative', duration: 0.1 } ),
			     new Effect.Scale(
				   container, 
				   100, 
				   { scaleX: true, 
					  scaleY: false, 
					  scaleContent: false, 
					  scaleMode: { originalWidth: wpos } } )
			   ], { beforeStart: function() { Element.setStyle(container,{ marginLeft: 0, left: ''}) } }
			)
			
		   
		}	
		return false
	},
	
	// y = 40  // kleine kolommen
	// z = 296 // grote kolommen
	// zpos = 1  // positie van z
	BuildStates: function(zpos) {
		this.states = new Array()
		for ( var i=0; i<=5; i++ ) {
			if ( i == this.doors.length+1 ) {
				break 
			}
			var xval = this.sumPos(i-1) || 0
			var wval = 0 
			if ( i == this.doors.length-1 ) {
				wval = this.menu_width - (this.doors.length+1 * this.y)
			} else if ( zpos == i ) {
				wval = this.z
			} else {
				wval = this.y
			}
			this.states.push({x: xval, w: wval})
		}
	},
	
	getPos: function(i) {
		if ( this.states[i] ) {
			return this.states[i]
		} else {
			this.debug("getPos: returning false for: " + i)
			return false
		}
	},
	
	sumPos: function(i) {
		if ( this.getPos(i)['x'] >= 0 && this.getPos(i)['w'] >= 0 ) {
			return this.getPos(i)['x']+this.getPos(i)['w']
		} else {
			return false
		}
	},
	
	debug: function(msg) {
		if ( ! this.debug_application ) {
			return false
		}
		// insert(element, content) -> HTMLElement
		if ( ! $('debugmsg') ) {
			var d = document.createElement("div")
			d.setAttribute("id","debugmsg")
			Element.insert('content', {before: d})
			Element.setStyle(d,{position: 'absolute'})			
			this.debugcounter = 0
      }
      this.debugcounter += 1

      var m = document.createElement("div")
      var p = document.createElement("pre")
      p.innerHTML = this.debugcounter + ' &#187; ' + msg
      m.appendChild(p)
      Element.insert('debugmsg',p)
      Element.setStyle(p,{ border: '1px solid #ddd', margin: '3px', padding: '4px' })
      new Effect.Highlight('debugmsg')
	}
}

function initCanvasMenu() { canvas_menu = new CanvasMenu(); }
Event.observe(window, 'load', initCanvasMenu, false);


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 = '' };
      new Effect.Appear(field);
   },
   /* -------------------------------------------------------------------------- */

   /* -------------------------------------------------------------------------- */
   init_error_message: function() {
      var message = ' Onderstaande formulier bevat fouten, controleer het formulier en probeer het opnieuw!';
      $('error_msg').innerHTML = message;
      new Effect.Appear('error_msg');
   }
   /* -------------------------------------------------------------------------- */
   
}
function initContact() { contact = new Contact(); }