﻿//CUFON STYLES

function initCufon() {
    if (!isIE6) {
        Cufon('#topNav ul li a.cf span', {
            fontFamily: 'bliss-pro-reg',
            color: '#ffffff',
            textShadow: '#000000 0px -1px'
        });

        Cufon('a.btn-blue-lg', {
            fontFamily: 'bliss-pro-med',
            color: '#ffffff',
            fontSize: '13px',
            textShadow: '#007b7b 0px -1px'
        });


        Cufon('.accordion-panel h2', {
            fontFamily: 'bliss-pro-reg',
            fontSize: '16px',
            fontWeight: 'normal',
            color: '#ffffff',
            textShadow: '#007b7b 0px -1px'
        });

        Cufon('h2.cuf', {
            fontFamily: 'bliss-pro-reg',
            fontSize: '16px',
            fontWeight: 'normal',
            color: '#ffffff',
            textShadow: '#007b7b 0px -1px'
        });
        Cufon('h4', {
            fontFamily: 'bliss-pro-light',
            color: '#ffffff'
        });

        Cufon('#mal-hotel .middleCol .promoContainer .promoPod.tall h2', {
            fontFamily: 'bliss-pro-light',
            color: '#ffffff'
        });

        Cufon('.mal-secret h2', {
            fontFamily: 'AnnlieEF-ExtraBoldItalic',
            fontSize: '25px',
            color: '#ff00fc',
            textShadow: '#000 1px 1px'
        });

        Cufon('.mal-secret p', {
            fontFamily: 'bliss-pro-light',
            fontSize: '14px',
            fontWeight: 'normal',
            color: '#fff'
        });

        Cufon('h1', {
            fontFamily: 'bliss-pro-light',
            color: '#ffffff'
        });

        Cufon('.profile_pod h2', {
            fontFamily: 'bliss-pro-light',
            color: '#ffffff',
            fontSize: '25px'
        });
    }
}

// global sharing options
var addthis_config = { 
    username: 'malhdvsharing',
    ui_use_css: true, 
    ui_click: false, 
    data_track_linkback: false,
    services_compact: 'facebook,twitter,linkedin,email',
    ui_email_note: 'Have you seen this at Malmaison...'
};
var addthis_share = {
    url_transforms : {
        clean: true
    }
}

// register partial postback handler
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(ajaxPostbackStart);
prm.add_endRequest(ajaxPostbackEnd);

// this function runs after a partial postback
// (ie via update panel)
function ajaxPostbackEnd(sender, args) {
    initScripts();
}

// this function runs on a partial postback
// (ie via update panel)
function ajaxPostbackStart(sender, args) {

}

// don't put scripts in here - put them in initScripts()
$(document).ready(function () {
    initScripts();
});

$(window).load(function () {
    // We use $(window).load here so that these jQuery functions are executed after all page elements are loaded including images
    // $(document).ready is triggered when the DOM is ready, but if we want to manipulate images we have to wait until they are loaded
    //Function to load popup for promos
    var $triggers = $(".popUpImageLink, .btn-blue-sm.popUpPromo, .enquireEvent a, .promoImageDIV img, .lnk-pop.popUpPromo").overlay({
        // some mask tweaks suitable for modal dialogs
        fixed: false,
        mask: { color: '#111',
            loadSpeed: 1000,
            opacity: 0.8
        },
        closeOnClick: false,
        onClose: function () {
            //intialiseWizard();
            toggleSelects(true);
        },
        onBeforeLoad: function () { toggleSelects(false); }
    });
});

function initScripts() {

    preSelectBookingFragment();

    // buttonize custom links - do before cufon
    buttonizeLinks();

    initCufon();
    // banner hover reveal functions
    $('.leadBanner .banners').hover(
			function () {
			    $('.leadBanner .overlay').stop().animate({ bottom: 0 }, 200, 'easeOutQuart');
			},
			function () {
			    $('.leadBanner .overlay').stop().animate({ bottom: -24 }, 200, 'easeInQuart');
			}
		);

    // Booking frag click reveal/class swaps
			$('#headBooking .bookingFrag li a').click(function (e) {
			    e.preventDefault();
			    $(this).blur();
			    //remove all "on" classes
			    $('#headBooking .bookingFrag li').removeClass('on').children('a').removeClass('on');
			    $(this).addClass('on').parent().addClass('on');
			    var $el = $(this).attr('rel');
			    $('#headBooking .bookingFrag-panel:not([id$=' + $el + '])').fadeOut(200);
			    $('#bookFrag-' + $el).delay(250).fadeIn(200, function () {
			        if ($.browser.msie) { this.style.removeAttribute('filter'); }
			    });
			    if ((!isMobile) && (!isIE6)) {
			        setTimeout(function () {
			            $('#bookFrag-' + $el + ' fieldset').jqTransform();
			        }, 255);
			    }
			});

    // Top nav bar rollover popdowns
    $('ul.sf-menu').superfish({
        delay: 150,                            // 400 milli-second delay on mouseout 
        animation: { height: 'show' },  				  // fade-in and slide-down animation 
        speed: 300,                          // faster animation speed 
        autoArrows: false,                           // disable generation of arrow mark-up 
        dropShadows: false                            // disable drop shadows 
    });

    // Right panel accordion
    $('.rightCol div.myMal-Accord .accordion-header a').click(function (e) {
        e.preventDefault();
        // collapse all
        $(this).parent().siblings('.accordion-panel').slideUp(500);
        $(this).parent().next('.accordion-panel').stop().slideDown(500);
        $(this).parent().siblings('.accordion-header').removeClass('on');
        $(this).blur().parent().addClass('on');
        $("#hidSelectAccordionIndex").val($(this).attr("rel"));
        if ((!isMobile) && (!isIE6)) {
            $('.myBookingsFrag').jqTransform();
        }
    });
    initAjax();
    bindValidationLinks();
    setFieldValidatorClick();
    autoexpand();
    liveValidator();

    // JQ transform all selects
    if ((!isMobile) && (!isIE6)) {
        $('body').jqTransform();
    }

    //Initialise the datepicker on all referenced objects
    var yr = new Date().getFullYear() - 16;
    var d = new Date().getDate();
    var m = new Date().getMonth();

    var $dates = $("fieldset .date input");

    $dates.each(function () {
        if ($(this).hasClass('dobPicker')) {
            $(this).datepicker({
                changeMonth: true,
                changeYear: true,
                minDate: new Date(1900, 0, 1),
                maxDate: new Date(yr, m, d),
                dateFormat: "dd/mm/yy",
                yearRange: "1900:" + yr
            });
        } else {
            $(this).datepicker({
                changeMonth: true,
                changeYear: true,
                minDate: 0,
                maxDate: '+10Y',
                dateFormat: "dd/mm/yy"
            });
        }
    });
    setDatePickersReadOnly();


    /*.overlay({
        // some mask tweaks suitable for modal dialogs
        fixed: false,
        mask: { color: '#111',
            loadSpeed: 1000,
            opacity: 0.8
        },
        closeOnClick: false,
        onClose: function () {
            intialiseWizard();
        }
    });*/


    /*setTimeout(function () {
        var triggers = $(".btn-blue-sm.popUpPromo, .enquireEvent a, .popUpImageLink, #promoImage, .lnk-pop.popUpPromo").overlay({
            // some mask tweaks suitable for modal dialogs
            fixed: false,
            mask: { color: '#111',
                loadSpeed: 1000,
                opacity: 0.8
            },
            closeOnClick: false,
            onClose: function () {
                intialiseWizard(); 
            }
        });
    }, 5000);*/

}

// button links that have buttonize class specified via CMS content editor
function buttonizeLinks() {
    $("a.buttonize").each(function () {
        var txt = $(this).text();
        $(this).html("<span class='body'>" + txt + "</span><span class='endIcon'></span><br class='clr micro'>");
        $(this).removeClass("buttonize").addClass("btn-blue-lg").after("<br class='clr micro'>");
    });
}

// shows the relavnt booking frag after postback
function preSelectBookingFragment()
{
    var sel;

    $(".bookingFrag li").removeClass("on").children('a').removeClass("on");

    try {
        sel = ".bookingFrag li." + bookingFragId;
    }
    catch(e){
        sel = ".bookingFrag li.first";
    }

    $(sel).addClass("on").children('a').addClass("on");
    var $el = $(sel + " a").attr('rel');
        $('#headBooking .bookingFrag-panel:not([id$=' + $el + '])').hide();
        $('#bookFrag-' + $el).show();
}

// set textareas to autoexpand
function autoexpand() {
    $('.autoExpand-sml,.autoExpand-med,.autoExpand-lrg').elastic();
}

// initialises jquery ajax handler
function initAjax() {
    $.ajaxSetup({
        type: "POST",
        cache: false,
        contentType: "application/json; charset=utf-8",
        error: function (req, statusText, errorThrown) {
            alert(req.status + " : " + req.statusText);
        }
    });
}

// gets webservice url for ajax call
function getServiceUrl(serviceName, methodName) {
    return baseUrl + "/services/" + serviceName + ".asmx/" + methodName;
}



// display the message bar
function setPromoCode(code, msg, hotel) {

    $("#promoWrap").fadeOut("slow");
    $("#exposeMask").fadeOut('slow');

    $("html").animate({ scrollTop: 0 }, "slow");
    displayMessageBar(msg, 'malmaison', false);
    var effOptions = { color: "#00D4D4" };
    $("#bookrm_txtPromoCode").val(code).effect("highlight", effOptions, 6000);
    $("#bookrm_txtFrom").val('');
    $("#bookrm_txtTo").val('');
    if (hotel != null && hotel != '') {
        var hotelList = $("#bookRoomWrap select");

        var val = hotelList.find("option:contains('" + hotel + "')").val();
        if (val != null && val != '') {
            hotelList.val(val);
            $("#bookRoomWrap .jqTransformSelectWrapper span:first").text(hotel);
        }
    }
}

// hides all validators when one is clicked
function setFieldValidatorClick() {
    $(".fieldValidator,label[id$=_passwordStrength]").each(function () {
        $(this).bind("click", function () {
            $(".fieldValidator").each(function () {
                $(this).delay(1000).fadeOut();
            });
            $("label[id$=_passwordStrength]").each(function () {
                $(this).delay(1000).fadeOut(function () {
                    $(this).text('');
                    $(this).attr('class', '');
                });

            });
        });
    });
}

// bind to click event of validation enabled links
function bindValidationLinks() {
    $("a.causesValidation").each(function () {
        $(this).bind("click", function () { validateInputs($(this).attr("validationGroup"), $(this).attr("validationErrorText")); });
    });
}

// check if page is valid
function validateInputs(group, msg) {
    if (!Page_ClientValidate(group)) {
        displayMessageBar(msg, "malmaison", true)
    }
}

// track the booking
function getPromo(id) {
    // only check the entry if the field is valid
    if (id > 0) {
        $.ajax({
            url: getServiceUrl("BookingEngineService", "GetPromotionContent"),
            data: "{'promoId': '" + id + "'}",
            success: function (result) {
                //alert(result.d.Message);
                $("#promoPopContent").html(result.d.Message);

                if (!isIE6) {
                    Cufon('h1', {
                        fontFamily: 'bliss-pro-light',
                        color: '#ffffff'
                    });
                    Cufon('h4', {
                        fontFamily: 'bliss-pro-light',
                        color: '#ffffff'
                    });
                }
            }
        });
    }
}


// track the booking
function getImage(id) {
    // only check the entry if the field is valid
    if (id > 0) {
        $.ajax({
            url: getServiceUrl("BookingEngineService", "GetImageGalleryContent"),
            data: "{'Id': '" + id + "'}",
            success: function (result) {
                //alert(result.d.Message);
                $("#imagePopContent").html(result.d.Message);

                if (!isIE6) {
                    Cufon('h1', {
                        fontFamily: 'bliss-pro-light',
                        color: '#ffffff'
                    });
                    Cufon('h4', {
                        fontFamily: 'bliss-pro-light',
                        color: '#ffffff'
                    });
                }
            }
        });
    }
}


// track the booking
function getVideo(id,$alt) {
    // only check the entry if the field is valid
    if (id > 0) {

        if (hasValidFlashPlayer()) {
            $.ajax({
                url: getServiceUrl("BookingEngineService", "GetVideoContent"),
                data: "{'vidId': '" + id + "'}",
                success: function (result) {
                    //alert(result.d.Message);
                    $("#videoPopContent").html(result.d.Message);
                    $('#videoPopContent').parent().siblings('h1').html($alt);
                    initCufon();
                }
            });
        } else {
            $("#videoPopContent").html("Please upgrade your Flashplayer to the latest version");
        }
    }
}

function setUpEventForm(event, venue, date, people) {
    //alert(event + ' , ' + venue + ' , ' + date + ' , ' + people);

    var lblEventName = $('#lblEventName');
    var lblEventDate = $('#lblEventDate');
    var lblVenue = $('#lblVenue');
    var lblPeople = $('#lblPeople');

    var hidVenue = $('#hidEnquiryVenue');
    $(hidVenue).val(venue);

    var hidEnquiryDate = $('#hidEnquiryDate');
    $(hidEnquiryDate).val(date);

    var hidEnquiryPeople = $('#hidEnquiryPeople');
    $(hidEnquiryPeople).val(people);

    var hidEventName = $('#hidEventName');
    $(hidEventName).val(event);

    $(lblEventName).text(event);
    $(lblEventDate).text(venue);
    $(lblVenue).text(date);
    $(lblPeople).text(people);
}



// Book a meeting header trigger function call
function bookMeeting($ovlyID,$el,$j,$msg,$confirm) {
    var $r = submitValidator($el, $j, $msg, null, 'malmaison');
    resetBookingMeetingPop();
    if ($r) {
        if ($('#' + $ovlyID).hasClass("init")) {
            $('#' + $ovlyID).overlay().load();
        }
        else {
            $('#' + $ovlyID).addClass("init");
            $('#' + $ovlyID).overlay({
                // some mask tweaks suitable for modal dialogs
                mask: { color: '#111',
                    loadSpeed: 1000,
                    opacity: 0.8
                },
                closeOnClick: false,
                onBeforeClose: function () { toggleSelects(true); },
                onBeforeLoad: function () { if (isIE6) { $("#requestSubmitWrapMeeting").appendTo($("#eventRequestWrap").parent()); } toggleSelects(false); },
                onClose: function () { },
                onLoad: function () { if (!isIE6) { $('.ReqForm').jqTransform(); } },
                load: true
            });
        }
    }
    return false;
}

function resetBookingMeetingPop() {
    var venueNameMeeting = $('#lstreqMeetingHotel :selected').text();
    var venueCodeMeeting = $('#lstreqMeetingHotel').val();
    var dtMeeting = $('#dateMeeting').val();
    var pplMeeting = $('#txtMeetingDelegatesMeeting').val();

    //alert(venueNameMeeting+" | "+venueCodeMeeting+" | "+dtMeeting+" | "+pplMeeting);

    $('#lblEnqDetailMeeting').text(venueNameMeeting + ' - ' + dtMeeting);
    $('#hidEnquiryVenueMeeting').val(venueNameMeeting);
    $('#hidEnquiryDateMeeting').val(dtMeeting);
    $('#hidEnquiryPeopleMeeting').val(pplMeeting);
    $('#hidEventNameMeeting').val('');
}

function isMobileDevice() {
    var $mobileDevices = new Array('iPad', 'iPhone', 'iPod', 'Android', 'Blackberry', 'Symbian');
    var $uAgent = navigator.userAgent.toLowerCase();
    var isMobile = false;

    $.each($mobileDevices, function (idx, val) {
        if ($uAgent.indexOf(val.toLowerCase()) != -1) {
            isMobile = true;
            return false;
        }
    });
    return isMobile;
}

// track the booking
function showEventPopup(id, moreId) {

    // only check the entry if the field is valid
    if (id > 0) {
        $.ajax({
            url: getServiceUrl("BookingEngineService", "GetEventPopupContent"),
            data: "{'eventId': " + id + ", 'moreDetailsId': " + moreId + "}",
            async: false,
            success: function (result) {

                // set content
                if (result.d.Success) {
                    $("#gPopupTitle").html(result.d.Message);
                    $("#gPopupText").html(result.d.ReturnData);

                    initCufon();

                    // if the overlay has already been initialised we
                    // just need to show it
                    if ($("#genericPopup").data("initialised")) {
                        $("#genericPopup").overlay().load();
                    }
                    else {
                        // show popup
                        $("#genericPopup").data("initialised", true);
                        $("#genericPopup").overlay({
                            fixed: false,
                            mask: { color: "#000",
                                loadSpeed: 200,
                                opacity: 0.7
                            },
                            load: true,
                            onClose: function () {
                                //intialiseWizard();
                                toggleSelects(true);
                            },
                            onBeforeLoad: function () { toggleSelects(false); }
                        });
                    }
                }
            }
        });
    }


}
