// this is done before document ready as some browsers will let you modidy <html>  earlier
// this allows .js_hide class in CSS to hide elements before the page is rendered
$('html,body').addClass('js_enabled');
$(function()
{
    // done in doc ready too, as some browsers don't let you do it earlier
    $('html,body').addClass('js_enabled');
});

//Cufon Text Control
$(function()
{
    Cufon.replace('.cufon_me_shadow', { textShadow: '1px 1px #333', fontFamily: 'Glypha LT Std' });
    Cufon.replace('.cufon_me', { fontFamily: 'Glypha LT Std' });
    Cufon.replace('.cufon_me_black', { fontFamily: 'Glypha LT Std Black' });
    Cufon.replace('.cufon_me_light', { fontFamily: 'Glypha LT Std Light' });
    Cufon.replace('.cufon_me_black_shadow', { fontFamily: 'Glypha LT Std Black', textShadow: '1px 1px #333' });
    Cufon.replace('.cufon_me_light_shadow', { fontFamily: 'Glypha LT Std Light', textShadow: '1px 1px #333' });
    Cufon.replace('.cufon_me_crazy', { fontFamily: 'Glypha LT Std Light', color: '-linear-gradient(white, black)' });
});

//Primary Nav Drop down
var primary_nav_visible = false;

function show_primary_nav_menu()
{
    primary_nav_visible = true;
    $('#primary_nav_menu_container').show();
    if ($.browser.msie && $.browser.version.substr(0, 1) < 8)
    {
        // IE6 and & don't do something with cufon
        $('.hidden_for_ie6').css({ visibility: 'hidden' });
    };
    $('html').delegate('*:not(a,#primary_nav_menu_container *)', 'click.primarymenu', hide_primary_nav_menu);
    //$('*:not(a)').not('#primary_nav_menu_container,#primary_nav_menu_container *').bind('click.primarymenu', hide_primary_nav_menu);
    // add event listender to body
    return false;
}

function hide_primary_nav_menu(e)
{
    if ($(e.currentTarget).is('#primary_nav_menu_container'))
    {
        // don't hide if clicking in  #primary_nav_menu_container
        return;
    }
    // this is done later so any buibbled events won't show the menu again
    setTimeout(function() { primary_nav_visible = false; }, 1);
    $('*').unbind('.primarymenu');
    $('#primary_nav_menu_container').hide();
    if ($.browser.msie && $.browser.version.substr(0, 1) < 8)
    {
        $('.hidden_for_ie6').css({ visibility: 'visible' });
    };

    // if it's a link, still go to it
    return true;
}

$(function()
{
    $('#primary_nav_menu').click(function(e)
    {
        return primary_nav_visible ? true : show_primary_nav_menu();
    });
});

//Search Box Magic Javascript to make the Background image move and select the
//right option for search filter
$(function()
{
    $.cookie && setTimeout(function()
    {
        $('#remove').addClass('search_input_step_1');
        $('#remove input').addClass('large_search_input');
        $('#search_controller').append('<ul></ul>');
        $('#search_area').parent().append(
			'<input id="theselect" type="hidden" name="' + $('#search_area', this).attr('name') + '" value="' + $('#search_area').val() + '">'
		).end().find('option').each(function()
		{
		    var t = this;
		    $('<a href="#" id="' + $(this).text() + '">' + $(this).text() + '</a>').click(function()
		    {
		        var position = 1 + $(this).parent().prevAll().length; // the LI's
		        $.cookie('search_tab', position);
		        // positionin
		        // the UL
		        $('#remove').removeAttr('class');
		        $('#remove').addClass('search_input_step_' + position);
		        // alert($(t).prevAll().length);
		        $('#theselect').val($(t).val());
		        return false;
		    })
			.wrap('<li />')
			.parent()
			.appendTo($('#search_controller ul'));
		})
		.end()
		.remove();

        //        if ($.cookie('search_tab'))
        //        {
        //            $($('#search_controller a')[parseInt($.cookie('search_tab')) - 1]).click();
        //        }

        $(function()
        {
            $('#advanced_controls').hide();
            $("#Advanced").click(function()
            {
                $('#advanced_controls').show();
                $('#advanced_controls label').show();
            });
            $("#Basic").click(function()
            {
                $('#advanced_controls').hide();
            });
        });


    }, 1);
});

//Replace Input Text For Search Input
$(function()
{
    $("input.blurValue").focus(function()
    {
        if (this.value == this.defaultValue) { this.value = ""; }
    }).blur(function()
    {
        if (this.value == "") { this.value = this.defaultValue; }
    })
});

//Caption Sliding (Partially Hidden to Visible)
$(function()
{
    $('.boxgrid.caption').hover(function()
    {
        $(".cover_img_h230px", this).stop().animate({ top: '130px' }, { queue: false, duration: 160 });
    }, function()
    {
        $(".cover_img_h230px", this).stop().animate({ top: '175px' }, { queue: false, duration: 160 });
    });
    $('.focus_on_me').focus(function()
    {
        $(this).parent().parent(".cover_img_h230px").stop().animate({ top: '130px' }, { queue: false, duration: 160 });
    }).blur(function()
    {
        $(this).parent().parent(".cover_img_h230px").stop().animate({ top: '175px' }, { queue: false, duration: 160 });
    });
});

//Caption Sliding (Partially Hidden to Visible) Short
$(function()
{
    $('.boxgrid.caption').hover(function()
    {
        $(".cover_img_h275px", this).stop().animate({ top: '200px' }, { queue: false, duration: 160 });
    }, function()
    {
        $(".cover_img_h275px", this).stop().animate({ top: '240px' }, { queue: false, duration: 160 });
    });
    $('.focus_on_me').focus(function()
    {
        $(this).parent().parent(".cover_img_h275px").stop().animate({ top: '200px' }, { queue: false, duration: 160 });
    }).blur(function()
    {
        $(this).parent().parent(".cover_img_h275px").stop().animate({ top: '240px' }, { queue: false, duration: 160 });
    });
});

//Calendar/Datepicker
$(function()
{
    $.datepicker && $("#datepicker").datepicker();
});

//Gallery
$(function()
{
    $().scrollable && $("#browsable").scrollable().navigator();
});

//Equal Heights 
$(function()
{
    $().equalHeights && $(".block").equalHeights();
});

//Zebra Table
$(function()
{
    $(".table_hover tr").hover(function()
    {
        $(this).addClass("highlight");
    }, function()
    {
        $(this).removeClass("highlight");
    });

    $(".table_zebra tr:nth-child(odd)").addClass("table_odd");
});

//JS disabled dropdown menu
$(function()
{
    $('#no_javascript_menu_dropdown').hide(); // dropdown menu
    $('.no_javascript').hide(); // generic class to hide elements
});

//Static Search From File
$(function()
{
    $(".smallSearchBox").autocomplete(search_data, {
        max: 5,
        highlight: false,
        multiple: false,
        multipleSeparator: " ",
        scroll: true,
        scrollHeight: 300
    });
});

//Show More on Search Results
$(function()
{
    $('.hide_numeric_expand').hide();

    $(".expand_more").click(function()
    {
        var id = $(this).attr('name');
        $("div.expand_" + id).slideToggle("Fast");
        return false;
    });
});

//Language Slideout Menu
$(function()
{
    $('#language_dropdown').hide();
    $('#language_menu a').click(
	function()
	{
	    $('#language_dropdown').slideToggle("normal");
	});
});

// Form Validator
$.validator && $.validator.addMethod("postcode", function(value, element)
{
    return this.optional(element) || /^[A-Z]{1,2}[0-9R][0-9A-Z]? ?[0-9][ABD-HJLNP-UW-Z]{2}$/i.test(value);
}, "Please enter a Postcode in the correct format");

$.validator && $().ready(function()
{
    var validator = $("#demoForm").bind("invalid-form.validate", function()
    {

        $("#summary").show();
        $("#summary").html("<h3>Please Correct the Errors below</h3>");
    }).validate({
        debug: true,
        errorElement: "span",
        errorPlacement: function(error, element)
        {
            var label = element.prev('label').text().replace('(required)', '');
            var error2 = error.clone();
            error2.text(error2.text().replace('This field', ' '));
            error.appendTo(element.next("span")); //.wrap('<span class="input-notification error png_bg" />');
            error2.appendTo($("#summary")).prepend(label);
        },
        rules: {
            cpostcode: {
                postcode: true
            },
            cemail: {
                required: true,
                email: true
            }
        }
    });

});

