function onLoad(){
    $(document).ready(function() {
        $('#restab').click(function(){handleSearchBarResponse('residential');});
        $('#bustab').click(function(){handleSearchBarResponse('business');});
        $('#commtab').click(function(){handleSearchBarResponse('community');});
        $('#whatDDArrow').click(function(){
            whatArrow();
        });
        $('#whereDDArrow').click(function(){
            whereArrow();
        });
        $(".directoryselect").each(function(){
            $(this).click(function(){
                var str = $(this).html();
                $('#where').addClass('inputActive');
                $('#where').val(str);
                $('#whereDD').toggle('blind', {}, 100);
            });
        });
        $('#searchButton').click(function(){
            if ($('#what').val() == ''
                || $('#where').val() == ''
            ) {
                $('#what').removeClass('inputActive');
            }
            dogo();
        });
        $('#what').click(function(){
            setTimeout('document.vform.what.select()',10);
        });
        $('#where').click(function(){
            setTimeout('document.vform.where.select()',10);
        });
    });
}

function onLoadHome(){
    var type = 'bus';
    $('#restab').click(function(){
        homeHandleSearchBarResponse('residential'); 
        type='res';
    });
    $('#bustab').click(function(){
        homeHandleSearchBarResponse('business');
        type='bus';
    });
    $('#commtab').click(function(){
        homeHandleSearchBarResponse('community');
        type='comm';
    });
    $('#whatDDArrow').click(function(){
        whatArrow(true);
    });
    $('#whereDDArrow').click(function(){
        whereArrow(true);
    });
    $(".wdirectoryselect").each(function(){
        $(this).click(function(){
            var str = $(this).html();
            $('#where').addClass('inputActive');
            $('#where').val(str);
            $('#whereDD').toggle('blind', {}, 100);
        });
    });
    $('#what').blur(function(){
        addWhat(this, type);
    });
    $('#where').blur(function(){
        return addWhere(this);
    });
    $('#what').keydown(function(event) {
        if (event.keyCode == '27'
            && (
                $('#what').val() == 'Business Name or Category'
                || $('#what').val() == 'Surname or Business Name'
                || $('#what').val() == 'Business Name or Department'
            )
        ) {
            $('#what').removeClass('inputActive');
        }
    });
    $('#setLocation').click(function(){
        var options = {};
        $('#currentLocDD').toggle('blind', options, 200);
        return false;
    });
    $('#selectTopCat').click(function(){
        var options = {};
        $('#topCatsDD').toggle('blind', options, 200);
        return false;
    });
    $('#selectPopSearch').click(function(){
        var options = {};
        $('#popSearchesDD').toggle('blind', options, 200);
        return false;
    });
    $('#searchButton').click(function(){
        dogo();
    });
    $('.home-facebook, .home-twitter').each(function() {
        $(this).click(function() {
            window.open($(this).attr('href'), $(this).attr('title'), 'width=600,height=400');
            return false;
        });
    });
}

function handleSearchBarResponse(search_type)
{
    if (search_type == 'residential') {
        $('#searchbox').css({'background':'url(/images/res_search_bar.png) no-repeat scroll 0 0 transparent'});
        $('#whatLabel').html('Who');
        $('#whereLabel').html('Where');
        $('#whatSubLabel').html('Surname or Business Name');
        //$('#searchForm').get(0).setAttribute('action',"/residential/");
        $('#whatLabel').removeClass('commWhite');
        $('#whereLabel').removeClass('commWhite');
        $('#whatSubLabel').removeClass('commWhite');
        $('#whereSubLabel').removeClass('commWhite');
        $('#searchButton').unbind('click');
        $('#searchButton').click(function(){
            dogo();
        });
        sectionPath="/Residences"
    }
    if (search_type == 'business') {
        $('#searchbox').css({'background':'url(/images/bus_search_bar.png) no-repeat scroll 0 0 transparent'})
        $('#whatLabel').html('What');
        $('#whereLabel').html('Where');
        $('#whatSubLabel').html('Business Name or Category');
        //$('#searchForm').get(0).setAttribute('action',"/newsearch/");
        $('#whatLabel').removeClass('commWhite');
        $('#whereLabel').removeClass('commWhite');
        $('#whatSubLabel').removeClass('commWhite');
        $('#whereSubLabel').removeClass('commWhite');
        $('#searchButton').unbind('click');
        $('#searchButton').click(function(){
            dogo();
        });
        sectionPath = '';
    }
    if (search_type == 'community') {
        $('#searchbox').css({'background':'url(/images/comm_search_bar.png) no-repeat scroll 0 0 transparent'})
        $('#whatLabel').html('Coming');
        $('#whereLabel').html('Soon');
        $('#whatSubLabel').html('Business Name or Department');
        //$('#searchForm').get(0).setAttribute('action',"/community/");
        $('#whatLabel').addClass('commWhite');
        $('#whereLabel').addClass('commWhite');
        $('#whatSubLabel').addClass('commWhite');
        $('#whereSubLabel').addClass('commWhite');
        $('#searchButton').unbind('click');
        $('#searchButton').click(function(){
            return false;
        });
    }
}

function homeHandleSearchBarResponse(search_type)
{
    var isiPad = navigator.userAgent.match(/iPad/i) != null;

    if (search_type == 'residential') {
        if(isiPad){
            $('#header_main').css({'background':'url(/images/header/ipad/top_w.png) no-repeat scroll 0 0 transparent'});
            $('#bottom-main').css({'background':'url(/images/footer/ipad/bottom_w.png) no-repeat scroll 0 0 transparent'});
        } else {
            $('#header_main').css({'background':'url(/images/header/top_w.png) no-repeat scroll 0 0 transparent'});
            $('#bottom-main').css({'background':'url(/images/footer/bottom_w.png) no-repeat scroll 0 0 transparent'});
        }
        $('#headlogo').css({'background':'url(/images/header/ld_w.png) no-repeat scroll 0 0 transparent'});
        $('#top_menu').css({'background':'url(/images/header/under_w.png) no-repeat scroll 0 0 transparent'});
        $('.search_wrapper').css({'background':'url(/images/header/topline_wy.png) repeat-x scroll 0 bottom transparent'});
        $('#whatLabel').html('Who');
        $('#whereLabel').html('Where');
        $('#what').val('Surname or Business Name');
        //$('#searchForm').get(0).setAttribute('action',"/residential/");
        $('#whatLabel').removeClass('commWhite');
        $('#whereLabel').removeClass('commWhite');
        $('#top_menu .link').removeClass('commWhite');
        $('#top_menu li.divider').css({'color':'#00368B'});
        $('#searchButton').unbind('click');
        $('#searchButton').click(function(){
            dogo();
        });
        vwhat="Surname or Business Name";
        sectionPath="/Residences"
    }
    if (search_type == 'business') {
        if(isiPad){
            $('#header_main').css({'background':'url(/images/header/ipad/top_y.png) no-repeat scroll 0 0 transparent'});
            $('#bottom-main').css({'background':'url(/images/footer/ipad/bottom_y.png) no-repeat scroll 0 0 transparent'});
        } else {
            $('#header_main').css({'background':'url(/images/header/top_y.png) no-repeat scroll 0 0 transparent'});
            $('#bottom-main').css({'background':'url(/images/footer/bottom_y.png) no-repeat scroll 0 0 transparent'});
        }
        $('#headlogo').css({'background':'url(/images/header/ld_y.png) no-repeat scroll 0 0 transparent'});
        $('#top_menu').css({'background':'url(/images/header/under_y.png) no-repeat scroll 0 0 transparent'});
        $('.search_wrapper').css({'background':'url(/images/header/topline_wy.png) repeat-x scroll 0 bottom transparent'});
        $('#whatLabel').html('What');
        $('#whereLabel').html('Where');
        $('#what').val('Business Name or Category');
        //$('#searchForm').get(0).setAttribute('action',"/newsearch/");
        $('#whatLabel').removeClass('commWhite');
        $('#whereLabel').removeClass('commWhite');
        $('#top_menu .link').removeClass('commWhite');
        $('#top_menu li.divider').css({'color':'#00368B'});
        $('#searchButton').unbind('click');
        $('#searchButton').click(function(){
            dogo();
        });
        vwhat="Business Name or Category";
        sectionPath= '';
    }
    if (search_type == 'community') {
        if(isiPad){
            $('#header_main').css({'background':'url(/images/header/ipad/top_b.png) no-repeat scroll 0 0 transparent'});
            $('#bottom-main').css({'background':'url(/images/footer/ipad/bottom_b.png) no-repeat scroll 0 0 transparent'});
        } else {
            $('#header_main').css({'background':'url(/images/header/top_b.png) no-repeat scroll 0 0 transparent'});
            $('#bottom-main').css({'background':'url(/images/footer/bottom_b.png) no-repeat scroll 0 0 transparent'});
        }
        $('#headlogo').css({'background':'url(/images/header/ld_b.png) no-repeat scroll 0 0 transparent'});
        $('#top_menu').css({'background':'url(/images/header/under_b.png) no-repeat scroll 0 0 transparent'});
        $('.search_wrapper').css({'background':'url(/images/header/topline_b.png) repeat-x scroll 0 bottom transparent'});
        $('#whatLabel').html('Coming');
        $('#whereLabel').html('Soon');
        $('#what').val('Business Name or Department');
        //$('#searchForm').get(0).setAttribute('action',"/community/");
        $('#whatLabel').addClass('commWhite');
        $('#whereLabel').addClass('commWhite');
        $('#top_menu .link').addClass('commWhite');
        $('#top_menu li.divider').css({'color':'#FFFFFF'});
        $('#searchButton').unbind('click');
        $('#searchButton').click(function(){
            return false;
        });
        vwhat="Business Name or Department";
    }
}

function clearWhat(thisform){
    if(thisform.value == 'Business Name or Category' || thisform.value == 'Surname or Business Name'|| thisform.value == 'Business Name or Department'){
        thisform.value = '';
        $('#what').addClass('inputActive');
        return false;
    } else {
        return true;
    }
}
function addWhat(thisform, action){
    if(thisform.value == '' && action == 'bus'){
        $('#what').removeClass('inputActive');
        thisform.value = 'Business Name or Category';
        return false;
    } else if(thisform.value == '' && action == 'res') {
        $('#what').removeClass('inputActive');
        thisform.value = 'Surname or Business Name';
        return false;
    } else if(thisform.value == '' && action == 'comm') {
        $('#what').removeClass('inputActive');
        thisform.value = 'Business Name or Department';
        return false;
    }else {
        return true;
    }
}

function clearWhere(thisform){
    if(thisform.value == 'Suburb, City or Postcode'){
        thisform.value = '';
        $('#where').addClass('inputActive');
        return false;
    } else {
        return true;
    }
}
function addWhere(thisform){
    if(thisform.value == ''){
        $('#where').removeClass('inputActive');
        thisform.value = 'Suburb, City or Postcode';
        return false;
    } else {
        return true;
    }
}

function playmovie(url,e){
    $('#accordion a').each(function(){
       $(this).css({'font-weight':'normal'});
    });
    $.ajax({
       type: "POST",
       url: "/async/changevidsrc/",
       data: "url="+url,
       success: function(html){
         $("#vidbox").html(html);
       }
     });
     $(e).css({'font-weight':'bold'});
}

function setCookie(c_name,value,exdays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name) {
            return unescape(y);
        }
    }
    return false;
}

function deleteCookie(c_name) {
    if (getCookie(c_name)) {
        document.cookie = c_name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT;";
    } else {
        return false;
    }
}

function strstr(haystack, needle, bool) {
    // Finds first occurrence of a string within another
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/strstr    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strstr('Kevin van Zonneveld', 'van');
    // *     returns 1: 'van Zonneveld'    // *     example 2: strstr('Kevin van Zonneveld', 'van', true);
    // *     returns 2: 'Kevin '
    // *     example 3: strstr('name@example.com', '@');
    // *     returns 3: '@example.com'
    // *     example 4: strstr('name@example.com', '@', true);    // *     returns 4: 'name'
    var pos = 0;

    haystack += '';
    pos = haystack.indexOf(needle);if (pos == -1) {
        return false;
    } else {
        if (bool) {
            return haystack.substr(0, pos);} else {
            return haystack.slice(pos);
        }
    }
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = r instanceof Array,        sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

function array_key_exists (key, search) {
    // Checks if the given key or index exists in the array
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/array_key_exists
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Felix Geisendoerfer (http://www.debuggable.com/felix)
    // *     example 1: array_key_exists('kevin', {'kevin': 'van Zonneveld'});
    // *     returns 1: true
    // input sanitation
    if (!search || (search.constructor !== Array && search.constructor !== Object)) {
        return false;
    }

    return key in search;
}

function urldecode (str) {
    // Decodes URL-encoded string
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/urldecode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      bugfixed by: Rob
    // +      input by: e-mike
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // %        note 2: Please be aware that this function expects to decode from UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    return decodeURIComponent((str + '').replace(/\+/g, '%20'));
}

function whatArrow(home)
{
    if(whatSuggest.justHidden) return;
    var el=document.getElementById('what');
    
    home = (typeof home == 'undefined') ? false : home;
    
    if (home) {
        if ($('#what').hasClass('inputActive') === false) {
            whatSuggest.autosuggest([
            "Restaurants",
            "Auto Parts",
            "Hotels",
            "Dentists",
            "New Cars",
            "Electrical Contractors",
            "Plumbing Contractors",
            "Fishing Trips",
            "Building Contractors",
            "Used Cars"
            ],false);
        } else {
            whatSuggest.provider.requestSuggestions(whatSuggest,true);
        }
    } else {
        if (el.value=="") {
            whatSuggest.autosuggest([
            "Restaurants",
            "Auto Parts",
            "Hotels",
            "Dentists",
            "New Cars",
            "Electrical Contractors",
            "Plumbing Contractors",
            "Fishing Trips",
            "Building Contractors",
            "Used Cars"
            ],false);
        } else {
            whatSuggest.provider.requestSuggestions(whatSuggest,true);
        }
    }
    
    
    
    
    el.focus();
}
function whereArrow(home)
{
    var valueCheck;
    home = (typeof home == 'undefined') ? false : home;
    
    if (home) {
        valueCheck = 'Suburb, City or Postcode';
    } else {
        valueCheck = '';
    }
    
    if(whereSuggest.justHidden) return;
    var el=document.getElementById('where');
    if (el.value==valueCheck) {
        var options = {};
        $('#whereDD').toggle('blind', options, 200);
        return false;
    } else {
        whereSuggest.provider.requestSuggestions(whereSuggest,true);
    }
    el.focus();
}

function openWindow(e)
{
    window.open(e.attr('href'),'Map', 'width=615,height=600')
}
