




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

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

        yuiDom = YAHOO.util.Dom,
        yuiEvent = YAHOO.util.Event,        

        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];
            if(link && container){
                var arrivalDate = Url.getParameter(link.getAttribute("href"),"arrivalDate");
                var rangeStart = CalUtils.dateFromString(arrivalDate);
                var propertyId = Url.getParameter(link.getAttribute("href"),"propertyID");
                var content = new Content();
                content.set("LOADING", " ");
                content.set("CLOSE", "Hide Calendar");
                var params = {                    
                    _animElement:container, // currently same as container, but could be different
                    trigger:link,
                    _baseHref:link.getAttribute("href"),
                    calStartDate:rangeStart,
                    ratePref:"ratePlanId",
                    ratePrefValue:Url.getParameter(link.getAttribute("href"),"ratePlanId"),
                    propertyId: propertyId,
                    numberRooms:Url.getParameter(link.getAttribute("href"),"numberOfRooms"),
                    numberAdults:Url.getParameter(link.getAttribute("href"),"numberOfAdults"),
                    container:container,
                    content: content
                };
                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));
                    //console.log(url);
                    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;

                var properties = yuiDom.getElementsByClassName("bookNow", "div", yuiDom.get("resultsContainer"));                
                properties.forEach(setupProperty);
            }

        };        
        return self;
    })();

    yuiEvent.onDOMReady(CalendarManager.initialize);

})();
