var productMinDuration = 0;
var productDefaultDuration = 0;
var countryHover = false;
var currentDepartureDate = false;

//Voor de paginator van strandvakanties en rondreizen
// Hierbij werdt alleen de zoekresultaten herladen. Omdat de top-paginator niet werdt vernieuwd, wordt nu de div 'mainItems' opnieuw geladen. (PB)
var rondstrand = {
    result_page: function(page, url) {
        var params1 = $('#leftMenu form').serializeArray();
		var url2 = url+' #mainItems';
		    params1.push({
            name: 'page',
            value: page
        });
        $('#mainItems').load(url2, params1);
        return false;
    }
}

$(document).ready(function() {

	//oude nieuwsbrieven link
	$('#lastnewsletterslink').click(function(){
		$('#lastnewsletterslink').slideUp();
		$('#lastnewsletters').slideDown();
	});

	//zoekveld
	$('#zoekveld_form').submit(function(event){
		var val = $('#zoekveld_input').val();
		if(val == '' || val == 'Reiscode of trefwoord'){
			$('#zoekveld_input').focus().val('');
			event.preventDefault();
			$(this).css('color', '#777777');
		}
	});

	$('#zoekveld_input').blur(function(){
		var val = $(this).val();
		if(val == ''){
			$(this).val('Reiscode of trefwoord');
			$(this).css('color', '#777777');
		}
	});

	$('#zoekveld_input').focus(function(){
		var val = $(this).val();
		if(val == 'Reiscode of trefwoord'){
			$(this).val('');
			$(this).css('color', '#555555');
		}
	});

	$('#zoekveld_input').keyup(function(){
		var val = $(this).val();
		if(val == 'Reiscode of trefwoord'){
			$(this).css('color', '#777777');
		}else{
			$(this).css('color', '#555555');
		}
	});

	$('#zoekveld_mini').click(function(){
		$(this).fadeOut(350, function(){
			$('#zoekveld').slideDown(350, function(){ $('#zoekveld_input').focus(); });
		});
	});
	//eind zoekveld


    if ($('#slideshow').length > 0) {
        $('#slideshow').innerfade({
            animationtype: 'fade',
            containerheight: '180px',
            speed: 4000,
            timeout: 8000,
            type: 'sequence'
        });
    }

    $('#destinations_information .country>h2').click(dest_info.h_over, dest_info.h_out, dest_info.open);
    
    $(".azieblock").click(function(event) {
        event.preventDefault();
        window.location = $(this).children("a:first").attr("href");
    });

    if ($('#booking_adults').length > 0) {
        $('#booking_adults, #booking_children, #booking_infants').change(updateTravelerSelect);
        updateTravelerSelect();
    }

    //inschrijven nieuwsbrief homepage
    $("#mailing_submit").click(function() {
        $("#nieuwsbrief_container").load(API_DIR + "nieuwsbrief.php?email=" + $("#mailing_emailaddress").val());
    });

    //inschrijven nieuwsbrief footer
    $("#footer_mailing_submit").click(function() {
        $("#nieuwsbrief_footer_response").load(API_DIR + "nieuwsbrief.php?email=" + $("#footer_mailing_emailadres").val());
    });
});

$(window).blur(function() {
    $(document).attr('pauseAnimation', true);
});
$(window).focus(function() {
    $(document).attr('pauseAnimation', false);
});

function updateTravelerSelect() {
    if ($('select#booking_adults').length == 0) return false;
    var adt = $('#booking_adults').val();
    var chd = $('#booking_children').val();
    var inf = $('#booking_infants').val();
    var opt = $('#booking_infants')[0].options;
    var len = opt.length;

    if (len < adt) {    // Add options
        for (var i=len; i<=adt; i++) {
            opt[i] = new Option(i, i);
        }
    } else {            // Remove options
        opt.length = parseInt(adt) + 1;
        if (parseInt(inf) > opt.length-1) {
            opt[parseInt(adt)].selected = true;
        }
    }
}


var dest_info = {
    over_timer: false,
    current_sel: false,

    h_over: function() {
        var b = $(this).parents('li').find('ul');

	    if (b.css('display') == 'none') {
            dest_info.current_sel = b;
       	    dest_info.over_timer = setTimeout(dest_info.open, 300);
	    }
    },

    h_out: function() {
        clearTimeout(dest_info.over_timer);
    },

    open: function() {
        var current_open = dest_info.current_sel.parents('ul.subMenu').find('.country_open>ul');

        if (current_open) {
            current_open.slideUp();
        }

        dest_info.current_sel.slideDown();
        dest_info.current_sel.parents('li.country').addClass('country_open');
    }
};


var select_destinations = function(locationcode) {
    $('#ticketDestination').empty();
    $.getJSON(API_DIR + 'get_destinations.php?loc='+locationcode, function(data) {
        //$('#ticketDestination').append('<option value=""></option>');
        $.each(data, function(i, item) {
            $('<option/>').attr('value', item.code).text(item.city).appendTo('#ticketDestination');
        });
    });
};

var set_date = function(field, start_date, modif)
{
  var d = new Date(start_date.split('-')[0], start_date.split('-')[1] -1, start_date.split('-')[2]);
  d.setDate(d.getDate()+modif);
  $(field).attr('value', d.getDate()+'-'+(((d.getMonth()+1)<10)?'0'+(d.getMonth()+1):(d.getMonth()+1))+'-'+d.getFullYear());
  $(field).value = d.getDate()+'-'+(((d.getMonth()+1)<10)?'0'+(d.getMonth()+1):(d.getMonth()+1))+'-'+d.getFullYear();
  $('#ticketsearchform').submit();
};

var destinationSlideShow = function(d) {
    var slides = $('a[rel=lightbox-'+d+']:first');
    slides.click();
    return false;
}

var opentarget = function(target) {
    window.opener(target.href, target.className);
    return false;
}

var openTargetInSelf = function(target) {
	window.location = target.href;
    return false;
}
