var preloadHeader = new Image();
preloadHeader.src = '/images/common/header-background.jpg';
var barnsley = new Image();
barnsley.src = '/images/common/nav-barnsley.jpg';
var branches = new Image();
branches.src = '/images/common/nav-branches.jpg';
var company = new Image();
company.src = '/images/common/nav-company.jpg';
var contact = new Image();
contact.src = '/images/common/nav-contact.jpg';
var departments = new Image();
departments.src = '/images/common/nav-departments.jpg';
var doncaster = new Image();
doncaster.src = '/images/common/nav-doncaster.jpg';
var financial = new Image();
financial.src = '/images/common/nav-financial.jpg';
var forsale = new Image();
forsale.src = '/images/common/nav-for-sale.jpg';
var home = new Image();
home.src = '/images/common/nav-home.jpg';
var maltby = new Image();
maltby.src = '/images/common/nav-maltby.jpg';
var mexborough = new Image();
mexborough.src = '/images/common/nav-mexborough.jpg';
var rotherham = new Image();
rotherham.src = '/images/common/nav-rotherham.jpg';
var surveys = new Image();
surveys.src = '/images/common/nav-surveys.jpg';
var torent = new Image();
torent.src = '/images/common/nav-to-rent.jpg';
var agricultural = new Image();
agricultural.src = '/images/common/nav-agricultural.jpg';
var auctions = new Image();
auctions.src = '/images/common/nav-auctions.jpg';
var commercial = new Image();
commercial.src = '/images/common/nav-auctions.jpg';
var farmAndCountry = new Image();
farmAndCountry.src = '/images/common/nav-farm-and-country.jpg';
var landAndNewHomes = new Image();
landAndNewHomes.src = '/images/common/nav-land-and-new-homes.jpg';
var prestigiousHomes = new Image();
prestigiousHomes.src = '/images/common/nav-prestigious-homes.jpg';
var post = new Image();
post.src = '/images/post/post.jpg';
var postAgricultural = new Image();
postAgricultural.src = '/images/post/agricultural.jpg';
var postCommercial = new Image();
postCommercial.src = '/images/post/commercial.jpg';
var postContactUs = new Image();
postContactUs.src = '/images/post/contact-us.jpg';
var postFinancial = new Image();
postFinancial.src = '/images/post/finance.jpg';
var postLandAndNewHomes = new Image();
postLandAndNewHomes.src = '/images/post/land-and-new-homes.jpg'
var postLettings = new Image();
postLettings.src = '/images/post/lettings.jpg';
var postResidential = new Image();
postResidential.src = '/images/post/residential.jpg';
var postSurveys = new Image();
postSurveys.src = '/images/post/surveys.jpg';


// Toggle searches
function toggle(strId) {
    var container = document.getElementById('search');
    var departments = container.getElementsByTagName('div');
    for (i = 0; i < departments.length; i++) {
        if (departments[i].className == 'departmentBox') {
            var anchor = departments[i].getElementsByTagName('a');
            var heading = departments[i].getElementsByTagName('h3');
            var search = departments[i].getElementsByTagName('fieldset');
            search[0].style.display = 'none';
            anchor[0].style.color = '#1A5491';
            heading[0].style.backgroundPosition = 'left top';
        }
    }
    var dept = document.getElementById(strId);
    var anchor = dept.getElementsByTagName('a');
    var heading = dept.getElementsByTagName('h3');
    var search = dept.getElementsByTagName('fieldset');
    anchor[0].style.color = '#FFFFFF';
    heading[0].style.backgroundPosition = 'left center';
    search[0].style.display = 'block';
}

// Initialise searches
function searchInit() {
    var container = document.getElementById('search');
    var departments = container.getElementsByTagName('div');
    for (i = 0; i < departments.length; i++) {
        if (departments[i].className == 'departmentBox') {
            var anchor = departments[i].getElementsByTagName('a');
            var heading = departments[i].getElementsByTagName('h3');
            var search = departments[i].getElementsByTagName('fieldset');
            search[0].style.display = 'none';
            anchor[0].onclick = function() {
                toggle(this.parentNode.parentNode.getAttribute('id'));
            }
            heading[0].onmouseover = function() {
                var test = this.parentNode.getElementsByTagName('fieldset');
                if (test[0].style.display != 'block') {
                    this.style.backgroundPosition = 'left bottom';
                }
            }
            heading[0].onmouseout = function() {
                var test = this.parentNode.getElementsByTagName('fieldset');
                if (test[0].style.display != 'block') {
                    this.style.backgroundPosition = 'left top';
                }
            }
        }
    }
}

// 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 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 initSemlyenHover() {
    if (objExists('navigation') && objExists('navigationBottom')) {
        var nav = new Array();
        nav[0] = 'navigation';
        nav[1] = 'navigationBottom';
        for (k = 0; k < nav.length; k++) {
            var i = 0;
            var j = 0;
            var ulMenu = document.getElementById(nav[k]);
            var liSubs = ulMenu.childNodes;
            for (i = 0; i < liSubs.length; i++) {
                if (liSubs[i].nodeName == 'LI') {
                    for (j = 0; j < liSubs[i].childNodes.length; j++) {
                        if (liSubs[i].childNodes[j].nodeName == 'UL') {
                            semlyenHoverOut(liSubs[i].childNodes[j]);
                            liSubs[i].onmouseover = ( function(n,m){ return function() {semlyenHoverOver(n,m);} } )(liSubs[i].childNodes[j])
                            liSubs[i].onmouseout = ( function(n,m){ return function() {semlyenHoverOut(n,m);} } )(liSubs[i].childNodes[j])
                        }
                    }
                }
            }
        }
    }
}

function semlyenHoverOver(ul) {
    ul.style.display = 'block';
}
function semlyenHoverOut(ul) {
    ul.style.display = 'none';
}

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

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

    selectEmpty(select);

    if (minmax == 'max') {
        values[0] = 9999;
        textValues[0] = 'Maximum rent';
    } else {
        values[0] = 0;
        textValues[0] = 'Minimum 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 doCLettings(select, minmax, selects) {
    var values = new Array();
    var textValues = new Array();

    selectEmpty(select);

    if (minmax == 'max') {
        values[0] = 9999999;
        textValues[0] = 'Maximum rent';
    } else {
        values[0] = 0;
        textValues[0] = 'Minimum rent';
    }
    values[1] = 1000;
    textValues[1] = '\u00a31,000 pa';
    values[2] = 2000;
    textValues[2] = '\u00a32,000 pa';
    values[3] = 3000;
    textValues[3] = '\u00a33,000 pa';
    values[4] = 4000;
    textValues[4] = '\u00a34,000 pa';
    values[5] = 5000;
    textValues[5] = '\u00a35,000 pa';
    values[6] = 6000;
    textValues[6] = '\u00a36,000 pa';
    values[7] = 7000;
    textValues[7] = '\u00a37,000 pa';
    values[8] = 8000;
    textValues[8] = '\u00a38,000 pa';
    values[9] = 9000;
    textValues[9] = '\u00a39,000 pa';
    values[10] = 10000;
    textValues[10] = '\u00a310,000 pa';
    values[11] = 12500;
    textValues[11] = '\u00a312,500 pa';
    values[12] = 15000;
    textValues[12] = '\u00a315,000 pa';
    values[13] = 20000;
    textValues[13] = '\u00a320,000 pa';
    values[14] = 24000;
    textValues[14] = '\u00a324,000 pa';
    values[15] = 30000;
    textValues[15] = '\u00a330,000 pa';
    values[16] = 50000;
    textValues[16] = '\u00a350,000 pa';
    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();

    selectEmpty(select);

    if (minmax == 'max') {
        values[0] = 9999999;
        textValues[0] = 'Maximum price';
    } else {
        values[0] = 0;
        textValues[0] = 'Minimum 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);
        }
    }
}

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;
}

var subDoncaster = {
    'DNR': 'All sub-areas',
    'ALS': 'Adwick le Street',
    'ARK': 'Arksey',
    'ARM': 'Armthorpe',
    'ASK': 'Askern',
    'AUC': 'Auckley',
    'AUS': 'Austerfield',
    'BAL': 'Balby',
    'BBD': 'Barnby Dun',
    'BAW': 'Bawtry',
    'BLV': 'Belle Vue',
    'BTP': 'Bennetthorpe',
    'BNT': 'Bently',
    'BSC': 'Bessacarr',
    'BCT': 'Bircotes',
    'BLX': 'Blaxton',
    'BRN': 'Branton',
    'BRD': 'Brodsworth',
    'BGW': 'Burghwallis',
    'CDB': 'Cadeby',
    'CAM': 'Campsall',
    'CNT': 'Cantley',
    'CFT': 'Carcroft',
    'CLN': 'Clay Lane',
    'CON': 'Conisbrough',
    'CNB': 'Conisbrough',
    'CUS': 'Cusworth',
    'DNC': 'Dunscroft',
    'DNV': 'Dunsville',
    'EDE': 'Edenthorpe',
    'EDG': 'Edlington',
    'EPW': 'Epworth ',
    'FIN': 'Finningley',
    'HRL': 'Harlington',
    'HRW': 'Harworth',
    'HAT': 'Hatfield',
    'HWH': 'Hatfield Woodhouse',
    'HAX': 'Haxey',
    'HES': 'Hesley',
    'HEX': 'Hexthorpe',
    'HIC': 'Hickleton',
    'HML': 'High Melton',
    'HFS': 'Highfields',
    'HPG': 'Hooton Pagnell',
    'HPK': 'Hyde Park',
    'INT': 'Intake',
    'KIR': 'Kirk Sandall',
    'LAK': 'Lakeside',
    'LOV': 'Loversall',
    'MAR': 'Marr',
    'MIS': 'Misson',
    'MRD': 'Moorends',
    'MOS': 'Moss',
    'NE': 'North Elmsall',
    'NOR': 'Norton',
    'PKB': 'Pickburn',
    'ROS': 'Rossington',
    'OLE': 'Old Edlington',
    'ORS': 'Old Rossington',
    'SCW': 'Scawsby',
    'SCT': 'Scawthorpe',
    'SKB': 'Skelbrooke',
    'SKL': 'Skellow',
    'SPR': 'Sprotbrough',
    'STF': 'Stainforth',
    'STI': 'Stainton',
    'STY': 'Styrrup',
    'SNF': 'Sunnyfields',
    'SUT': 'Sutton',
    'SYK': 'Skyehouse',
    'FST': 'Fosterhouses',
    'TIC': 'Tickhill',
    'THN': 'Thorne',
    'TIB': 'Thorpe in Balne',
    'TSL': 'Thorpe Salvin',
    'TOL': 'Toll Bar',
    'TOW': 'Town Moor',
    'WAD': 'Wadworth',
    'WMW': 'Warmsworth',
    'WPB': 'Woodfield Plantation',
    'FSH': 'Fishlake',
    'WHT': 'Wheatley',
    'WHL': 'Wheatley Hills',
    'WOO': 'Woodlands'
}

var subBarnsley = {
    'BNY': 'All sub-areas',
    'ARD': 'Ardsley',
    'ANS': 'Athersley Noth South',
    'BGR': 'Barugh Greem',
    'BIL': 'Billingley',
    'BIR': 'Birdwell',
    'BLK': 'Blacker Hill',
    'BRA': 'Brampton Bierlow',
    'BRI': 'Brierley',
    'CAR': 'Carlton',
    'CAW': 'Cawthorne',
    'CUD': 'Cudworth',
    'CCL': 'Cundy Cross Lundwood',
    'DAR': 'Darfield',
    'DTN': 'Darton',
    'DOD': 'Doworth',
    'ELS': 'Elsecar',
    'GAW': 'Gawber',
    'GLD': 'Goldthorpe',
    'GRE': 'Great Houghton',
    'GRI': 'Grimethorpe',
    'HAI': 'Haigh',
    'HEM': 'Hemmingfield',
    'HIG': 'Higham',
    'HYM': 'Holye Mill',
    'HOO': 'Hood Green',
    'HOY': 'Hoyland Hoyland Common',
    'HSW': 'Hoylandwaine',
    'ING': 'Ingbirchworth',
    'JUM': 'Jump',
    'KEN': 'Kendray',
    'KEX': 'Kexbrough',
    'KIN': 'Kingston',
    'LIT': 'Little Houghton',
    'LVY': 'Low Valley',
    'MAP': 'Mapplewell',
    'MIL': 'Millhouse Green',
    'MON': 'Monk Bretton',
    'NEW': 'New Lodge',
    'NOT': 'Notton',
    'OXS': 'Oxspring',
    'PEN': 'Penistone',
    'PIL': 'Pilley',
    'PLA': 'Platts Common',
    'POG': 'Pogmoor',
    'RED': 'Redbrook',
    'ROY': 'Royston',
    'SHA': 'Shafton',
    'SIL': 'Silkstone Silkstone Common',
    'SMI': 'Smithies',
    'SOU': 'South Hiendley',
    'STA': 'Stainborough',
    'SCR': 'Staincross',
    'SFT': 'Stairfoot',
    'STH': 'Swaithe',
    'TAN': 'Tankersley',
    'THU': 'Thurgoland',
    'TST': 'Thurlstone',
    'TSC': 'Thurnscoe',
    'WAR': 'Ward Green',
    'WM' : 'West Melton',
    'WCM': 'Worsbrough Common',
    'WDL': 'Worsbrough Dale',
    'WBR': 'Worsbrough Bridge',
    'WVL': 'Worsbrough Village',
    'WOR': 'Wortley',
    'WOM': 'Wombwell',
    'WOS': 'Worsbrough',
    'WLY': 'Woolley'
}

var subRotherham = {
    'RTM': 'All sub-areas',
    'AN': 'Anston',
    'AS': 'Aston',
    'AG': 'Aughton',
    'BL': 'Blackburn',
    'BR': 'Braithwell',
    'BM': 'Bramley',
    'BB': 'Brampton Bierlow',
    'BL': 'Brampton-en-le-Morthern',
    'BK': 'Brecks',
    'BN': 'Brinsworth',
    'BK': 'Brookhouse',
    'BO': 'Broom',
    'CA': 'Canklow',
    'CR': 'Carr',
    'CT': 'Catcliffe',
    'CL': 'Clifton',
    'CV': 'Clifton Village',
    'DA': 'Dalton',
    'DP': 'Dalton Parva',
    'DN': 'Dinnington',
    'ED': 'East Dene',
    'EH': 'East Herringthorpe',
    'EA': 'Eastwood',
    'FI': 'Firbeck',
    'FL': 'Flanderwell',
    'GR': 'Grange',
    'GS': 'Greasbrough',
    'HL': 'Hellaby',
    'HE': 'Herringthorpe',
    'HM': 'Holmes',
    'HT': 'Hooton Levitt',
    'HR': 'Hooton Roberts',
    'KL': 'Kilnhurst',
    'KI': 'Kimberworth',
    'KV': 'Kiveton',
    'LA': 'Laughton',
    'LE': 'Letwell',
    'LI': 'Listerdale',
    'MA': 'Maltby',
    'MS': 'Masbrough',
    'MD': 'Meadowbank',
    'MX': 'Mexborough',
    'MI': 'Micklebring',
    'MO': 'Moorgate',
    'MT': 'Morthern',
    'MU': 'Munsbrough',
    'NH': 'Nether Haugh',
    'OR': 'Old Ravenfield',
    'OL': 'Oldcotes',
    'OU': 'Oughtibridge',
    'PA': 'Parkgate',
    'RA': 'Ravenfield',
    'RW': 'Rawmarsh',
    'RP': 'Richmond Park',
    'RK': 'Rockingham',
    'RO': 'Rotherham',
    'SC': 'Scholes',
    'SH': 'Sheffield',
    'SL': 'Slade Hooton',
    'SP': 'Spinneyfield',
    'ST': 'Stag',
    'SN': 'Stainton',
    'SU': 'Sunnyside',
    'SW': 'Swallownest',
    'TO': 'Thornhill',
    'TH': 'Thorpe Hesley',
    'TS': 'Thorpe Salvin',
    'TP': 'Throapham',
    'TY': 'Thrybergh',
    'TC': 'Thurcroft',
    'TY': 'Tinsley',
    'TO': 'Todwick',
    'TT': 'Treeton',
    'UL': 'Ulley',
    'UH': 'Upper Haugh',
    'WA': 'Wales',
    'WD': 'Wath Upon Dearne',
    'WE': 'Wentworth',
    'WM': 'West Melton',
    'WH': 'Whiston',
    'WI': 'Wickersley',
    'WB': 'Wincobank',
    'WG': 'Wingfield',
    'WK': 'Worksop',
    'WO': 'Woodlaithes'
}

var subMexborough = {
    'MEX': 'All sub-areas',
    'AWD': 'Adwick upon Dearne',
    'BNB': 'Barnburgh',
    'BMT': 'Brampton',
    'BBW': 'Brampton Bierlow',
    'BOD': 'Bolton-on-Dearne',
    'CBO': 'Conisbrough',
    'DVA': 'Dearne Valley',
    'DBM': 'Denaby Main',
    'HGT': 'Harlington',
    'KLH': 'Kilnhurst',
    'GDT': 'Goldthorpe',
    'KLH': 'Kilnhurst',
    'ODB': 'Old Denaby',
    'SWN': 'Swinton',
    'TUR': 'Thurnscoe',
    'WUD': 'Wath upon Dearne',
    'WMT': 'West Melton'
}

var subDinnington = {
    'DIN': 'All sub-areas',
    'AT': 'Aston',
    'BP': 'Brampton-en-le-Morthern',
    'DT': 'Dinnington',
    'WD': 'Woodhouse',
    'WL': 'Wales',
    'KM': 'Kilamarsh',
    'LT': 'Laughton-en-le-Morthern',
    'GW': 'Guildingwells',
    'TF': 'Thurcroft',
    'LC': 'Laughton Common',
    'TW': 'Todwick',
    'SA': 'Swallownest',
    'WP': 'Worksop',
    'KP': 'Kiveton Park',
    'NA': 'North Anston',
    'SA': 'South Anston'
}

var subMaltby = {
    'MAL': 'All Maltby'
}

$(document).ready(function() {
    $('<select name="area[]" class="sub"><option>Select an area</option></select>').insertAfter($('.departmentBox .area'));
    $('.sub').attr('disabled', 'true');
    $('.departmentBox').each(function(){    
        // Update event
        $(this).find('.area').bind('change', function() {
            var i;
            switch($(this).val()) {
            case 'BNY':
                $(this).parent().children('.sub').attr('name', 'area[]');
                $(this).attr('name', 'oarea');
                $(this).parent().children('.sub').children('option').remove();
                for (i in subBarnsley) {
                    $('<option value="' + i + '">' + subBarnsley[i] + '</option>').appendTo($(this).parent().children('.sub'));
                    $(this).parent().children('.sub').attr('disabled', false);
                }
                break;
            case 'DNR':
                $(this).parent().children('.sub').attr('name', 'area[]');
                $(this).attr('name', 'oarea');
                $(this).parent().children('.sub').children('option').remove();
                for (i in subDoncaster) {
                    $('<option value="' + i + '">' + subDoncaster[i] + '</option>').appendTo($(this).parent().children('.sub'));
                    $(this).parent().children('.sub').attr('disabled', false);
                }
                break;
            case 'MAL':
                $(this).parent().children('.sub').attr('name', 'area[]');
                $(this).attr('name', 'oarea');
                $(this).parent().children('.sub').children('option').remove();
                for (i in subMaltby) {
                    $('<option value="' + i + '">' + subMaltby[i] + '</option>').appendTo($(this).parent().children('.sub'));
                    $(this).parent().children('.sub').attr('disabled', false);
                }
                break;
            case 'MEX':
                $(this).parent().children('.sub').attr('name', 'area[]');
                $(this).attr('name', 'oarea');
                $(this).parent().children('.sub').children('option').remove();
                for (i in subMexborough) {
                    $('<option value="' + i + '">' + subMexborough[i] + '</option>').appendTo($(this).parent().children('.sub'));
                    $(this).parent().children('.sub').attr('disabled', false);
                }
                break;
            case 'RTM':
                $(this).parent().children('.sub').attr('name', 'area[]');
                $(this).attr('name', 'oarea');
                $(this).parent().children('.sub').children('option').remove();
                for (i in subRotherham) {
                    $('<option value="' + i + '">' + subRotherham[i] + '</option>').appendTo($(this).parent().children('.sub'));
                    $(this).parent().children('.sub').attr('disabled', false);
                }
                break;
            case 'DIN':
                $(this).parent().children('.sub').attr('name', 'area[]');
                $(this).attr('name', 'oarea');
                $(this).parent().children('.sub').children('option').remove();
                for (i in subDinnington) {
                    $('<option value="' + i + '">' + subDinnington[i] + '</option>').appendTo($(this).parent().children('.sub'));
                    $(this).parent().children('.sub').attr('disabled', false);
                }
                break;
            default:
                $(this).parent().children('.sub').attr('name', 'oarea');
                $(this).attr('name', 'area[]');
                $('<option value="">Select an area</option>').appendTo($(this).parent().children('.sub'));
                $(this).parent().children('.sub').attr('disabled', true);
            }
        });
    });
                
    $('.departmentBox .area').each(function() {
        $(this).trigger('change');
    });
    $('.departmentBox .sub option').each(function() {
        //if ($(this).val() == $(this).parent().parent().children('.prev').val()) {
            //$(this).attr('selected', true);
        //}
        var that = this;
        $(this).parent().parent().children('.prev').each(function() {
            if ($(this).val() == $(that).val()) {
                $(that).attr('selected', true);
            }
        });
    });
});

addLoadEvent(initSemlyenHover);
addLoadEvent(searchInit);
addLoadEvent(imageOver);
addLoadEvent(buttonOver);
