﻿
             
jQuery.timer = function(interval, callback) {
    var interval = interval || 100;

    if (!callback)
        return false;

    _timer = function(interval, callback) {
        this.stop = function() {
            clearInterval(self.id);
        };

        this.internalCallback = function() {
            callback(self);
        };

        this.reset = function(val) {
            if (self.id)
                clearInterval(self.id);

            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };

        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);

        var self = this;
    };

    return new _timer(interval, callback);
};
jQuery.fn.captions = function(value) {
    var adjWidth = (parseInt(this.attr("width")) - 2);
    this.wrap("<div class='caption' style='width:" + this.attr("width") + "px;height:" + this.attr("height") + "px;'></div>");
    this.after("<span style='width:" + adjWidth + "px;'>" + this.attr("alt") + "</span>");
};

$(document).ready(function() {
    $.timer(6000, function(timer) {
        swapImages();
        var nextInterval = getRandomIndex(1, 9) * 10000;
        timer.reset(nextInterval);
    });
    $("#txtSearch").focus(function() {
        $(this).filter(function() {
        return $(this).val() == '' || $(this).val() == '< search here >'
        }).removeClass('searchWM').val('');
    });
    $("#txtSearch").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ''
        }).addClass('searchWM').val('< search here >');
    });
    $("img.caption").captions();
});
function getRandomIndex(lowerBound, upperBound) {
    var randVal = lowerBound + (Math.random() * (upperBound - lowerBound));
    return Math.round(randVal);
}
function swapImages() {
    var linkCount = $('#divLinkContent').find('img').size();
    var firstIndex = getRandomIndex(1, linkCount);
    var secondIndex = getRandomIndex(1, linkCount);

    while (secondIndex == firstIndex) {
        secondIndex = getRandomIndex(1, linkCount);
    }
    var firstContainer = $('#divLink' + firstIndex);
    var firstContent = firstContainer.html();
    var secondContainer = $('#divLink' + secondIndex);
    var secondContent = secondContainer.html();

    firstContainer.fadeOut('slow', function() {
        secondContainer.fadeOut('slow', function() {
            firstContainer.html(secondContent);
            firstContainer.fadeIn('slow', function() {
                secondContainer.html(firstContent);
                secondContainer.fadeIn('slow');
            });
        });
    });
}
function showContactForm() {
    if (contactFormLoaded == false) {
        $("#contactForm").dialog({ modal: true, width: 690, height: 525, title: "Contact Request", closeOnEscape: true, draggable: false, resizable: false });
        $("#contactFrame").attr("src", "/contact.aspx");
        contactFormLoaded = true;
    } else {
        $("#contactForm").dialog("open");
    }
}
function cancelFormClick() {
    $("#contactForm").dialog("close");
}
