$(document).ready(function(){
    //home slide show.
    $('#slider-home').aviaSlider({betweenBlockDelay:2000});
    
    $("ul.sf-menu").superfish().find('ul').bgIframe({opacity:false});

    //handel regions cities and suburbs selection
    $('#membership_form_1_city_id').attr('disabled','disabled');
    $('#membership_form_1_region_id').change(function(){
        if(this.value > 0)
        {
            $('#membership_form_1_city_id').attr('disabled','disabled');
            $('#membership_form_1_city_id').html('<option value="">Loading Cities.........</option>');

            $('#membership_form_1_suburb_id').attr('disabled','disabled');
            $('#membership_form_1_suburb_id').html('<option value="">-- Please select a suburb --</option>');

            var url = $(this).parents('form').attr('action').toString();
             $('#membership_form_1_city_id').load(
                    url.substr(0, url.lastIndexOf('/')) + '/get_city_list/' + this.value,
                    function() {
                        $('#membership_form_1_city_id').attr('disabled','');
                    }
             );
        }
        else
        {
           $('#membership_form_1_city_id').attr('disabled','disabled');
           $('#membership_form_1_city_id').html('<option value="">-- Please select a city --</option>');
           $('#membership_form_1_suburb_id').attr('disabled','disabled');
           $('#membership_form_1_suburb_id').html('<option value="">-- Please select a suburb --</option>');
        }
    });

    $('#membership_form_1_city_id').attr('disabled','disabled');
    $('#membership_form_1_city_id').change(function(){
        if(this.value>0){
            $('#membership_form_1_suburb_id').html('<option value="">Loading Suburbs.........</option>');
            var url = $(this).parents('form').attr('action').toString();
             $('#membership_form_1_suburb_id').load(
                    url.substr(0, url.lastIndexOf('/')) + '/get_suburb_list/' + this.value,
                    function() {$('#membership_form_1_suburb_id').attr('disabled','');}
             );
        }else{
             $('#membership_form_1_suburb_id').attr('disabled','disabled');
             $('#membership_form_1_suburb_id').html('<option value="">-- Please select a suburb --</option>');
        }
    });

    //reset select list when refresh
    $('#membership_form_1_region_id').find('option:first').attr('selected', 'selected').parent().trigger('change');

    $('#membership_form_2_city_id').attr('disabled','disabled');
    $('#membership_form_2_region_id').change(function(){
        if(this.value > 0)
        {
            $('#membership_form_2_city_id').attr('disabled','disabled');
            $('#membership_form_2_city_id').html('<option value="">Loading Cities.........</option>');

            $('#membership_form_2_suburb_id').attr('disabled','disabled');
            $('#membership_form_2_suburb_id').html('<option value="">-- Please select a suburb --</option>');

            var url = $(this).parents('form').attr('action').toString();
             $('#membership_form_2_city_id').load(
                    url.substr(0, url.lastIndexOf('/')) + '/get_city_list/' + this.value,
                    function() {
                        $('#membership_form_2_city_id').attr('disabled','');
                    }
             );
        }
        else
        {
           $('#membership_form_2_city_id').attr('disabled','disabled');
           $('#membership_form_2_city_id').html('<option value="">-- Please select a city --</option>');
           $('#membership_form_2_suburb_id').attr('disabled','disabled');
           $('#membership_form_2_suburb_id').html('<option value="">-- Please select a suburb --</option>');
        }
    });

    $('#membership_form_2_city_id').attr('disabled','disabled');
    $('#membership_form_2_city_id').change(function(){
        if(this.value>0){
            $('#membership_form_2_suburb_id').html('<option value="">Loading Suburbs.........</option>');
            var url = $(this).parents('form').attr('action').toString();
             $('#membership_form_2_suburb_id').load(
                    url.substr(0, url.lastIndexOf('/')) + '/get_suburb_list/' + this.value,
                    function() {$('#membership_form_2_suburb_id').attr('disabled','');}
             );
        }else{
             $('#membership_form_2_suburb_id').attr('disabled','disabled');
             $('#membership_form_2_suburb_id').html('<option value="">-- Please select a suburb --</option>');
        }
    });

    //reset select list when refresh
    $('#membership_form_2_region_id').find('option:first').attr('selected', 'selected').parent().trigger('change');

    //reset select list when refresh
    $('#search_form_region_id').find('option:first').attr('selected', 'selected').parent().trigger('change');

    $('#search_form_city_id').attr('disabled','disabled');
    $('#search_form_region_id').change(function(){
        if(this.value > 0)
        {
            $('#search_form_city_id').attr('disabled','disabled');
            $('#search_form_city_id').html('<option value="">loading cities...</option>');

            $('#search_form_suburb_id').attr('disabled','disabled');
            $('#search_form_suburb_id').html('<option value="">-- Please select a suburb --</option>');

            var url = $(this).parents('form').attr('action').toString();
             $('#search_form_city_id').load(
                    url.substr(0, url.lastIndexOf('/')) + '/get_city_list/' + this.value,
                    function() {
                        $('#search_form_city_id').attr('disabled','');
                    }
             );
        }
        else
        {
           $('#search_form_city_id').attr('disabled','disabled');
           $('#search_form_city_id').html('<option value="">-- City --</option>');
           $('#search_form_suburb_id').attr('disabled','disabled');
           $('#search_form_suburb_id').html('<option value="">-- suburb --</option>');
        }
    });

    $('#search_form_city_id').attr('disabled','disabled');
    $('#search_form_city_id').change(function(){
        if(this.value>0){
            $('#search_form_suburb_id').html('<option value="">loading suburbs...</option>');
            var url = $(this).parents('form').attr('action').toString();
             $('#search_form_suburb_id').load(
                    url.substr(0, url.lastIndexOf('/')) + '/get_suburb_list/' + this.value,
                    function() {$('#search_form_suburb_id').attr('disabled','');}
             );
        }else{
             $('#search_form_suburb_id').attr('disabled','disabled');
             $('#search_form_suburb_id').html('<option value="">-- Suburb --</option>');
        }
    });

    //reset select list when refresh
    $('#search_form_region_id').find('option:first').attr('selected', 'selected').parent().trigger('change');

    //search form keywords
    $('#search_form_keywords,#search_form_therapy_id,#search_form_region_id').click(function(){
        if($('#search_form_keywords').attr('value')=='Enter keywords...'){
            $('#search_form_keywords').attr('value','');
        }
    });


    //The contact edit form
    $('#contact_details_form_region_id').change(function(){
        if(this.value > 0)
        {
            $('#contact_details_form_city_id').attr('disabled','disabled');
            $('#contact_details_form_city_id').html('<option value="">Loading Cities.........</option>');

            $('#contact_details_form_suburb_id').attr('disabled','disabled');
            $('#contact_details_form_suburb_id').html('<option value="">-- Please select a suburb --</option>');

            var url = $(this).parents('form').attr('action').toString();
             $('#contact_details_form_city_id').load(
                    url.substr(0, url.lastIndexOf('/')) + '/get_city_list/' + this.value,
                    function() {
                        $('#contact_details_form_city_id').attr('disabled','');
                    }
             );
        }
        else
        {
           $('#contact_details_form_city_id').attr('disabled','disabled');
           $('#contact_details_form_city_id').html('<option value="">-- Please select a city --</option>');
           $('#contact_details_form_suburb_id').attr('disabled','disabled');
           $('#contact_details_form_suburb_id').html('<option value="">-- Please select a suburb --</option>');
        }
    });

    //$('#contact_details_form_city_id').attr('disabled','disabled');
    $('#contact_details_form_city_id').change(function(){
        if(this.value>0){
            $('#contact_details_form_suburb_id').html('<option value="">Loading Suburbs.........</option>');
            var url = $(this).parents('form').attr('action').toString();
             $('#contact_details_form_suburb_id').load(
                    url.substr(0, url.lastIndexOf('/')) + '/get_suburb_list/' + this.value,
                    function() {$('#contact_details_form_suburb_id').attr('disabled','');}
             );
        }else{
             $('#contact_details_form_suburb_id').attr('disabled','disabled');
             $('#contact_details_form_suburb_id').html('<option value="">-- Please select a suburb --</option>');
        }
    });

    //The contact edit form for professional members
    $('#profile_form_1_region_id').change(function(){
        if(this.value > 0)
        {
            $('#profile_form_1_city_id').attr('disabled','disabled');
            $('#profile_form_1_city_id').html('<option value="">Loading Cities.........</option>');

            $('#profile_form_1_suburb_id').attr('disabled','disabled');
            $('#profile_form_1_suburb_id').html('<option value="">-- Please select a suburb --</option>');

            var url = $(this).parents('form').attr('action').toString();
            if(url.lastIndexOf('frontend_dev.php')){
                url = 'http://wellness/frontend_dev.php';
            }else if(url.lastIndexOf('backend_dev.php')){
                url = 'http://wellness/backend_dev.php';
            }else{
                url = 'http://www.thewellnessdirectory.co.nz';
            }
            $('#profile_form_1_city_id').load(
                    url + '/get_city_list/' + this.value,
                    function() {
                        $('#profile_form_1_city_id').attr('disabled','');
                    }
             );
        }
        else
        {
           $('#profile_form_1_city_id').attr('disabled','disabled');
           $('#profile_form_1_city_id').html('<option value="">-- Please select a city --</option>');
           $('#profile_form_1_suburb_id').attr('disabled','disabled');
           $('#profile_form_1_suburb_id').html('<option value="">-- Please select a suburb --</option>');
        }
    });

    $('#profile_form_1_city_id').change(function(){
        if(this.value>0){
            $('#profile_form_1_suburb_id').html('<option value="">Loading Suburbs.........</option>');
            var url = $(this).parents('form').attr('action').toString();
            if(url.lastIndexOf('frontend_dev.php')){
                url = 'http://wellness/frontend_dev.php';
            }else if(url.lastIndexOf('backend_dev.php')){
                url = 'http://wellness/backend_dev.php';
            }else{
                url = 'http://www.thewellnessdirectory.co.nz';
            }

             $('#profile_form_1_suburb_id').load(
                    url + '/get_suburb_list/' + this.value,
                    function() {$('#profile_form_1_suburb_id').attr('disabled','');}
             );
        }else{
             $('#profile_form_1_suburb_id').attr('disabled','disabled');
             $('#profile_form_1_suburb_id').html('<option value="">-- Please select a suburb --</option>');
        }
    });


    //top banner cycling
    $('#top-banner-ads').css('display','block').cycle({
            random: 1,
            timeout: 10000
        });
        
    $('#ask-the-expert-slides').css('display','block').cycle({
            random: 0,
            timeout: 3000
        });

    //tabs
    $('#tabs').smartTab({autoProgress: false,stopOnFocus:true,transitionEffect:'none'});
});


/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* Modified to support Opera */
function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
}
else if(document.all){ // ie
	window.external.AddFavorite(url, title);
}else{
    alert('Please press Ctrl(Command) + D to bookmark this page!');
}
}
