var current_page;
var selected;
var current_pageid;
var current_destination;
var colors  = new Array();
var pages = new Array();


function loadpage(pageid, destination){
		selected = pageid;

		if (pages[pageid] == true) {
			current_page = base_url+'index.php/b2b/catalog/overview/'+catalog_chapter+'/'+selected;
			var location = 	base_url+'index.php/b2b/catalog/catPage/'+destination;
			
			$.post(location,{current_page:current_page}, function(result){
				$("#scrollpos").attr({'value':$(".chapter_pages").scrollTop()}); 
				$("#cat_page").html(result);
				$("area").easyTooltip();
				$("#cat_page").show();
				$("#paginator a").each(function(e){
				if ($(this).attr('href') == '#') $(this).attr({'class':'disabled'});
				//	$(this).hide();
				})
			})
		}
			 
	}
	
$(document).ready(function(){

	$("#cat_items_view").draggable();

	$(".checkstock").click(function(){
		var location = 	base_url+'index.php/b2b/items/get_stocklevel/'+$(this).attr('rel');
		$.post(location,{}, function(result){
			alert(result);
		})
	})

	if ($(".divider").length > 0) {
		$(".divider").each(function(){
			$(this).parent().parent().css({'background-color':'#505050'});
		})
	}

	$(".hoverorder").mouseenter(function(){
		$("tr").each(function(){
			//if ($(this).children(":first").children(":first").html() == $(this).attr('rel')) alert('x');
		})
	})

	$(".list table").tablesorter(); 

	$(".page_item").click(function(e){

		$('.page_item').css({'background-color':'#565656'});
		$(this).css({'background-color':'#31302E'});
		
		
		current_pageid =  $(this).attr('id');
		pages[$(this).attr('id')-1] = false;
		pages[$(this).attr('id')+1] = false;
		pages[$(this).attr('id')] = true;
		current_destination = $(this).attr('rel');
		
		loadpage( current_pageid, current_destination);
		
		
	
	/* 	loadpage( $(this).attr('id'), $(this).attr('rel')); */
	//	setTimeout('alert("test")', 5000);
		/* selected = $(this).attr('id')
		$(this).css({'background-color':'#343434'});
		$(this).prev().css({'background-color':'#565656'});
		$(this).next().css({'background-color':'#565656'});
		current_page = base_url+'index.php/b2b/catalog/overview/'+catalog_chapter+'/'+selected;
		var location = 	base_url+'index.php/b2b/catalog/catPage/'+$(this).attr('rel');
		
		$.post(location,{current_page:current_page}, function(result){
			$("#scrollpos").attr({'value':$(".chapter_pages").scrollTop()}); 
			$("#cat_page").html(result);
			$("area").easyTooltip();
			$("#cat_page").show();
			$("#paginator a").each(function(e){
			if ($(this).attr('href') == '#') $(this).attr({'class':'disabled'});
			//	$(this).hide();
			})
		}) */
		
	})
	
	
	
	
	$(".chapter").click(function(e){

		var location = 	base_url+'index.php/b2b/catalog/catPage/'+$(this).attr('rel');
		if ($(".cat_impression").length > 0){	
			$(".cat_impression").remove();
			$(".cat_photo").remove();
		}
		$.post(location,function(result){
			//$("#scrollpos").attr({'value':$(".chapter_pages").scrollTop()}); 
			$("#cat_page").html(result);
			$("area").easyTooltip();
			$("#cat_page").show();
		})
		
	})
	
	$('area').live('mouseenter', function(e) { 
	
	
		$("area").easyTooltip();
		var position = $(".view_"+$(this).attr('rel')).find('a').position();
		delta = 250-position.top;
	
		colors[$(this).index()] = $(".view_"+$(this).attr('rel')).find('a').css('color');
		$(".view_"+$(this).attr('rel')).css({'color': '#FFAA2A'});
		$(".view_"+$(this).attr('rel')).find('a').css({'color': '#FFAA2A'});
		
		if ($("#cat_items_view").height() > 300 ) {
	/*	$("#cat_items_view").animate({
			
			top: delta
		  }, 150, function() {
			// Animation complete.
		  });*/
		}
	})
	
	$('area').live('mouseleave', function() { 
		
		//alert(	$(this).attr('rel'));
	//	$(".view_"+$(this).attr('rel')).find('a').css({'color': colors[$(this).attr('rel')]});
	//	$(".view_"+$(this).attr('rel')).css({'color': colors[$(this).attr('rel')]});
		$(".view_"+$(this).attr('rel')).find('a').css({'color': '#FFF'});
		$(".view_"+$(this).attr('rel')).css({'color': '#FFF'});
	
	})
	
	
	$('.catitem').live('click', function(e) { 
	
	$("#cat_items_view").animate({
			
			top: delta
		  }, 150, function() {
			// Animation complete.
		  });
	
	return false; 
	  
	});
	
	$('.catitem').live('dblclick', function(e) { 
	
	$("#cat_items_view").animate({
			
			top: delta
		  }, 150, function() {
			// Animation complete.
		  });
	

	    var url = $(this).attr('href'); 
		rel = $("#catimg").attr('rel');
	
		current_page = base_url+'index.php/b2b/catalog/overview/'+catalog_chapter+'/'+selected;
		var location = 	base_url+'index.php/b2b/catalog/catPage/000';
	
		$.post(location,{current_page:current_page}, function(result){
			rel = $(e.target).attr('rel').split(',');
			selected = rel[0];
			
			window.location = base_url+'index.php/b2b/catalog/item/'+selected;
		
		})
		
		return false; 
	});
	
 	if ($("#catimg").length > 0) {
		var pos = $('#'+$('#catimg').attr('rel')).offset();
	
		$(".chapter_pages").scrollTo({ top:pos.top-150, left:0 });
		if ($('#page_item').length > 0) $(".chapter_pages").scrollTo($('#'+$('#page_item').attr('rel')));
		$('#'+$('#catimg').attr('rel')).css({'background-color':'#343434'});
	} 
	
	$("#paginator a").each(function(e){
		if ($(this).attr('href') == '#') $(this).attr({'class':'disabled'});
	//	$(this).hide();
	})
	
	$(".b2badd_item").click(function(){
		//alert( $(this).prev().val());
		add_item($(this).attr('rel'), $(this).prev().val());
		return false;
	})
		
	$(".catadd_item").click(function(){

		add_item($(this).attr('rel'), $(this).prev().val(), '2');
		return false;	
	})
	
	$(".select_quantity").change(function(){
		$(this).next().attr({'value':$(this).val()});
	})
	 
	//$('.chapter_pages').jScrollPane(); 

	if ($(".customers").length > 0)
	{
		$("#leftbar").remove();
		$("#content").css({'float':'left','width':'1006px'});
	
		$(".customers").css({'width':'810px','float':'left'});
		/*
		$(".customers").mouseenter(function(){
			$("#rightbar").hide();
			$(".customers ").css({'width':'1006px'});
		})
		
		$("#content_body").mouseleave(function(){
			$("#rightbar").show();
			$(".customers ").css({'width':'810px'});
		})*/
		
	}
	
	$("#sync").click(function(e){
		$(this).html("Bezig met sychroniseren....");
	//	$(this).attr({'href':''});
		
	})
		
	$("#newcustomer input").change(function(){
		if ($(this).attr('id') == 'ip_Phone') $("#ip_ConsumerPhoneNo").attr({'value':$(this).attr('value')});
		if ($(this).attr('id') == 'ip_CountryRegionCode') $("#ip_ConsumerCountryCode").attr({'value':$(this).attr('value')});
		$("#"+$(this).attr('id').replace('ip_','ip_Consumer')).attr({'value':$(this).attr('value')});
	})
	
	$("#tab_catalog").click(function(e){
		$("#normal_cart").hide();
		$("#catalog_cart").show();
		$("#tab_normal").css({'background-color':'#787878'});
		$(this).css({'background-color':'#3E3E3E'});
		$('#content').css({'height':$("#catalog_cart").height()+700});
		$('#rightbar').css({'height': $("#content").height()});
	})
		
	$("#tab_normal").click(function(e){
		$("#normal_cart").show();
		$("#catalog_cart").hide();
		$("#tab_catalog").css({'background-color':'#787878'});
		$(this).css({'background-color':'#3E3E3E'});
		$('#content').css({'height':$("#normal_cart").height()+700});
		$('#rightbar').css({'height': $("#content").height()});
	})

	$("#paginator span").live('click',function(){
		var page = ($(this).attr('rel'));
		$("#content_body").load(page+' #content_body', function(result){
			
		}); 
	})
		
	if ($("#nextpage").length > 0)
	{
		
		$("#pageloader").load($("#nextpage").attr('href')+" #pageloader");
	}
	
	if ($("#nextitempage").length > 0 && $("#VTW_bar").length > 0)
	{
	
		$("#pageloader").load($("#nextitempage").parent().attr('href')+" #pageloader");
		$("body").scrollTo(1000,0);
	}
		
})

