﻿var offers = {
    contentPanelId: null,
    loaderId: null,
    numPeople: '',
    offerType: '', //day, 24plus, residential
    offerRating: '',
    fvd: true, //first view details
    fe: true, //first enquire
    fm: true, //first mail button click
    fmg: true, //first mail go button click
    fs: true, // first search
    country: null,
    get_action_viewdetails: function() {
        if (this.fvd == true) {
            this.fvd = false;
            return "FVD";
        } else return "SVD";
    },
    get_action_enquire: function() {
        if (this.fe == true) {
            this.fe = false;
            return "FE";
        } else return "SE";
    },

    get_action_email_click: function() {
        if (this.fm == true) {
            this.fm = false;
            return "FM";
        } else return "SM";
    },

    get_action_mail_submit: function() {
        if (this.fmg == true) {
            this.fmg = false;
            return "FMG";
        } else return "SMG";
    },

    get_action_search: function() {
        if (this.fs == true) {
            this.fs = false;
            return "FS";
        } else return "SS";
    },
    init: function(loaderId, contentPanelId, country) {
        this.loaderId = loaderId;
        this.contentPanelId = contentPanelId;
        this.country = country;
    },
    arrayContains: function(arr, val) {
        var f = false;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                f = true;
                break;
            }
        }
        return f;
    },
    arrayRemove: function(arr, val) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                arr.splice(i, 1);
                break;
            }
        }
    },
    trackEvent: function(func) {
        setTimeout(func, 3000);
    },
    arrayToString: function(arr, sep) {
        var val = "";
        for (var i = 0; i < arr.length; i++) {
            val += (val.length > 1 ? sep + arr[i] : arr[i]);
        }
        return val;
    },
    selPeople: function(link, val) {
        $(".selector .c_3 a").removeClass('selected');
        $(link).addClass("selected");
        this.numPeople = val;
    },

    selOffType: function(link, val) {
        $(".c_2 a").removeClass("selected");
        $(link).addClass("selected");
        this.offerType = val;

    },
    clearRatings: function() {
        for (var i = 0; i < $(".c_4 a img").length; i++) {
            $(".c_4 a img")[i].src = $(".c_4 a img")[i].src.replace('large', 'small');
        }

        $(".c_4 a").removeClass("selected");
        this.offerRating = '';
    },
    hovOutRating: function(link, ratImgId, imgSrc) {
        if (!$(link).hasClass('selected')) {
            $("#" + ratImgId).attr({ src: imgSrc });
        }
    },
    hovRating: function(link, ratImgId, imgSrc) {

        if (!$(link).hasClass('selected')) {
            $("#" + ratImgId).attr({ src: imgSrc });
        }
    },
    selRating: function(link, val, ratImgId, imgSrc) {
        this.clearRatings();
        $("#" + ratImgId).attr({ src: imgSrc.replace('small', 'large') });
        $(link).addClass("selected");
        this.offerRating = val;
    },
    offersLoaded: function() {
        $('#' + this.contentPanelId).slideDown();
    },
    ResetFilters: function() {

        this.clearRatings();
        $(".c_2 a").removeClass("selected");
        this.offerType = '';
        $(".selector .c_3 a").removeClass('selected');
        this.numPeople = '';

        var bef = "$('#" + this.contentPanelId + "').hide();offers.showLoader()";
        var aft = "offers.hideLoader()";
        var onSuccess = "offers.initOffersEffects();offers.offersLoaded();";
        var params = "ContentPanelId=" + this.contentPanelId + "&country=" + this.country + "&OnSuccess=" + onSuccess;
        hcAjax.call("/Webservices/Offers.asmx/GetRandomResults", params, bef, aft, true);
        this.trackEvent("pageTracker._trackEvent('" + this.country + "','" + this.get_action_search() + "', 'Reset filters')");
        return false;
    },
    getAllOffers: function() {

        var bef = "$('#" + this.contentPanelId + "').hide();offers.showLoader()";
        var aft = "offers.hideLoader()";
        var onSuccess = "offers.initOffersEffects();offers.offersLoaded();";
        var params = "ContentPanelId=" + this.contentPanelId + "&country=" + this.country + "&OnSuccess=" + onSuccess;
        hcAjax.call("/Webservices/Offers.asmx/GetAllOffers", params, bef, aft, true);
        this.trackEvent("pageTracker._trackEvent('" + this.country + "','" + this.get_action_search() + "', 'Show all')");
        return false;
    },
    applyFilter: function() {
        var bef = "$('#" + this.contentPanelId + "').hide();offers.showLoader()";
        var aft = "offers.hideLoader()"; //$('#" + this.contentPanelId + "').show();
        //        var bef = "document.getElementById('" + this.contentPanelId + "').style.visibility = 'hidden';offers.showLoader();";
        //        var aft = " document.getElementById('" + this.contentPanelId + "').style.visibility = 'visible'; offers.hideLoader()";
        var onSuccess = "offers.initOffersEffects();offers.offersLoaded();";
        var params = "NumPeople=" + this.numPeople + "&OfferType=" + this.offerType + "&OfferRating=" + this.offerRating + "&country=" + this.country + "&ContentPanelId=" + this.contentPanelId + "&OnSuccess=" + onSuccess;
        hcAjax.call("/Webservices/Offers.asmx/GetOffers", params, bef, aft, true);
        this.trackEvent("pageTracker._trackEvent('" + this.country + "','" + this.get_action_search() + "', 'Apply filter','| " + this.offerType + " | " + this.numPeople + " | " + this.offerRating + "')");
        return false;
    },
    initOfferDetailsEffects: function(panelId) {

        $('#' + panelId + ' .airport').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            showBody: " - ",
            extraClass: "airport_tooltip",
            fixPNG: true
        });
    },

    featuredDetailsLoaded: function(panelId) {
        $('.specials').hide();
        $('#' + panelId).show();
    },
    closeFeaturedDetails: function(panelId) {
        $('.specials').show();
        $('#' + panelId).hide();
    },
    LoadFeaturedDetails: function(offerDetails, matchType, offerId, panelId, linkId) {
        if (document.getElementById(panelId).innerHTML.length < 10) {
            var bef = "$('#" + linkId + "').addClass('loading');";
            var aft = "$('#" + linkId + "').removeClass('loading');";
            var onSuccess = "offers.featuredDetailsLoaded('" + panelId + "');offers.initOfferDetailsEffects('" + panelId + "');";
            var onClose = "return offers.closeFeaturedDetails('" + panelId + "');";
            var params = "OfferId=" + offerId + "&ContentPanelId=" + panelId + "&country=" + this.country + "&OnClose=" + onClose + "&OnSuccess=" + onSuccess;
            params += "&Featured=true";
            hcAjax.call("/Webservices/Offers.asmx/LoadOfferDetails", params, bef, aft, true);
            this.trackEvent("pageTracker._trackEvent('" + this.country + "','" + this.get_action_viewdetails() + "', 'Featured offer(" + matchType + ")', '" + offerDetails + "')");
        }
        else {
            this.featuredDetailsLoaded(panelId);
        }
        return false;
    },
    offerDetailsLoaded: function(panelId, openid, closeId) {
        $('#' + panelId).slideDown();
        $('#' + openid).hide();
        $('#' + closeId).show();
    },
    closeOfferDetails: function(openid, closeId, panelId) {

        //$('#' + panelId).slideUp();
        $('#' + panelId).hide();
        $('#' + openid).show();
        $('#' + closeId).hide();
    },
    LoadOfferDetails: function(openid, closeId, offerDetails, matchType, offerId, panelId, loaderId) {

        if (document.getElementById(panelId).innerHTML.length < 10) {
            var bef = "$('#" + loaderId + "').show();";
            var aft = "$('#" + loaderId + "').hide();";
            var onSuccess = "offers.offerDetailsLoaded('" + panelId + "','" + openid + "','" + closeId + "');offers.initOfferDetailsEffects('" + panelId + "');";
            var onClose = "return offers.closeOfferDetails('" + panelId + "');";
            var params = "OfferId=" + offerId + "&country=" + this.country + "&ContentPanelId=" + panelId + "&OnClose=" + onClose + "&OnSuccess=" + onSuccess;
            params += "&Featured=false";
            hcAjax.call("/Webservices/Offers.asmx/LoadOfferDetails", params, bef, aft, true);
            this.trackEvent("pageTracker._trackEvent('" + this.country + "','" + this.get_action_viewdetails() + "', 'Other offer(" + matchType + ")', '" + offerDetails + "')");
        }
        else {
            this.offerDetailsLoaded(panelId, openid, closeId);
        }
        return false;
    },
    emailOffer: function(offerDetails, offerId, emailBoxId, loaderId, PopupsPanelId) {
        var bef = "$('#" + loaderId + "').show();";
        var aft = "$('#" + loaderId + "').hide();";
        if (document.getElementById(emailBoxId).value.length == 0) {
            alert('Please enter an email address an try again.');
            document.getElementById(emailBoxId).focus();
        }
        else {
            var onSuccess = "offers.closePopups('" + PopupsPanelId + "');";
            var params = "OfferId=" + offerId + "&to=" + encodeURI(document.getElementById(emailBoxId).value) + "&country=" + this.country + "&onSuccess=" + onSuccess;
            hcAjax.call("/Webservices/Offers.asmx/EmailOffer", params, bef, aft, true);
            this.trackEvent("pageTracker._trackEvent('" + this.country + "','" + this.get_action_mail_submit() + "', '" + offerDetails + "')");
        }
        return false;
    },
    newsletterSignUp: function(emailBoxId) {
        var bef = "$('#" + emailBoxId + "').attr('disabled', true);";
        var aft = "$('#" + emailBoxId + "').attr('disabled',false);";
        if (document.getElementById(emailBoxId).value.length == 0) {
            alert('Please enter an email address an try again.');
            document.getElementById(emailBoxId).focus();
        }
        else {
            var onSuccess = "document.getElementById('" + emailBoxId + "').value = '';";
            var params = "Email=" + encodeURI(document.getElementById(emailBoxId).value) + "&onSuccess=" + onSuccess;
            hcAjax.call("/Webservices/Offers.asmx/NewsletterSignup", params, bef, aft, true);
        }
        return false;
    },
    closePopups: function(popupPanelId) {
        $('#' + popupPanelId + " .popup").hide();
        return false;
    },
    showPopupEnquire: function(offerDetails, popupPanelId, popupId) {
        $('#' + popupPanelId + " .popup").hide();
        $('#' + popupId).show();
        this.trackEvent("pageTracker._trackEvent('" + this.country + "','" + this.get_action_enquire() + "', '" + offerDetails + "')");
        return false;
    },
    showPopupEmail: function(offerDetails, popupPanelId, popupId) {
        $('#' + popupPanelId + " .popup").hide();
        $('#' + popupId).show();
        this.trackEvent("pageTracker._trackEvent('" + this.country + "','" + this.get_action_email_click() + "', '" + offerDetails + "')");
        return false;
    },
    showLoader: function() {
        $('#' + this.loaderId).show();
    },
    hideLoader: function() {
        $('#' + this.loaderId).hide();
    },
    emailKeyPress: function(ctlId, event) {
        if ((window.event && window.event.keyCode == 13) || (event && event.which == 13)) {
            document.getElementById(ctlId).click();
            return false;
        }
    },
    initOffersEffects: function() {
        $('.results .item .c_3 img').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            showBody: " - ",
            extraClass: "icon",
            fixPNG: true
        });
        $('.results .item .c_4 .theatre, .results .item .c_4 .capacity').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            showBody: " - ",
            extraClass: "tooltip-capacity",
            fixPNG: true
        });

    }

}




$(document).ready(function() {
    offers.initOffersEffects();
});
