$(document).ready(function() {
    $("input[title], textarea[title]").hint("blur");

    $("ul#slideshow").cycle({
        timeout: 5000,
        speed: 400,
        pager: "#slideshow_nav",
        pagerEvent: "mouseover",
        pauseOnPagerHover: true,
        pagerAnchorBuilder: function(idx, slide) {
            return '<div class="slideshow_nav_link">' + $(slide).children("div.link_text").html() + '</div>';
        }
    });
    // turn the slideshow nav links back into normal links
    $(".slideshow_nav_link").unbind("click");

    jQuery("#carousel").jcarousel({
        scroll: 3,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

    $("#footer #tabs").tabs();

    $("#footer .ui-tabs-panel").each(function() {
        $(this).css("height", $("#contactInfo").height());
    });

    // homepage contact form stuff
    var bodyFormOptions = {
        target: "#body_contact_form_warning",
        beforeSubmit: validate,
        clearForm: true,
        success: function() {
            $("#submit").css("display", "none");
        },
        whichForm: "body"
    };
    if($('#contact').length > 0) {
        $('#contact').ajaxForm(bodyFormOptions);
    }

    var footerFormOptions = {
        target: "#footer_contact_form_warning",
        beforeSubmit: validate,
        clearForm: true,
        success: function() {
            $("#footerSubmit").css("display", "none");
        },
        whichForm: "footer"
    };
    if($('#footerContact').length > 0) {
        $('#footerContact').ajaxForm(footerFormOptions);
    }


    // IE6 fixes
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
        // IE6 doesn't support transparent PNGs
        //$(document).pngFix();
    }

    // IE fixes
    if(jQuery.browser.msie) {
        // IE6, IE7, & IE8 don't support :last-child
        $("#markets #slideshow_nav div.slideshow_nav_link:last-child").addClass("last-child");
    }
});


function mycarousel_initCallback(carousel) {
    jQuery('#carouselRight').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#carouselLeft').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function validate(formData, jqForm, options) {
    var fields = {
        name:           "Name",
        email:          "Email",
        organization:   "Organization",
        phone:          "Phone",
        zip:            "ZIP Code",
        comments:       "Tell us about your project..."
    };

    var whichForm = options.whichForm;

    var no_errors = true;
    var whichField = null;
    for(var i = 0; i < formData.length; i++) {
        whichField = formData[i].name;
        if(fields[whichField] == null) {
            continue;
        }
        else if(!formData[i].value) {
            no_errors = false;
            $("#" + whichForm + "_contact_form_warning").text(fields[whichField] + " is required.");
            break;
        }
        else if(whichField == "email") {
            var emailPattern = /^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i;
            if(!formData[i].value.match(emailPattern))
            {
                no_errors = false;
                $("#" + whichForm + "_contact_form_warning").text("A valid email address is required.");
                break;
            }
        }
    }

    return no_errors;
}

