






//### BEGIN CALENDAR ### //
(function(){

    var SuperClass = SW.widget.Calendar.AvailabilityCalendar.prototype,
        Copy = SW.widget.Calendar.Copy,
        CopyCommon = SW.widget.Calendar.CopyCommon,
        CalUtils = SW.widget.Calendar.Utils,
        Url = SW.tools.Url,

        yuiDom = YAHOO.util.Dom,
        yuiEvent = YAHOO.util.Event,
//        PropertyManager = SW.widget.SearchResults.PropertyManager,

        CalendarManager;

    // ### CalendarManager ### //
    CalendarManager = (function(){
        // private
        var initialized = false;
        function setupProperty(property){
            var cal,
                link = yuiDom.getElementsByClassName("availCalLink","a",property)[0],
                container = yuiDom.getElementsByClassName("calContainer","div",property)[0];
                /*,
                detailsLink = yuiDom.getElementsByClassName("moreDetails","div",property.container)[0];*/
            if(link && container){
                var arrivalDate = Url.getParameter(link.getAttribute("href"),"arrivalDate");
                var rangeStart = SW.widget.Calendar.Utils.dateFromString(arrivalDate);
                var propertyId = Url.getParameter(link.getAttribute("href"),"propertyID");
                var copy = new Copy(Copy.getDefaultInstance());
                copy.set("LOADING", " ");
                copy.set("CLOSE", "Ocultar calendario");
                var params = {
                    _animElement:container, // currently same as container, but could be different
                    trigger:link,
                    _baseHref:link.getAttribute("href"),
                    calStartDate:rangeStart,
                    ratePlan:"null",
                    propertyId: propertyId,
                    numberRooms:Url.getParameter(link.getAttribute("href"),"numberOfRooms"),
                    numberAdults:Url.getParameter(link.getAttribute("href"),"numberOfAdults"),
                    container:container,
                    copy: copy
                };
//                cal = new CashPointsCalendar(params);
                cal = new SW.widget.Calendar.AvailabilityCalendar(params);
                cal.beforeShowEvent.subscribe(function(params){
                    yuiDom.setStyle("CalShow_"+propertyId, "display", "none");
                });
                cal.beforeHideEvent.subscribe(function(params){
                    yuiDom.setStyle("CalShow_"+propertyId, "display", "block");
                });

                cal.beforeShowEvent.subscribe(function(params){
                    yuiDom.setStyle("CalShow_"+propertyId, "display", "none");
                });
                cal.submitEvent.subscribe(function(param){
                    var url = cal.settings._baseHref;
                    url = Url.setParameter(url,"arrivalDate",CalUtils.dateToSystemFormat(cal.rangeStartDate));
                    url = Url.setParameter(url,"departureDate",CalUtils.dateToSystemFormat(cal.rangeEndDate));
                    url = Url.setParameter(url,"lengthOfStay",CalUtils.getLengthOfStay(cal.rangeStartDate,cal.rangeEndDate));
                    document.location.href = url;
                });
                yuiEvent.addListener(cal.triggers.show, "click", cal.handleShow, cal,true);
                /*
                hide listener is added internally since you can't always grab reference to
                the hide trigger until the calendar is actually rendered
                and if you're toggling, then you'd probably only use one trigger which is the show trigger
                */
//                yuiEvent.addListener(cal.triggers.hide, "click", cal.handleHide, cal, true);
                container.calendar = cal;
            }
        }
        // public
        var self = {
            initialize:function(){
                if(initialized){
                    return false;
                }
                initialized = true;
/*
                Copy.setCopy("LOADING", "");
                Copy.setCopy("");
*/
                var properties = yuiDom.getElementsByClassName("propertyListing", "div", yuiDom.get("searchResultPage"));
                properties.forEach(setupProperty);
            }

        };
        return self;
    })();

    yuiEvent.onDOMReady(CalendarManager.initialize);

})();