$(window).load(function() {
    // round the corners of the content box -- uses browser-rendered CSS rules when possible
    // this has to be delayed until images load for IE to know how tall the content is, which is why this is in the load() event instead of the ready() event
    if(jQuery.browser.msie) {
        $("#content").corner("tr br bl 20px");
    }
});

$(document).ready(function() {

    // target="_blank" is invalid XHTML so rel="external" is used instead
    $("a[rel*='external']").click(function() {
        window.open($(this).attr('href'));
        return false;
    });




    // login form -- default form field text
    $("input[title]").hint();

    // login form -- default password field text
    $('#u_password').hide();
    $('#password_clear').show();

    $('#password_clear').focus(function() {
        $('#password_clear').hide();
        $('#u_password').show();
        $('#u_password').focus();
    });

    $('#u_password').blur(function() {
        if($('#u_password').val() == '') {
            $('#password_clear').show();
            $('#u_password').hide();
        }
    });




    // round the corners of the content box -- uses browser-rendered CSS rules when possible
    if(!jQuery.browser.msie) {
        $("#content").corner("tr br bl 20px");
        $("#loginform input.text, #contact_form input.text, #contact_form textarea").corner("3px");
    }




    // highlight the thumbnail of the current page in the Awesome tab's right column
    var currentpage = jQuery.url.attr("file");
    $("#content p.photo_grid a").each(function() {
        if($(this).attr("href") == currentpage || $(this).attr("href") == "/" + currentpage) {
            $(this).addClass("currentpage");
        }
    });




    // create a slideshow for the projects in the Awesome tab and generate thumbnail navigation
    $("ul#slideshow").after('<ul id="slideshow_nav" class="clearfix">').cycle({
        timeout: 5000,
        speed: 400,
        pager: "#slideshow_nav",
        pagerAnchorBuilder: function(idx, slide) {
            var image_src = $(slide).children("img").attr("src");
            image_src = image_src.replace("sft490/", "sft490/thumb_");
            return '<li><a href="#"><img src="' + image_src + '" width="116" height="116" /></a></li>'; 
        }
    });

    // pause the slideshow when a thumbnail is clicked
    $("ul#slideshow_nav a").click(function() {
        $("ul#slideshow").cycle('pause');
    });




    // homepage contact form stuff
    var formOptions = {
        target: "#contact_form_warning",
        beforeSubmit: validate,
        clearForm: true,
        success: function() {
            $("#contact_submit").css("display", "none");
        }
    };

    if($('#contact_form').length > 0) {
        $('#contact_form').ajaxForm(formOptions);
    }




    /*var missions = [
        "to try to take over the world",
        "to explore strange new worlds",
        "to seek out new lifeforms and new civilizations",
        "to boldly go where no man has gone before",
        "to bowl over 200",
        "to achieve 100% on “Trogdor” in Expert mode in <cite>Guitar Hero 2</cite>",
        "to roll up our sleeves and get dirty",
        "to make user-friendly websites",
        "to design direct mail pieces that don’t get thrown away",
        "to get someone’s attention",
        "to have our clients love us",
        "to never lose sight of our purpose",
        "to have Jordan single-handedly keep the local Jimmy John’s in business",
        "to always continue learning",
        "to make you famous"
    ];*/

    var awesome_things = [
        "Results",
        "Websites",
        "Campaigns",
        "Stuff",
        "Marketing",
        "Relationships",
        "Strategies",
        "Ideas",
        "SEO",
        "Pancakes",
        "Logos",
        "Rock Albums",
        "Facebook Pages",
        "Twitter Posts",
        "LEGO Cities",
        "Designs",
        "User Interfaces",
        "Ads",
        "Radio Spots",
        "Networks",
        "Guitar Solos",
        "Sandcastles",
        "Looks",
        "Buzz",
        "Word of Mouth",
        "Posters",
        "Writing",
        "Search Results",
        "Art"
    ];

    /*for(var i = 0; i < missions.length; i++) {
        $("<span>" + missions[i] + "</span>").appendTo("#mission .tagline");
    }*/

    for(var i = 0; i < awesome_things.length; i++) {
        $("<span>" + awesome_things[i] + "</span>").appendTo("#tagline .tagline");
    }

    if(jQuery.browser.msie && parseInt(jQuery.browser.version) <= 8) {
        $("#tagline .tagline").cycle({
            timeout: 1500,
            speed: 400,
            random: true,
            fx: "jordanIE",
            cleartype: true,
            cleartypeNoBg: true
        });
    }
    else {
        $("#tagline .tagline").cycle({
            timeout: 1500,
            speed: 300,
            random: true,
            fx: "jordan",
            delay: -900
        });
    }

    /*$("#mission .tagline").cycle({
        timeout: 6500,
        speed: 500,
        random: true,
        cleartype: true,
        cleartypeNoBg: true
    });*/

});


// these custom Cycle animation functions are the same as the fade one but add color and font size rules
// they're used for the tagline animation
$.fn.cycle.transitions.jordan = function($cont, $slides, opts) {
    $slides.not(':eq('+opts.currSlide+')').css('opacity',0);
    opts.before.push(function(curr,next,opts) {
        $.fn.cycle.commonReset(curr,next,opts);
        opts.cssBefore.opacity = 0;
    });
    opts.animIn	   = { opacity: 1, fontSize: "35px", color: "#a0ca3e" };
    opts.animOut   = { opacity: 0, fontSize: "30px", color: "white" };
    opts.cssBefore = { top: 0, left: 0 };
};

$.fn.cycle.transitions.jordanIE = function($cont, $slides, opts) {
    $slides.not(':eq('+opts.currSlide+')').css('opacity',0);
    opts.before.push(function(curr,next,opts) {
        $.fn.cycle.commonReset(curr,next,opts);
        opts.cssBefore.opacity = 0;
    });
    opts.animIn	   = { opacity: 1, color: "#a0ca3e" };
    opts.animOut   = { opacity: 0, color: "#ffffff" };
    opts.cssBefore = { top: 0, left: 0 };
};





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

    return no_errors;
}
