var scroller;
var scrollindex = 1;
var scrollinterval = 4000;
var scrollduration = 1500;
var bag_scrollpos = 60;
	 

	
function scrollImpression(index){
	$(".impression_btn").css({'background-position':'0px 0px'});
	var current_button = $(".impression_btn").get(index);
	$(current_button).css({'background-position':'0px -23px'});
	
	$("#impressionblock").scrollTo(index*672, scrollduration);
	scrollindex = index+1;
	if (scrollindex > ($(".impressionitem").size()-1)) scrollindex = 0;
	
}


function add_item(itemno, qty, ordertype){
		//$.scrollTo(0,0);
		
		$.post(base_url+'index.php/'+shoptype+'cart/addItem/'+qty, {itemNo: itemno, ordertype: ordertype},function(result){
			
			if(result !== 'nostock'){
				
				$("#bag_items_content").scrollTo(bag_scrollpos,500);
				bag_scrollpos += 59;
				if ($('#addcount').length > 0) $('#addcount').html(' ('+qty+'x)');
	
				$('.itemadded').show();
				$('.itemadded').fadeOut(2000);
				if (bag_scrollpos > 200) {
					bag_scrollpos = 59;
					$("#bag_items_content").scrollTo(53, 0);
					bag_scrollpos += 59;
					
				}
				$('.shopping_bag_add').html(result);
				return false;
			}
			else alert($("#nostock").attr('value'));
		})
		
		return false;
		
	}

$(document).ready(function(){

	var img_dir = base_url+'public/images/elements/';
	if ($("#home_impression").length ==0 && screen.width >= 1280 && $("#navigator").length ==0 ) $("body").append('<div id="navigator"><span class="nav_button"><a href="'+base_url+'index.php/b2c"><img width="40px" src="'+img_dir+'buy.jpg"></a></span><span class="nav_button"><a href="'+base_url+'index.php/home/experience"><img width="90px" src="'+img_dir+'experience.jpg"></a></span><span class="nav_button"><a href="'+base_url+'index.php/b2b"><img width="40px" src="'+img_dir+'sell.jpg"></span></a></div>');

	if ($("#leftbar").length == 0){
		
		if ($("#xcontent_body").length == 0) $("#navigator").css({'left':$("#content_body").position().left-108});	
			else  $("#navigator").css({'left':$("#xcontent_body").position().left-123});	
	}
	else
	{
		$("#navigator").css({'left':$("#leftbar").position().left-122});	
		$("#navigator").css({'top':'114px'});	
	}
	


	$("#logo").click(function(){
		window.location = base_url;
	})	
	
	$(".sb_details").live('click',function(){
		$(this).html($(".sb_total").last().html());
		setTimeout("$('.sb_details').html($('.sb_details').attr('rel'))", 5000);
	})
	
	//$(".ptmdlogo").css({'left':$(".coll_photo img").position().left-200});

	
	$(".impression_btn:first").css({'background-position':'0px -23px'});
	var content_height = $("#content").height()
	var leftbar_height = $("#leftbar").height()

	
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	if (!is_chrome) jQuery(document.body).imageZoom();

	$("#impressionblock").scrollTo(0,0);
	
	$("#bag_items_content").scrollTo(0);	
	
	if (leftbar_height >= content_height) $('#content').css({'height':leftbar_height});

	if ($("#maingroupblock").length > 0) $('#rightbar').css({'height': $("#content_body").height()}); else $('#rightbar').css({'height':$('#content').height()}); 
	
//	$("#maingroupblock").css({'height': leftbar_height-$("#impressionblock").height()});
	
//	$(".maingroupblockitem").css({'height':$("#maingroupblock").height()*0.5});
	
	$("#middlecontainer").css({'height': $("#content").height()});
	
	if ($("#maingroupblock").length > 0) $("#content").css({'background-color': '#5E5750'});
	
	$("."+$("#current_lang").val()).css({'background-position':'-16px'});

	$("#num_res").change(function(){
		$("#num_res_form").submit();
	})	
	
	$("#num_cols").change(function(){
		$("#num_cols_form").submit();
	})
	
	$("#sort_filter").change(function(){
		$("#set_sorting").submit();
	})
	
	$("#color_filter").change(function(){
		$("#set_color").submit();
	})
	
	$("#search_field, #storelocator_search").click(function(){
		if ($(this).attr('rel') !== '1') {
			$(this).attr({'value':''});
			$(this).attr({'rel':'1'});
		}	
	})
	
	$(".listitem").mouseover(function(e){
		$(this).find("p").scrollTo(100, 500);
	})	


	//artikelen toevoegen aan winkelwagen
	$(".add_item").click(function(e){
	
		add_item($(this).attr('rel'),1);
		return false;
	})
	
	
	
	//afmetingen laten zien
	$(".item_img_placeholder").mouseenter(function(){
		$(this).find('span').show();
		if ($(this).find('span').css('margin-top') == '80px'){
			$(this).find('span').animate({ 
	    
	        marginTop: "-30px"

			}, 400 );
		}
	})
	
	//afmetingen verbergen
	$(".item_img_placeholder").mouseleave(function(){
	
			$('.listitem_dim').css({'margin-top':'80px'}); 
			$('.listitem_dim').hide();
	
		
	})
	
	//volgend artikel in overzicht
	$("#next_related").click(function(){
		next_item(); 
	})	
	
	function next_item(){
		var next_link = $(".related_selected").next(".item_img_placeholder").find('a').attr('href');
		if (next_link) window.location = next_link + '/next';
	}
	
	function prev_item(){
		var prev_link =  $(".related_selected").prev(".item_img_placeholder").find('a').attr('href');
		if (prev_link) window.location = prev_link + '/prev';
	}
	
	if ($("#highlight_postcode").length > 0) {
		$("#storelocator_search").css({'background-color':'#F7F797'}); 
	}
	
	$(window).keydown(function(event){
		
		
		
		if ($("#item_content").length > 0) {
			if (event.keyCode == 39) next_item();
			if (event.keyCode == 37) prev_item();
			//if (event.keyCode == 13)  add_item();
			
		}
		
		if ($("#itemsoverview").length > 0) {
			var next_link = ($("#paginator").find('strong').next('a').attr('href'));
			var prev_link = ($("#paginator").find('strong').prev('a').attr('href'));
			
			if (event.keyCode == 39 && next_link) {
				window.location = next_link;
			
			}
			if (event.keyCode == 37 && prev_link){
				window.location = prev_link;
			
			}
		
		}
		
		
		
	})	
	
	//vorig artikel in overzicht
	$("#prev_related").click(function(){
		prev_item();
	})
	
	
	if ($("#direction").val() =='next') {
		var position =	$(".related_selected").position().left - $("#related_items").position().left+30;
	
		//gerelateerde artikelen. Huidig artikel centeren
		$("#related_items").scrollTo($(".related_selected").prev().prev().prev(), 0, {axis:'x'} );
		$("#related_items").scrollTo($(".related_selected").prev().prev(), 500, {axis:'x'});
		if (position < 200) $("#related_items").css({'background-position': position+'px 80px'}); 
	} 

	if ($("#direction").val() =='prev') {
		var position =	$(".related_selected").position().left - $("#related_items").position().left+30;

		//gerelateerde artikelen. Huidig artikel centeren
		$("#related_items").scrollTo($(".related_selected").prev(), 0, {axis:'x'} );
		$("#related_items").scrollTo($(".related_selected").prev().prev(), 500, {axis:'x'} );
		if (position < 200)	$("#related_items").css({'background-position': position+'px 80px'});
	} 	

	if ($("#direction").val() == ''){
		/*if ($("#related_items").length > 0) {
			var position =	$(".related_selected").position().left - $("#related_items").position().left+30;
			if (position < 200) $("#related_items").css({'background-position': position+'px 80px'});

			$("#key_note").fadeIn();
		}*/
	}
	
	//breadcrumbs
	
	$("#breadcrumbs_content span").each(function(){
	//	alert(".menuitem_"+$(this).html().toUpperCase());
		if ($(".menuitem_"+$(this).html().toUpperCase().replace(" ","").replace(' ','')+" strong").length > 0) $(this).html($(".menuitem_"+$(this).html().toUpperCase().replace(' ','')+" strong").html());
		if ($(".menuitem_"+$(this).html().toUpperCase().replace(" ","").replace(' ','')+" a").length > 0) $(this).html($(".menuitem_"+$(this).html().toUpperCase().replace(' ','')+" a").html());
	})
	
	$("#itemsoverview h1 span").each(function(){
		if ($(".menuitem_"+$(this).html().replace(" ","")+" strong").length > 0) $(this).html($(".menuitem_"+$(this).html().replace(" ","")+" strong").html());
		if ($(".menuitem_"+$(this).html().replace(" ","")+" a").length > 0) $(this).html($(".menuitem_"+$(this).html().replace(" ","")+" a").html());
	})
	//punt door komma vervangen
	
	if (comma) {
		
		if ($("#item_content .price").length > 0) {
			if ($("#item_content .price").html() !== '')
				$("#item_content .price ").html(cur_sym+$(".price").html().replace('.',','));
		}
		if ($(".carttotalPrice").length > 0) $(".carttotalPrice").html(cur_sym+$(".carttotalPrice").html());
		if ($(".minprice").length > 0) $(".minprice").html(cur_sym+$(".minprice").html());
		
		$(".impression_text .price").each(function(){
			$(this).html(cur_sym+$(this).html().replace('.',','));
		})
		
		
		$(".cartitemprice b").each(function(){
			$(this).html(cur_sym+$(this).html().replace('.',','));
		})

		$(".price, .oldPrice").each(function(){
			$(this).html($(this).html().replace('.',','));
		})
	}
	
	//taal instellen
	$("#language_select .country, .lang .country").click(function(){
		
		$.post(base_url+"index.php/"+shoptype+"customers/setLanguage/", { lang: $(this).attr('rel')}, function(){
		
			window.location = $("#location").val();
		});
		//window.location = base_url+'/index.php/customers/setLanguage/' + $(this).attr('rel');
	})
	
	$("#item_photo").hover(function(){
		$("#item_photo #zoom_note").fadeIn("slow");
	})	
	
	$("#item_photo").click(function(){
		$("#zoom_note").remove();
	})

/* 	$("#impression_buttons .impression_btn").click(function(){
	
		window.clearInterval(scroller);
		var position = ($(this).html()-1);	
		scrollImpression(position);
		
	})
 */
	$("#impressionblock").hover(function(){
		window.clearInterval(scroller);
	})
	
	$("#impressionblock").mouseout(function(){
	//	scroller = window.setInterval("scrollImpression(scrollindex)", 4000);
	})	

	
	//scroller = window.setInterval("scrollImpression(scrollindex)", scrollinterval);
	

	if ($('#num_res :selected').text() == 18) $.scrollTo(200,0);
	if ($('#num_res :selected').text() == 24) $.scrollTo(250,0);
	
	$("#shippingyes").click(function(){
		$("#shippingbox form input:text").removeAttr('disabled');
	})	
	
	$("#shippingno").click(function(){
		$("#shippingbox form input:text").attr({'disabled':'true'});
	})
	
	$(".view_pdf").live('click',function(){
		base_url = 'http://consumer.twee-pi-er.nl/';
		var itemid = $(this).attr('rel');
		pdf_url = base_url+ 'public/pdf/'+itemid+'.pdf';
		$("#pdf").show();
		$("#overlay_bg").show();
		$("#pdf_content").html('<iframe src="http://docs.google.com/gview?url='+pdf_url+'&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>');
	})
	
	$(".close_window").click(function(){
		$(this).parent().parent().hide();
	})
	
	$("#shippingno").click(function(){
		$("#shipping_address").hide();
	})	
	
	$("#shippingyes").click(function(){
		$("#shipping_address").show();
	})

	
})

$(function() {
 
	if ($("#impressionblock").length > 0) {
		 $('.impressionblock_content').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...,
			pager:   '#nav',
			pagerAnchorBuilder: pagerFactory
		});
	}
	   function pagerFactory(idx, slide) {
		
      
        return '<a href="#">'+(idx+1)+'</a>';
    };
   
	$('.arrivalmap').live('click',function(e){
		$("#arrival_popup").remove();
		var groups = $(this).attr('rel').split(',');
		var collection = '<a href="'+base_url+'index.php/'+shoptype+'items/collection/'+groups[0]+'">'+ucfirst(groups[0])+'</a>';
		$('arrival_popup').show('fast');
		var translation;		
		var productgroup;		
		$('.arrivalmap').data.pg = groups[1];
		
		$.get(base_url+'index.php/items/translate_pg/'+groups[1], function(result){
			
			var temp = '<a href="'+base_url+'index.php/'+shoptype+'items/overview/set/'+$('.arrivalmap').data.pg+'">'+result+'</a>';
			$('body').append('<div id="arrival_popup">'+collection+'<br>'+temp+'</div>');
			$("#arrival_popup").css({'top':e.pageY+15,'left':e.pageX+15});
			$("#arrival_popup").show('fast');
		
		});
	})
	
	function ucfirst(str) {
		var firstLetter = str.substr(0, 1);
		return firstLetter.toUpperCase() + str.substr(1);
	}
	
	$("#arr_prev").live('click',function(){
		prev_arrival();
	})
	
   	$("#arr_next").live('click',function(){
		next_arrival();
	})
	
	function next_arrival(){
		$("#arrival_popup").remove();
		if ($("#arr_next").length <= 0) return false;
		var url = $("#arr_next").attr('rel');
		$('#arr_next .loader').html('<img src="'+base_url+'public/images/ajax-loader.gif">');
		$("#arr_next a").hide();
		$('#middlecontainer').load(url +' #middlecontainer', function(){
			$("#arr_next a").show();
			$("#breadcrumbs_content b").html($("#coll_description a").html().toLowerCase());
			
		});
	}
	
	function prev_arrival(){
		$("#arrival_popup").remove();
		if ($("#arr_prev").length <= 0) return false; 
		var url = $("#arr_prev").attr('rel');
		$("#arr_prev a").hide();
		$('#arr_prev .loader').html('<img src="'+base_url+'public/images/ajax-loader.gif">');
		$('#middlecontainer').load(url +' #middlecontainer', function(){
			$("#arr_prev a").show();
			$("#breadcrumbs_content b").html($("#coll_description a").html().toLowerCase());
			
		});
	}
	
	$(window).keydown(function(event){
		if ($('.coll_photo').length > 0){
		
			if (event.keyCode == 37) {
				prev_arrival();
			}
			if (event.keyCode == 39){
				next_arrival();		
			}
	  }
  })
  
  

});
