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

$(function() {

    $(".select")

        .mousedown(function(){
            $(this).css("width", "auto");
        })

        .blur(function(){
            $(this).css("width", '130px');
        })

        .change(function(){
            $(this).css("width", '130px');
        });

});

$(document).ready(function() {
	InitPanel(1);
	InitPanel(2);

	$("input.opslaan").change(function(){
		save(this);
	})
	
	
	  $(".popup").fancybox({
			'overlayShow':	true,
			'overlayOpacity':0.5,
			'zoomSpeedIn':	0, 
			'zoomSpeedOut':	0,
			'frameWidth':900,
			'frameHeight':600
	  });
	
	set_height();
    setInterval("doRequest()", 60000);
});

function doRequest(){
	$.post('../ajax/blank.php');
}

function load_page(url){
	$.get(url, function(data){
		if(url=='http://www.aesculaapwebshop.nl/pages/shoppingcart.php'){
			$('#maindiv_c').val('shoppingcart');
		}
		$('#scrolldiv').html(data);
		set_height();
	});
}

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').length));   
       
	// Get max num of panels
	totalPanels[i] = $('#panel'+i+' div').length-1;
	
	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').width($('#mask'+i).width());   
		   
		//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);
		});
		
		$('#prev'+i).click(function () {
			if (currPanel[i] != 0) {
				currPanel[i]--;
			} else {
				currPanel[i]=totalPanels[i];
			}
			goToPanel(i);
		});
		
		goToPanel(i);	
	}
}

function goToPanel(i) {	
	// 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, 800);	
}

function show_route() {
	$('#route').toggle();
}

function set_height(iframe){
/*	var height
	height = $('#main').css('height');
	height = height.substr(0, height.length-2);

	$('#scrolldiv').css('height',(height-280)+"px");
	$('#overlay').css('height',(height)+"px");
*/
	var height
	height = $('#main').height();
	window_height = $(window).height();
	var scrolldivHeight = (height-250)>(window_height-420)?(height-250):(window_height-420);

	$('#scrolldiv').css('height',scrolldivHeight+"px");
	$('#overlay').css('height',(height>window_height?height:window_height)+"px");
}

function set_periodiek(display){
	if(display){
		$('#periodiek').css('display','block');
		$('#periode_aantal').addClass('verplicht');
		$('#periode_aantal').addClass('aantal');
		$('#vanaf_field').html('Vanaf');
	} else {
		$('#periodiek').css('display','none');
		$('#periode_aantal').removeClass('verplicht');
		$('#periode_aantal').removeClass('aantal');		
		$('#vanaf_field').html('Gewenste leverdatum');
	}
}

function save(input){
	var vars = new Object();
	vars[$(input).attr('name')]  = input.value;
	$.get('ajax/save.php', vars	);
}

function select_all(c){
		var value = '';
		$('.'+c).each(function(){
			if($(this).attr('checked')==false){
				value='true';
			}
		});
		$('.'+c).each(function(){
			$(this).attr('checked', value);
		});
}

function open_doc(id){
	$.post("ajax/mark_read.php", { id: id }, function(){
			refresh_account();										  
	});
}

function refresh_account(){
		$.get("modules/logout.php", null, function(data){
			document.getElementById('account').innerHTML = data;
		});
}
