$(document).ready(function() {

    /* to create valid xhtml external links */
    $("a[rel=external]").attr('target', '_blank');
       
    /* needed for gallery popups */
    $("a[rel^='prettyPhoto']").prettyPhoto({
        animationSpeed: 'fast', /* fast/slow/normal */
        padding: 40, /* padding for each side of the picture */
        opacity: 0.8, /* Value between 0 and 1 */
        showTitle: true, /* true/false */
        theme: 'light_square' /* light_rounded / dark_rounded / light_square / dark_square */
    });
    
    /* needed for index image rotation */
    $('#slideshow').cycle({ 
        delay: 5000, 
        speed: 1500,
        timeout: 7500
    });
    
    /* needed for renderings swap */
    $('a.rendThumb').click(function(){
        // get the ID of the image to fade in
        var targetID = $(this).attr("id").replace("thumb-", "large-");
        // if there are any active images in the container
        if ($('#largeRendDiv img.active').length > 0){
            // fade them out (and remove the flag)
            $('#largeRendDiv img.active').fadeOut(100, function(){
                // when the fade out is done, fade in the new image and flag it
                $('#'+targetID).fadeIn(100).addClass('active');
            }).removeClass('active');

        } else {
            // just fade in the target image and flag it
            $('#'+targetID).fadeIn('normal').addClass('active');
        }
    });
    
    /* needed for 'apply now' form validation */
    $("#application").validate({
        rules: {
                applicant_first_name: "required",
                applicant_last_name: "required",
                applicant_home_phone: "required",
                applicant_email: "required",
                applicant_ss_or_ins_num: "required",
                applicant_date_of_birth: "required",
                applicant_drivers_license_number: "required",
                applicant_drivers_license_state: "required",
                present_address: "required",
                present_address_city: "required",
                present_address_state: "required",
                present_address_zip: "required",
                filed_for_bankruptcy: "required",
                ever_been_evicted: "required",
                refused_to_pay_rent: "required",
                convicted_of_a_crime: "required",
                read_and_agreed_to_terms: "required"
                },
        messages: {

        }
    });
    
    /* needed for walking map */
    imageTooltip();
    
});

/* needed for walking map */
this.imageTooltip = function(){
    
    /* set offsets */
    XOffset = 15;
    YOffset = 15;
    
    /* preload images */
    $('#campus-map area').each(
        function(){
           $('<img />').attr("src", this.href); 
        }
    );
    
    $("#largeMap").attr("alt", '');
    
    $("#campus-map area").hover(function(e){
        /* calculate offsets */
        mapOffsetLeft = $("#mapHolder").offset().left;
        mapOffsetTop = $("#mapHolder").offset().top;
        
        if( ((e.pageY) + 150) >= ($("#mapHolder").offset().top + $("#mapHolder").height()) ){
            YOffset = YOffset + 150;
        }
        
        /* set title var and remove title att from area to prevent tooltip  */
        imageTitle = this.title;
        this.title = "";
        
        $("#mapHolder").append("<div id='image_popup'><img src='"+ this.href +"' alt='"+ imageTitle +"' /><br/>"+ imageTitle +"</div>");                                 
        $("#image_popup")
            .css("top",(e.pageY - YOffset) + "px")
            .css("left",(e.pageX + XOffset) + "px")
            .fadeIn(100);                        
    },
    function(){
        /* reset title att */
        this.title = imageTitle;
        $("#image_popup").remove();
        YOffset = 15;
    });    
    $("#campus-map area").mousemove(function(e){
        $("#image_popup")
            .css("top",(e.pageY - YOffset) + "px")
            .css("left",(e.pageX + XOffset) + "px");
    });            
};