$(document).ready(function(){
               
        
        stabs('ul.tabs','.search-form');
        stabs('ul.register','.steps');

        $('.add-business-block .step A').click(function(){
                var area = $(this).parent().find('.step-area');
                var step = $(this).parent();
                $(area).slideToggle(500,function(){
                        if ($(step).hasClass('drop-block')) {
                                $(step).removeClass('drop-block');
                                $(step).addClass('implicit-block');
                        }else {
                                $(step).addClass('drop-block');
                                $(step).removeClass('implicit-block');
                        }
                });
                
                return false;
        });
        
        
        $('.auto-hint[title]').each(function(i, el){
                if($(this).val() == ''){
                        $(this).val($(this).attr('title'));
                }
                $(el).focus(function(){
                        if ($(this).val() == $(this).attr('title')) {
                                $(this).val('').css({ color: '#4c4c4c' });
                        }               
                });
                $(el).blur(function(){
                        if ($(this).val() == '') {
                                $(this).val($(this).attr('title')).css({ color: '#959595'});
                        }
                });
        });
        
        $('.right-cont a.title').click(function(){             
            var $title = $(this);
            var $obj   = $(this).parent();
            var $popup = $('#' + $obj.attr('class'));

            var open = function() {
                $obj.css('position', 'relative');
                $popup.slideDown('normal', function() {
                  $title.addClass('open');
                });
            }
            
            var close = function() {
                $popup.slideUp('normal', function() {
                  $obj.css('position', 'static');
                  $title.removeClass('open');
                });
            };

            $popup.find('a.close').click(function(){
                close();
                return false;
            });

            if ($popup.is(':hidden')) open();
            else close();

            return false;
        });
        
        $('.number .help').hover(function(){
                $(this).find('.user-recom').show();
        },function(){
                $(this).find('.user-recom').hide();
        });
        
        $('ul.recom-tabs li a').click(function(){
                var showRecom = $(this).attr('href');
                $('ul.recom-tabs li').removeClass('active');
                $(this).parent().addClass('active');
                $('.recommendations-block').hide();
                $('.recommendations-block' + showRecom).show();
                //stabs('ul.register','.steps');
                return false;
        });
        
        
});

function stabs(ul,tabcontent){
        $(ul+' li a').click(function(){
                var showForm = $(this).attr('href');
                var showFormId = showForm.split("#");
                if (showFormId[1]) showFormId = showFormId[1];
                else showFormId = showFormId[0];
                
                var link = this;
                if ( $(tabcontent+' .tab-content:visible').length == 0 || $(tabcontent+' .tab-content:visible').attr('id') == showFormId || ($(ul+' li a').hasClass("disabled")) ) return false;
                $(tabcontent+' .tab-content:visible').fadeOut(100,function(){
                        $(ul+' li a').addClass("disabled");
                        $(ul+' li').removeClass('active');
                        $(link).parent().addClass('active');
                        $(showForm).fadeIn(600,function(){
                                $(ul+' li a').removeClass("disabled");
                        });
                });     
                return false;
        });
}

function show(close_id, className1, className2) {
        var close = document.getElementById(close_id);
        
        if (close.className == className1) {
                close.className = className2;
        }else { 
                close.className = className1;
        }
};

$(document).ready(function(){
        
        $('#menu A,.left-column UL A').click(function(){
                var anchors = $(this).attr('href').split('#');
                if (anchors.length == 3) { //Sub Drop
                        $('#'+anchors[1]).attr('class','step drop-block');
                        location.hash = anchors[2];
                }else if (anchors.length == 2) {
                        $('#'+anchors[1]).attr('class','step drop-block');
                }
        });
        
        $('.menu').hover(
                  function () {
                        $(this).css("z-index", 101);
                  },
                  function () {
                        $(this).css("z-index", 2);
                    }
        );
        $('#menu').hover(
                  function () {
                        $(this).css("z-index", 101);
                  },
                  function () {
                        $(this).css("z-index", 2);
                    }
        );
        $('#wrapper').hover(
                  function () {
                        $(this).css("z-index", 10);
                  },
                  function () {
                        $(this).css("z-index", 1);
                    }
        );
                $(".help").hover(function(e){
                if ( $(this).attr('title') == '' ) return false;
                var linkPos = $(this).offset();
                $('body').append('<div id="help-popup">'+$(this).attr('title')+'</div>');
                $(this).attr('title','');
                $('#help-popup').css({'background-color' : '#feffd4','border' : '1px solid #000000','font-size' : '10px','color' : '#000000','max-width' : '470px', 'padding' : '2px','text-align' : 'left'});
                if($.browser.msie && $.browser.version == '6.0'){
                        if ( parseInt($('#help-popup').width()) > parseInt($('#help-popup').css('max-width')) ) {
                                $('#help-popup').width( $('#help-popup').css('max-width') );
                        }
                }
                $('#help-popup').css({ 'position' : 'absolute','left' : e.pageX + 15 + 'px', 'top' : parseInt(e.pageY) + 10 + 'px' });
        }, function(){
                $(this).attr('title',$('#help-popup').text());
                $('#help-popup').remove();
        }).click(function() { return false; });


        /* Signin/My Account Popup */
        $('.sign-in .link').click(function(){
            var $popup = $(this).siblings('#popup');
            if ($popup.is(':hidden')) {
                $popup.slideDown('normal', function() {
                  $popup.find('a.close').click(function() {
                    $popup.slideUp('normal');
                    return false;
                  });
                });
            }
            else {
                $popup.slideUp('normal');
            }
            return false;
        });


});
        
function checkForm(object) {
    var fields; var message; var focus;
    var form_name = $(object).attr('name');
    if (form_name == 'refine-tag') {
      fields = new Array('catid', 'query2');
      message = 'Please select a category and enter your postcode';
      focus   = 'query2';
    }
    else if (form_name == 'refine-subcat') {
      fields = new Array('query2');
      message = 'Please enter your postcode';
      focus   = 'query2';
    }
    else if (form_name == 'refine-box') {
      fields = new Array('Suburb');
      message = 'Please enter your postcode';
      focus   = 'Suburb';
    }
    else if (form_name == 'sort-tag') {
      fields = new Array('Location');
      message = 'Please enter your postcode';
      focus   = 'Location';
    }
    else {
      var searchFlag = $('#business-name').is(':visible');
      fields  = searchFlag ? new Array('Title', 'query2') : new Array('query', 'query2');
      message = searchFlag ? "Please enter business name or location" : "Please enter a keyword or location";
      focus   = searchFlag ? 'Title' : 'query';
    }

    var num_null = 0;

    for (var i=0; i<fields.length; i++) {
      var input = $(object).find('[name="' + fields[i] + '"]');
      if (input.length == 0) continue;

      var val = input.val();
      val = val.replace(/^\s*/, '');
      val = val.replace(/\s*$/, '');
      if (val.search(/sydney\s*,\s*nsw\s*,\s*2000...|enter\s*keyword|enter\s*business\s*name|enter\s*your\s*|city\s*or\*suburb\*here\s*|refine\s*by\s*postcode\s*|select\s*a\s*category/i) == 0) input.val('');
      if (input.val().length == 0) num_null++;
    }

    if (form_name == 'refine-subcat') {
      if (num_null > 0) {
        alert(message);
        $(object).find('input[name="' + focus + '"]').focus();
        return false;
      }
    }
    else if (num_null == fields.length) {
      alert(message);
      $(object).find('input[name="' + focus + '"]').focus();
      return false;
    }

    return true;
}

function gmapShow(id, loc) {
    var map = new google.maps.Map2(document.getElementById(id), { backgroundFillStyle: '#e9ebd9' }); 
    var customUI = map.getDefaultUI();
    customUI.maptypes.hybrid = false;
    map.setUI(customUI);

    var geocoder = new GClientGeocoder();                                                                         
    var getLatLng = function(point) {                                                                             
        map.setCenter(point, 14, G_NORMAL_MAP);
        var marker = new GMarker(point, { clickable: loc.markerDrag ? true : false, draggable: loc.markerDrag ? true : false, dragCrossMove: "False", bouncy: "False", bounceGravity: 1 });
        GEvent.addListener(marker, "dragend", function() { 
            var lat = new Number(marker.getPoint().lat());                                                        
            var lng = new Number(marker.getPoint().lng());                                                        
            $('#Latitude').val(lat.toFixed(7));
            $('#Longitude').val(lng.toFixed(7));
            $('#geoValidated').val(1);
        });         
        map.addOverlay(marker);                                                                                   

        if ($('#Latitude').length > 0) {
            var geo = map.getCenter();
            $('#Latitude').val(geo.lat());                                                                            
            $('#Longitude').val(geo.lng());                                                                           
            $('#geoValidated').val(1);
        }
    };  

    if (loc.lat && loc.lng) {
        getLatLng(new GLatLng(loc.lat, loc.lng));                                                                 
    }               
    else {
        if (loc.address.search(/po\s*box|mobile|onsite|by\s*appointment|level|suite/) != -1) {
            loc.address = '';
        }
        else {
            loc.address += ', ';
        }

        var indexof = loc.state.search(/:/);
        if (indexof != -1) loc.state = loc.state.substring(0, indexof);
        loc.address += loc.suburb + ', ' + loc.state + ', ' + loc.postcode + ', ' + loc.country;

        geocoder.getLatLng(loc.address, function(point) {                                                         
            if (point) {
                getLatLng(point);
            }       
            else {  
                geocoder.getLatLng(loc.postcode, function(point) {
                    if (point) getLatLng(point);
                });         
            }
        });         
    }
} 

/* Filter object */
function Filter(args) {
    if (!args || !args.url || !args.obj) return;

    this.config = args;
    if (!this.config.static_url) this.config.static_url = '/static';

    if (args && args.manual) {
        var filter = this;
        $(this.config.obj).find('a[rel="more"]').click(function() {
            filter.load(true);
            return false;
        });
        return;
    }
    this.load();
}

Filter.prototype.load = function(more) {
    var url  = this.config.url + (more ? ';more=1' : '');
    var $obj = $(this.config.obj);
    $obj.find('a[rel="more"]').parent().remove();

    var filter_obj = this;
    var $filter = $obj.find('.filter-content');
    $filter.after('<div class="filter-loading" style="margin-top: 5px"><img src="' + this.config.static_url + '\/images\/loading.gif" title="loading" style="margin-left: 43px" \/><\/div>');
    $.get(url, null, function(data, textStatus) {
        if (data != '') {
            if ($obj.is(':hidden')) $obj.fadeIn('normal');
            var html = '<div style="display: none;"><ul>' + data + '</ul></div>';

            $filter.css('margin-bottom', 0);
            $obj.find('.filter-loading').fadeOut('normal', function() {
                $(this).remove();
                $filter.append(html).show();
                $filter.find('div:hidden').slideDown('normal', function() {
                    $(this).find('a[rel="more"]').click(function() {
                        filter_obj.load(true);
                        return false;
                    });
                });
            });
        }
        else {
            $obj.slideUp('normal');
        }
    }, 'html');
}

/* Form tip plugin */
jQuery.fn.extend({
    formTips: function(args) {
        return this.each(function() { new jQuery.formTips(this, args); });
    }
});

$.formTips = function(object, args) {
    var $input  = $(object);
    var $tip = $('#formtip');
    if ($tip.length == 0) {
        $(document.body).append('<div class="tipbox" id="formtip"><div class="arrow"><\/div><div class="tipcontent"><\/div><\/div>');
        $tip = $('#formtip');
    }

    $input.title = $input.attr('title');
    $input.attr('title', '')

    var showTip = function() {
        $tip.oneTime(200, 'loading_tip', function() {
            var title = $input.title;
            if (title == '') return;

            $input.tip = $tip;
            $tip.find('.tipcontent').html(title);
            var offset = $input.offset();
            $tip.css({ top: offset.top + 'px', left: offset.left + $input.outerWidth() + 1 + 'px' }).show(200);
        });
    };

    var hideTip = function() {
        $tip.stopTime('loading_tip');
        $tip.hide(200);
    };

    if ($input.is(':input')) {
        $input.focus(function() { showTip(); }).blur(function() { hideTip(); });
    }
    else {
        $input.mouseover(function() { showTip(); }).mouseout(function() { hideTip(); });
    }
}

$(document).ready(function() {
    $('.form-tip[title]').formTips();
})