$(document).ready(function() {
    $('.js_slideshow').cycle({
 		fx: 'fade',
		speed:  1000,
		cleartype: 0,
		zoom: 1,
		containerResize: 1
	});
	$('.js_submenulink').mouseenter(function() {
		$(this).find('.js_menuknop').addClass("selected");
		$(this).children('.js_dropdown').css('display','block');
	}).mouseleave(function() {
		$(this).find('.js_menuknop').removeClass("selected");
		$(this).children('.js_dropdown').css('display','none');
	}); 
});
$(document).ready(function(){
	// flash inladen
	// als een div aangemaakt wordt met als class "js_flash",
	// en als inhoud het absolute pad naar de flash en de variable
	// wordt deze automatisch ingeladen
	$('.js_flash').each(function(){
		slideshowvars = $(this).find('.js_flashlink').html();
		//strip HTML comment from flash vars
		slideshowvars = slideshowvars.replace(/^<!--/, "");
		slideshowvars = slideshowvars.replace(/-->$/, "");
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			wmode: 'transparent',
			allowFullScreen: 'true'
		});
	});

	// png fix op document
	// edaarna colorboxinladen (ivm conflict)
    if (typeof $().colorbox != 'function')
    {
        // png fix op document
        // edaarna colorboxinladen (ivm conflict)
        $(document).pngFix().getScript('/public/js/jquery.colorbox-min.js', function() {


            /**
             *	-- Gebruik --
             *	Geef een anchor de class .js_colorbox en de href de locatie van de afbeelding
             *	Voor een slideshow:
             *	Geef meerdere anchor's met .js_colorbox dezelfde rel="" waarde
             *	Voorbeeld slideshow:
             *	<a class="js_colorbox" rel="testblaat" href="/public/img/video2.jpg">klik</a>
             *	<a class="js_colorbox" rel="testblaat" href="/public/img/banner.jpg">klik</a>
             */
            //set
            $('.js_colorbox').colorbox({
                transition:'elastic'
            });

            /**
            * Html tonen in colorbox:
            * <a class="js_htmlcolorbox" rel="idvanjehtmldiv">Klik</a>
            */
            $('.js_htmlcolorbox').click(function(){
                var targetid = $(this).attr('rel');
                if (typeof targetid == 'undefined' || targetid == '') return ;

                $(this).colorbox({
                    open: true,
                    inline:true,
                    transition:'none',
                    href:"#" + targetid
                });
            });

            /**
            * Contact form tonen in colorbox:
            * Verschil met htmlcolorbox is
            * dat deze de pagina herlaadt als hij sluit
            * (om form te resetten)
            * <a class="js_contactcolorbox" rel="idvanjehtmldiv">Klik</a>
            */
            $('.js_contactcolorbox').click(function(){
                var targetid = $(this).attr('rel');
                if (typeof targetid == 'undefined' || targetid == '') return ;

                $(this).colorbox({
                    open: true,
                    inline:true,
                    transition:'none',
                    href:"#" + targetid,
                    onClosed:function(){
                        // pagina herladen
                        if(ajaxFormChanged) {
                            window.location.href = window.location.href;
                        }
                    }
                });
            });

            /**
            * Youtube movie tonen in colorbox:
             *	Voor een slideshow:
             *	Geef meerdere anchor's met .colorbox dezelfde rel="" waarde
             *	Voorbeeld slideshow:
            * <a class="js_youtubecolorbox" rel="testblaat" href="http://www.youtube.com/v/[movie-id]">Klik</a>
            */
            $('.js_youtubecolorbox').colorbox({
                iframe:true,
                innerWidth: 640,
                innerHeight:505,
                transition:'elastic'
            });
        });
    }

	//## Klapblokken ##

	if (0 < $('.js_uitklapblokken').length)
	{
		$('.js_uitklaplink').click(function ()
		{
			var obj = $(this).parents('.js_uitklapblok:first').find('.js_uitklapdetail:first');

			if (!$(this).hasClass('js_keepothers')) //andere blokken sluiten
			{
				$(this).parents('.js_uitklapblokken').find('.js_uitklapdetail').each(function(){
					if (!$(this).hasClass('js_dontclose')) //dit blok niet altijd open houden
					{
						$(this).slideUp();
						$(this).find('.js_uitklaplink').removeClass('open');
					}
				})
			}

			//overal active verwijderen
			$(this).parents('.js_uitklapblokken').find('.js_uitklaplink').removeClass('active');

			if (obj.css('display')=='none') //geklikt item openen
			{
				obj.slideDown('fast', function(){$(this).css('display', 'block');}); //callback zodat het in IE8 nog steeds werkt
				$(this).addClass('active');
				$(this).addClass('open');
			}
			else //geklikt item sluiten
			{
				obj.slideUp('fast');
				$(this).removeClass('open');
			}
		});
	}

	//field hints
	$(".js_hintfield").each(function(i) {
        var beginStyle = $(this).attr("style");
		$(this).addClass("js_hint");
		$(this).val($(this).attr("title"));

        $(this).bind("focus", function(e) {
            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
				$(this).removeClass("js_hint");
            }
        });
        $(this).bind("blur", function(e) {
            if ($(this).val() == "") {
				$(this).addClass("js_hint");
				$(this).val($(this).attr("title"));
            } else {
				$(this).removeClass("js_hint");
			}
        });
    });

	//submit form link
	$('.js_submitform').click(function(){
		$(this).parents('form:first').submit();
		return false;
	})

	//AJAX FORMS automatisch
	if (typeof $().ajaxForm == 'function')
	{
		$('form.js_ajaxform').each(function(){
			$(this).append('<input type="hidden" name="ajax_post" value="oke" />');
			var form = $(this);
			$(this).ajaxForm({
				dataType: 'json',
				beforeSerialize: function(){
					form.find('.js_hint').val('');
				},
				beforeSubmit: function(){
					form.parent().find('.js_errormelding').remove();
					form.find('.errorfield').removeClass('errorfield');
				},
				success: function(response){
					if (response.success)
					{
						if ( response.message != '' )
							form.html('<p>'+response.message+'</p>');
						
						
						if ( typeof _gaq == 'object' )
						{
							if ( form.attr('analytics-success') != '' )
								_gaq.push(['_trackPageview', form.attr('analytics-successurl')]);
							else
								_gaq.push(['_trackPageview', location.pathname + '-bedankt']);
							
						}
						else if ( typeof pageTracker == 'object' )
						{
							if ( form.attr('analytics-success') != '' )
								pageTracker._trackPageview( form.attr('analytics-successurl') );
							else
								pageTracker._trackPageview( location.pathname + '-bedankt' );
						}
						
						if ( response.onSuccess != '' )
						{
							eval(response.onSuccess);
						}
					}
					else if (response.message)
					{
						if ( response.asAlert )
							alert(response.message);
						else
							form.before($('<p class="foutmelding js_errormelding">'+response.message+'</p>'));
					}
					
					if ( response.errorfields )
					{
						for(row in response.errorfields)
						{
							form.find('[name="'+response.errorfields[row]+'"]').addClass('errorfield');
						}
					}
					form.find('.js_hint').trigger('blur');
				}
			})
		})
	}
	
	
	
	$( ".js_datepicker" ).datepicker({
		showOn: "button",
		buttonImage: "/public/img/calendar.gif",
		buttonImageOnly: true,
		dateFormat: 'dd-mm-yy'
	});
	
});


function fill_retour()
{
	$('#onlineboeken_form').find('input[name="ride"]').val('2');
	
	var from_location = $('#onlineboeken_form').find('input[name="from_location"]').val();
	var from_number = $('#onlineboeken_form').find('input[name="from_number"]').val();
	var from_city = $('#onlineboeken_form').find('input[name="from_city"]').val();
	
	var to_location = $('#onlineboeken_form').find('input[name="to_location"]').val();
	var to_number = $('#onlineboeken_form').find('input[name="to_number"]').val();
	var to_city = $('#onlineboeken_form').find('input[name="to_city"]').val();
	
	$('#from_city_open').attr('disabled','').show();
	$('#from_city_fixed').attr('disabled','disabled').hide();
	
	$('#to_city_fixed').attr('disabled','').show();
	$('#to_city_open').attr('disabled','disabled').hide();
	
	
	$('#onlineboeken_form').find('[name="from_location"]').val(to_location);
	$('#onlineboeken_form').find('[name="from_number"]').val(to_number);
	$('#onlineboeken_form').find('[name="from_city"]').val(to_city);
	
	$('#onlineboeken_form').find('[name="to_location"]').val(from_location);
	$('#onlineboeken_form').find('[name="to_number"]').val(from_number);
	$('#onlineboeken_form').find('[name="to_city"]').val(from_city);
	
	$('#book_retour_field').remove();
}
