var preloadHeader = new Image();
preloadHeader.src = '/images/common/header-background.jpg';

function doResidential(select, selects) {
            $('maximumprice').style.display = 'block';
            $('maximumprice').name = 'maximumprice';
            $('minimumprice').style.display = 'block';
            $('minimumprice').name = 'minimumprice';
            $('minimumbedrooms').style.display = 'block';
            $('minimumbedrooms').name = 'minimumbedrooms';
            $('lettings').style.marginLeft = '50px';
            $('both').style.display = 'none';
            $('bothlabel').style.display = 'none';
    var values = new Array();
    var textValues = new Array();
    
    
    if ( $('both').checked == true) {
        $('sales').checked = true;
        doSales('minimumprice', 'min', 0);
        doSales('maximumprice', 'max', 0);
    }
    
    if (objExists(select)) {
        selectEmpty(select);
        values[0] = 0;
        textValues[0] = 'Property Type';
        values[1] = '';
        textValues[1] = 'All types';
        values[2] = 1;
        textValues[2] = 'Appartments/Flats';
        values[3] = 2;
        textValues[3] = 'Bungalow';
        values[4] = 3;
        textValues[4] = 'Cottage';
        values[5] = 4;
        textValues[5] = 'Commercial';
        values[6] = 5;
        textValues[6] = 'Detached House';
        values[7] = 7;
        textValues[7] = 'Semi-detached House';
        values[8] = 8;
        textValues[8] = 'Terraced';
        var i;
        for (i = 0; i < values.length; i++) {
            selectAddOption(select, textValues[i], values[i]);
            if (selects == values[i]) {
                selectIndex(select, i);
            }
        }
        selectIndex('propertytype', 0);
    }
}
function doCommercial(select, selects) {
            $('maximumprice').style.display = 'none';
            $('maximumprice').name = 'unused';
            $('minimumprice').style.display = 'none';
            $('minimumprice').name = 'unused';
            $('minimumbedrooms').style.display = 'none';
            $('minimumbedrooms').name = 'unused';
            $('lettings').style.marginLeft = '16px';
            $('both').style.display = 'block';
            $('bothlabel').style.display = 'block';
    var values = new Array();
    var textValues = new Array();
    if (objExists(select)) {
        selectEmpty(select);
        values[0] = 0;
        textValues[0] = 'Property Type';
        values[1] = '';
        textValues[1] = 'All types';
        values[2] = '01';
        textValues[2] = 'Office';
        values[3] = '02';
        textValues[3] = 'Shop';
        values[4] = '03';
        textValues[4] = 'Industrial';
        values[5] = '04';
        textValues[5] = 'Misc';
        values[6] = '05';
        textValues[6] = 'Land';
        var i;
        for (i = 0; i < values.length; i++) {
            selectAddOption(select, textValues[i], values[i]);
            if (selects == values[i]) {
                selectIndex(select, i);
            }
        }
        selectIndex('propertytype', 0);
    }
}

function doLettings(select, minmax, selects) {
    var values = new Array();
    var textValues = new Array();
    if (objExists(select)) {
        selectEmpty(select);

        if (minmax == 'max') {
            values[0] = 9999999;
            textValues[0] = 'Max Rent';
        } else {
            values[0] = 0;
            textValues[0] = 'Min Rent';
        }
        values[1] = 100;
        textValues[1] = '\u00a3100 pcm';
        values[2] = 200;
        textValues[2] = '\u00a3200 pcm';
        values[3] = 300;
        textValues[3] = '\u00a3300 pcm';
        values[4] = 400;
        textValues[4] = '\u00a3400 pcm';
        values[5] = 500;
        textValues[5] = '\u00a3500 pcm';
        values[6] = 600;
        textValues[6] = '\u00a3600 pcm';
        values[7] = 700;
        textValues[7] = '\u00a3700 pcm';
        values[8] = 800;
        textValues[8] = '\u00a3800 pcm';
        values[9] = 900;
        textValues[9] = '\u00a3900 pcm';
        values[10] = 1000;
        textValues[10] = '\u00a31000 pcm';
        values[11] = 1250;
        textValues[11] = '\u00a31250 pcm';
        values[12] = 1500;
        textValues[12] = '\u00a31500 pcm';
        values[13] = 2000;
        textValues[13] = '\u00a32000 pcm';
        var i;
        for (i = 0; i < values.length; i++) {
            selectAddOption(select, textValues[i], values[i]);
            if (selects == values[i]) {
                selectIndex(select, i);
            }
        }
    }
}

function doSales(select, minmax, selects) {
    var values = new Array();
    var textValues = new Array();
    
    if (objExists(select)) {
        selectEmpty(select);

        if (minmax == 'max') {
            values[0] = 9999999;
            textValues[0] = 'Max Price';
        } else {
            values[0] = 0;
            textValues[0] = 'Min Price';
        }
        values[1] = 75000;
        textValues[1] = '\u00a375,000';
        values[2] = 100000;
        textValues[2] = '\u00a3100,000';
        values[3] = 200000;
        textValues[3] = '\u00a3200,000';
        values[4] = 300000;
        textValues[4] = '\u00a3300,000';
        values[5] = 400000;
        textValues[5] = '\u00a3400,000';
        values[6] = 500000;
        textValues[6] = '\u00a3500,000';
        values[7] = 700000;
        textValues[7] = '\u00a3600,000';
        values[8] = 800000;
        textValues[8] = '\u00a3800,000';
        values[9] = 900000;
        textValues[9] = '\u00a3900,000';
        values[10] = 1000000;
        textValues[10] = '\u00a31,000,000';
        values[11] = 1250000;
        textValues[11] = '\u00a31,250,000';
        values[12] = 1500000;
        textValues[12] = '\u00a31,500,000';
        var i;
        for (i = 0; i < values.length; i++) {
            selectAddOption(select, textValues[i], values[i]);
            if (selects == values[i]) {
                selectIndex(select, i);
            }
        }
    }
}


// Button hovers
function buttonOver() {
    if (objExists('content')) {
        thewrap = document.getElementById('content');
        buttons = thewrap.getElementsByTagName('input');
        for (i = 0; i < buttons.length; i++) {
            if(buttons[i].getAttribute('type') == 'image') {
                buttons[i].onmouseover = function() {
                    this.src = this.src.substring(0, this.src.length - 5) + "2.jpg";
                }
                buttons[i].onmouseout = function() {
                    if (this.src.substring( (this.src.length - 5) , (this.src.length) ) == "2.jpg" ) {
                        this.src = this.src.substring(0, this.src.length - 5) + "1.jpg";
                    }
                }
            }
        }
    }
}

// Image hovers
function imageOver() {
    if (objExists('content')) {
        var thewrap = document.getElementById('content');
        var images = thewrap.getElementsByTagName('img');
        var preLoadImgs = new Array();

        for (i = 0; i < images.length; i++) {
            if(images[i].className.substring(0, 5) == "hover") {
                preLoadImgs[i] = new Image;
                preLoadImgs[i].src = images[i].src.substring(0, images[i].src.length - 5) + "2.jpg";

                images[i].onmouseover = function() {
                    this.src = this.src.substring(0, this.src.length - 5) + "2.jpg";
                }
                images[i].onmouseout = function() {
                    if (this.src.substring( (this.src.length - 5) , (this.src.length) ) == "2.jpg" ) {
                        this.src = this.src.substring(0, this.src.length - 5) + "1.jpg";
                    }
                }
            }
        }
    }
}

function initSearch() {
    if (objExists('residential')) {
        $('residential').onclick = function() {
            $('category').value = '1';
            $('maximumprice').style.display = 'block';
            $('maximumprice').name = 'maximumprice';
            $('minimumprice').style.display = 'block';
            $('minimumprice').name = 'minimumprice';
            $('minimumbedrooms').style.display = 'block';
            $('minimumbedrooms').name = 'minimumbedrooms';
            doResidential('propertytype', 0);
        }
    }
    if (objExists('commercial')) {
        $('commercial').onclick = function() {
            $('category').value = '3';
            $('maximumprice').style.display = 'none';
            $('maximumprice').name = 'unused';
            $('minimumprice').style.display = 'none';
            $('minimumprice').name = 'unused';
            $('minimumbedrooms').style.display = 'none';
            $('minimumbedrooms').name = 'unused';
            doCommercial('propertytype', 0);
        }
    }
    if (objExists('sales')) {
        $('sales').onclick = function() {
            doSales('minimumprice', 'min', 0);
            doSales('maximumprice', 'max', 0);
        }
    }
    if (objExists('lettings')) {
        $('lettings').onclick = function() {
            doLettings('minimumprice', 'min', 0);
            doLettings('maximumprice', 'max', 0);
        }
    }
}

addLoadEvent(initSearch);


function methodize(methodize_func,methodize_scope){
    return (function(){methodize_func.call(methodize_scope);});
}

function objExists(theVal) {
    if (document.getElementById(theVal) != null) {
        return true;
    } else {
        return false;
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addUnloadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    } else {
        window.onunload = function() {
            oldonunload();
            func();
        }
    }
}

function objExists(theVal) {
    if (document.getElementById(theVal) != null) {
        return true;
    } else {
        return false;
    }
}

function selectAddOption(selectbox, text, value) {
    var opt = document.createElement("option");
    opt.text = text;
    opt.value = value;
    document.getElementById(selectbox).options.add(opt);
}

function selectEmpty(selectbox) {
    var i;
    var select = document.getElementById(selectbox);
    for(i = select.options.length-1; i >= 0; i--) {
        select.remove(i);
    }
}

function selectIndex(selectbox, newIndex) {
    document.getElementById(selectbox).selectedIndex = newIndex;
}

function $(x) {
    return document.getElementById(x);
}

addLoadEvent(imageOver);
addLoadEvent(buttonOver);