var jQ = jQuery.noConflict();

jQ(document).ready(function(){
  
	//define browser compat mode
	Essentials.setCompatOldBrowser();
	Essentials.getURLdetails();
	
	//language bar
	jQ('#language a').updateLanguageSwitchURL();
	
	if( jQ('#breadcrumb a').length == 0){
		jQ('#breadcrumb').remove();
	} else {
		jQ('.grijzelijngeorgia').addClass('reset-margin');
		jQ('.grijzelijngeorgia').eq(0).css('border','0');
	}
	if( jQ('.csc-textpicHeader').length > 0){
		jQ('.csc-textpicHeader').each(function(){
			var parentEl = jQ(this).parent().parent().parent();
			if ( parentEl.parent().hasClass("groen") ){
				//do nothing
			}else{
				var sibEl = parentEl.find('.csc-textpic-imagewrap');
				sibEl.before(jQ(this));
			}
		});
	}  
	
	/* display large map image */
	if ( jQ("#right img[longdesc]").length > 0){
	
	  jQ("#right img[longdesc]").each(function(){
	        
	  	var img = new Image();
		var srcL = jQ(this).attr('longdesc');
		
		jQ(this).hover(function(){
			jQ('#contentContainer').append('<div class="imgLarge"><img></img></div>');
			jQ(".imgLarge").css({
				'background':'#fff url(../images/loader.gif) no-repeat center center',
				'height':'400px',
				'padding':'10px',
				'position':'absolute',
				'width':'655px'
			});
			jQ(".imgLarge img").attr('src', srcL);
			
			jQ("#leftContent").css('display', 'none');
		}, function(){		
			jQ('#contentContainer .imgLarge').remove();
			jQ("#leftContent").css('display', 'block');
		});
	  })
	}
	
	/* correction for header position */
	if( jQ('.groen.horizontaal').length > 0){
		var header = jQ('.groen.horizontaal .csc-header.csc-header-n1');
		jQ('.groen.horizontaal .csc-textpic-imagewrap').after(header);
	}
	if( jQ('#menuL1 li.one').length > 1){
		jQ('#menuL1 li.one').eq(1).remove();
		
	}
	jQ('#menuL1 li li:last').css('border', '0');
	
	jQ('a, li').hoverPointer();
	
	//Bestel formulier
	if( jQ('legend').length > 0){
		jQ('#tx-powermail-pi1_fieldset_15').children().each(function(){
			jQ('#tx-powermail-pi1_fieldset_8').append(this);
		});
	
		jQ('legend').each(function (){
			jQ(this).after('<div class="legend">'+jQ(this).html()+'</div>');
			jQ(this).remove();
		});
		
		
		var nHeight = jQ('#tx-powermail-pi1_fieldset_8').height() + 100;
 
		if ( jQ('#contentContainer').height() < nHeight ){
			jQ('#contentContainer').css('height', nHeight);
		}
		
	}
	
	jQ('.aantal_eenheden input').numeric({allowControlKeys: true}).calcPrice();
	//Bestel formulier end
});

(function($) {
   $.fn.calcPrice = function(){

	this.each(function(){ 

		var c = this;
		$(this,c).blur(function(){
 
			var calcVal = $(this,c).parent().parent().find('.product-eenheidsprijs-waarde').text();
			var calcTotalObj = $(this,c).parent().parent().find('.product-totaal input');
			
			calcVal = calcVal.replace(',','.');
			var calcUnits = $(this,c).val();
			
			calcVal = calcVal*1;
			var calcFinal = calcUnits * calcVal;
			calcTotalObj.val(calcFinal.toFixed(2)).totalOrder();
			
		});
		
	});
   };
})(jQuery);

(function($) {
  $.fn.updateLanguageSwitchURL = function(){
  
  	var myURL = Essentials.siteURL
	var endvalue = myURL.segments.length;
		
  	var el = $(this);
  	var i=0;	
	var url = '';

	if (( myURL.segments[0] != 'en') && (myURL.segments[0] != '')){
    		for(i = 0; i <= endvalue - 1; i++){
     			url += "/" + myURL.segments[i] ;
    		}
   		url = '/en'+url;
   		el.attr('href', url);
   	}
   			
   	if( myURL.segments[0] == 'en' ){
   		$(this).text('Nederlands');
   		
		for(i = 1; i <= endvalue - 1; i++){
     			url += "/" + myURL.segments[i] ;
    		}
   		el.attr('href', url);
   	}
   	var obj = Essentials.siteURL.params;
   	if ( obj['L'] == 1 ){
		jQ('#language a').text('Nederlands');
		jQ('#language a').attr('href', '');
	} 
  };
})(jQuery);

(function($) {
   $.fn.totalOrder = function(){
   	var total = '';
   	$('.product-totaal input').each(function(){
   		total = (total*1) + ($(this).val()*1);
   	});
   	$('#uid134').val(total.toFixed(2));
   	return null;
   };
})(jQuery);  

// resize the sidebar of the bestel form, legacy
(function($) {
   $.fn.resizeSidebar = function() {
	var parent = jQ('#tx-powermail-pi1_fieldset_6');

	if( parent.height() + 19 > parent.data('bHeight') ){
		var top = (jQ('.powermail_mandatory_js:visible').length + 1) * 19;
		var nTop = parseInt( parent.next().data('bTop') ) + top;
		parent.next().css('top',nTop);
	} else{
		parent.next().css('top', parent.next().data('bTop') );
	}
   	return null;
   };
})(jQuery); 

(function($) {
   $.fn.numeric = function(options)
   {
      var opts = $.extend($.fn.numeric.defaults, options);
      return this.each(function()
      {
         $(this).keydown(function(e)
         {
            var key = e.charCode || e.keyCode || 0;
            if (opts.allowControlKeys && isControlKey(key))
            {
               return true;
            }
            return ((key >= 48 && key <= 57) || (key >= 96 && key <= 105));
         }); 
      });
   };
   function isControlKey(key)
   {
      var controlKeys = new Array(8, 9, 46, 37, 38, 39, 40);    
      return $.inArray(key, controlKeys) > - 1;
   }
   $.fn.numeric.defaults = {allowControlKeys: true};
})(jQuery);

(function($) {
   $.fn.hoverPointer = function(){
	this.each(function(){ 
		var c = this;
		
		jQ(this,c).hover(function(){
    			jQ(this,c).addClass('hover');
  		},function(){
  			jQ(this,c).removeClass('hover');
  		});
  	});
  };
  return this;
})(jQuery);

Essentials = {
	compatOldBrowser : false ,
	siteURL: null,
	setCompatOldBrowser : function (){
		if (jQ.browser.msie && jQ.browser.version.substr(0,1)<7) {
  			this.compatOldBrowser = true;
  		}
		return null;
	},
	getCompatOldBrowser : function (){
		return this.compatOldBrowser;
	},	
	debugCompatOldBrowser : function(){
		alert( this.getCompatOldBrowser() );
	},
	getURLdetails: function(){
		this.siteURL = this.parseURL(document.location.href);
	},
	// This function creates a new anchor element and uses location
	// properties (inherent) to get the desired URL data. Some String
	// operations are used (to normalize results across browsers).
	parseURL: function (url) {
		var a =  document.createElement('a');
		a.href = url;
		return {
			source: url,
			protocol: a.protocol.replace(':',''),
			host: a.hostname,
			port: a.port,
			query: a.search,
			params: (function(){
			var ret = {},
			seg = a.search.replace(/^\?/,'').split('&'),
			len = seg.length, i = 0, s;
			for (;i<len;i++) {
				if (!seg[i]) { continue; }
					s = seg[i].split('=');
					ret[ unescape(s[0]) ] = s[1];
				}
				return ret;
			})(),
			file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
			hash: a.hash.replace('#',''),
			path: a.pathname.replace(/^([^\/])/,'/$1'),
			relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
			segments: a.pathname.replace(/^\//,'').split('/')
		};
	}
};