﻿var Class = {
    create: function() {
        return function() {
            this.initialize.apply(this, arguments);
        }
    }
}

Object.extend = function(destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
}

var TransformView = Class.create();
TransformView.prototype = {
    initialize: function(container, slider, parameter, count, options) {
        if(parameter <= 0 || count <= 0) return;
        var oContainer = document.getElementById(container), oSlider = document.getElementById(slider), oThis = this;

        this.Index = 0;

        this._timer = null;
        this._slider = oSlider;
        this._parameter = parameter;
        this._count = count || 0;
        this._target = 0;

        this.SetOptions(options);

        this.Up = !!this.options.Up;
        this.Step = Math.abs(this.options.Step);
        this.Time = Math.abs(this.options.Time);
        this.Auto = !!this.options.Auto;
        this.Pause = Math.abs(this.options.Pause);
        this.onStart = this.options.onStart;
        this.onFinish = this.options.onFinish;

        oContainer.style.overflow = "hidden";
        oContainer.style.position = "relative";

        oSlider.style.position = "absolute";
        oSlider.style.top = oSlider.style.left = 0;
    },

    SetOptions: function(options) {
        this.options = {
            Up:	true,
            Step: 5,
            Time: 10,
            Auto: true,
            Pause: 5000,
            onStart: function(){},
            onFinish: function(){}
        };
        Object.extend(this.options, options || {});
    },

    Start: function() {
        if(this.Index < 0){this.Index = this._count - 1;}
        else if (this.Index >= this._count){ this.Index = 0; }

        this._target = -1 * this._parameter * this.Index;
        this.onStart();
        this.Move();
    },

    Move: function() {
        clearTimeout(this._timer);
        var oThis = this, style = this.Up ? "top" : "left", iNow = parseInt(this._slider.style[style]) || 0, iStep = this.GetStep(this._target, iNow);

        if (iStep != 0) {
            this._slider.style[style] = (iNow + iStep) + "px";
            this._timer = setTimeout(function(){ oThis.Move(); }, this.Time);
        } else {
            this._slider.style[style] = this._target + "px";
            this.onFinish();
            if (this.Auto) { this._timer = setTimeout(function(){ oThis.Index++; oThis.Start(); }, this.Pause); }
        }
    },

    GetStep: function(iTarget, iNow) {
        var iStep = (iTarget - iNow) / this.Step;
        if (iStep == 0) return 0;
        if (Math.abs(iStep) < 1) return (iStep > 0 ? 1 : -1);
        return iStep;
    },

    Stop: function(iTarget, iNow) {
        clearTimeout(this._timer);
        this._slider.style[this.Up ? "top" : "left"] = this._target + "px";
    }
};

//正则验证
function check_Value(_value,_myreg)
{
    return _myreg.test(_value);
}

//验证为空
function check_Null(_value)
{
    if(_value.replace(/^\s+$/,'') == "")
    {
        return false;
    }
    else
    {
        return true;
    }
}

//检验URL地址
function check_Url(_value)
{
    var myreg = "^http://(([0-9A-z_!~*'().&=+$%-]+: )?[0-9A-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
          + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
          + "|" // 允许IP和DOMAIN（域名）
          + "([0-9A-z_!~*'()-]+\.)*" // 域名- www. 
          + "([0-9A-z][0-9A-z-]{0,61})?[0-9A-z]\." // 二级域名 
          + "[A-z]{2,6})" // first level domain- .com or .museum 
          + "(:[0-9]{1,4})?" // 端口- :80 
          + "((/?)|" // a slash isn't required if there is no file name 
          + "(/[0-9A-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re=new RegExp(myreg); 
    if (re.test(_value)){
        return true; 
    }else{ 
        return false; 
    }
}

//检验电子邮箱
function check_Email(_value)
{
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return check_Value(_value,myreg);
}

//检验身份证号码
function check_IdCard(_value)
{
    var myreg = /^\d{17}[\d|X]|\d{15}$/;
    return check_Value(_value,myreg);
}

//检验年龄
function check_Age(_value)
{
    var myreg = /^[1-9]\d{0,1}$/;
    return check_Value(_value,myreg);
}

//检验OICQ号码
function check_OICQ(_value)
{
    var myreg = /^[1-9]\d{4,11}$/;
    return check_Value(_value,myreg);
}

//检验邮政编码
function check_Zip(_value)
{
    var myreg = /^\d{6}$/;
    return check_Value(_value,myreg);
}

//检验电话号码
function check_Phone(_value)
{
    var myreg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
    return check_Value(_value,myreg);
}

//检验手机号码
function check_Mobile(_value)
{
    var myreg = /^(?:13\d|15[012589])-?\d{5}(\d{3}|\*{3})$/;
    return check_Value(_value,myreg);
}

//验证自然数
function chech_Naturalnumber(_value)
{
    var myreg = /^[1-9]\d*|0$/;
    return check_Value(_value,myreg);
}

//验证[0-255]的数字
function check_num(_value)
{
    var myreg = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
    return check_Value(_value,myreg);
}

//密码
function check_Pass(_value){//允许数字，字母，特殊符号(组合)
	var reg = /^[\w!@#$%\^&\*\(\)_]{6,16}$/;
	if(_value.match(reg)!=null)
	{
		return true;
	}
	else
	{
		return false;
	}
}