$(document).ready(function() 
{
    $(".mainNavLink").mouseover(function() 
    {
        sub_menu = $(this).attr('rel');

        $('.subNav').hide();
        $('#submenu_'+sub_menu).show();
    });

    $("input:image, input:button, input:submit").click(function(){
        $(this.form.elements).each(function(){
            if(this.type =='text' || this.type =='textarea' || this.type =='password' ){
                if(this.value == this.title && this.title != ''){
                    this.value='';
                }
            }
        });
    });


    $("input:checkbox.a_refer").click(function()
    {
        counter = 0;
        $(this.form.elements).each(function()
        {
            if ($(this).attr('checked') == true)
                {
                counter += 1;
            }
        });

        if (counter > 4)
            {
            alert('You can only select 4 companies to request a quote from.');
            $(this).attr('checked',false);
        }

    });

    $('.form_help').mouseover(function() {
        id = $(this).attr('rel');
        $('#form_help_'+id).fadeIn('fast');
    });

    $('.form_help').mouseout(function() {
        id = $(this).attr('rel');
        $('#form_help_'+id).fadeOut('slow');
    });

    $('.refine_search').click(function() {
        $('#search_link').slideToggle();
        $('#search_form').slideToggle();
    });

    $('.showhide').click(function() {
        rel = $(this).attr('rel');
        $('#'+rel).toggle();
    });


    $('.update_counter').bind("change keyup", function() 
    {
        rel = $(this).attr('rel');

        the_length = $(this).val().length;

        remaining = 230 - the_length;

        $(this).css('border','1px solid #CCC');
        $(this).css('background','#fff');

        if (remaining <= 0)
            {
            text = $(this).val().substr(0,230);
            remaining = '0';
            $(this).val(text);
            $(this).css('border','1px solid #609659');
            $(this).css('background','#f6fcf5');
        }


        $('#'+rel).html(remaining);

    });

    $('textarea.update_counter').autoResize({
        // On resize:
        onResize : function() {
            $(this).css({opacity:0.8});
        },
        // After resize:
        animateCallback : function() {
            $(this).css({opacity:1});
        },
        // Quite slow animation:
        animateDuration : 300,
        // More extra space:
        extraSpace : 40
    });    


    $('.showhide_saved_search').click(function () {

        var display_status = $('#'+$(this).attr('rel')).css('display');
        if (display_status == 'table-row')
            {
            $('#'+$(this).attr('rel')).hide();
            $(this).html('View');
        }
        else
            {      
            $('.result_row').hide();
            $('.showhide_saved_search').html('View');
            $('#'+$(this).attr('rel')).show();
            $(this).html('Hide');
        }
    });


    $('.switch_login').click(function() {
        if ($('#supplier_login').css('display') == 'block')
            {
            $('#supplier_login').fadeOut('slow',function () {
                $('#account_login').fadeIn('slow');        
            });

        }
        else
            {
            $('#account_login').fadeOut('slow',function () {
                $('#supplier_login').fadeIn('slow');        
            });
        }
    });

    $('.openclose').click(function () {

        var display_status = $('#'+$(this).attr('rel')).css('display');
        if (display_status == 'block')
            {
            $(this).removeClass('colours2');
            $(this).addClass('colours1');
            $('#'+$(this).attr('rel')).hide();
            $('.'+$(this).attr('rel')).attr('src','_ui/media/downarrow.gif');
        }
        else
            {
            $(this).removeClass('colours1');
            $(this).addClass('colours2');
            $('#'+$(this).attr('rel')).show();
            $('.'+$(this).attr('rel')).attr('src','_ui/media/uparrow.gif');
        }
    });

    $('.openclosedetails').click(function () {

        var display_status = $('#'+$(this).attr('rel')).css('display');
        if (display_status == 'block')
            {
            $('#'+$(this).attr('rel')).slideUp();
            $(this).html('More Details');
        }
        else
            {
            $('#'+$(this).attr('rel')).slideDown();
            $(this).html('Hide Details');
        }
    });   

    $('.agreedtoterms').click(function ()
    {
        if ($(this).attr('checked') == true)
            {
            $('.submit').attr('disabled',false)
            $('.submit2').attr('disabled',false)
        }
        else
            {
            $('.submit').attr('disabled',true)
            $('.submit2').attr('disabled',true)
        }
    })  

    $('#RightColumn_thin .widgetSections a').corner('tl tr');
    $('#BlurbColumn .about_uz').corner('10px');
    $('#BlurbColumn .about_uz h1').corner('11px tl tr');





});            


$(function() 
{
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    $( "#dialog-form" ).dialog({
        title: 'Reviews',
        autoOpen: false,
        height: 550,
        width: 650,
        modal: true,
        buttons: {
                Close: function() {
                    $( this ).dialog( "close" );
                }
            }
    });

    $(".view_reviews").click(function() {
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-form" ).dialog( "open" );
        $( "#dialog-form" ).html('<div style="text-align:center;"><img src="/_ui/media/loading.gif"></div>');

        $.get("/_ui/js/AJAX_loadreviews.php", { id: $(this).attr('rel') },
        function(data)
        {
            $( "#dialog-form" ).html(data);
        });        
    });
});
