﻿
/* Setup application namespaces */
var Smartjs = {};

/* Creates a slideshow effect from a set of images. */
Smartjs.SlideShow = function (id) {
    var $active = $('#' + id + ' IMG.active');
    if ($active.length == 0) {
        $active = $('#' + id + ' IMG:last');
    }
    var $next = $active.next().length ? $active.next()
        : $('#' + id + ' IMG:first');
    $active.addClass('last-active');
    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.removeClass('active last-active');
        });
};

/* Validates a set of form elements aren't empty.  Integrate in the
   following way using jQuery:

      //Instantiate validator.
      var validator = new Smartjs.Validator('.requiredElements'); 
      // Bind validate function to form submit.
      $('#').submit(validator.validate);
*/
Smartjs.Validator = function (jQuerySelector) {

    // Allows us to access 'this' when it would be out of scope.
    _this = this;
    // Clear any existing error highlighting.
    $(jQuerySelector).removeClass("error");
    // Save selector arguement.
    _this.selector = jQuerySelector;

    _this.validate = function () {
        var passed_validation = true;
        // Clear any pre-existing error highlighting.
        $(_this.selector).removeClass("error");
        // Define function to call if validation fails.
        var error = function (element_id) {
            $("#" + element_id).addClass("error");
            passed_validation = false;
        };
        // Check each element has a non-blank value.
        $.each($(_this.selector), function () {
            if ($(this).val() == "") {
                error(this.id);
            }
        });
        return passed_validation;
    };

};  

/* /home.aspx */
Smartjs.Home = function () {

    // Holds slideshow function to be called at set intervals.
    this.slide_show = null;

    this.pageLoad = function () {
        // Bind validation event handler to form.
        var validator = new Smartjs.Validator('#username, #password');
        $('#LoginForm').submit(validator.validate);
        // Run the image slideshow.
        this.slide_show = setInterval("Smartjs.SlideShow('slideshow')", 5000);
    };
};

/* /members/reqsupport.aspx */
Smartjs.ReqSupport = function () {

    this.pageLoad = function () {
	    // Bind validation event handler to form.
        var validator = new Smartjs.Validator('.required');
        $('#aspnetForm').submit(validator.validate);
    };
};

/* /members/foprgottenpassword.aspx */
Smartjs.ForgottenPassword = function () {

    this.pageLoad = function () {
        // Bind validation event handler to form.
        var validator = new Smartjs.Validator('.required');
        $('#aspnetForm').submit(validator.validate);
    };
};


