/*
var _gaq = _gaq || [];
	_gaq.push(['gwo._setAccount', 'UA-8007393-1']);
*/

$(function(){

	// go to shop/product link
		
	$("a.visitshoplink").click(function(){
		
		//_gaq.push(['gwo._trackPageview', '/0233425781/goal']);
					
		var newurl = $(this).attr('href');
		var newwin = window.open(newurl);

		return false;
			
	});
});

/*
(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() {

		/*
		// abc
		if ($("#websitesetting_liststyle").length) {
			var val = $("#websitesetting_liststyle").attr('rel');
			$.cookie('listtype', val, { expires: 7, path: '/'});
			if (val == 'c') {
				$("div.productslist div a").css({color:'black'});
			}
		}
		// abcd
		if ($("#websitesetting_pricelisttitle").length) {
			var val = $("#websitesetting_pricelisttitle").attr('rel');
			if (val == 'b') $("#prosellerstitle").text('Prijzen');	
			if (val == 'c') $("#prosellerstitle").text('Vergelijk prijzen');
			if (val == 'd') $("#prosellerstitle").text('Prijzen vergelijken');	
		}
		*/
		
		// ie6 fixes css

		if ($.browser.msie) $("body").addClass('MSIE');
		if ($.browser.msie && $.browser.version == 6 ) $("body").addClass('MSIE6');
		
		// product images right bloack spy
		
		$("#viewedproducts ul li img").error(function(){
			$(this).attr("src", website_url+"mod/laptopsvergelijken/nopicture.gif");
		});
		
		// Specs
		
		specs_def = $.extend(true, {}, specs_range); 
		cookie_arg = { expires: 7, path: '/'};
		
		var updatelink = function() {
			
			var url = website_url+'selection/';
			url += '&price_min='+$("#pricerange").slider("values", 0)+'&price_max='+$("#pricerange").slider("values", 1);
			url += '&screenwidth_min='+$("#screenwidthrange").slider("values", 0)+'&screenwidth_max='+$("#screenwidthrange").slider("values", 1);
			url += '&mem_min='+$("#memrange").slider("values", 0)+'&mem_max='+$("#memrange").slider("values", 1);
			url += '&hdmem_min='+$("#hdmemrange").slider("values", 0)+'&hdmem_max='+$("#hdmemrange").slider("values", 1);
			url += '&weight_min='+$("#weightrange").slider("values", 0)+'&weight_max='+$("#weightrange").slider("values", 1);
			
			if ($("input[name=moreoptions]:checked").val() == '1') {
				$("div.selectoptions input[type=checkbox]").each(function(){
					var name = $(this).attr('name');
					if ($(this).is(':checked')) url += '&'+name+'=1';
				});
			}
			
			$("#showselection").attr('href',url);
	
		}
				
		if (typeof $.cookie('specs_def_price_min') == 'string') specs_def.price_min = parseInt($.cookie('specs_def_price_min')); 
		if (typeof $.cookie('specs_def_price_max') == 'string') specs_def.price_max = parseInt($.cookie('specs_def_price_max'));
		
		if (typeof $.cookie('specs_def_screensize_min') == 'string') specs_def.screensize_min = parseInt($.cookie('specs_def_screensize_min')); 
		if (typeof $.cookie('specs_def_screensize_max') == 'string') specs_def.screensize_max = parseInt($.cookie('specs_def_screensize_max'));
		
		if (typeof $.cookie('specs_def_mem_min') == 'string') specs_def.mem_min = parseInt($.cookie('specs_def_mem_min')); 
		if (typeof $.cookie('specs_def_mem_max') == 'string') specs_def.mem_max = parseInt($.cookie('specs_def_mem_max'));
		
		if (typeof $.cookie('specs_def_hdmem_min') == 'string') specs_def.hdmem_min = parseInt($.cookie('specs_def_hdmem_min')); 
		if (typeof $.cookie('specs_def_hdmem_max') == 'string') specs_def.hdmem_max = parseInt($.cookie('specs_def_hdmem_max'));
		
		if (typeof $.cookie('specs_def_weight_min') == 'string') specs_def.weight_min = parseInt($.cookie('specs_def_weight_min')); 
		if (typeof $.cookie('specs_def_weight_max') == 'string') specs_def.weight_max = parseInt($.cookie('specs_def_weight_max'));
		
		// Specs, more options
		
		$("input[name=moreoptions]").click(function(){
		
			if ($(this).val() == '1') {
				if (typeof $.cookie('specs_def_moreoptions') == 'string') $("div.selectoptions").show();
				else $("div.selectoptions").show('fast');
			} else {
				$("div.selectoptions").hide('fast');
			}
			
			$.cookie('specs_def_incbusiness', $("input[name=moreoptions]:checked").val(), cookie_arg);
			
		});
		
		if (typeof $.cookie('specs_def_moreoptions') == 'string') {
			var num = parseInt($.cookie('specs_def_moreoptions'));
			num = 1-num;
			$("input[name=moreoptions]:eq("+num+")").click();
		} 
		
		$("input[name=moreoptions]").click(function(){
			$.cookie('specs_def_moreoptions', $("input[name=moreoptions]:checked").val(), cookie_arg);
			updatelink();
		});
		
		// checkboxes
		
		$("div.selectoptions input[type=checkbox]").each(function(){
		
			var name = $(this).attr('name');
			
			if ($.cookie('specs_def_'+name) == '1') {
				$(this).click();
			}
		
		});
		
		$("div.selectoptions input[type=checkbox]").click(function(){
		
			var name = $(this).attr('name');
			
			if (name == 'passive3d' || name == 'active3d') {
				$("input[name=screentype]:eq(2)").click();
			}
			
			var val = '0';
			if ($(this).is(':checked')) val = '1';

			$.cookie('specs_def_'+name, val, cookie_arg);
			updatelink();
			
		});
	
		// price
		$("#pricerange").slider({
			range: true,
			min: specs_range.price_min,
			max: specs_range.price_max,
			step:10,
			values: [specs_def.price_min, specs_def.price_max],
			slide: function(event, ui) {
				var a = Math.round(ui.values[0]);
				var b = Math.round(ui.values[1]);
				$.cookie('specs_def_price_min', a, cookie_arg);
				$.cookie('specs_def_price_max', b, cookie_arg);
				$("#priceamount").html('&#8364; '+a+' - &#8364; '+b);
			},
			change: function () {
				updatelink();
			}
		});
		$("#priceamount").html('&#8364; ' + $("#pricerange").slider("values", 0) + ' - &#8364; ' + $("#pricerange").slider("values", 1));
		
		// screenwidth
		$("#screenwidthrange").slider({
			range: true,
			min: specs_range.screensize_min,
			max: specs_range.screensize_max,
			step:1,
			values: [specs_def.screensize_min, specs_def.screensize_max],
			slide: function(event, ui) {
				var a = Math.round(ui.values[0]);
				var b = Math.round(ui.values[1]);
				$.cookie('specs_def_screensize_min', a, cookie_arg);
				$.cookie('specs_def_screensize_max', b, cookie_arg);
				$("#screenwidthamount").html(a + ' - ' + b + ' inch');
				
			},
			change: function () {
				updatelink();
			}
		});
		$("#screenwidthamount").html($("#screenwidthrange").slider("values", 0) + ' - ' + $("#screenwidthrange").slider("values", 1) +' inch');
		
		// mem
		$("#memrange").slider({
			range: true,
			min: specs_range.mem_min,
			max: specs_range.mem_max,
			step:1,
			values: [specs_def.mem_min, specs_def.mem_max],
			slide: function(event, ui) {
				var a = Math.round(ui.values[0]);
				var b = Math.round(ui.values[1]);
				$.cookie('specs_def_mem_min', a, cookie_arg);
				$.cookie('specs_def_mem_max', b, cookie_arg);
				$("#memamount").html(a + ' - ' + b + ' gb');
				
			},
			change: function () {
				updatelink();
			}
		});
		$("#memamount").html($("#memrange").slider("values", 0) + ' - ' + $("#memrange").slider("values", 1) +' gb');
		
		// harddisk mem
		$("#hdmemrange").slider({
			range: true,
			min: specs_range.hdmem_min,
			max: specs_range.hdmem_max,
			step:10,
			values: [specs_def.hdmem_min, specs_def.hdmem_max],
			slide: function(event, ui) {
				var a = Math.round(ui.values[0]);
				var b = Math.round(ui.values[1]);
				$.cookie('specs_def_hdmem_min', a, cookie_arg);
				$.cookie('specs_def_hdmem_max', b, cookie_arg);
				$("#hdmemamount").html(a + ' - ' + b + ' gb');
			},
			change: function () {
				updatelink();
			}
		});
		$("#hdmemamount").html($("#hdmemrange").slider("values", 0) + ' - ' + $("#hdmemrange").slider("values", 1) +' gb');
		
		// weight
		$("#weightrange").slider({
			range: true,
			min: specs_range.weight_min,
			max: specs_range.weight_max,
			step:1,
			values: [specs_def.weight_min, specs_def.weight_max],
			slide: function(event, ui) {
				var a = Math.round(ui.values[0]);
				var b = Math.round(ui.values[1]);
				$.cookie('specs_def_weight_min', a, cookie_arg);
				$.cookie('specs_def_weight_max', b, cookie_arg);
				$("#weightamount").html(a + ' - ' + b + ' kg');
			},
			change: function () {
				updatelink();
			}
		});
		$("#weightamount").html($("#weightrange").slider("values", 0) + ' - ' + $("#weightrange").slider("values", 1) +' kg');
		
		$("a.fg-button, input.fg-button").hover(function(){ 
				$(this).addClass("ui-state-hover"); 
		}, function(){ 
			$(this).removeClass("ui-state-hover"); 
		});		
		
		updatelink();
		
});

// compair list


addToCompairListCookie = function(productid) {
	var cookie_arg = { expires: 30, path: '/'};
	var cookie_str = $.cookie('compairlist');
	if (cookie_str == null) cookie_str = '';
	cookie_str = cookie_str+'-'+productid;
	$.cookie('compairlist', cookie_str, cookie_arg);
}

$(function(){

	// add product to list button
	if ($("#prodesc #addcompairbutton").length) {
	
		var relstr = $("#prodesc #addcompairbutton").attr('rel');
		var relarr = relstr.split('|');
		productid = relarr[0];
		productname = relarr[1];

		$("#prodesc #addcompairbutton").click(function(){
		
			if ($(this).is('.disabled')) {
				return;
			}
			
			addToCompairListCookie(productid);
			
			if (!$("div.compairlist").length) {
				var button = '<a id="showcompairlist" class="ui-state-default ui-corner-all fg-button disabled" href="#">Bekijk vergelijking</a>';
				$("#rightblock").prepend('<div class="block"><div class="headinnerblock"><b>Vergelijk laptops</b></div><div class="innerblock compairlist"><ul></ul>'+button+'</div></div>');
			}
			
			var checkbox = '<input type="checkbox" value="'+productid+'" name="compairlistproductsselected[]" />';
			var delbutton = '<img class="delete_icon" src="'+website_url+'tpl/laptopsvergelijken2/delete_icon.png">';
			$("div.compairlist ul").append('<li>'+checkbox+'<a href="'+document.location.href+'">'+productname+'</a>'+delbutton+'</li>');
			
			$(this).addClass('disabled').text('Product staat in uw vergelijkingslijst');
			
			if ($("div.compairlist input:checkbox:checked").length > 2) {
				$("div.compairlist input:checkbox").not(':checked').attr('disabled', true);
			} else {
			
				if ($("div.compairlist ul li").length < 4) {
					$("div.compairlist input[value='"+productid+"']:checkbox").not(':checked').attr('checked','checked').click().attr('checked','checked');
				}
			
			}
			
		});

		if ($.cookie('compairlist') != null && $.cookie('compairlist').indexOf('-'+productid) !== -1) {
			$("#prodesc #addcompairbutton").addClass('disabled').text('Product staat in uw vergelijkingslijst');
		}
	
	}
		
	// button
	$("#showcompairlist").live('click', function(){
	
		if ($("div.compairlist ul li").length < 2) {
			alert('Voeg eerst nog een product toe om te kunnen vergelijken.');
			return false;
		} else {
			var ids = [];
			$("div.compairlist input:checkbox:checked").each(function() {
				ids.push(this.value);
			});
			
			if (ids.length > 1) window.location = website_url+"vergelijk/&product_ids="+ids;
			else alert('Selecteer 2 of 3 producten om te kunnen vergelijken.');

			return false;
		}
	});
	
	// checkbox click
	$("div.compairlist input:checkbox").live('click', function(){
	
		if ($("div.compairlist input:checkbox:checked").length > 2) {
			$("div.compairlist input:checkbox").not(':checked').attr('disabled', true);
		}
		if ($("div.compairlist input:checkbox:checked").length < 3) {
			$("div.compairlist input:checkbox").not(':checked').removeAttr('disabled');
		}
		
		var ids = [];
		$("div.compairlist input:checkbox:checked").each(function() {
			ids.push(this.value);
		});
		
		var cookie_arg = { expires: 7, path: '/'};
		var cookie_str = ids.join('-');
		
		$.cookie('compairlistselected', cookie_str, cookie_arg);
		
	});
	
	// remove button
	$("div.compairlist ul li").live('mouseover mouseout',
			function (ev) {
				if (ev.type == 'mouseover') {
					$(this).addClass("hover");
            }
            if (ev.type == 'mouseout') {
                $(this).removeClass("hover");
            }
    });
	
	// remove button
	$("div.compairlist ul li img").live('click', function() {

			var remove_id = $(this).parent("li").children('input:checkbox').val();
			var remove_name = $(this).parent("li").children('a').text().replace( /\s\s+/g, ' ' );
			var remove_url = $(this).parent("li").children('a').attr('href');
			
			if (confirm(remove_name+" verwijderen uit uw vergelijkingslijst?")) {
			
				// remove from compairlist selected cookie
				var ids = [];
				$("div.compairlist input:checkbox:checked").each(function() {
					if (this.value != remove_id) ids.push(this.value);
				});
			
				var cookie_arg = { expires: 7, path: '/'};
				var new_cookie_str = ids.join('-');
				$.cookie('compairlistselected', new_cookie_str, cookie_arg);
				
				// remove from compair list cookie
				var cookie_str = $.cookie('compairlist');
				if (cookie_str != null) {
				
					var compairlist_arr = cookie_str.split('-');
					
					var ids = [];
					$(compairlist_arr).each(function(key, value) {
						if (value != remove_id && value != '') ids.push(value);
					});
					
					var new_cookie_str = '-'+ids.join('-');
					
					var cookie_arg = { expires: 30, path: '/'};
					$.cookie('compairlist', new_cookie_str, cookie_arg);
					
				}	
				
				// delete	
				$(this).parent("li").remove();
				if ($("div.compairlist ul li").length == 0) {
					$("div.compairlist").parent('div.block').remove();
					
				}
				
				if (remove_url == document.location.href) {
					$("#addcompairbutton.disabled").removeClass('disabled').text('Voeg toe aan vergelijking');
				}
				
				if ($("div.compairlist input:checkbox:checked").length < 3) {
					$("div.compairlist input:checkbox").not(':checked').removeAttr('disabled');
				}
				
			}
			
	});
	
	// init checkboxes
	if ($("div.compairlist").length) {
	
		if ($.cookie('compairlistselected') != null) {
			var ids = $.cookie('compairlistselected').split('-');
			for(var i=0; i<ids.length; i++) {
				var value = ids[i];
				$("div.compairlist input[value='"+value+"']:checkbox").attr('checked','checked');
			}
			if ($("div.compairlist input:checkbox:checked").length > 2) {
				$("div.compairlist input:checkbox").not(':checked').attr('disabled', true);
			}
		}	
		
	}
	
	
});

// menu block with tabs

$(function(){
	
	$("div.blocktabs div.innerblock ul").hide();
	
	$("div.blocktabs div.headinnerblock").click(function(){
		
		var classes = $(this).find(".blocktabtitle").attr('class');
		var classes = classes.split(' ');
		var relname = null;
		
		for (i=0;i<classes.length;i++) {
			if (classes[i].match('tabrel-')) {
				relname = classes[i].split('-')[1];
			}
		}
		
		if (relname != null) {
			$("div.blocktabs div.innerblock ul").hide();
			$("div.blocktabs div.innerblock ul."+relname).show();
			$("div.blocktabs div.headinnerblock").removeClass('default');
			$(this).addClass('default');
		}
		
	});
	
	$("div.blocktabs div.headinnerblock.default").trigger('click');
	
});

