﻿var le;
var searchInputDefaultText = "Enter street, town or postcode";

function initPage() {

    if ($("searchInput")) {
        Event.observe($("searchInput"), "focus", focusInput);
        Event.observe($("searchInput"), "blur", blurInput);
        blurInput();
    }

    $$("#nav img").each(
        function(elm) {
            Event.observe(elm, "mouseover", overNav); 
            Event.observe(elm, "mouseout", outNav);         
        }
    );
    
    if (window.location.href.indexOf("/property/") != -1) {
         $("resultsContainer").style.display = "block";
         advSearchOptions(document.forms.searchForm.useAdvanced);
    }      
    /*
    $$("#nav a").each(
        function(elm) {
            Event.observe(elm, "mouseover", overNav);
            Event.observe(elm, "mouseout", outNav);
            Event.observe(elm, "click", clickNav);
        }
    );
    */
};

        
function overNav(ev) {
    var elm = Event.element(ev);
    var i = new Image();
    i.src = elm.src.replace(/[01]/g, "1");
    elm.src = i.src;
};

function outNav(ev) {
    var elm = Event.element(ev);
    var i = new Image();
    i.src = elm.src.replace(/[01]/g, "0");
    elm.src = i.src;
};

function advSearchOptions(chkBx) {
    var checked = chkBx.value == "3";
    var ignore = { "btnSmpSearch":"1", "useAdvanced": "1", "useAdvanced2": "1", "useAdvanced3": "1", "companyID":"1", "colour":"1" }
    var state = ! checked;
    var opacity = state ? "disabled" : "enabled";
    
    for (var i = 0; i < document.forms["searchForm"].elements.length; i++) {
        var elm = document.forms["searchForm"].elements[i];
        if (elm.id && ! ignore[elm.id]) elm.disabled = state;
    }
    $("advSearchOptionsArea").className = opacity;
    $("saleTypeAll").value = chkBx.value || 1;
    $("saleTypeAll").disabled = checked;
}  

/*
function overNav(ev) {
    var elm = Event.element(ev);
    new Effect.Morph(elm, {
        style: 'background:#758738; color: #ffffff;',
        duration: 0.2
    });
};

function outNav(ev) {
    var elm = Event.element(ev);
    new Effect.Morph(elm, {
        style: 'background:#95ab47; color: #ffffff;',
        duration: 0.2
    });    
};

function clickNav(ev) {
    var elm = Event.element(ev);
    new Effect.Morph(elm, {
        style: 'background:#5c6a2c; color: #ffffff;',
        duration: 0.2
    });
}
*/

function focusInput() {
    if ($("searchInput").value == searchInputDefaultText) {
        $("searchInput").value = "";
        $("searchInput").className = "";
    }
};

function blurInput() {
    if ($("searchInput").value == "") {
        $("searchInput").value = searchInputDefaultText;
        $("searchInput").className = "empty";
    }
};

function showSearch() {
    Effect.SlideDown($("boxSearch"), {duration: 0.4});
};

function hideSearch() {};

function toggleAdvSearch() {
    var advSearch = $("searchAdvOpts");
    var persistentState;

    if (advSearch.style.display == "none") {
        Effect.SlideDown(advSearch, {duration: 0.4});
        //$("searchOptionsViewText").innerHTML = "Less search options";
        var img = new Image();
        img.src = "/images/text_lessoptions.gif";
        $("searchOptionsViewText").src = img.src;
        var arrow = new Image();
        arrow.src = "/images/icon_arrow_up.gif";
        $("optionsArrow").src = arrow.src;
        persistentState = 1;
    } else {
        Effect.SlideUp(advSearch, { duration: 0.4 });
        //$("searchOptionsViewText").innerHTML = "More search options";
        var img = new Image();
        img.src = "/images/text_moreoptions.gif";
        $("searchOptionsViewText").src = img.src;        
        var arrow = new Image();
        arrow.src = "/images/icon_arrow_down.gif";
        $("optionsArrow").src = arrow.src;
        persistentState = 0;     
    }
    
    Cookie.set("search", persistentState, 360);
};

function loadSearchResults() {
    if (!le)
        le = Event.observe($("hovInline"), "load", resultsLoaded);
        
    $("hovInline").height = 770;
    focusInput();
    $("parentContainer").style.display = "none";
    return true;
};

function resultsLoaded(ev) {
    if ($("hovInline").style.display == "none")
        Effect.Appear("hovInline", {duration: 0.5});
};