﻿; function VenueRequest(externalEntityId, control, action) {
    this.externalEntityId = externalEntityId;
    this.control = control;
    this.action = action;
}

; function Venue() {
}
jQuery.extend(Venue.prototype, {
    constants: {
        venueHotelListChangeHandlerUrl: "/layouts/VenueSearch/ChangeHotelsList.ashx?id=##entityid##&action=##action##",
        venueSearchHandlerUrl: "/layouts/VenueSearch/VenueSearchHandler.ashx?Location=##location##&DelegatesAmount=##delegates##&Capacity=##capacity##&Classification=##classification##&WithAccomodation=##withAccomodation##",
        searchRequestParameter: "##searchRequest##",
        location: "##location##",
        delegates: "##delegates##",
        capacity: "##capacity##",
        classification: "##classification##",
        withAccomodation: "##withAccomodation##",

        itemIdParameter: "##entityid##",
        actionParameter: "##action##"
    },

    addToRequest: function (externalEntityId, control) {
        var request = new VenueRequest(externalEntityId, jQuery(control).parent(), "Add");

        this.animateAddToRequestList(control);

        this.sendChangeHotelsListRequest(this.constructChangeHotelListUrl(request), request);
    },

    removeFromRequest: function (externalEntityId, control) {
        var request = new VenueRequest(externalEntityId, jQuery(control).parent(), "Remove");

        this.sendChangeHotelsListRequest(this.constructChangeHotelListUrl(request), request);
    },

    animateAddToRequestList: function (link) {

        var startPos = jQuery(link).offset();
        var clonedObj = jQuery('.addToRequestListNotify.original').clone();
        clonedObj.prependTo('body');
        clonedObj.css({ 'top': (startPos.top - 160) + 'px', 'left': startPos.left + 'px' });
        clonedObj.show();
        clonedObj.removeClass('original');

        clonedObj.show(1000);

        jQuery('.addToRequestListNotify.original').hide();

        setTimeout(function () { clonedObj.remove(); }, 3000);
    },

    updateHotelsAmount: function () {
        var venueSearchBox = jQuery('.venueSearchBoxHeadline');
        // no venue search box
        if (!venueSearchBox.length) {
            return;
        }

        this.sendHotelsSearchAmountRequest(this.constructUpdateFoundHotelsAmountUrl(venueSearchBox));
    },

    constructUpdateFoundHotelsAmountUrl: function (venueSearchBox) {
        var locationId = venueSearchBox.find('select[id*=uiDdlLocation]').val();
        var delegatesId = venueSearchBox.find('select[id*=uiDdlDelegates]').val();
        var capacityId = venueSearchBox.find('select[id*=uiDdlCapacity]').val();
        var classificationId = venueSearchBox.find('select[id*=uiDdlClasification]').val();
        var withAccomodation = venueSearchBox.find('input[id*=uiCbWithAccommodation]').is(':checked');
        var hotelSpecificParameters = jQuery('.hotelSpecificParameters');
        var url = this.constants.venueSearchHandlerUrl.replace(this.constants.location, locationId);
        url = url.replace(this.constants.delegates, delegatesId);
        url = url.replace(this.constants.capacity, capacityId);
        url = url.replace(this.constants.classification, classificationId);
        url = url.replace(this.constants.withAccomodation, withAccomodation);


        if (withAccomodation) {
            hotelSpecificParameters.show();
        }
        else {
            hotelSpecificParameters.hide();
        }

        return url;
    },

    constructChangeHotelListUrl: function (venueRequest) {
        var url = this.constants.venueHotelListChangeHandlerUrl.replace(this.constants.itemIdParameter, venueRequest.externalEntityId);
        url = url.replace(this.constants.actionParameter, venueRequest.action);

        return url;
    },

    sendHotelsSearchAmountRequest: function (handlerUrl) {
        jQuery.ajax({
            type: "POST",
            url: handlerUrl,
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            dataType: "text",
            success: function (msg) {
                venue.updateHotelsAmountSuccessHandler(msg);
            }
        });
    },

    sendChangeHotelsListRequest: function (handlerUrl, venueRequest) {
        jQuery.ajax({
            type: "POST",
            url: handlerUrl,
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            dataType: "text",
            success: function (msg) {
                venue.notifyAddToHotelListSucceed(venueRequest, msg);
            }
        });
    },

    updateHotelsAmountSuccessHandler: function (resultValue) {
        var result = eval('(' + resultValue + ')');
        jQuery('span[id*=uiLbMatches]').html(result.HotelCount);
    },

    notifyAddToHotelListSucceed: function (venueRequest, hotelListChangeResponseJson) {

        if (!venueRequest.control) {
            return;
        }

        var hotelListChangeResponse = eval('(' + hotelListChangeResponseJson + ')');

        this.updateRequestHotelsList(hotelListChangeResponse);



        //show added notification
        if (venueRequest.action == "Add") {
            /*
            var guidePopup = jQuery('#myGuidePopup');
            guidePopup.html(guideChangeResponse.PopupNotificationHtml);
            guidePopup.show();
            setTimeout(function () { guidePopup.hide() }, 5000);
            */
        }
        //swap remove 
        else {
            if (venueRequest.control && jQuery('#requestedHotelsListExtended').length) {
                var focusLink = jQuery('dl.items a[rel=removeFromRequest' + venueRequest.externalEntityId + ']');
                var dt = focusLink.parents('dt');
                var dd1 = dt.next();
                var dd2 = dd1.next();
                var dd3 = dd2.next();
                dt.animate({ opacity: 0 }, 500);
                dd1.animate({ opacity: 0 }, 500);
                dd2.animate({ opacity: 0 }, 500);
                dd3.animate({ opacity: 0 }, 500, function () { dt.hide(); dd1.hide(); dd2.hide(); dd3.hide(); });
            }
        }

        //if( &&  )


        this.swapChangeHotelListLinks(venueRequest);
    },

    swapChangeHotelListLinks: function (venueRequest) {
        if (venueRequest.control) {
            var addLink = jQuery('a[rel="addToRequest' + venueRequest.externalEntityId + '"]');
            var removeLink = jQuery('a[rel="removeFromRequest' + venueRequest.externalEntityId + '"]');
            if (venueRequest.action == "Add") {
                removeLink.show();
                addLink.hide();
            }
            else {
                addLink.show();
                removeLink.hide();
            }
        }
    },

    updateRequestHotelsList: function (hotelListChangeResponse) {
        var hotelsListDiv = jQuery('#requestedHotelsList');

        if (hotelsListDiv && hotelsListDiv.length) {
            hotelsListDiv.parent().html(hotelListChangeResponse.HotelsList);
        }
        var tmpVenue = new WoCo.venueSearch();
        tmpVenue.pageUpdate(0);

    }
});


function VenueListMeetingRoomFacilitiesScript() {
    var meetingRoomFacilities = jQuery('.meetingRoomFacilities');
    var theadHeight = meetingRoomFacilities.find('thead').height();
    meetingRoomFacilities.find('.subTableHeader').css({ 'height': theadHeight + 'px', 'visibility': 'visible' });
    meetingRoomFacilities.find('table').css('margin-top', '-' + theadHeight + 'px');
};


var venue = new Venue();



window.onload = function ()
{


}
