﻿

//----------------------- Image Preloading ---------------------//

// Preload the images common to all pages
//urls in javascript resolve relative to the page they are run from rather than relative to this javascript file.
//urls in CSS on the other hand resolve relative to their own location (opposite)

// Preload image method
jQuery.preloadImages = function() {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}
// Preload this site's images
var b = "App_Themes/MCC/images/"; //basePaths
$.preloadImages(b + "Menu/AboutMCC_s2.jpg",
b + "Menu/Alumni_s2.jpg",
b + "Menu/AmeriCorps_s2.jpg",
b + "Menu/Awards_s2.jpg",
b + "Menu/Constituents_s2.jpg",
b + "left/leftMenu/Grants_s2.jpg",
b + "left/leftMenu/Members_s2.jpg",
b + "left/leftMenu/Programs_s2.jpg",
b + "left/leftMenu/Resources_s2.jpg"
);

//----------------------- DOM Load Activities ---------------------//

//After the page has loaded...
$(document).ready(function() {
    CenterScrolling(0);
    window.onresize = function() { DetectResize(); };

    //Detect enter key press
    $("#txtSearch").keypress(function(e) {
        //check for key code in FF and IE respectively
        if (e.which == 13 || e.keyCode == 13) {
            Search(this);
            return false;
        }
    });

    //detect button click
    $("#divSearch_Go").click(function(e) {
        var t = document.getElementById('txtSearch');
        Search(t);
        return false;
    });

    /*SearchBox Watermark behaviors*/
    SetupWatermarkBehaviors();
});

//----------------------- Search ---------------------//

function Search(sender) {
    var param = sender.value;
    if (param.length == 0) {
        return;
    }
    else {
        window.open('/search.aspx?q=' + param, '_self', '');
    }
}

function SetupWatermarkBehaviors() {
    var searchBox = $("#txtSearch")

    //Username Behaviors
    searchBox.mouseover(function() {
        //Switch the class to entry mode
        WaterMarkControl(this.id, 'TextBox');
    });

    searchBox.focus(function() {
        //Switch the class to entry mode
        WaterMarkControl(this.id, 'TextBox');
    });

    searchBox.mouseout(function() {
        //Switch the class to watermark mode only if nothing has been entered in the textbox
        WaterMarkControl(this.id, 'watermark');
    });

    searchBox.blur(function() {
        //Switch the class to watermark mode only if nothing has been entered in the textbox
        WaterMarkControl(this.id, 'watermark');
    });

    //TextBox Css Default Class
    if (searchBox.val() != null && searchBox.val().length > 0) {
        WaterMarkControl(searchBox.attr('id'), 'TextBox');
    }
    else {
        WaterMarkControl(searchBox.attr('id'), 'watermark');
    }

}

function WaterMarkControl(control_Id, mode) {
    var control = document.getElementById(control_Id);
    if (mode == 'TextBox') {
        if (control.value == 'keywords') {
            control.value = ''; //clear the box for the user
            control.className = '';
        }
        control.focus();
    }
    else {   //watermarked
        if (control.value.length == 0) {
            control.value = 'keywords';
            control.className = 'watermark';
        }
    }
}


//----------------------- Window Scrolling ---------------------//

var TimeOfLastResize = 0;
var IsResizing = false;

function DetectResize() {
    var d = new Date();
    TimeOfLastResize = d.getTime();
    IsResizing = true;
    setTimeout('DetectResizeComplete();', 1000);
}

function DetectResizeComplete() {

    var d = new Date();
    var currentRequestTime = d.getTime();
    var msSinceLastResize = currentRequestTime - TimeOfLastResize;
    if (IsResizing && msSinceLastResize > 800) {
        CenterScrolling(800);
        $('.HomeLink').text(currentRequestTime);
        IsResizing = false;
    }
}

function CenterScrolling(duration) {
    var currentWindowWidth = document.documentElement.clientWidth; //for IE, FF, and Safari
    var widthDiff = document.getElementById('divContent').clientWidth - currentWindowWidth;
    var xChange = Math.max(0, parseInt(widthDiff / 2));
    var veryOffCenter = xChange - window.pageXOffset > 50;

    //Requires jquery.scrollTo-min.js
    $.scrollTo(xChange + 'px', duration, { axis: 'x' });
}

