$(document).ready(function() {
    $("a[rel*='external']").click(function() {
        window.open($(this).attr('href'));
        return false;
    });

    $("#gallery").cycle({
        fx:     "fade",
        speed:  "slow",
        timeout: 5000
    });

    $("#headerimgbox img").reflect({height: 0.2, opacity: 0.5});
    $("#headerimgbox img").load(function() {
        $(this).reflect({height: 0.2, opacity: 0.5});
    });

    $("#headerimgbox h1").css("marginTop", "-15px");

    $("#sitemap_container li li li").prepend('<span class="arrow">&gt;</span> ');

    $("ul.faq li").prepend('<span class="arrow">&gt;</span> ');

    $("#col1, #col2").equalizeCols();

    // homepage newsletter form
    if($("body.homepage").length > 0)
    {
        var formOptions = {
            target: "#nl_message",
            beforeSubmit: validate
        };

        $('#homepage_newsletter').ajaxForm(formOptions);
    }

    if($.browser.msie && parseInt($.browser.version) <= 6) {
        // Fix CSS background PNG on homepage
        if($("#homepage_whymidstates").length > 0) {
            var bgIMG = $("#homepage_whymidstates").css('background-image');
            if(bgIMG.indexOf(".png")!=-1) {
                var iebg = bgIMG.split('url("')[1].split('")')[0];
                $("#homepage_whymidstates").css('background-image', 'none');
                $("#homepage_whymidstates").get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
                // <a> tags on top of an element with a PNG background image that has the alpha filter applied are not clickable
                // So we position a div with that content on top of the <div> with the background image
                var savedContent = $("#homepage_whymidstates").html();
                $("#homepage_whymidstates").html("");
                $("#contents").prepend('<div class="homepage_whymidstates_pnghack">' + savedContent + '</div>');
            }
        }
    }

    if($.browser.msie && parseInt($.browser.version) <= 7) {
        // Change where the <h1>'s line break occurs on the "Why Choose Mid-States Aluminum?" page
        if(window.location.href.indexOf("why_choose_midstates") != -1) {
            $("h1").css("paddingRight", "100px");
        }
    }
});



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

    return no_errors;
}
