﻿jQuery(document).ready(function() {
    jQuery("#Login").click(user.login);
    jQuery("#LoginOut").click(user.loginOut);
    rotate.init();
    jQuery('#Icon_0,#Icon_1,#Icon_2,#Icon_3').click(rotate.click);
});
var user = {
    login: function() {
        var userName = jQuery("#UserName").val();
        var password = jQuery("#Password").val();
        if (user.isNotBlank(userName) && user.isNotBlank(password)) {
            jQuery.ajax({
                url: "/Ajax.aspx",
                type: "post",
                data: { MethodName: "Login", UserName: encodeURIComponent(userName), Password: encodeURIComponent(password), Time: new Date().getTime() },
                dataType: "text",
                success: function(result) {
                    if (result != "") {
                        jQuery("#LoginName").text(result);
                        jQuery("#Logined").show();
                        jQuery("#UserInfo").hide();
                    } else {
                        alert("This user has not been exists !");
                    }
                }
            });
        }
        else {
            alert("Please enter your login name or password !");
        }
    },
    loginOut: function() {
        jQuery.ajax({
            url: "/Ajax.aspx",
            type: "post",
            data: { MethodName: "LoginOut", Time: new Date().getTime() },
            dataType: "text",
            success: function(result) {
                if (result == "ok") {
                    jQuery("#Logined").hide();
                    jQuery("#UserInfo").show();
                }
            }
        });
    },
    isNotBlank: function(obj) {
        var flag = true;
        if (obj == undefined || obj == null) {
            flag = false;
        }
        else {
            var reg = new RegExp(/\S+/);
            flag = reg.test(obj);
        }
        return flag;
    }
}
var rotate = (function() {
    var timer, current = 0;
    function $g(id) {
        return typeof id == 'string' ? document.getElementById(id) : null;
    }
    function Empty() { }
    return {
        init: function() {
            timer = setInterval(this.process, 4000);
        },
        process: function() {
            var objs = jQuery('#Rotate div[id^=Content_]');
            if (objs == null) return;
            if (++current > 3) current = 0;
            jQuery.each(objs, function(index, item) {
                if (item.style.display == '') {
                    item.style.display = 'none';
                }
                else if (index == current) {
                    item.style.display = '';
                }
            });
        },
        click: function() {
            rotate.stop();
            var $this = this;
            var objs = jQuery('#Rotate div[id^=Content_]');
            if (objs == null) return;
            var currentIndex = $this.id.substr($this.id.length - 1, 1);
            jQuery.each(objs, function(index, item) {
                if (currentIndex == index) {
                    item.style.display = '';
                }
                else {
                    item.style.display = 'none';
                }
            });
            current = currentIndex;
            rotate.init();

        },
        stop: function() {
            clearInterval(timer);
            current = 0;
            timer = null;
        }
    }
})();
