(function(){
    var yuiDom = YAHOO.util.Dom;
    var yuiEvent = YAHOO.util.Event;
    var yuiConnect = YAHOO.util.Connect;

    var homepagePromo= (function(){
        var backgroundContainer, crisisMessage;
        var currentIndex = 0;
        var properties,propertyOffers;
        var offersContainer, offerTopMax = 920, offerTopMin = 505, crisisMinOffset = 53, crisisMaxOffset=10;
        var isAnimating = false;
        var isOfferAnimating = false;
        var autoPlay = true;
        var hideOffersTimer = null;
        var slides = [];
        var self= {
            init:function(){
                backgroundContainer = yuiDom.get('background');
                crisisMessage = yuiDom.get('crisisMessageContainer');
                properties = propertiesArray;
                propertyOffers = yuiDom.getElementsByClassName("backgroundOffer","div",'backgroundOffers');

                if(crisisMessage){
                    offerTopMax-=crisisMaxOffset;
                    offerTopMin+=crisisMinOffset;
                }

                var nextPropControl = yuiDom.get("nextProp");
                yuiEvent.addListener(nextPropControl,"click",self.nextPropertyBridge);
                var previousPropControl = yuiDom.get("previousProp");
                yuiEvent.addListener(previousPropControl,"click",self.previousPropertyBridge);
                self.setupOffers();
                self.newModule.setup();

                setTimeout(function() {
                    self.nextPropertyAutoPlay();
                }, 15000);
            },
            updatePropertyInfo:function(property){
                yuiDom.get('propertyName').innerHTML = (property.propertyName ? property.propertyName : '&nbsp');
            },
            showNewBackground:function(params){
                var animateOut = new yuiAnim(params.current, {opacity:{from:1,to:0}},1);
                animateOut.onComplete.subscribe(function(){
                    yuiDom.removeClass(params.current,"currentImg");
                    yuiDom.addClass(params.current,params.nextClass);

                    yuiDom.setStyle(params.current,"background-image","url(\'"+properties[params.nextIndex].imgUrl+"\')");

                    isAnimating = false;
                });

                var animateIn = new yuiAnim(params.next, {opacity:{from:0,to:1}},.75);
                animateIn.onComplete.subscribe(function(){
                    yuiDom.removeClass(params.next,params.nextClass);
                    yuiDom.addClass(params.next,"currentImg");
                });

                animateIn.onStart.subscribe(function(){
                    isAnimating = true;
                    yuiDom.setStyle(params.next,"opacity","0");
                    yuiDom.setStyle(params.next,"visibility","visible");
                    yuiDom.removeClass(propertyOffers[params.previousIndex],"show");
                    yuiDom.addClass(propertyOffers[params.currentIndex],"show");
                    animateOut.animate();
                });
                animateIn.animate();

                yuiDom.setStyle(params.previous,"background-image","url(\'"+properties[params.previousIndex].imgUrl+"\')");
                self.updatePropertyInfo(properties[params.currentIndex]);

            },
            nextProperty:function(){
                var previousIndex = currentIndex;

                currentIndex++;
                if (currentIndex==properties.length){
                    currentIndex =  0;
                }
                var nextIndex = currentIndex+1;
                if (nextIndex==properties.length){
                    nextIndex =  0;
                }

                var background = {
                    nextIndex: nextIndex,
                    currentIndex: currentIndex,
                    current : yuiDom.getElementsByClassName("currentImg","div",backgroundContainer)[0],
                    next :  yuiDom.getElementsByClassName("nextImg","div",backgroundContainer)[0],
                    nextClass: "nextImg",
                    previous: yuiDom.getElementsByClassName("previousImg","div",backgroundContainer)[0],
                    previousIndex: previousIndex
                }

                self.showNewBackground(background);
            },
            nextPropertyBridge:function(e){
                yuiEvent.preventDefault(e);
                autoPlay = false;
                if (!isAnimating){
                    self.nextProperty();
                    // omniture tracking
                    self.tracking('event39','Next Promo');
                }
            },
            nextPropertyAutoPlay:function(){
                if (autoPlay){
                    self.nextProperty();

                    setTimeout(function() {
                        self.nextPropertyAutoPlay();
                    }, 10000);
                }
            },
            previousProperty:function(){
                var previousIndex = currentIndex;
                currentIndex--;

                if (currentIndex <0){
                    currentIndex =  properties.length-1;
                }

                var nextIndex = currentIndex-1;
                if (nextIndex <0){
                    nextIndex =  properties.length-1;
                }

                var background = {
                    nextIndex: nextIndex,
                    currentIndex: currentIndex,
                    current : yuiDom.getElementsByClassName("currentImg","div",backgroundContainer)[0],
                    next :  yuiDom.getElementsByClassName("previousImg","div",backgroundContainer)[0],
                    nextClass: "previousImg",
                    previous: yuiDom.getElementsByClassName("nextImg","div",backgroundContainer)[0],
                    previousIndex: previousIndex
                }

                self.showNewBackground(background);

            },
            previousPropertyBridge:function(e){
                yuiEvent.preventDefault(e);
                autoPlay = false;
                if (!isAnimating){
                    self.previousProperty();
                    // omniture tracking
                    self.tracking('event40','Previous Promo');
                }
            },
            setupOffers:function(){
                offersContainer = yuiDom.get('floatingContainer');
                yuiEvent.addListener(window,"scroll",self.updateOfferPosition);
                yuiEvent.addListener(window,"resize",self.updateOfferPosition);
                self.updateOfferPosition();
            },
            updateOfferPosition:function(){
                var scrollTop = (document.documentElement.scrollTop ?
                        document.documentElement.scrollTop :
                        document.body.scrollTop);

                var viewportSize = yuiDom.getViewportHeight();

                viewportSize+=scrollTop;

                if (viewportSize>offerTopMax){
                    yuiDom.addClass(offersContainer,"absoluteBottom");
                }else if (viewportSize<offerTopMin){
                    yuiDom.addClass(offersContainer,"absoluteTop");
                }else{
                    yuiDom.removeClass(offersContainer,"absoluteTop");
                    yuiDom.removeClass(offersContainer,"absoluteBottom");
                }
            },
            newModule:{
                currentSlide:0,
                setup:function(){
                    if (yuiDom.get('slidesContainer')){
                        slides = yuiDom.getElementsByClassName('slide','div','slidesContainer');
                        var previousSlide = yuiDom.getElementsByClassName('previousSlide','div','slideControl')[0];
                        var nextSlide = yuiDom.getElementsByClassName('nextSlide','div','slideControl')[0];
                        yuiEvent.addListener(previousSlide,'click',self.newModule.previousSlideBridge);
                        yuiEvent.addListener(nextSlide,'click',self.newModule.nextSlideBridge);
                    }
                },
                previousSlideBridge:function(e){
                    yuiEvent.preventDefault(e);
                    var currentIndex = self.newModule.currentSlide;
                    self.newModule.currentSlide--;
                    if (self.newModule.currentSlide == -1){
                        self.newModule.currentSlide = slides.length-1;
                    }
                    var newIndex = self.newModule.currentSlide;
                    self.newModule.updateSlide(currentIndex,newIndex);
                },
                nextSlideBridge:function(e){
                    yuiEvent.preventDefault(e);
                    var currentIndex = self.newModule.currentSlide;
                    self.newModule.currentSlide++;
                    if (self.newModule.currentSlide == slides.length){
                        self.newModule.currentSlide = 0;
                    }
                    var newIndex = self.newModule.currentSlide;
                    self.newModule.updateSlide(currentIndex,newIndex);
                },
                updateSlide:function(currentIndex,newIndex){
                    yuiDom.removeClass(slides[currentIndex],"active");
                    if (yuiDom.hasClass(slides[currentIndex],"property")){
                        yuiDom.setStyle(yuiDom.get('seeAllNew'),'display','none');
                    }
                    yuiDom.addClass(slides[newIndex],"active");
                    if (yuiDom.hasClass(slides[newIndex],"property")){
                        yuiDom.setStyle(yuiDom.get('seeAllNew'),'display','block');
                    }
                }
            },
            tracking:function(eventId,eventDesc){
                if (typeof s != 'undefined') {
                    s.linkTrackVars = 'events';
                    s.linkTrackEvents=eventId;
                    s.events = eventId;
                    s.tl(this, 'o', eventDesc);
                }
            }
        }
        return self;
    })();
    YAHOO.namespace("SW.local.homepagePromo");
    SW.local.homepagePromo = homepagePromo;

    yuiEvent.onDOMReady(function(){
        SW.local.homepagePromo.init();
    },true);
})();



(function (){
    var yuiDom = YAHOO.util.Dom;

    var yuiEvent = YAHOO.util.Event;

    var SearchFormHandler = (function () {
        var searchForm = null;
        var localeCode = "en_US";
        var searchType = "standard";
        var standardTrigger =  null;
        var standardFormToggler = null;
        var pinYinTrigger = null;
        var pinYinFormToggler = null;
        var dateInputs = null;
        var standardInputs = null;
        var pinYinInputs = null;
        var loadingText = "Loading...";
        var cityText = "City";
        var self = {
            initialize: function(params) {
                localeCode = params.localeCode || localeCode;
                loadingText = params.loadingText || loadingText;
                cityText = params.cityText || cityText;
                searchForm = yuiDom.get("findAndBookForm");

                dateInputs = {
                    arrivalDate: yuiDom.get("fnb_arrivalDate"),
                    departureDate: yuiDom.get("fnb_departureDate")
                }

                if(localeCode == "zh_CN" || localeCode == "ja_JP") {
                    standardInputs = {
                        city: yuiDom.get("fnb_citySelect"),
                        state: yuiDom.get("fnb_stateSelect"),
                        country: yuiDom.get("fnb_countrySelect")
                    }
                    yuiEvent.addListener(standardInputs.country,"change",self.checkState, {countrySelect: standardInputs.country, stateSelect: standardInputs.state, citySelect: standardInputs.city});
                    yuiEvent.addListener(standardInputs.state,"change",self.getCities);
                }else{
                    yuiEvent.addListener(searchForm.complexSearchField,"focus",self.inputFocus);
                    yuiEvent.addListener(searchForm.complexSearchField,"blur",self.inputBlur);
                }
            },
            inputFocus:function(){
                var defaultValue = searchForm.complexSearchField.getAttribute("label");
                var errorValue = searchForm.complexSearchField.getAttribute("error");
                var searchValue = searchForm.complexSearchField.value;
                if (searchValue == errorValue){
                    yuiDom.removeClass(searchForm.complexSearchField,"error");
                }
                if (searchValue == defaultValue || searchValue == errorValue){
                    searchForm.complexSearchField.value = "";
                }
            },
            inputBlur:function(){
                var defaultValue = searchForm.complexSearchField.getAttribute("label");
                var searchValue = searchForm.complexSearchField.value;
                if (searchValue == ''){
                    searchForm.complexSearchField.value = defaultValue;
                }
            },
            getCities: function () {
                standardInputs.city.disabled = true;
                yuiConnect.asyncRequest("GET", "/dp/en_US/common/search/get_cities.jsp?country="
                        + standardInputs.country.value
                        + "&state="
                        + (standardInputs.state.selectedIndex > 0 ? standardInputs.state.value : ""),
                {
                    success: self.populateSelect
                }
                        );
                standardInputs.city.options[0].text = loadingText;
            },
            populateSelect: function (xhr) {
                var selectOptions = xhr.responseXML.getElementsByTagName("city");

                standardInputs.city.options.length = 1;
                standardInputs.city.selectedIndex = 0;
                standardInputs.city.options[0].text = cityText;
                if (selectOptions.length > 0) {
                    standardInputs.city.disabled = false;
                    for (var i = 0; i < selectOptions.length; i++) {
                        var selected = false;
                        standardInputs.city.options[standardInputs.city.options.length] = new Option(selectOptions[i].firstChild.nodeValue, selectOptions[i].getAttribute("en"), selected);
                    }
                }
            },
            updateCountry: function () {

            },
            checkState: function(e, params) {
                yuiDom.removeClass(searchForm.country,"error");

                if(params.countrySelect.value == "US" || params.countrySelect.value == "CA") {
                    params.stateSelect.disabled = false;
                    params.citySelect.disabled = true;
                }
                else {
                    params.stateSelect.selectedIndex = 1;
                    params.stateSelect.value = "";
                    params.stateSelect.disabled = true;
                    self.getCities();
                }
            },
            validateForm: function (e,searchForm,hidingErrors) {
                var errors = false;
                var searchTermCount = 0;
                if(searchForm.complexSearchField && searchForm.complexSearchField.value != ""){
                    searchTermCount = searchForm.complexSearchField.value.replace(/^\s*|\s*$/g,'').length;
                }
                if(searchForm.complexSearchField && (searchForm.complexSearchField.value == "" || searchTermCount==0 || searchForm.complexSearchField.value == searchForm.complexSearchField.getAttribute("label") || searchForm.complexSearchField.value == searchForm.complexSearchField.getAttribute("error"))) {
                    searchForm.complexSearchField.value = searchForm.complexSearchField.getAttribute("error");
                    if (!hidingErrors){ yuiDom.addClass(searchForm.complexSearchField,"error");}
                    errors = true;
                }
                else if(localeCode == "ja_JP" || localeCode == "zh_CN") {
                    if((standardInputs.country.value == "")) {
                        if (!hidingErrors) {
                            yuiDom.addClass('searchErrorMsg',"show");
                        }
                        errors = true;
                    }
                }
                return !errors;
            }
        }

        return self;
    })();
    YAHOO.namespace("SW.local.SearchFormHandler");
    SW.local.SearchFormHandler = SearchFormHandler;
})();

(function() {
    var yuiDom = YAHOO.util.Dom;

    var yuiEvent = YAHOO.util.Event;

    var DEFAULT_CONFIG = {
        arrivalDateInput: null,
        departureDateInput: null,
        calendarIcon: null,
        localeCode: "en_US",
        dateFormatString: "MM/DD/YYYY"
    }

    function CalendarMaker (config) {

        if(!config.calendarId){
            throw("no calendarId supplied");
        }

        if(!config.arrivalDateInput){
            throw("no arrivalDateInput supplied");
        }

        if(!config.departureDateInput){
            throw("no departureDateInput supplied");
        }

        if(!config.calendarIcon){
            throw("no calendarIcon supplied");
        }

        for(var prop in DEFAULT_CONFIG){
            if(!config.hasOwnProperty(prop)){
                config[prop] = DEFAULT_CONFIG[prop];
            }
        }

        var calendar = null;
        var arrivalDateInput = config.arrivalDateInput;
        var departureDateInput = config.departureDateInput;
        var calendarIcon = config.calendarIcon;
        var localeCode = config.localeCode || localeCode;
        localeCode = localeCode.split("_")[0];
        var dateFormatString = config.dateFormatString || dateFormatString;

        function getArrivalDate () {
            return setDateFromString(localeCode, arrivalDateInput.value);
        }

        function getDepartureDate () {
            return setDateFromString(localeCode, departureDateInput.value);
        }

        function setDates (arrivalDate, departureDate, fromContinue) {
            arrivalDateInput.value = getFormatedDate(localeCode, arrivalDate);
            yuiDom.removeClass(arrivalDateInput, "hasDefaultText");
            departureDateInput.value = getFormatedDate(localeCode, departureDate);
            yuiDom.removeClass(departureDateInput, "hasDefaultText");
            if (!fromContinue) {
                closeCalendar();
            }

            if (SW.local.SearchFormHandler.validateForm(null,yuiDom.get('findAndBookForm'),true)){
                yuiDom.get('findAndBookForm').submit();
            }
        }

        function checkArrivalDateFocus () {
            if (arrivalDateInput.value == dateFormatString) {
                arrivalDateInput.value = "";
                return;
            }
            var checkIn = new validDate(arrivalDateInput);
            if (!checkIn.valid) {
                arrivalDateInput.value = "";
            }
            return;
        }

        function checkArrivalDateBlur () {
            var checkIn = new validDate(arrivalDateInput);
            if (checkIn.valid) {
                checkIn.setField();
                var isValid = true;
                if (departureDateInput.value == dateFormatString) {
                    isValid = false;
                }
                var checkOut = validDate(departureDateInput);
                var before;
                if (!isValid) {
                    departureDateInput.value = dateFormatString;
                }
                if (checkOut.valid) {
                    before = (checkIn.diffDate(checkOut.d) <= 0);
                }

                if (departureDateInput.value == dateFormatString) {
                    checkOut.valid = false;
                }
                if ((! (checkOut.valid)) || before) {
                    checkOut.setD(checkIn.d);
                    checkOut.nextDay();
                    checkOut.setField();
                    yuiDom.removeClass(departureDateInput, "hasDefaultText");
                }
            } else {
                arrivalDateInput.value = dateFormatString;
            }
        }

        function checkDepartureDateFocus () {
            if (departureDateInput.value == dateFormatString) {
                departureDateInput.value = "";
                return;
            }
            var checkOut = new validDate(departureDateInput);
            if (!checkOut.valid) {
                departureDateInput.value = "";
            }
        }

        function checkDepartureDateBlur () {
            var checkOut = new validDate(departureDateInput);
            if (checkOut.valid) {
                checkOut.setField();
            } else {
                departureDateInput.value = dateFormatString;
            }
        }

        var self = {
            toggleCalendar: function (e, calendarPos) {
                if(!calendarPos) {
                    var coords = yuiDom.getXY(this);
                    calendarPos = (coords[0] - 450) + "," + (coords[1] + 20);
                }
                calendar.show(this, calendarPos);
                var e = e || window.event;
                e.cancelBubble = true;
                if (e.stopPropagation) e.stopPropagation();
            },
            closeCalendar: function () {
                if (calendar) {
                    if (calendar.display == "block") {
                        calendar.toggleDisplay();
                    }
                }
            }
        }

        calendar = new multiDisplayCalendar(config.calendarId,
                getArrivalDate,
                getDepartureDate,
                setDates, null);

        yuiEvent.addListener(calendarIcon, "click", self.toggleCalendar, config.calendarPos);

        return self;
    }

    SW.local.CalendarMaker = function(config){
        return new CalendarMaker(config);
    }

    YAHOO.namespace("SW.local.CalendarMaker");
})();




