﻿function openMap(l1, l2) {

   
    URL = "/utils/viewmap.aspx?";
    URL += "&l1=" + l1;
    URL += "&l2=" + l2;
    //URL += "&z=" + z;

    window.open(URL, 'mapwin', 'width=730,height=520,scrollbars=no,status=yes,resizable=no');

}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function UpdateCookie() {

    // page with search form 
    if ($(".c_selIsland").length > 0) {

        var i, d, r, ci, co;

        // read cookie if exist
        var info = $.cookie('searchinfo');
        if (info != null) {
            i = info.split("#")[0];
            d = info.split("#")[1];
            r = info.split("#")[2];
            ci = info.split("#")[3];
            co = info.split("#")[4];
        }

        // replace existing vars on query string 
        if (getParameterByName("i") != ""){
            i = getParameterByName("i");
        }
        if (getParameterByName("d") != "") {
            d = getParameterByName("d");
        }

        if (getParameterByName("r") != "") {
            r = getParameterByName("r");    
        }

        if (getParameterByName("ci") != "") {
            ci = getParameterByName("ci");
        }

        if (getParameterByName("co") != "") {
            co = getParameterByName("co");
        }

        //save cookie
        if (i!=null)
            $.cookie('searchinfo', i + "#" + d + "#" + r + "#" + ci + "#" + co);
    }
}

function RetrieveL2s() {
    $.ajax({
        type: "POST",
        url: "/searchhelper.asmx/GetAllL2s",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $(".c_selIsland").empty();
            $(".c_selIsland").append(data.d);

            var info = $.cookie('searchinfo');
            if (info != null) {
                $(".c_selIsland").val(info.split("#")[0]);
            }

            RetrieveDestinations();
        },
        error: AjaxError
    });
}

function RetrieveDestinations() {
    $.ajax({
        type: "POST",
        url: "/searchhelper.asmx/GetAllDestinations",
        data: "{l2id:" + $(".c_selIsland").val() + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $(".c_selLocation").empty();
            $(".c_selLocation").append("<option value=0>Any</option>");
            $(".c_selLocation").append(data.d);

            var info = $.cookie('searchinfo');
            if (info != null) {
                $(".c_selLocation").val(info.split("#")[1]);
            }

        },
        error: AjaxError
    });
}

function RetrieveRoomTypes() {
    $.ajax({
        type: "POST",
        url: "/searchhelper.asmx/GetAllRoomTypes",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $(".c_selRoomType").empty();
            $(".c_selRoomType").append("<option value=0>Any</option>");
            $(".c_selRoomType").append(data.d);

            var info = $.cookie('searchinfo');
            if (info != null) {
                $(".c_selRoomType").val(info.split("#")[2]);
            }
            
        },
        error: AjaxError
    });
}

function SavePanelStatus() {
    $.cookie('panelinfo', $("#accomodationtypes").is(":visible") + "#" + $("#HotelFacilities").is(":visible") + "#" + $("#RoomFacilities").is(":visible"));
}

function RetrievePanelStatus() {

    if ($("#accomodationtypes").length > 0) {
        var searchpanels = $.cookie('panelinfo');
        if (searchpanels != null) {

            if (searchpanels.split("#")[0]=="true"){
                $("#accomodationtypes").show();
            }
            else {
                $("#accomodationtypes").hide();
            }

            if (searchpanels.split("#")[1] == "true") {
                $("#HotelFacilities").show();
            }
            else {
                $("#HotelFacilities").hide();
            }

            if (searchpanels.split("#")[2] == "true") {
                $("#RoomFacilities").show();
            }
            else {
                $("#RoomFacilities").hide();
            }
        }
    }
}

function Search() {
    
    
    var ci = $(".checkin_setup_date_selector").val();
    var co = $(".checkout_setup_date_selector").val();
    
    if (ci=="" || co=="")
        return;
    
    var i = $(".c_selIsland").val();
    var d = $(".c_selLocation").val();
    var r = $(".c_selRoomType").val();

    var url = "/results.aspx?a=0&ci=" + ci + "&co=" + co + "&i=" + i;
    
    // destination (optional)
    if (d!="0")
        url += "&d=" + d;

    // room type (optional)
    if (r != "0")
        url += "&r=" + r;

    $.cookie('searchinfo', i+"#"+d+"#"+r+"#"+ci+"#"+co);
    document.location.href = url;
}

function AjaxError(request, status, error) {
    alert(request.statusText);
}


Date.prototype.addDays = function (days) {
    this.setDate(this.getDate() + days);
}



$(document).ready(function () {

    // populate l2 if contol exists
    if ($(".c_selIsland").length > 0) {
        RetrieveL2s();
    }

    // populate room types if contol exists
    if ($(".c_selRoomType").length > 0) {
        RetrieveRoomTypes();
    }

    $('.c_selIsland').change(function () {
        RetrieveDestinations();
    });

    $('.c_searchbutton').click(function (event) {
        Search();
    });

    // toggle pannels
    if ($("#accomodationtypetitle").length > 0) {
        $('#accomodationtypetitle').click(function (event) {
            $('#accomodationtypes').toggle();
            SavePanelStatus();
        });
    }
    if ($("#hotelfacilitiestitle").length > 0) {
        $('#hotelfacilitiestitle').click(function (event) {
            $('#HotelFacilities').toggle();
            SavePanelStatus();
        });
    }
    if ($("#roomfacilitiestitle").length > 0) {
        $('#roomfacilitiestitle').click(function (event) {
            $('#RoomFacilities').toggle();
            SavePanelStatus();
        });
    }

    // if query string update search cookie ...
    if ($(".c_selIsland").length > 0) {
        UpdateCookie();
    }

    // retrive panel status ....
    RetrievePanelStatus();
    //--------------------------------------------------
    Sys.Application.add_init(function () {

        //        var prm = Sys.WebForms.PageRequestManager.getInstance();

        //        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
        //        function BeginRequestHandler(sender, args) {
        //            $.blockUI({ message: '<div id="loading"></div>' });
        //        }

        //        // Unblock the form when a partial postback ends.
        //        prm.add_endRequest(function () {
        //            $.unblockUI();
        //        });




    });
    //--------------------------------------------------

    //--------------------------------------------------
    Sys.Application.add_load(function () {

        $.datepicker.setDefaults($.datepicker.regional[locale]);

        //DATE PICKER 2 ----------------------------------------
        $(".checkin_setup_date_selector").datepicker({
            showOn: "button",
            buttonImage: "/static/images/calendar.jpg",
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            dateFormat: dateformat,
            onSelect: function (dateText, inst) {
                var day = dateText.toString().substring(0, 2);
                var month = dateText.toString().substring(3, 5);
                var year = dateText.toString().substring(6, 11);
                var d = new Date(year, month, day);
                d.addDays(7);

                var day1 = d.getDate().toString();
                if (day1.length == 1) { day1 = "0" + day1; }

                var month1 = d.getMonth().toString();
                if (month1.length == 1) { month1 = "0" + month1; }

                var f = dateformat;
                f = f.replace('dd', day1);
                f = f.replace('mm', month1);
                f = f.replace('yy', year);
                //var f = day1 + "/" + month1 + "/" + year;

                $('.checkout_setup_date_selector').val(f);

            }
        });

        $(".checkout_setup_date_selector").datepicker({
            showOn: "button",
            buttonImage: "/static/images/calendar.jpg",
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            dateFormat: dateformat
        });

        $(".ui-datepicker-trigger").mouseover(function () {
            $(this).css('cursor', 'pointer');
        });
        //        $.datepicker.setDefaults(
        //            $.extend(
        //                { 'dateFormat': 'dd/mm/yy' },
        //                 $.datepicker.regional['gr']
        //            )
        //        );


        // ci date default value
        var info = $.cookie('searchinfo');
        if (info != null) {
            $(".checkin_setup_date_selector").val(info.split("#")[3]);
        }
        else {
            $(".checkin_setup_date_selector").datepicker('setDate', "+1D");
        }

        // co date default value
        var info = $.cookie('searchinfo');
        if (info != null) {
            $(".checkout_setup_date_selector").val(info.split("#")[4]);
        }
        else {
            $(".checkout_setup_date_selector").datepicker('setDate', "+8D");
        }

        //END FILTERS -----------------------------------------------------------



        //these are the default settings for the component
        // you can redefine this defaults or change each parameter on the component call
        $.fn.mbGallery.defaults = {
            containment: "body",
            cssURL: "css/",
            skin: "white",
            overlayBackground: "#333",
            exifData: false, //todo

            galleryTitle: "Photo Gallery",
            imageSelector: ".imgFull",
            thumbnailSelector: ".imgThumb",
            titleSelector: ".imgTitle",
            descSelector: ".imgDesc",

            minWidth: 300,
            minHeight: 200,
            maxWidth: 0,
            maxHeight: 0,
            fullScreen: true,
            addRaster: false,
            overlayOpacity: .5,
            startFrom: 0, //"random"
            fadeTime: 500,
            slideTimer: 6000,
            autoSlide: true,

            onOpen: function () { },
            onBeforeClose: function () { },
            onClose: function () { },
            onChangePhoto: function () { }
        };

        $('#g1').hide();
        $('#galleryOpener')
    .bind("click", function () {
        $('#g1').mbGallery({ overlayBackground: '#f0f0f0', overlayOpacity: .8, startFrom: 0 });
    });


    });



});



