/**
* @author Raduly Botond
*/
//Search result description height
var newsletterSliderContainer;
var promoSliderContainer;
var houseFeatures;
var sortCombo, sortListFx;
var sliderPosIndex = 0, selectedClassification = 0;
window.addEvent("domready", function () {
    sliderPosIndex = 0;
    selectedClassification = 0;
    if (Cookie.read("searchquery") != null && Cookie.read("searchquery") != "undefined" && Cookie.read("searchquery") != "") {
        var cObject = JSON.decode(Cookie.read("searchquery"));
        if (cObject.mins != null && cObject.mins != "undefined") { selectedClassification = cObject.mins - 1 }
        if (cObject.minb != null && cObject.minb != "undefined") { sliderPosIndex = cObject.minb - 1 }
    }

    var ocButton = new Element("div", { "class": "bns-ocbutton", "html": opcButtonLabel[0] });
    var bnsDescription = $("bns-description");
    var scroll = new Fx.Scroll(window, { wait: false, duration: 0, transition: Fx.Transitions.Quad.easeInOut });
    if (bnsDescription.getScrollSize().y > 0) {
        var visibleLines = 3;
        bnsDescription.store("state", "closed");
        bnsDescription.setStyles({ overflow: "hidden", height: visibleLines * 15 + "px" });
        ocButton.inject(bnsDescription, 'after');
        ocButton.addEvent("click", function () {
            var bnsDescriptionState = bnsDescription.retrieve("state");
            if (bnsDescriptionState == "closed") {
                ocButton.set('html', opcButtonLabel[1]);
                ocButton.setStyle("position", "static");
                bnsDescription.setStyles({ overflow: "visible", height: "auto" });
                bnsDescription.store("state", "opened");
            } else {
                ocButton.set('html', opcButtonLabel[0]);
                bnsDescription.setStyles({ overflow: "hidden", height: visibleLines * 15 + "px" });
                bnsDescription.store("state", "closed");
                ocButton.setStyle("position", "absolute");
                scroll.toElement($("mainmenu"));
            }
        })
    }
    //-->
    var tabcontainer = $("htypemenu");
    if (typeof tabcontainer != "undefined" && tabcontainer != null) {
        var b = true;
        var menuLine = $("housetypemenu");
        var moreMenu;
        var topPosition = tabcontainer.getPosition().y;
        var parentTopPadding = parseInt(menuLine.getStyle("padding-top"));
        var ddContMenu

        tabcontainer.getElements("li").each(function (e, i) {
            if ((e.getPosition().y - topPosition - parentTopPadding) > 0) {
                if (b) {
                    //if more menu doesn't exist
                    $("htype-more").setStyle("display", "block");
                    ddContMenu = new Element("div", { "id": "ddmoremenu" });
                    moreMenu = new Element("ul", { "id": "more-menu" });

                    ddContMenu.adopt([new Element("div", { "class": "ddhead" }), moreMenu, new Element("div", { "class": "ddfoot" })]);
                    ddContMenu.inject($("htype-more"));
                }
                moreMenu.grab(e);
                b = false;
            }
        })
        //if more menu exists
        if (!b) {
            var moreMenuContentHeight = 0;
            var chldCount = parseInt(moreMenu.getElements("li").length);
            var moreMenuFx = new Fx.Morph(moreMenu, { delay: 0, fps: 650, duration: chldCount * 40, link: 'ignore' });
            moreMenu.store("openState", false);
            moreMenuFx.addEvent("complete", function () {
                if (parseInt(moreMenu.getStyle("height")) == 0) {
                    ddContMenu.setStyle("display", "none");
                    moreMenu.store("openState", false);
                } else {
                    moreMenu.store("openState", true);
                }
            })

            $("htype-more-button").setStyle("display", "block");
            $("htype-more-button").addEvent("click", function () {
                if (ddContMenu.getStyle("display") == "none") {
                    ddContMenu.setStyle("display", "block");
                    if (moreMenuContentHeight == 0) { moreMenuContentHeight = moreMenu.getScrollSize().y }
                    moreMenuFx.cancel().start({ 'height': moreMenuContentHeight + "px" });

                } else {
                    moreMenuFx.cancel().start({ 'height': 0 + "px" });
                }
            })
            $(document.body).addEvent('click', function (e) {
                if ((moreMenu && !e.target || !$(e.target).getParents().contains(moreMenu)) && moreMenu.retrieve("openState")) {
                    moreMenuFx.cancel().start({ 'height': 0 + "px" });
                }
            });
        }
    }
    //->

    //promobanner slider    
    var cookiePromoObj = Cookie.read("sinterklaas");
    var isNotClosed = true;
    var brwSize = window.size();
    var positionLeft = brwSize - 412;
    if (brwSize.width < 1218) {
        positionLeft = 1218 - 412;
    }
    if (!mbopen && cookiePromoObj != 0 && cookiePromoObj != 2) {//issetObj(cookiePromoObj) && 
        var topPosition = 900;
        var distanceFromTop = brwSize.height - 435 - 20;
        if (brwSize.height < 435) {
            var distanceFromTop = 10;
        }
        promoSliderContainer = $('promoSliderRightContainer');
        promoSliderContainer.setStyle("top", (topPosition + distanceFromTop) + "px");

        $("closePromoSlider").addEvent("click", function () {
            //close promo slider
            Cookie.write("sinterklaas", '0', { duration: 360, path: "/" });
            var myfx = new Fx.Tween(promoSliderContainer);
            myfx.start("left", "-412px");
            myfx.start("width", "0px");            
            isNotClosed = false;
        })
        window.addEvent("scroll", function () {
            promoSliderContainer.tween("top", window.getScroll().y + distanceFromTop + "px");
        });
        var ss = new ScrollSpy({
            min: topPosition,
            onEnter: function (position, state, enters) {
                if (isNotClosed) {
                    var myfx = new Fx.Tween(promoSliderContainer);
                    myfx.start("left", "0px");
                    myfx.start("width", "412px");                    
                }
            },
            onLeave: function (position, state, leaves) {
                if (isNotClosed) {
                    var myfx = new Fx.Tween(promoSliderContainer);
                    myfx.start("left", "-412px");
                    myfx.start("width", "0px");                    
                }
            },
            container: window
        });
    }

    //newsletterbanner slider
    var cookieNewsObj = Cookie.read("newsletter");
    isNotClosed = true;
    brwSize = window.size();
    positionLeft = brwSize - 342;
    if (brwSize.width < 1218) {
        positionLeft = 1218 - 342;
    }
    if (!mbopen && issetObj(cookieNewsObj) && cookieNewsObj != 0 && cookieNewsObj != 2) {
        var topPosition = 900;
        var distanceFromTop = brwSize.height - 435 - 20;
        if (brwSize.height < 435) {
            var distanceFromTop = 10;
        }
        newsletterSliderContainer = $('newsletterSliderRightContainer');
        newsletterSliderContainer.setStyle("top", (topPosition + distanceFromTop) + "px");

        $("closeNewsletterSlider").addEvent("click", function () {
            //close newsletter slider
            Cookie.write("newsletter", '0', { duration: 360, path: "/" });
            var myfx = new Fx.Tween(newsletterSliderContainer);
            myfx.start("width", "0px");
            myfx.start("left", (positionLeft + 342) + "px");
            isNotClosed = false;
        })
        window.addEvent("scroll", function () {
            newsletterSliderContainer.tween("top", window.getScroll().y + distanceFromTop + "px");
        });
        var ss = new ScrollSpy({
            min: topPosition,
            onEnter: function (position, state, enters) {
                if (isNotClosed) {
                    var myfx = new Fx.Tween(newsletterSliderContainer);
                    myfx.start("width", "342px");
                    myfx.start("left", positionLeft + "px");
                }
            },
            onLeave: function (position, state, leaves) {
                if (isNotClosed) {
                    var myfx = new Fx.Tween(newsletterSliderContainer);
                    myfx.start("width", "0px");
                    myfx.start("left", (positionLeft + 342) + "px");
                }
            },
            container: window
        });
    }
    houseFeatures = $$('#iconset input');
    houseFeatures.each(function (object, index) {
        object.addEvent("click", function () {
            this.blur();
            if (this.getProperty("class").substring(0, 3) != "dis") {
                this.setProperty("class", addRemoveIconList(this) ? "sel" + (this.getProperty("class").substring(0, 3) == "hov" ? this.getProperty("class").substring(3) : this.getProperty("class")) : "hov" + this.getProperty("class").substring(3));                
                getRedirectLocation();
            }
        });
        object.addEvent("mouseenter", function () {
            if (this.getProperty("class").substring(0, 3) != "dis") {
                this.setProperty("class", (cookieObject.hf[this.name] != null && cookieObject.hf[this.name] != undefined) ? this.getProperty("class") : "hov" + this.getProperty("class"));
            }
        });
        object.addEvent("mouseleave", function () {
            if (this.getProperty("class").substring(0, 3) != "dis" && this.getProperty("class").substring(0, 3) != "bni") {
                this.setProperty("class", (cookieObject.hf[this.name] != null && cookieObject.hf[this.name] != undefined) ? this.getProperty("class") : this.getProperty("class").substring(3));
            }
        });
    });
    initCert();
    initBadrooms();
    //Sort by this combo
    if (typeof sortComboId != "undefined") {
        sortCombo = $("sbt-listcont");
        var sortByThisCombo = $(sortComboId);
        if (sortByThisCombo != null && sortByThisCombo != "undefined") {
            var sortationList = new Element("ul", { "id": "sbtlist" });
            sortCombo.adopt([new Element("div", { "class": "sbthead" }), sortationList, new Element("div", { "class": "sbtfoot" })]);
            sortCombo.store('openState', false);
            sortListFx = new Fx.Morph(sortCombo, { delay: 0, fps: 650, duration: 550, link: 'ignore' });
            sortListFx.addEvent("complete", function () {
                if (sortCombo.retrieve("openState")) {
                    sortCombo.store('openState', false);
                } else {
                    sortCombo.store('openState', true);
                }
            })

            if (sortCombo.retrieve("openState")) { }
            sortByThisCombo.getElements("option").each(function (e, i) {
                var nElement = new Element("li", {
                    html: e.innerHTML,
                    events: {
                        click: function () {
                            e.selected = true;
                            sortByThisCombo.value = e.value;
                            setTimeout(sortByThisCombo.getProperty('onchange'), 0);
                        }
                    }
                });
                if (e.selected) {
                    nElement.set("class", "sel")
                }
                sortationList.adopt(nElement);
            });
            $(document.body).addEvent('click', function (e) {
                if ((sortationList && !e.target || !$(e.target).getParents().contains(sortationList)) && sortCombo.retrieve("openState")) {
                    slideSortList()
                }
            });
        }
    }
    //Video Collumn request
    var filterCollumn = $("sr-leftfilterscont");
    var searchResultCollumn = $("sr-rightresultcontent");
    var filterContSize = filterCollumn.getSize();
    var searchResultCollumnSize = searchResultCollumn.getSize();
    var numberOfVideos = 0;
    var containerHeight = 0;
    var pagerHeight = 75;
    if (searchResultCollumnSize.y > (filterContSize.y + 180)) {
        numberOfVideos = Math.round(((searchResultCollumnSize.y - (filterContSize.y + 180) - pagerHeight) / 115) - 0.5);
        containerHeight = searchResultCollumnSize.y - filterContSize.y - 29;
        loadSideVideoMarkup(numberOfVideos, containerHeight, 0, (typeof (cookieObject.cid) != "undefined") ? cookieObject.cid : null, selectedLanguage);
    }
})

//load the markup for the videos on the left side of the search result pages
function loadSideVideoMarkup(numberOfVideos, containerHeight, pageNum, cid, lid) {
    PageMethods.GetSideVideoMarkup(numberOfVideos, containerHeight, pageNum, cid, lid, function(response) {
        if (!issetObj($("video-collum-cont"))) {
            var videoContainer = new Element("div", { id: "video-collum-cont", html: response });
            videoContainer.inject($("sr-leftfilterscont"), "bottom");
        }
        else {
            $("video-collum-cont").setProperty("html", response);
        }
    }, null);
}

/* generals */
function slideSortList() {
    var sortListContainer = sortCombo;
    var sortListContainerHeight = sortListContainer.getScrollSize().y;
    if (sortCombo.retrieve("openState")) {
        sortListFx.cancel().start({ "height": "0px" });
    } else {
        sortListFx.cancel().start({ "height": sortListContainerHeight + "px" });
    }
}
function initCert() {
    var clasificationDOMobj = $("search-clasifications");
    var scDOMobj = $("search-clasifications-selected");
    var fdisabled;
    if (clasificationDOMobj != null || clasificationDOMobj != undefined) {
        var classholders = clasificationDOMobj.getElements('span');
        classholders.each(function(el, index) {
            if (index < selectedClassification) {
                el.className = "nosel" + (index + 1)
            }
        });
        if (selectedClassification == 4) scDOMobj.getElement('div').innerHTML = "5";
        else scDOMobj.getElement('div').innerHTML = (selectedClassification + 1) + "-5";
        classholders.each(function(val, index) {
            val.addEvent("mouseout", function() {
                for (i = 0; i < selectedClassification; i++) {
                    classholders[i].className = "nosel" + (i + 1)
                }
            });
        })
        classholders.each(function(val, index) {
            val.addEvent("mouseover", function() {
                var i;
                for (i = selectedClassification - 1; i >= index; i--) {
                    classholders[i].className = "hover" + (i + 1);
                }
                if (index < selectedClassification) {
                    for (i = 0; i <= index - 1; i++) {
                        classholders[i].className = "nosel" + (i + 1);
                    }
                }
                else {
                    for (i = 0; i < selectedClassification; i++) {
                        classholders[i].className = "nosel" + (i + 1);
                    }
                }
                return false;
            })
            val.addEvent("click", function() {
                this.blur();
                var i;
                for (i = fdisabled; i >= index; i--) {
                    classholders[i].className = "sel" + (i + 1);
                }
                for (i = index - 1; i >= 0; i--) {
                    classholders[i].className = "nosel" + (i + 1);
                }
                if (index == 4) {
                    scDOMobj.getElement('div').innerHTML = 5;
                }
                else {
                    scDOMobj.getElement('div').innerHTML = (index + 1) + "-5";
                }
                selectedClassification = index;
                addSpecialSearchTerm("mins", selectedClassification + 1);
                window.location = window.location;
            })
        })
    }
}
function initBadrooms() {
    var bedroomsDOMobj = $("search-bedroom");
    var selectedBedroomsCont = $("search-bedroom-selected");
    if (bedroomsDOMobj != null || bedroomsDOMobj != undefined) {
        var bedroomsholder = bedroomsDOMobj.getElements('span');
        for (i = 0; i < 6; i++) {
            bedroomsholder[i].className = "nosel" + (i + 1)
        }
        bedroomsholder[sliderPosIndex].className = "sel" + (sliderPosIndex + 1)
        if (sliderPosIndex != 0) selectedBedroomsCont.getElement("div").innerHTML = "min" + (sliderPosIndex + 1);
        bedroomsholder.each(function(val, index) {
            if (!val.get('class').test("noclick")) {
                val.addEvent("mouseover", function() {
                    if (index != sliderPosIndex)
                        bedroomsholder[index].className = "hover" + (index + 1);
                });
                val.addEvent("mouseout", function() {
                    if (index != sliderPosIndex)
                        bedroomsholder[index].className = "nosel" + (index + 1);
                });
                val.addEvent("click", function() {
                    this.blur();
                    if (index != sliderPosIndex) {
                        bedroomsholder[sliderPosIndex].className = "nosel" + (sliderPosIndex + 1)
                        bedroomsholder[index].className = "sel" + (index + 1)
                        sliderPosIndex = index;
                        addSpecialSearchTerm("minb", sliderPosIndex + 1)
                        bedroomsDOMobj.getElement('span').innerHTML = "min " + (index + 1);
                    }
                    window.location = window.location;
                })
            } else {
                val.addEvent("click", function() { this.blur(); return false; })
            }
        })
    }
}
function disableList(obj) {
    var i
    for (i = 0; i < houseFeatures.length; i++) {
        if (obj[houseFeatures[i].name] != null && obj[houseFeatures[i].name] != "undefined" && (cookieObject.hf[houseFeatures[i].name] == null || cookieObject.hf[houseFeatures[i].name] == "undefined")) {
            houseFeatures[i].setProperty("class", "disbni" + houseFeatures[i].name.substring(2));
        } else {
            houseFeatures[i].setProperty("class", (cookieObject.hf[houseFeatures[i].name] != null && cookieObject.hf[houseFeatures[i].name] != "undefined") ? "selbni" + houseFeatures[i].name.substring(2) : (houseFeatures[i].getProperty("class").substring(0, 3) != "bni") ? houseFeatures[i].getProperty("class").substring(3) : houseFeatures[i].getProperty("class"));
        }
    }
}

function addSpecialSearchTerm(wht, val) {
    cookieObject[wht] = val;
    Cookie.write("searchquery", JSON.encode(cookieObject), cookieOptions);
}

function clearSearchTerms() {
    var emptyObj = { "hf": {}, "lid": selectedLanguage };
    cookieObject = emptyObj;
    Cookie.write("searchquery", JSON.encode(emptyObj), cookieOptions);
}
