$(function(){
    
    /*
     * Pretty forms
     */
    $('form.prettyform input[type*=radio]').css('border','none');
    $('form.prettyform input[type*=checkbox]').css('border','none');
    if($.browser.mozilla) { prettyform(); }
    
    /*
     * LI hover fix for IE6
     */
    $('#navigation ul li').each( function(){
        $(this).mouseover( function(){ $(this).addClass('hover'); } ).mouseout( function(){ $(this).removeClass('hover'); } );
    });
    
    /*
     * Blur on focus
     */
    $('a').focus(function() { this.blur(); });
    $('input[type*=submit]').focus(function() { this.blur(); });
    $('input[type=text], input[type=password]').addClass("input");
    $('.required').append(" <span>*</span>"); 
    
    $('.resizer a').click(function() {
           $('.resizer a').removeClass('active');
           var textSize = $(this).parent().attr('class');
           $('body').removeClass('small medium large').addClass(textSize);
           $.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
           $(this).addClass('active');
           return false;
     });
    
    if ($("#loginBtn").length > 0) {
        $("#loginBtn").colorbox({width:"600px", height:"320px", inline:true, href:"#hiddenLogin"});
    }
    
    if ($(".cbox").length > 0) {
        $(".cbox").colorbox({width:"610px", height:"460px"});
    }
    
    if ($("#slideShow").length) {
        $('#slideShow').cycle({
            fx:     'fade',
            speed:  'slow',
            timeout: 10000,
            pause:   1,
            pager:  '#sliderNav'
        });
    }
    
    if($.cookie('TEXT_SIZE')) {
        var size = $.cookie('TEXT_SIZE');
        $('body').addClass(size); 
        $('ul.resizer li.'+size+' a').click();
        //alert( $('ul.resizer li.'+size+' a').text() );
        //alert( $.cookie('TEXT_SIZE') );
    } else {
        $("#default").click();
    }
    
    if ($('#ref').length) {
        var ref = $('#ref').val();
        $('#nav li a').each(function() {
            if ($(this).attr('href') == ref) {
                $(this).parent().addClass('active');
            }
        });
    }
});

function showDistrict(name, url) {
    $('#district-' + name).siblings('a.show-district').hide();
    $('#district-' + name).html('<br /><div style="clear: both"><img src="/assets/images/lightbox/loadingAnimation.gif" /></div>');
    $.ajax({
        url : url + "?layout=0&showClubs=1",
        type : "POST",
        success : function(msg) {
            $('#district-' + name).fadeOut('fast', function() {
                $('#district-' + name).html($(msg).find('#district-' + name).html());
                $('#district-' + name).fadeIn('fast');
                $('#district-' + name).siblings('a.show-district').attr('href', 'javascript:revealDistrict("' + name + '")');
                $('#district-' + name).siblings('a.hide-district').show();
            });
        },
        error : function(msg) {
            $('#district-' + name).fadeOut('fast', function() {
                $('#district-' + name).html('<br /><div style="clear: both"><p>Sorry there was an error retrieving the data for this district</p>');
                $('#district-' + name).fadeIn('fast');
            });
        }
    });
}

function revealDistrict(name) {
    $('#district-' + name).fadeIn('fast', function() {
        $('#district-' + name).siblings('a.show-district').hide();
        $('#district-' + name).siblings('a.hide-district').show();
    });
}

function hideDistrict(name) {
    $('#district-' + name).fadeOut('fast', function() {
        $('#district-' + name).siblings('a.show-district').show();
        $('#district-' + name).siblings('a.hide-district').hide();
    });
}

function showContacts(name) {
    $('#table-' + name).show();
    $('#contract-' + name).show();
    $('#expand-' + name).hide();
}

function hideContacts(name) {
    $('#table-' + name).hide();
    $('#contract-' + name).hide();
    $('#expand-' + name).show();
}

/*
 * Pretty forms
 */
function prettyform(){
  /*
   * Hide forms
   */
  $( 'form.prettyform' ).hide().end();
  /*
   * Label & list formatting
   */
  $( 'form.prettyform' ).find( 'label' ).not( '.noprettyform' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = '';
    this.appendChild( labelSpan );
  } ).end();
  
  /*
   * Show forms
   */
  $( 'form.prettyform' ).show().end();
}
