// ====================================================================================== //
// Do on domready
var hide;
var kids;
$(document).ready(function() {
	
load_standard_tools();
	
	
	$(".productAfbeeldingPlus").live("click",function()
	{
		pid = $(this).attr("id").replace("i_","");
		$("#aplus_"+pid).toggle();
		$.get("/product/getProductInfoPlus/"+pid, function(data){
			$("#aplus_"+pid).html( data );
			
		});
		
	});
$("#projectSlider").bxSlider( 
	{
		auto: true,
		infiniteLoop: true,
		prevText: ' ',
		pause: 5000,  
		speed: 800,
		randomStart: true,
            nextText: ' '  

	}
	);	
	
$("#aanbiedingSlider").bxSlider( 
	{
		auto: true,
		infiniteLoop: true,
		prevText: 'Vorige',
		pause: 5000,  
		speed: 800,
		randomStart: true,
            nextText: 'Volgende'  

	}
	);
	 
	$(".menuTopPop").hide();

	
	 $("#menuTop li").hover(function() {
      var timeout = $(this).data("timeout");
      if(timeout) clearTimeout(timeout);
      $(this).find("div").slideDown('fast');
    }, function() {
        $(this).data("timeout", setTimeout($.proxy(function() {
            $(this).find("div").slideUp();
        }, this), 500));
    });
   
   

	$("#prijsSliderMenu").slider({
			range: true,
			min: 0,
			max: 3500,
			step: 10,

			values: [0, 3500],
			slide: function(event, ui) {
				$("#prijsMinM").val(ui.values[0]);
				$("#prijsMaxM").val(ui.values[1]);
				if( ui.values[1] == 3500 ) ui.values[1] = "3500+";
				$("#selPrijsValM").html( "<b>"+ui.values[0]+"</b> tot  <b>"+ui.values[1]+"</b> euro");
			}
		});
		
	$(".addWinkelmand").live("click", function(){
		id2 = $(this).attr('rel').split(".");
		id = id2[0];
		oid = id2[1];
		divid = id2[2];
		$("#mandjeAmount").trigger("click");
		
		
		
		$.post("/winkelmandje/addProduct/"+id+"/"+oid+"/", function( d ){
			d = d.split("@@@");
			$("#winkelmandjeMenu").html( d[0] );
			$("#winkelmandjeOverzicht").html( d[1] );
			$("#mandjeAmount").html( "WINKELMANDJE ("+d[2]+")");	
		});
		
	});
	
	$(".delWinkelmand").live("click", function(){
		id2 = $(this).attr('rel').split(".");
		id = id2[0];
		oid = id2[1];
		divid = id2[2];
		
		$.post("/winkelmandje/delProduct/"+id+"/"+oid+"/1", function( d ){
			d = d.split("@@@");
			$("#winkelmandjeMenu").html( d[0] );
			$("#winkelmandjeOverzicht").html( d[1] );
			$("#mandjeAmount").html( "WINKELMANDJE ("+d[2]+")");			
		});
		
	});
	
	$(".verzendmethode").live("click", function(){
		id = $(this).val();		
		
		$.post("/winkelmandje/verzendmethode/"+id, function( d ){
		
			$("#winkelmandjeOverzicht").html( d );			
		});
		
	});

		
		if( $.browser.msie )
		{
			$("#darkMoment").css("z-index",2);
			$("#darkMoment").show();
		}
		else
		{
			$("#darkMoment").delay(4000).fadeIn(20000);
		}
		$("#dark").delay(3000).fadeOut(15000);
	
	
	
});
// ============== GOOGLE ANALITICS  ===================================================== //

var _gaq = _gaq || [];
		  _gaq.push(['_setAccount', 'UA-17717890-3']);
		  _gaq.push(['_trackPageview']);

		  (function() {
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		  })();
		  
// ====================================================================================== //

function load_standard_tools()
{
	
	
	$(".datepicker").datepicker();
	$("#tabs").tabs();
	$("#tabs2").tabs({selected:1});
	$("#tabs3").tabs();

	
	//$('#menuRight').accordion( { active: 0 } );
	
	if( document.location.href.match(/lampen/) )
	{
		$('#menuLeft').accordion( { active: 2 } );
	}
	else if( document.location.href.match(/projecten/) )
	{
		//$('#menuLeft').accordion( { active: 0 } );
	}
	else if( document.location.href.match(/inspiratie/) )
	{
		$('#menuLeft').accordion( { active: 2 } );
	}
	else if( document.location.href.match(/damad/) )
	{
		$('#menuLeft').accordion( { active: 3 } );
	}
	else
	{
		$('#menuLeft').accordion( { active: 3 } );
	}
	
	$("#zoekBoven").click(function(){
		if( $(this).val() == '  zoeken...  ' )
		{
			$(this).val("");
		}
	});
	
	
		


	$(".prodSel").live("change", function(){
		g = document.location.href.split('?');
		
		go( g[0]+"?"+$.URLDecode($("#get").val())+"&"+$("#productsForm").serialize()+"&" );
	});
	
	load_fancy();
	
	externalLinks();
	autocompleteOff();
		
	$(".digits").keydown(function (e){
	  if( e.which!=8 && e.which!=46 && e.which!=39 && e.which!=37 && e.which!=9 && e.which!=110 && e.which!=109 && e.which!=188 &&  e.which!=0 && (e.which<48 || e.which>57) && (e.which<96 || e.which>105) )
	  {
	    return false;
	  }
	});
	
	$(".no_input").keydown	(function (e){
	    return false;
	});
	
	
	
}

// ====================================================================================== //

function externalLinks() {

  $("._blank").each( function(){
     this.target = "_blank";
	});
}

function autocompleteOff() {

  $(".autocompleteOff").each( function(){
     $(this).attr("autocomplete", "off");
	});
}


// ====================================================================================== //

function load_fancy()
{
	$(".ajax").fancybox({
		'autoDimensions': true,
		'autoScale': false,
		'overlayOpacity': 0.8,
		'overlayColor': '#555',
		'scrolling': 'no',
		'loop': true,
		'onComplete': function(){ 
			load_standard_tools();
			var customTitle = $("#customTitle").html();		
			if( ( customTitle != null ) && customTitle.length > 1 )
			{
				$("#fancybox-title-main").html(customTitle); 
			}
			if( $("#fancybox-title-main").html().length == 0 ||  $("#fancybox-title-main").html() == 'undefined')
			{
				$("#fancybox-title").remove();
			}
		}
	});
}

// ====================================================================================== //
function show(id,s){	
	
	if( $("#"+id).hasClass("hide")){
		if(s!=1){
			$("#"+id).slideToggle(function() { });
		}
		$("#"+id).addClass("show");
		$("#"+id).removeClass("hide");
	}else{
		if(s!=1){
			$("#"+id).slideToggle(function() {  });
		}
		$("#"+id).addClass("hide");
		$("#"+id).removeClass("show");
	}
	if( id == "alerti" || id == 'loading2'){
    	$.fancybox.resize();
	}
	
}

// ====================================================================================== //
// Calculate prices incl tax
function calc_incl(price_ex_view,price_ex,tax_rate,tax_view,price_incl,amount,total)
{
	exv = input_number($("#"+price_ex_view).val());
	exv = exv*1;
	exv = exv.toFixed( 6 );
	
	$("#"+price_ex).val(exv);
	
	rate = $("#"+tax_rate).val();
	
	
	if ( rate.length > '0')
	{

		rate = rate*1;
		rate = (rate+100)/100;
		 
		 incl = exv * rate;
		 tax = incl - exv;
		 
		$("#"+tax_view).html( tax.toFixed( 2 ) );
		$("#"+price_incl).val( incl.toFixed( 2 ) );
		
		amount = $("#"+amount).val();
		amount = amount*1;
		
		tot = exv*amount;
		$("#"+total).html( tot.toFixed( 2 ) );
	
	}
	
}



// ====================================================================================== //
// Calculate prices excl tax
function calc_excl(price_ex_view,price_ex,tax_rate,tax_view,price_incl,amount,total)
{
	exv = input_number( $("#"+price_incl).val() );
	exv = exv*1;
	exv = exv.toFixed( 6 );

	rate = $("#"+tax_rate).val();
	
	
	if ( rate.length > '0')
	{
		rate = rate*1;
		rate = (rate+100)/100;
		 
		 excl = exv / rate;
		 tax = exv - excl;
		 
		$("#"+tax_view).html( tax.toFixed( 2 ) );
		$("#"+price_ex).val(excl);
		$("#"+price_ex_view).val( excl.toFixed( 2 ) );
		
		amount = $("#"+amount).val();
		amount = amount*1;
		  
		
		tot = excl*amount;
		$("#"+total).html( tot.toFixed( 2 ) );
		
		
	}
	
	
}

// ====================================================================================== //

function input_number( num )
{
	return num.replace(",",".")*1;
}

// ====================================================================================== //
var hideInfobox;
var infoboxSet = 0;
function infoBox( info )
{
	clearTimeout( hideInfobox );
	if( infoboxSet == 0 )
	{
		$("#header").append("<div id='infoBox'></div>");
		infoboxSet = 1;
	}
	$("#infoBox").hide();
	$("#infoBox").html( info );
	$("#infoBox").fadeTo( 1500 , 0.92);
	hideInfobox = setTimeout( hideInfoBox, 5000 ); 
}
function hideInfoBox()
{
	$("#infoBox").fadeOut( 2000 );
}

// ====================================================================================== //
function go( url )
{
	top.document.location.href = url;
}

// ====================================================================================== //
// Nice functions																		  //
// ====================================================================================== //
function explode (delimiter, string, limit) {
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||
        typeof arguments[1] == 'undefined' ) {
        return null;
    }
 
    if ( delimiter === '' ||
        delimiter === false ||
        delimiter === null ) {
        return false;
    }
 
    if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' ) {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}
// ====================================================================================== //
function fancyresize()
{
	unloadTinyMCE();
	$.fancybox.resize();
	loadTinyMCE()
}


function filemanager(field_name, url, type, win) 
{
		var ajaxfilemanagerurl = "/modules/admin/filemanager/ajaxfilemanager.php?editor=tinymce";
			switch (type) {
				case "image":
					break;
				case "media":
					break;
				case "flash": 
					break;
				case "file":
					break;
				default:
					return false;
			}
			
		 tinyMCE.activeEditor.windowManager.open({
				file : ajaxfilemanagerurl,
				title : 'File Manager',
				width : 782,  
				height : 440,
				resizable : "yes",
				inline : "yes",  
				close_previous : "no"
			}, {
				window : win,
				input : field_name
		});
    return false;
}


//BASE64 En/DEcoder
	(function($){
		
		var keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		
		var uTF8Encode = function(string) {
			string = string.replace(/\x0d\x0a/g, "\x0a");
			var output = "";
			for (var n = 0; n < string.length; n++) {
				var c = string.charCodeAt(n);
				if (c < 128) {
					output += String.fromCharCode(c);
				} else if ((c > 127) && (c < 2048)) {
					output += String.fromCharCode((c >> 6) | 192);
					output += String.fromCharCode((c & 63) | 128);
				} else {
					output += String.fromCharCode((c >> 12) | 224);
					output += String.fromCharCode(((c >> 6) & 63) | 128);
					output += String.fromCharCode((c & 63) | 128);
				}
			}
			return output;
		};
		
		var uTF8Decode = function(input) {
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
			while ( i < input.length ) {
				c = input.charCodeAt(i);
				if (c < 128) {
					string += String.fromCharCode(c);
					i++;
				} else if ((c > 191) && (c < 224)) {
					c2 = input.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				} else {
					c2 = input.charCodeAt(i+1);
					c3 = input.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
			}
			return string;
		}
		
		$.extend({
			base64Encode: function(input) {
				var output = "";
				var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
				var i = 0;
				input = uTF8Encode(input);
				while (i < input.length) {
					chr1 = input.charCodeAt(i++);
					chr2 = input.charCodeAt(i++);
					chr3 = input.charCodeAt(i++);
					enc1 = chr1 >> 2;
					enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
					enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
					enc4 = chr3 & 63;
					if (isNaN(chr2)) {
						enc3 = enc4 = 64;
					} else if (isNaN(chr3)) {
						enc4 = 64;
					}
					output = output + keyString.charAt(enc1) + keyString.charAt(enc2) + keyString.charAt(enc3) + keyString.charAt(enc4);
				}
				return output;
			},
			base64Decode: function(input) {
				var output = "";
				var chr1, chr2, chr3;
				var enc1, enc2, enc3, enc4;
				var i = 0;
				input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
				while (i < input.length) {
					enc1 = keyString.indexOf(input.charAt(i++));
					enc2 = keyString.indexOf(input.charAt(i++));
					enc3 = keyString.indexOf(input.charAt(i++));
					enc4 = keyString.indexOf(input.charAt(i++));
					chr1 = (enc1 << 2) | (enc2 >> 4);
					chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
					chr3 = ((enc3 & 3) << 6) | enc4;
					output = output + String.fromCharCode(chr1);
					if (enc3 != 64) {
						output = output + String.fromCharCode(chr2);
					}
					if (enc4 != 64) {
						output = output + String.fromCharCode(chr3);
					}
				}
				output = uTF8Decode(output);
				return output;
			}
		});

	})(jQuery);
	
	
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});


