var currPanel = Array();
currPanel[1] = 0;
var totalPanels = Array()
totalPanels[1] = 0;


function objMaxHeight(obj, val, dots, frontpage) {
 var startLen = $(obj).html().length;
 var meer = '... <span class="bold wit">meer &gt;</span>';
 
 while ($(obj).height() > val) {
  $(obj).html( $(obj).html().substr(0, $(obj).html().length-5) );
 }
 
 if (frontpage) {
  $(obj).html( $(obj).html().substr(0, $(obj).html().length-2) + meer );
  while ($(obj).height() > val) {
   $(obj).html( $(obj).html().substr(0, $(obj).html().length-2-meer.length) + meer );
  }
 } else if (dots && startLen > $(obj).html().length) {
  $(obj).html( $(obj).html().substr(0, $(obj).html().length-3) + '...' );
 }
 
 $(obj).css('height', val+'px');
}

function fixInputDefaults(obj) {
 $(obj).each(function() {
  if ($(this).attr('default')) {
   $(this).focus(function() {
    if ($(this).attr('value') == $(this).attr('default')) {
     $(this).attr('value', ''); 
    }
   }).blur(function() {
    if ($(this).attr('value').length == 0) {
     $(this).attr('value', $(this).attr('default')); 
    }
   });
   
   $(this).attr('value', $(this).attr('default')); 
  }
 });
}

$(document).ready(function() {

	InitPanel(1);

	$('.stnd').blur(function() {
		if($(this).val()==''){
			$(this).val($(this).attr('alt'));
		}
	});
	$('.stnd').focus(function() {
		if($(this).val()==$(this).attr('alt')){
			$(this).val('');
		}
	});
	$('.stnd').each(function(){
		if($(this).val()==''){
			$(this).val($(this).attr('alt'));
		}
	});

	$("#keyword").keypress(function (e) {
		if (e.which == 13) {
		   window.location=url+'shop/0/'+$('#keyword').val();
		}
	});

	if($(".popup")){
	  $(".popup").fancybox({
			'overlayShow':	true,
			'overlayOpacity':0.5,
			'zoomSpeedIn':	300, 
			'zoomSpeedOut':	0
	  });
	}
});


function InitPanel(i) {	
    //Get the height of the first item   
    $('#mask'+i).css({'height':$('#panel'+i+'_0').height()});     
       
    //Calculate the total width - sum of all sub-panels width   
    //Width is generated according to the width of #mask * total of sub-panels   
    $('#panel'+i).width(parseInt($('#mask'+i).width() * $('#panel'+i+' div.panel').length));   
       
	// Get max num of panels
	totalPanels[i] = $('#panel'+i+' div.panel').length-1;
	
	setPaginationBox();
	
	if(totalPanels[i]>=0){

		//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)   
		$('#panel'+i+' div.panel').width($('#mask'+i).width());   
		$('#panel'+i+' div.panel').height($('#mask'+i).height());   

		//Get all the links with rel as panel   
		$('.next'+i).click(function () {
			if (totalPanels[i] > currPanel[i]) {
				currPanel[i]++;
			} else {
				currPanel[i]=0;
			}
			goToPanel(i, false);
		});
		
		$('.prev'+i).click(function () {
			if (currPanel[i] != 0) {
				currPanel[i]--;
			} else {
				currPanel[i]=totalPanels[i];
			}
			goToPanel(i, false);
		});
		
		goToPanel(i, true);	
	}
	
}

function setPaginationBox()
{
	var _pages 	= totalPanels[1];
	var curr	= currPanel[1];
	var start,end;

	if (_pages>12)
	{
		start	= curr==_pages?curr-5:curr-5;
		start	= start>1?start:1;
		end		= curr<5?10:curr+5;
		end		= end >_pages-1?_pages-1:end;						
	}
	else
	{
		start	= 1;
		end		= _pages-1;
	}	

	var output = '<li '+(curr==0?'class="selected"':'')+'><a href="javascript:void(0);" onclick="setPanel(0,1);">1</a> ' + (start>1?'<span>...</span>':'') + '</li>';
	
	for (var i = start; i <= end; i++)
	{
		output	+= '<li '+(curr==i?'class="selected"':'')+'><a href="javascript:void(0);" onclick="setPanel('+i+',1);">'+(i+1)+'</a></li>';
	}
	
	output		+= '<li class="last '+(curr==_pages?'selected':'')+'">' + (end<_pages-2?'<span>...</span>':'') + '<a href="javascript:void(0);" onclick="setPanel('+_pages+',1);">'+(_pages+1)+'</a></li>';
				  
	$('.paginationBox').find('div').html(output); 
}

function setPanel(tab, i) {	
	currPanel[i] = tab;
	goToPanel(i, false);
}

function goToPanel(i, notfirst) {	
	// Links
	if (currPanel[i] == totalPanels[i]) {
		//$('#next'+i).fadeOut("fast");
	} else {
		//$('#next'+i).fadeIn("fast");
	}
	if (currPanel[i] == 0) {
		//$('#prev'+i).fadeOut("fast", function() {
		//	$('#mask'+i).css('margin-left',30);
		//});
	} else {
		//$('#mask'+i).css('margin-left',0);
		//$('#prev'+i).fadeIn("fast");
	}

	// Panel
	var p = '#panel'+i+'_'+currPanel[i];
	
	//Get the height of the sub-panel
	var panelheight = $(p).height();
	
	//set data
	$('#panel'+i+'_value').val($(p).attr('title'));

//Resize the height
	$('#mask'+i).animate({'height':panelheight},{queue:false, duration:500});			
	
	//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
	$('#mask'+i).scrollTo(p, {duration: 800 });	
	
	if(notfirst){
		if($(p+' a.active')){
			//viewphoto($('.wdmBox a.active').attr('id').substr(5));
		} else {
			//viewphoto($(p+' a:first').attr('id').substr(5));
		}
	} else {
		//viewphoto($(p+' a:first').attr('id').substr(5));
	}
	
	setPaginationBox();
}


function logout(){
	$.post('../controller/logout.php', function(){
		window.location='checkout';
	});
}

function show_bankkeuze(val){
	if(val==1){
		$('#ideal').show();
	} else {
		$('#ideal').hide();
	}
}


function friendly_url_encode(tekst){
	tekst = tekst.toLowerCase();
	tekst = tekst.replace(/%/g, '');
	tekst = tekst.replace(/ &amp; /g, '-en-');
	tekst = tekst.replace(/&amp;/g, '-en-');
	tekst = tekst.replace(/ & /g, '-en-');
	tekst = tekst.replace(/&/g, '-en-');
	tekst = tekst.replace(/ \/ /g, '\\');
	tekst = tekst.replace(/\//g, '\\');
	tekst = tekst.replace(/ /g, '-');
	return(tekst);
}

function submitZoek(f)
{
	f.action = f.action + friendly_url_encode($('#keyword').val());
}

function quickadd(id){
	$.post(url+'controller/quickadd.php', { add: id }, function(data){
		$('#basketMSG').html(data);
	});
}

