/* Eksakte Frontend - javascript functions */
/* RDC 03.11.2009 */

/*
//Load ajax function
function LoadDiv(toLoad) {
$('#overlay').show(0);							  
$('#productWrapper').hide('fast');
$('#load').remove();
$('#wrapper').append('<span id="load">LOADING...</span>');
$('#load').fadeIn('normal');
$('#productWrapper').load(toLoad,'', function (){
$('#productWrapper').show('normal');
$('#load').fadeOut('normal');
});
}
*/

$(document).ready(function () {
    /*Begin document ready function*/

    $('.CompleteTextLink').click(function () {
        if ($('.CompleteText').is(":hidden")) {
            $('.CompleteText').css("visibility", "visible");
            $('.CompleteSubTitleText').css("visibility", "hidden");
            $('.CompleteSubTitleText').css("display", "none");
            $('.CompleteText').slideDown("slow");
        } else {
            $('.CompleteText').hide();
            $('.CompleteText').css("visibility", "hidden");
        }
    });

    /*Ad Gallery plugin*/
    $('.ad-gallery').not('.dialogContainer').adGallery({
        cycle: true,
        display_next_and_prev: false,
        display_back_and_forward: false
    });

    $('.dialogContainer').dialog({
        autoOpen: false,
        width: 730,
        modal: true,
        resizable: false
    });

    // Dialog Link
    $('.dialog_link').click(function () {
        var dialogLinkid = $(this).attr("id").replace("dialog_link_", "");
        $('#dialog_' + dialogLinkid).dialog('open');
        $('#dialog_' + dialogLinkid).not('.adGalleryApplied').addClass('adGalleryApplied').adGallery({
            cycle: true,
            display_next_and_prev: false,
            display_back_and_forward: false
        });
        return false;
    });








    //	$('#switch-effect').change(
    //		function() {
    //			galleries[0].settings.effect = $(this).val();
    //			return false;
    //		}
    //	);
    //	$('#toggle-slideshow').click(
    //		function() {
    //			galleries[0].slideshow.toggle();
    //			return false;
    //		}
    //	);

    /*Ad Gallery customization*/
    $(".mImageGallery .navigation .thumbs ul.list li:last").addClass("last"); /*tiltag for at sidste thumb flugter med det store billedes højrekant*/

    //$(".mImageGallery .navigation .thumbs ul.list li a.ad-active").removeAttr("href");/*for at undgå at det aktive billede hentes igen*/


    //context.showImage();


    //Navigation submenu (IE6)
    $('#header ul li div').hover(
		function () { $('ul', this).css('display', 'block'); },
		function () { $('ul', this).css('display', 'none'); });

    //Show mLogin  	
    $('#header ul li.login').click(function () {
        //console.log($(".mLogin").is(":visible"));
        if ($(".mLogin").is(":visible")) {
            $('.mLogin').hide();
        }
        else {
            $('.mLogin').show();
        }
    });
    $(document).click(function (event) {
        //console.log(event.target);
        if ($(event.target).parents(".mLogin").size() == 0 && $(event.target).hasClass("mLogin") == false && $(event.target).is("#header ul li.login *") == false) {
            $(".mLogin").hide();
            //$(".filterElement.selected").removeClass("selected");
        }
    });

    //Show mSearch  	
    $('#header ul li.search').click(function () {
        if ($(".mSearch").is(":visible")) {
            $('.mSearch').hide();
        }
        else {
            $('.mSearch').show();
            $('.mSearch .text').focus();
        }
    });
    $(document).click(function (event) {
        if ($(event.target).parents(".mSearch").size() == 0 && $(event.target).hasClass("mSearch") == false && $(event.target).is("#header ul li.search *") == false) {
            $(".mSearch").hide();
        }
    });



    // Image background slider/fader				// type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence')
    // animationtype: Type of animation 'fade' or 'slide' (Default: 'fade')

    $('.mImageSlide1').innerfade({
        animationtype: 'fade',
        speed: 1500,
        timeout: 5000,
        type: 'sequence',
        containerheight: '560px'
    });
    /*
    $('ul#portfolio').innerfade({
    speed: 1000,
    timeout: 5000,
    type: 'sequence',
    containerheight: '220px'
    });
		
    $('.fade').innerfade({
    speed: 1000,
    timeout: 6000,
    type: 'random_start',
    containerheight: '1.5em'
    });
		
    $('.adi').innerfade({
    speed: 'slow',
    timeout: 5000,
    type: 'random',
    containerheight: '150px'
    });
    */


    /*
    $('.mLogin').mouseout(function() {
    $('.mLogin').css('display', 'none');
    return false;
    });
    */





    /*
    //Load ajax - call function
    $('#nav li a').click(function() {
    var toLoad = $(this).attr('href') + ' #productPage';
    LoadDiv(toLoad);
    return false;
    });
    
    
    
    //Unload ajax pages
    $('#productPage a.closeButton').live("click", function() {
    $('#productWrapper').hide();
    $('#productPage').remove();
    $('#overlay').hide();
    });
    



    //Load Flash
    $('.video').flash(
    { src: '/css/graphics/flash/flamme.swf',
    width: 110,
    height: 120,
    menu: false,
    wmode: 'transparent'
    },
    { version: 6 }
    );
    */


    //Load lightbox for the Floor plans
    $('a[rel*="lightbox"]').lightBox(); // Select all links that contains lightboxy in the attribute rel

    StartFrontpageCycle();

    /*End document ready function*/


    // Date pickers
    //var dates = $("input.arrival, input.departure").datepicker({ firstDay: 1 });

    $.arrDateId = $("input.arrival:last").attr("id");
    $.depDateId = $("input.departure:last").attr("id");

    var dates = $("#" + $.arrDateId + "," + "#" + $.depDateId).datepicker({
        firstDay: 1,
        dateFormat: 'dd/mm/yy',
        onSelect: function (selectedDate) {
            var option = this.id == $.arrDateId ? "minDate" : "maxDate",
					instance = $(this).data("datepicker"),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings);
            dates.not(this).datepicker("option", option, date);
        }
    });

});


function customRange(input) {
    var min = new Date(); //Set this to your absolute minimum date
     var   dateMin = new Date(2020,1,1);
     var   dateMax = null;
     var   dayRange = 6000; // Set this to the range of days you want to restrict to

    if (input.id == $.arrDateId) {
        if ($(".departure").datepicker("getDate") != null) {
            dateMax = $(".departure").datepicker("getDate");
            dateMin = $(".departure").datepicker("getDate");
            dateMin.setDate(dateMin.getDate() - dayRange);
            if (dateMin < min) {
                dateMin = min;
            }
        }
        else {
            dateMax = new Date; //Set this to your absolute maximum date
        }
    }
    else if (input.id == $.depDateId) {
        dateMax = new Date; //Set this to your absolute maximum date
        if ($(".arrival").datepicker("getDate") != null) {
            dateMin = $(".arrival").datepicker("getDate");
            var rangeMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + dayRange);

            if (rangeMax < dateMax) {
                dateMax = rangeMax;
            }
        }
    }
    return {
        minDate: dateMin,
        maxDate: dateMax
    };

}



function StartFrontpageCycle() {

	var strFullClass = $('.frontPageNews').attr('class');
	if (strFullClass != undefined) {

		if (strFullClass.indexOf('cycleTimeOut') > -1) {

			var strCycleTimeOutClass = strFullClass.match(/cycleTimeOut(\d+)/gi)[0];
			var intCycleTimeOut = strCycleTimeOutClass.replace('cycleTimeOut', '');

			if (isNaN(intCycleTimeOut) == false) {
				intCycleTimeOut = intCycleTimeOut * 1000;

				$('.frontPageNews').cycle({
					timeout: intCycleTimeOut
				});
			}
		}
	}
}
