jQuery.noConflict();

jQuery(function($) {

	//Lang
	var $lang = $('#lang');
	$lang.bind('click', function(){		
		$(document).bind('click.lang', function(event){
			if (!$(event.target).parents('#lang')[0]) {
				$lang.removeClass('on');
				$(document).unbind('click.lang');
			}
		});
		
		if (!$lang.is('.on')) {
			$lang.addClass('on');
			return false;
		}
	});
	
	//Filter
	$links = $('#filter .links');
	$links.each(function(){
		var $el = $(this);
		$el.bind('click', function(event){			
		
			if ($(event.target).is('a')) {
				$('a', $el).removeClass('active');
				$(event.target).addClass('active');																
			}
		
			if (!$el.is('.on'))	{
				$links.not($el).removeClass('on');
				$el.addClass('on');
					$(document).bind('click.filter', function(event){
						$links.removeClass('on');
						$(document).unbind('click.filter');			
					});
				return false;
			} else {
				
				url = document.location.href;
				if (url.indexOf('?') < 0) url += '?';
				
				switch ($el.parent().index()){
					case 0:
						var segment = url.indexOf('&country=');
						if(segment > 0) {
							var newUrl = url.substring(0,segment);
							url = newUrl + url.substring(segment+11);
						}
						url += '&country=';
						break;
					case 1:
						var segment = url.indexOf('&region=');
						if(segment > 0) {
							var newUrl = url.substring(0,segment);
							url = newUrl + url.substring(segment+10);
						}
						url += '&region='
						break;
					case 2:
						var segment = url.indexOf('&product=');
						if(segment > 0) {
							var newUrl = url.substring(0,segment);
							url = newUrl + url.substring(segment+11);
						}
						url += '&product='
						break;
				}
				
				url += event.target.href.split('#')[1];				
				document.location.href = url;
				
				$el.removeClass('on');
				return false;
			}														
		});
	});
	
	//Contact
	var $contactCountries = $('#contact-countries');
	var $titles = $('.title', $contactCountries);
	var $contents = $('.content', $contactCountries);
	
	$titles.click(function(){
		$contents.hide();
		$(this).siblings('.content').show();
		return false;
	}).eq(0).click();
	
	//Showing forms
	var $brochure = $('#brochure');
	var $informatie = $('#informatie');
 	$contactCountries.click(function(event){
		if ($(event.target).is('[href="#brochure"]')) {
			$brochure.show();
			$informatie.hide();
			return false;
		} else if ($(event.target).is('[href="#informatie"]')) {
			$brochure.hide();
			$informatie.show();
			return false;
		}
	});
	
	//Fancybox
	$("[rel=lightbox], .img a").fancybox({
				'titleShow'		: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'overlayOpacity': .8,
				'overlayColor'  : '#352a6c'
	});
	
	$('.csc-textpic-image a').unbind('click').attr('onclick','').each(function(){
		
		href = this.href;
		href = href.split('&file=')[1];
		href = href.split('&')[0];
		this.href = Url.decode(href);
		$(this).fancybox({
				'type'			: 'image',
				'titleShow'		: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'overlayOpacity': .8,
				'overlayColor'  : '#352a6c'
		});
		
	});

	
	//IE7 referenties
	if ($.browser.msie && ($.browser.version <= 7)){
		if (document.getElementById('ref')){
			$('#ref .img img').each(function(){
			
				if ($(this).height() < 155) {
					$(this).css('margin-top', (155 - $(this).height())/2);
				}
			});
		}
	}

	
	//Homepage
	if (document.getElementById('home')){
		$('.box:empty').hide();
	}
	
});


function SendContact() {
	var name = document.form1.naam.value;
	var email = document.form1.email.value;
	var telefoonnummer = document.form1.telefoonnummer.value;
	var adres = document.form1.adres.value;
	var postcode = document.form1.postcode.value;
	var plaats = document.form1.plaats.value;
	var land = document.form1.land.value;
	
	var k = 1; var er ='';
	if (name=='') { k=0; er=er+'Please type in the name \n'; }
	if (email=='') { k=0; er=er+'Please type in the email \n'; }
	if (telefoonnummer=='') { k=0; er=er+'Please type in the telefoonnummer \n'; }
	if (adres=='') { k=0; er=er+'Please type in the adres \n'; }
	if (postcode=='') { k=0; er=er+'Please type in the postcode \n'; }
	if (plaats=='') { k=0; er=er+'Please type in the plaats \n'; }
	if (land=='') { k=0; er=er+'Please type in the land \n'; }
	
	if (k==0) {
		alert(er);
	} else {
		document.form1.submit();
	}
}

function SendContact2() {
	var name = document.form2.naam.value;
	var email = document.form2.email.value;
	var vraag = document.form2.vraag.value;
	
	var k = 1; var er ='';
	if (name=='') { k=0; er=er+'Please type in the name \n'; }
	if (email=='') { k=0; er=er+'Please type in the email \n'; }
	if (vraag=='') { k=0; er=er+'Please type in the vraag \n'; }
	
	if (k==0) {
		alert(er);
	} else {
		document.form2.submit();
	}
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}