$(document).ready(function() {

    resizeColumns("#topnav_findProducts div.mega-column");
    resizeColumns("#topnav_explore div.mega-column");
    resizeColumns(".grid_box");
    resizeColumns("#fixture_matrix li");
    //resizeColumns("#col0, #col1");    // 5/3/2010 - this got moved to $(window).load() because we need to wait for images to load -- their heights aren't always known

    textFieldEraser("#footerEmailField");
    textFieldEraser("#searchquery");
    textFieldEraser("#zipcode");

    // newsletter signup in footer
    if($('#footerNewsletter').length > 0) {
        $('#footerNewsletter').ajaxForm({
            target: "#footerNewsletter_warning",
            beforeSubmit: validate,
            clearForm: true,
            success: function() {
                //$("#footerSubmit").css("visibility", "hidden");
            }
        });
    }

    $("div.gallery_full").addClass("clearfix");
    $("div.gallery_cluster:first").css("width", "660px");
    $("div.gallery_cluster:first .gallery_img a img.gallery_overlay").css("left", "-39px");
    $("img.gallery_overlay").css("visibility", "hidden").attr("src", "/images/play.png").css("visibility", "visible");


    // swap image caption thing
    $("#fixtureimg a.caption2").hover(function() {
        var caption = $(this).attr("title");
        $("#putcaptionhere").text(caption);
    });



    // homepage stuff
    if($("#homepage_container").length > 0 /*&& $("#cms_overall").length == 0*/) {
        // the fading banners
        $("#subheader").cycle({
            random: 1,
            fx: "fade",
            timeout: 9000
        });

        // the project carousel
        $("#homepage_carousel").wrapInner('<ul class="jcarousel-skin-manning">');
        $(".grid_box").wrap("<li>");
        $("#homepage_carousel ul").jcarousel({
            //scroll: 1,
            //wrap: 'circular',
            easing: "easeOutCubic",
            animation: 500
        });

        // the tabbed interface
        $("#tabs").tabs();
    }


    $("#sitemap_container div.mega-column").wrap("<li>");



    // IE6 fixes
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {

        // IE6 doesn't support transparent PNGs
        //$("#header").pngFix();

        // IE6 doesn't support :last-child
        $("div.mega-column:last-child").addClass("last-child");
        $("#homepage_container .ui-tabs .ui-tabs-nav li:last-child").addClass("last-child");

        // add :hover pseudoclass support to certain things
        $("div.topnav, #fixture_matrix li, #fixtureimg img").hover(function() {
            $(this).addClass("ie6_hovering");
        }, function() {
            $(this).removeClass("ie6_hovering");
        });
    }

    // IE7 fixes
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 7) {

        // IE7 doesn't support :last-child
        $("div.mega-column:last-child").addClass("last-child");
        $("#homepage_container .ui-tabs .ui-tabs-nav li:last-child").addClass("last-child");
    }
});


$(window).load(function() {
    if(!$("#col0").hasClass("overlay") && $("#homepage_container").length == 0) {   // don't run column resize on the fixture details pages because there is only one column
        resizeColumns("#col0, #col1");      // 5/3/2010 - this was moved from $(document).ready() because we need to wait for images to load -- their heights aren't always known
    }
});




function validate(formData, jqForm, options) {
    var no_errors = true;
    if(!$("#footerEmailField").val()) {
        no_errors = false;
        $("#footerNewsletter_warning").text("Please enter your email address.");
    }
    else
    {
        var emailPattern = /^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i;
        if(!$("#footerEmailField").val().match(emailPattern))
        {
            no_errors = false;
            $("#footerNewsletter_warning").text("A valid email address is required.");
        }
    }

    return no_errors;
}









function resizeColumns(columnSelector) {

    var curHeight = 0;
    var maxHeight = 0;

    // find height of tallest column
    $(columnSelector).each(function() {
        curHeight = $(this).height();
        if(curHeight > maxHeight) {
            maxHeight = curHeight;
        }
    });

    // set all columns to height of tallest column
    $(columnSelector).height(maxHeight);
}






function textFieldEraser(textFieldSelector) {
    textFieldSelector = $(textFieldSelector);
    var defaultText = textFieldSelector.val();

    textFieldSelector.focus(function() {
        if ($(this).val() == defaultText) {
            $(this).val("");
        }
    }).blur(function() {
        if ($.trim($(this).val()) == "") {
            $(this).val(defaultText);
        }
    });

    $(textFieldSelector).parents("form").submit(function() {
     //   if($.trim(textFieldSelector.val()) == "" || $.trim(textFieldSelector.val()) == $.trim(defaultText)) {
     //       return false;
     //   }
    });

}

