﻿var rollingSettingModuleId;

function Rolling(containerID, autorun){
    this.id = containerID;
    this.bound = new myBound(555, 135);
    this.size = new myBound(139, 100);
    this.speed = 4;
    this._interval = 100;
    this.INTERVAL = 100;
    this.moving = false;//用于鼠标移动到产品栏时，产品不滚动
    this.keepOn = false;//是否滚动
    this.period = 10;//滚动周期
    this.count = 0;//要滚动的对象的个数
    this.cloneEle = null;
    this.setSpeed=function(sp)
    {
        var isScroll = this.keepOn;//修改速度的过程中，不进行滚动
        if (this.keepOn == true) {
            this.keepOn = false;
        }
        sp = parseInt(sp);
        if (sp > 0 && sp<100) {
            this._interval = this.INTERVAL;
            if (sp<10) {
                this._interval=parseInt(this.INTERVAL*10/sp);
                sp = 1;
            }
            else {
                var spe=parseInt(sp/10);
                var spu=(spe+1)*10;
                this._interval = parseInt(this.INTERVAL * spu  /sp);
                sp = spe+1;
            }
        }
        if (sp != 0 && sp != this.speed) {
            this.speed = sp;
            var ele = $(this.id);
            for (var i = 0, j = 0; i < ele.childNodes.length; i++) {
                var child = ele.childNodes[i];
                if (child.nodeName != '#text') //火狐特性
                {
                    child.style.left = j * this.size.width + 'px';
                    j++;
                }
            }
        }
        if (isScroll != this.keepOn) {
            this.keepOn = isScroll;
        }
    }
    this.setPeriod = function(period2){
        var isScroll = this.keepOn;//修改速度的过程中，不进行滚动
        if (this.keepOn == true) {
            this.keepOn = false;
        }
        var sp = this.speed;
        if (period2 > 0) {
            this._interval = this.INTERVAL;
            if (period2 <= (this.bound.width * this.INTERVAL) / 1000) {
                sp = parseInt((this.bound.width * this.INTERVAL) / (period2 * 1000));
            }
            else {
                this._interval = parseInt(period2 * 1000 / this.bound.width);
                sp = 1;
            }
        }
        if (sp != 0 && sp != this.speed) {
            this.speed = sp;
            var ele = $(this.id);
            for (var i = 0, j = 0; i < ele.childNodes.length; i++) {
                var child = ele.childNodes[i];
                if (child.nodeName != '#text') //火狐特性
                {
                    child.style.left = j * this.size.width + 'px';
                    j++;
                }
            }
            this.period = period2;
        }
        if (isScroll != this.keepOn) {
            this.keepOn = isScroll;
        }
    }
    this.pause = function(pause){
        if (arguments.length == 1 && arguments[0] == false) {
            this.keepOn = true;
        }
        else {
            this.keepOn = false;
        }
    }
    this.stop = function(){
        this.moving = false;
        var parentEle = $(this.id);
        if (this.cloneEle == null) {
            return;
        }
        parentEle.style.position = this.cloneEle.style.position;//父元素相对定位
        parentEle.style.overflow = this.cloneEle.style.overflow;//滚出父对象时，不显示
        parentEle.style.width = this.cloneEle.style.width;
        parentEle.style.height = this.cloneEle.style.height;
        for (var i = 0, j = 0; i < parentEle.childNodes.length; i++)//滚动前，设置滚动的子元素的初始位置
        {
            var child = parentEle.childNodes[i];
            var child2 = this.cloneEle.childNodes[i];
            if (parentEle.childNodes[i].nodeName != '#text') {
                child.style.position = child2.style.position;
                child.style.left = child2.style.left;
                child.style.top = child2.style.top;
                j++;
            }
        }
    }
    this.start = function(){
        this.count = _getChildrenCount($(this.id));
        this.resetRollingWidth();
        var parentEle = $(this.id);
        parentEle.style.position = 'relative';//父元素相对定位
        parentEle.style.overflow = 'hidden';//滚出父对象时，不显示
        parentEle.style.width = '100%';
        parentEle.style.height = this.bound.height + 'px';
        
        this.keepOn = true;
        if (this.moving == false) {
            this.moving = true;
            this.run(this);
        }
    }
    this.setState = function(rollOrStop){
        if (arguments.length == 1 && arguments[0] == true) {
            this.start();
        }
        else {
            this.stop();
        }
    }
    this.resetRollingWidth = function(width)//重置滚动的宽度
    {
        var rs = this.keepOn;
        this.keepOn = false;
        if (arguments.length > 0 &&  parseInt(arguments[0]) > 0) {
            this.bound.width = parseInt(arguments[0]);
        }
        if (this.bound.width < this.size.width * this.count) {
            this.bound.width = this.size.width * this.count;
        }
        var parentEle = $(this.id);
        if (parentEle != null) {
            parentEle.wid = width;
        }
        for (var i = 0, j = 0; i < parentEle.childNodes.length; i++)//滚动前，设置滚动的子元素的初始位置
        {
            var child = parentEle.childNodes[i]
            if (parentEle.childNodes[i].nodeName != '#text') {
                child.style.position = 'absolute';
                child.style.left = j * this.size.width + 'px';
                child.style.top = '0px';
                j++;
            }
        }
        this.setPeriod(this.period);
        if (rs != this.keepOn) {
            this.keepOn = rs;
        }
    }
    this.rolling = function(myself)//滚动
    {
        if (myself.moving == true) {
            if (myself.keepOn == true) {
                var ele = $(myself.id);
                if (ele != null) {
                    var sp = myself.speed;//防止被修改时，各个产品移动的间隔不一致
                    var wid = myself.bound.width;
                    for (var i = 0; i < ele.childNodes.length; i++) {
                        var child = ele.childNodes[i];
                        if (child.nodeName != '#text') //火狐特性
                        {
                            var pos = parseInt(child.style.left);
                            pos -= sp;//向左移动
                            if (pos <= -myself.size.width)//滚动超过左边界
                            {
                                pos += wid;
                            }
                            child.style.left = pos + 'px';
                        }
                    }
                }
            }
            myself.run(myself);
        }
    }
    this.run = function(myself){
        setTimeout(function(){
            myself.rolling(myself);
        }, myself._interval);//IE计时器传参数
    }
    $(containerID).rolling = this;//嵌套引用，必须释放
    if ($(this.id) != null && this.cloneEle == null)//复制节点
    {
        this.cloneEle = $(this.id).cloneNode(true);
    }
    if (arguments.length > 1 && arguments[1] == true)//自动滚动
    {
        this.start();
    }
}

function _getChildrenCount(parentEle)//获取子元素个数(针对火狐定制的)
{
    var count = 0;
    if (parentEle != null) {
        for (var i = 0; i < parentEle.childNodes.length; i++)//火狐特性
        {
            if (parentEle.childNodes[i].nodeName != '#text') {
                count++;
            }
        }
    }
    return count;
}

function myBound(width, height){
    this.width = arguments[0];
    this.height = arguments[1];
}

function getContainerID(moduleId)//获取滚动时，容器元素的ID
{
    if (moduleId == '2') {
        return 'cp';
    }
    else if(moduleId == '3'){
        return 'qyzs';
    }
    return null;
}

function createRolling(id, autorun)//安全创建Rolling对象，并绑定到到该元素的rolling属性中去
{
    var ele = $(id);
    if (ele != null) {
        if (ele.rolling != null) {
            ele.rolling.stop();
            ele.rolling.id = '';
            ele.rolling.bound.width = 0;
            ele.rolling.bound.height = 0;
            ele.rolling.size.width = 0;
            ele.rolling.size.height = 0;
            ele.rolling = null;
        }
        if (arguments.length > 1 && arguments[1] == true) {
            new Rolling(id, true);
        }
        else {
            new Rolling(id);
        }
    }
    else {
        setTimeout(function(){
            createRolling(id, autorun);
        }, 1000);//当指定ID的元素暂时不存在时，反复检测，知道它存在时，创建滚动
    }
    
}

function setRollingPeriod(id, period)//设置周期
{
    var ele = $(id);
    if (ele != null && ele.rolling != null && ele.rol==true && ele.typ == '1' && ele.cou!='0') {
        if (ele.rolling != null) {
            ele.rolling.setPeriod(period);
        }
    }
}
function setRollingSpeed(id, speed)//设置速度
{
    var ele = $(id);
    if (ele != null) {
        if (ele.rolling != null) {
            ele.rolling.setSpeed(speed);
        }
    }
}

function rollable(id, rol)//滚动或不滚动
{
    var ele = $(id);
    if (ele != null && ele.rolling != null) {
        if (arguments.length > 1 && arguments[1] == true) {
            ele.rolling.setState(true)
        }
        else {
            ele.rolling.setState(false);
        }
    }
}

function rollingOrPause(id, keepOn)//滚动或暂停
{
    var ele = $(id);
    if (ele != null && ele.rolling != null) {
        if (arguments.length > 1 && arguments[1] == true) {
            ele.rolling.pause(false);
        }
        else {
            ele.rolling.pause(true);
        }
        
    }
}

function setRollingWidth(id, width)//设置滚动宽度
{
    var ele = $(id);
    if (ele != null && ele.rolling != null && ele.rol==true && ele.typ == '1' && ele.cou!='0') {
        ele.rolling.resetRollingWidth(width);
    }
}

function rollingSettingCancel()//取消滚动
{
    $('rollingSettingDiv').style.display = 'none';
}

function setRollingDiv(moduleId, evt)//设置滚动
{
    evt = evt ? evt : window.event;
    evt.cancelBubble = true;
    rollingSettingModuleId = moduleId;
    var id = getContainerID(moduleId);
    var exist=false;
    if(id!=null)
    {
        var ele = $(id);
        if (ele != null && ele.rolling != null)
        {
            $('autorolling').checked=ele.rolling.moving;
            $('rollPeriod').value=ele.rolling.period;
            exist=true;
        }
    }
    if(!exist)
    {
        $('autorolling').checked=false;
        $('rollPeriod').value='';alert('');
    }
    if (moduleId == "2") {//产品
        $("setRolling_msg").innerHTML = "请输入所有产品滚动一周的时间(1-99秒)";
    }
    else if (moduleId == "3") {//产品
        $("setRolling_msg").innerHTML = "请输入所有展示滚动一周的时间(1-99秒)";
    }
    else {
        $("setRolling_msg").innerHTML = "请输入滚动一周的时间(1-99秒)";
    }
    $('rollingSettingDiv').style.display = "block";
    $('rollPeriod').focus();
}

function rollingSettingSubmit(){
    var rol = $('autorolling').checked;
    var period = $('rollPeriod').value;
    var id = getContainerID(rollingSettingModuleId);
    var ele;
    if (rol == false && period == '')//没输入周期
    {
        period = '0';
    }
    if (id != null) {
        ele = $(id);
        if (ele != null && rol == ele.rol && period == ele.per)//无修改
        {
            rollingSettingCancel();
            return;
        }
    }
    if (period.length == 0) {
        alert('请输入滚动周期');
        $('rollPeriod').focus();
        return;
    }
    else 
        if (period == '0' && rol==true) {
            alert('错误的周期');
            $('rollPeriod').focus();
            return;//错误的字符串
        }
    for (var i = 0, c; i < period.length; i++) {
        c = period.charCodeAt(i);
        if (c < 48 || c > 57) {
            alert('错误的周期');
            $('rollPeriod').focus();
            return;//错误的字符串
        }
    }
    if (ele != null) {
        ele.rol = rol;
        ele.per = period;
    }
    var setRollingScript = document.createElement("script");
    setRollingScript.type = "text/javascript";
    document.body.appendChild(setRollingScript);
    setRollingScript.src = "../AjaxServer/SetRolling.aspx?userid=" + g_poiId + "&typeNumList=" + g_typeNumList + "&mid=" + rollingSettingModuleId + "&rol=" + rol + "&per=" + period +
    "&side=" +
    $(rollingSettingModuleId).parentNode.id +
    "&layout=" +
    getFileName(g_init_layout) +
    '&time=' +
    new Date().toLocaleTimeString();
    
}

function manualRolling(moduleId)//手动启动滚动
{
    var id = getContainerID(moduleId);
    if (id != null) {
        var ele = $(id);
        var dataEle = $(moduleId + '_TypeNumList');
        if (ele != null && dataEle != null) {
            var data = dataEle.value.split(',');
            if (ele.typ == null) {
                ele.typ = data[0];
            }
            if (ele.cou == null) {
                ele.cou = data[1];
            }
            if (ele.rol == null) {
                if (data[2] == '1') {
                    ele.rol = true;
                }
                else {
                    ele.rol = false;
                }
            }
            if (ele.per == null) {
                ele.per = data[3];
            }
            if (ele.wid == null) {
                ele.wid = data[4];
            }
            createRolling(id);
//            setRollingSpeed(id, ele.per);
            if (ele.typ == '1' && ele.rol == true && ele.cou!='0') {
                setRollingPeriod(id, ele.per);
                setRollingWidth(id, ele.wid);
                rollable(id, true);
            }
        }
    }
}

function unrolling(moduleId)//主要是为了释放计时器
{
    var id = getContainerID(moduleId);
    if (id != null) {
        var ele = $(id);
        if (ele != null && ele.rolling != null) {
            ele.rolling.stop();
            ele.rolling.id = '';
            ele.rolling.bound.width = 0;
            ele.rolling.bound.height = 0;
            ele.rolling.size.width = 0;
            ele.rolling.size.height = 0;
            ele.rolling = null;
        }
    }
}

function periodFilter(evt){
    evt = evt ? evt : window.event;
    if (evt.keyCode < 48 || evt.keyCode > 57) {
        evt.returnValue = false;
    }
    else 
        if (evt.keyCode == 48 && $('rollPeriod').value.length == 0) {
            evt.returnValue = false;
        }
    if (evt.keyCode == 13) {
        rollingSettingSubmit();
    }
}

//设置模块滚动宽度
function setModuleRollingWidth(moduleId,divClass1,layoutCss1){
    if(moduleId==null){
        return;
    }
    var id = getContainerID(moduleId);
    if($(moduleId)==null || id==null){
        return;
    }
    var divClass;
    var layoutCss;
    if(arguments.length==1){
        divClass=$(moduleId).parentNode.id;
        layoutCss=getFileName(g_init_layout);
    }
    else{
        divClass=arguments[1];
        layoutCss=arguments[2];
    }
    var width;
    if (layoutCss == "layout1.css"){
        if (divClass == "left"){
            width= 555;
        }
        else if (divClass == "center"){
            width= 280;
        }
    }
    else if (layoutCss == "layout2.css"){
        if (divClass == "left"){
            width= 280;
        }
        else if (divClass == "center"){
            width= 555;
        }
    }
    setRollingWidth(id, width);
}
