/*
 * Client-Side JavaScript for SFV Vara fastigheter
 * Version: CVS $Id: search.js,v 1.5 2011/05/17 17:06:21 martin Exp $
 */
var APP_ROOT = ROOT + "/app/vara_fastigheter";

function selectLocation(location) {
    if (!location) { return; }
    var url = "/iframe/update-location/" + location;
    retrieve(url);
}

function doSearch() {
    var type = document.getElementById('type');
    var location = document.getElementById('location');
    if (location.value == 'all' && type.value == 'all') {
        EDIT_showHideLayers('messageDiv', '', 'show');
        return;
    }
    var url = "/iframe/search/" + type.value + "/" + location.value;
    retrieve(url);
}

function showPage(path) {
    var url = "/show/page" + path;
    retrieve(url);
}

function retrieve(url) {
    var dataContainer = document.getElementById("dataContainer");
    if (dataContainer) {
        dataContainer.src = APP_ROOT + url;
    }
}

function callback(doc, component) {
    try {
        var data = doc.getElementById("response");
        var divId = component + "Div";
        var componentDiv = document.getElementById(divId);
        componentDiv.innerHTML = data.innerHTML;
    } catch (e) {
        cuwing.log("Callback error: " + e);
    }
}

function showPage_callback(doc) {
    transferHTML(doc, document, "bigpic_div");
    transferHTML(doc, document, "content_areas");
    transferHTML(doc, document, "pagehelp");
}

/** Copy innerHTML from source id 'transfer_<idName>' to target id '<idName>' */
function transferHTML(source, target, idName) {
    try {
        var sourceElement = source.getElementById("transfer_" + idName);
        if (!sourceElement) {
            return;
        }
        var targetElement = target.getElementById(idName);
        if (!targetElement) {
            return;
        }
        targetElement.innerHTML = sourceElement.innerHTML;
    } catch (e) {
        cuwing.log("transferHTML error on id " + idName + ": " + e);
    }
}

function SwitchMenu(obj) {
    if (document.getElementById) {
        var el = document.getElementById(obj);
        el.style.display = el.style.display != "block" ? "block" : "none";
    }
}

function EDIT_showHideLayers() {
    var args = EDIT_showHideLayers.arguments;
    var i, p, v, obj;
    if (document.getElementById) {
        for (i = 0; i < (args.length - 2); i += 3) {
            obj = tmt_findObj(args[i]);
            if (obj) {
                v = args[i + 2];
                v = (v == 'show') ? 'visible' : (v = 'hide') ? 'hidden' : v;
                obj.style.visibility = v;
            }
        }
    } else {
        for (i = 0; i < (args.length - 2); i += 3)
            if ((obj = EDIT_findObj(args[i])) != null) {
                v = args[i + 2];
                if (obj.style) {
                    obj = obj.style;
                    v = (v == 'show') ? 'visible' : (v = 'hide') ? 'hidden' : v;
                }
                obj.visibility = v;
            }
    }
}

function EDIT_findObj(n, d) {
    var p, i, x;
    if (!d)
        d = document;
    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document;
        n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all)
        x = d.all[n];
    for (i = 0; !x && i < d.forms.length; i++)
        x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++)
        x = EDIT_findObj(n, d.layers[i].document);
    if (!x && d.getElementById)
        x = d.getElementById(n);
    return x;
}

function tmt_findObj(n) {
    var x, t;
    if ((n.indexOf("?")) > 0 && parent.frames.length) {
        t = n.split("?");
        x = eval("parent.frames['" + t[1] + "'].document.getElementById('"
                + t[0] + "')");
    } else {
        x = document.getElementById(n);
    }
    return x;
}

