// Wax Main Javascript - (c) 2006 Wax Interactive - http://www.wax.be/ - "Thou shall not steal"

var wax = {
	
	scalePhotos	: null,

	init		: function() {
		
		// move the contactform from bottom to top
			new Insertion.Before('wrapper', '<div id="contactBlock" style="display: none;">' + $('contactBlockNJ').innerHTML + '</div>');
			Element.remove('contactBlockNJ');
		
		// Contact button toggle
			$('navContact').onclick = function() {
				
				// using small temp class deleteme since we want it to have 
				// the selected class when opening, and only removing it when 
				// it has completely closed
				if ($('navContact').hasClassName("selected")) {
					$('navContact').addClassName("deleteme");
				} else {
					$('navContact').addClassName("selected");
				}
				Effect.toggle($('contactBlock'), 'blind', {
					duration:0.5,
					afterFinish: function() {
						if ($('navContact').hasClassName("deleteme")) {
							$('navContact').removeClassName("deleteme");
							$('navContact').removeClassName("selected");
						} else {
							Form.focusFirstElement($('contactForm'));
						}
					}
				});
				
				return false;
			}
		
		// Contacform onsubmit hook
			$('contactForm').onsubmit	= function() {
				if(formChecker.checkForm(this)) {
					wax.submitContactForm(this.action.toString());
				}
				return false;
			}
			
		// projects slider
			if ($('projectstrack')) {
				
				wax.scalePhotos		= document.getElementsByClassName("scale-image");
				
				var projectsSlider 	= new Control.Slider('projectshandle','projectstrack', { 
										axis:'horizontal', 
										minimum: 0, 
										maximum: 190, 
										alignX: 15, // what the wiki does not mention is that you should also set the left-padding equal to this value!
										increment: 10, 
										// Bramus! remarks : This ain't workin' >> Bug in script.aculo.us! 
										// Solution : call setValue afterwards.
										//sliderValue: 127/190,
										onSlide: function(value){
											wax.scaleProjects(value);
										},
										onChange: function(value){
										  wax.scaleProjects(value);
										}
				});
				
				// Bramus! remarks : This is wrongly documented in script.aculo.us wiki >> schould not be value, but percentage.
				// Solution : divide wanted value by maximum value
				projectsSlider.setValue(127/190);
			}
			
		// bulletPointsList
			if ($('bulletPointsLink')) {
				$('bulletPointsLink').onclick = function() {
					Effect.toggle($('bulletPointsList'), 'blind', { duration:0.5 });
					return false;
				}
			}
			
		// IE vs. $('navContact').onclick : IE has troubles when sliding open contact when the map is shown ... so we're disabling it
			if ($('b_contact') && (navigator.appVersion.match(/MSIE (\d+\.\d+)/, '') != null)) {
				$('navContact').onclick	= null;
			}
			
		// external window links
			getNewWindowLinks();
			
		// listen up ladies!
			//Event.observe(document, 'keypress', wax.listenToThemKeys);
	},
	
	submitContactForm	: function(formAction) {
		
		new Ajax.Request( formAction, {
			method		: "post", 
			postBody	: "action=doSendMail" + "&ajax=true" + "&naam=" + $F('naam') + "&voornaam=" + $F('voornaam') + "&email=" + $F('email') + "&bedrijf=" + $F('bedrijf') + "&straat_nr=" + $F('straat_nr') + "&pc_gem=" + $F('pc_gem') + "&tel=" + $F('tel') + "&vraag=" + escape($F('vraag')),
			onSuccess	: function(t) {
				var json	= t.responseText.parseJSON();
				if (json.status.code == "error") { alert("Fout tijdens het verzenden van uw aanvraag, probeert u het opnieuw."); } 
				if (json.status.code == "success") {
					toAdd = '<div style="width: 100%; height: 360px; -moz-opacity:0.75; opacity:0.75; filter:alpha(opacity=75); background: #000; position: absolute; top: 0; left: 0;" id="contactOverlay"><div style="width:985px; margin: 315px auto 0;"><p style="color: #FFF; text-align: right;">Bedankt, uw bericht werd goed ontvangen!</p></div></div>';
					new Insertion.Top( $('contactBlock'), toAdd);
					$('spinner').style.visibility	= 'hidden';
					setTimeout(function() { Effect.Fade('contactOverlay') }, 1000);
				}						
			}
		});
	},
	
	scaleProjects		: function(v) {
		floorSize = .26;
		ceilingSize = 1.0;
		v = floorSize + (v * (ceilingSize - floorSize));
		
		for (i=0; i < wax.scalePhotos.length; i++) {
			wax.scalePhotos[i].style.width = (v*180)+'px';
		}
	},
	
	listenToThemKeys	: function(event) {
		alert(event.keyCode);
	}
}

// trimmer - by Bramus! - http://www.bram.us/
var trimmer		= {
	LTrim		: function (value) {
					var re = /\s*((\S+\s*)*)/;
					return value.replace(re, "$1");	
	},

	RTrim		: function (value) {
					var re = /((\s*\S+)*)\s*/;
					return value.replace(re, "$1");
	},
	
	trim		: function (value) {
					return trimmer.LTrim(trimmer.RTrim(value));
	}
}

// speeding up onload - http://www.agileweb.org/articles/2006/07/28/onload-final-update
// disabling atm (by adding DISABLED to the observer method) ... IE still has issues with it!
Object.extend(Event, {
  observeDISABLED: function(element, name, observer, useCapture) {
    var element = $(element);
    useCapture = useCapture || false;
    if (name == 'keypress' && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || element.attachEvent))
      name = 'keydown';
    if (name == 'load' && element.screen)
      this._observeLoad(element, name, observer, useCapture);
    else
      this._observeAndCache(element, name, observer, useCapture);
  },
  _observeLoad : function(element, name, observer, useCapture) {
    if (!this._readyCallbacks) {
      var loader = this._onloadWindow.bind(this);
      if (document.addEventListener)
          document.addEventListener("DOMContentLoaded", loader, false);
      /*@cc_on @*/
      /*@if (@_win32)
	if (!$("__ie_onload")) {
          document.write("<script id='__ie_onload' defer='true' src='://'><\/script>");
          var script = $("__ie_onload");
          script.onreadystatechange = function() { if (this.readyState == "complete") loader(); };
      } else {
        loader();
      }
      /*@end @*/
      if (navigator.appVersion.match(/Konqueror|Safari|KHTML/i))
        Event._timer = setInterval(function() {if 
(/loaded|complete/.test(document.readyState))loader();}, 10);
      Event._readyCallbacks =  [];
      this._observeAndCache(element, name, loader, useCapture);
    }
    Event._readyCallbacks.push(observer);
  },
  _onloadWindow : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;
    if (this._timer) clearInterval(this._timer);
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
  }
});

Event.observe(window, 'load', wax.init, false);