/* Nieuwe pagina openen */

	$(function(){
		$('a.newWindow').click(function(){
			window.open(this.href);
			return false;
	});
	});

// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){
	/**
	 * Most jQuery.serialScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://flesler.demos.com/jquery/scrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.serialScroll.
	 */
	
	/**
	 * The plugin binds 6 events to the container to allow external manipulation.
	 * prev, next, goto, start, stop and notify
	 * You use them like this: $(your_container).trigger('next'), $(your_container).trigger('goto', [5]) (0-based index).
	 * If for some odd reason, the element already has any of these events bound, trigger it with the namespace.
	 */		
	
	/**
	 * IMPORTANT: this call to the plugin specifies ALL the settings (plus some of jQuery.ScrollTo)
	 * This is done so you can see them. You DON'T need to specify the commented ones.
	 * A 'target' is specified, that means that #screen is the context for target, prev, next and navigation.
	 */
	$('#screen').serialScroll({
		target:'#sections',
		items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		prev:'img.prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'img.next',// Selector to the 'next' button (absolute too)
		axis:'xy',// The default is 'y' scroll on both ways
		navigation:'#navigation li',
		duration:700,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		//queue:false,// We scroll on both axes, scroll both at the same time.
		//event:'click',// On which event to react (click is the default, you probably won't need to specify it)
		// stop:true,// Each click will stop any previous animations of the target. (false by default)
		// lock:true, // Ignore events if already animating (true by default)		
		// start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )		
		// cycle:true,// Cycle endlessly ( constant velocity, true is the default )
		//step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
		//jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
		//lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
		interval:15000, // It's the number of milliseconds to automatically go to the next
		//constant:true, // constant speed
		
		onBefore:function( e, elem, $pane, $items, pos ){
			/**
			 * 'this' is the triggered element 
			 * e is the event object
			 * elem is the element we'll be scrolling to
			 * $pane is the element being scrolled
			 * $items is the items collection at this moment
			 * pos is the position of elem in the collection
			 * if it returns false, the event will be ignored
			 */
			 //those arguments with a $ are jqueryfied, elem isn't.
			e.preventDefault();
			if( this.blur )
				this.blur();
		},
		onAfter:function( elem ){
			//'this' is the element being scrolled ($pane) not jqueryfied
		}
	});
	
});


/*	
   Twitter API koppeling
 */ 
 
	getTwitters('dmpTweets', {
        id: 'desperadomp', 
        prefix: '<a href="http://twitter.com/%screen_name%">@%screen_name%</a>', 
        clearContents: false,
        count: 1, 
        withFriends: false,
        ignoreReplies: true,
        newwindow: true
   });


/* quirksmode.org form validation script */
/* aangepast door Rutger Laurman - i-Aspect */
/* versie 1.1 */

/* event adding */
   function addEventSimple(obj,evt,fn) {
		if (obj.addEventListener)
			obj.addEventListener(evt,fn,false);
		else if (obj.attachEvent)
			obj.attachEvent('on'+evt,fn);
	}

/* form validating */
   var validationErrorMessage = new Object();
   validationErrorMessage['required'] = 'Dit is een verplicht veld';
   validationErrorMessage['requiredsmall'] = 'Verplicht';
   validationErrorMessage['numeric'] = 'Dit veld moet numeriek zijn';
   validationErrorMessage['postcode'] = 'Dit veld moet een postcode zijn';
   validationErrorMessage['pattern'] = 'Patroon onjuist';
   validationErrorMessage['email'] = 'Onjuist email adres';
   validationErrorMessage['emailsmall'] = 'Onjuist email adres';

   var validationFunctions = new Object();
   validationFunctions["required"] = isRequired;
   validationFunctions["requiredsmall"] = isRequired;
   validationFunctions["pattern"] = isPattern;
   validationFunctions["postcode"] = isPostCode;
   validationFunctions["numeric"] = isnumeric;
   validationFunctions["emailsmall"] = isEmail;
   validationFunctions["email"] = isEmail;

   function isRequired(formField) {
   	switch (formField.type) {
   		case 'text':
   		case 'textarea':
   		case 'select-one':
   			if (trim(formField.value))
   				return true;
   			return false;
   		case 'radio':
   			var radios = formField.form[formField.name];
   			for (var i=0;i<radios.length;i++) {
   				if (radios[i].checked) return true;
   			}
   			return false;
   		case 'checkbox':
   			return formField.checked;
   	}
   }

   function isPattern(formField,pattern) {
   	var pattern = pattern || formField.getAttribute('pattern');
   	var regExp = new RegExp("^"+pattern+"$","");
   	var correct = regExp.test(formField.value);
   	if (!correct && formField.getAttribute('patternDesc'))
   		correct = formField.getAttribute('patternDesc');
   	return correct;
   }

   function isPostCode(formField) {
      return isPattern(formField,"\\d{4}\\s*\\D{2}");
   }

   function isnumeric(formField) {
   	return isPattern(formField,"\\d+");
   }

   // aangepast, oude functie voldeed niet
   function isEmail(formField) {
   	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
   	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; // valid
   	if (!reg1.test(formField.value) && reg2.test(formField.value))
        return true;
   	else
   	   return false;

   }

   function emptyFunction() {
   	return true;
   }


   function trim(value) {
     value = value.replace(/^\s+/,'');
     value = value.replace(/\s+$/,'');
     return value;
   }

   /*********************************/

   var W3CDOM = document.createElement && document.getElementsByTagName;

   function validateForms() {
   	if (!W3CDOM) return;
   	var forms = document.forms;
   	for (var i=0;i<forms.length;i++) {
   	   if(forms[i].id=='newsletterform')continue;
   		forms[i].onsubmit = validate;
   	}
   }

   addEventSimple(window,'load',validateForms);

   function validate() {
   	var els = this.elements;
   	var validForm = true;
   	var firstError = null;
   	for (var i=0;i<els.length;i++) {
   		if (els[i].removeError)
   			els[i].removeError();
   		var req = els[i].getAttribute('validation');
   		if (!req) continue;
   		var reqs = req.split(' ');
   		if (els[i].getAttribute('pattern'))
   			reqs[reqs.length] = 'pattern';
   		for (var j=0;j<reqs.length;j++) {
   			if (!validationFunctions[reqs[j]])
   				validationFunctions[reqs[j]] = emptyFunction;
   			var OK = validationFunctions[reqs[j]](els[i]);
   			if (OK != true) {
   				var errorMessage = OK || validationErrorMessage[reqs[j]];
   				writeError(els[i],errorMessage)
   				validForm = false;
   				if (!firstError)
   					firstError = els[i];
   				break;
   			}
   		}
   	}

   	if (!validForm) {
   		//alert("Het formulier is niet juist ingevuld");
   		/*location.hash = '#contactform';*/
   		writeSingleError(this, 'Het formulier is niet juist ingevuld.');

   	}
   	return validForm;

   }

   function writeError(obj,message) {
      // kijk of errorMessage al is gezet, zo nee, toevoegen
      if(obj.className.indexOf('errorMessage')==-1)
         obj.className += ' errorMessage';

   	obj.onchange = removeError;
   	if (obj.errorMessage || obj.parentNode.errorMessage) return;
   	var errorMessage = document.createElement('label');
   	errorMessage.className = 'errorMessage';
   	errorMessage.setAttribute('for',obj.id);
   	errorMessage.setAttribute('htmlFor',obj.id);
   	errorMessage.appendChild(document.createTextNode(message));
   	obj.parentNode.appendChild(errorMessage);
   	obj.errorMessage = errorMessage;
   	obj.parentNode.errorMessage = errorMessage;

/*
   Fixing Explorer
   - Explorer wants no borders on a select box
   If obj is selectbox, search for holding P tag and set class on P tag.
*/
/*
      holderTag = obj;
      if(obj.nodeName=='SELECT'){
         while(holderTag.nodeName!='P')
            holderTag = holderTag.parentNode;
         holderTag.className='errorMessage';
      }*/
   }

   function removeError() {
   	this.className = this.className.replace(/errorMessage/,'');
   	if (this.errorMessage) {
   		this.parentNode.removeChild(this.errorMessage);
   		this.errorMessage = null;
   		this.parentNode.errorMessage = null;
   		removeSingleError();
   	}
   	this.onchange = null;
   }


   /* show error next to submit button */
   function writeSingleError(formObject,message){

      // search for button
      var button = formObject.getElementsByTagName('button')[0];

      // check if error message exists
      if(!document.getElementById('singleErrorMessage')){

         // create error message
         var errorButtonMessage = document.createElement('span');
         /*errorButtonMessage.className = 'errorMessage';*/
         errorButtonMessage.appendChild(document.createTextNode(message));
         errorButtonMessage.id = 'singleErrorMessage';
         button.parentNode.appendChild(errorButtonMessage);
      }

   }
   /* remove error next to submit button */
   function removeSingleError(){
      var errorObject = document.getElementById('singleErrorMessage');
		if(errorObject) errorObject.parentNode.removeChild(errorObject);
   }


/* custom extended email validation */
function validateEmail(email){
   if(email.length <= 0){
      return false;
   }
   var splitted = email.match("^(.+)@(.+)$");
   if(splitted == null) return false;
   if(splitted[1] != null ) {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
   }
   if(splitted[2] != null) {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) {
         var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
         if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
   }
   return false;
}

/* ie6 melding */

function badBrowser(){
	if($.browser.msie && parseInt($.browser.version) <= 6){ return true;}
	
	return false;
}

function getBadBrowser(c_name)
{
	if (document.cookie.length>0)
	{
	c_start=document.cookie.indexOf(c_name + "=");
	if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}	

function setBadBrowser(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

if(badBrowser() && getBadBrowser('browserWarning') != 'seen' ){
	$(function(){
		$("<div id='browserWarning'>You are using an <strong>unsupported</strong> browser. Please switch to <strong><a href='http://getfirefox.com'>FireFox</a></strong>, <strong><a href='http://www.apple.com/safari/'>Safari</a></strong> or <strong><a href='http://www.microsoft.com/windows/downloads/ie/getitnow.mspx'>Internet Explorer 7 or 8</a></strong>. Thanks!&nbsp;&nbsp;&nbsp;[<a href='#' id='warningClose'>close</a>] </div> ")
			.css({
				backgroundColor: '#fcfdde',
				'width': '100%',
				'font-size': '13px',
				'border-top': 'solid 1px #000',
				'border-bottom': 'solid 1px #000',
				'text-align': 'center',
				padding:'5px 0px 5px 0px'
			})
			.prependTo("body");
		
		$('#warningClose').click(function(){
			setBadBrowser('browserWarning','seen');
			$('#browserWarning').slideUp('slow');
			return false;
		});
	});	
}