﻿///<refence path="mootools121.js"/>

//主要负责左边导航栏的滚动

var FloatDiv = new Class({
     Implements:Options,
 
     options:{
         y:0,//到文档视图顶端的距离
         interval:0.01,//时长，单位为秒
         step:0.1//步长，数字越小，动画越慢，为1时无缓动动画
     },
     
     initialize:function(obj,options){
         this.setOptions(options);
         this.obj = obj;
         this.isActive = true;
         this.scrollFunc.periodical(this.options.interval*1000,this);
         this.obj.setStyle("position","absolute");
         this.obj.setStyle('top',"257px");//ie下记得要设置一个初始化
     },
 
     scrollFunc:function(){
         if(this.isActive){  
             //if(this.obj.getStyle("position") == "none") return; 
             var posNow = this.obj.getStyle('top').toInt();
             var offset = (window.getScroll().y+this.options.y-posNow)*this.options.step;
             offset = offset > 0 ? Math.ceil(offset) : Math.floor(offset);
             this.obj.setStyle('top',posNow+offset);
         }
     },
 
     toggleActive:function(){
         this.isActive = this.isActive ? false:true;
     }
});
var PPVACM = {

    floatDivs:{},
    
    target:null,
    
    tip:null,
    
    info:null,
    
    checkboxes:null,
    
    inputs:null,
    
    tipTimer:-1,
    
    init:function(){
        //        navigator.appVersion.split("MSIE")[1];
        
        PPVACM.target = $$("div.layer_wrap")[0];
        PPVACM.tip = $$("div.layer_prompt")[0];        
        PPVACM.info = $$("div.layer_wrap .layer_foot i")[0];
        PPVACM.checkboxes = $$("div.layer_wrap .layer_content_left input.layer_content_box");
        PPVACM.inputs = $$("div.layer_wrap input.layer_content_text");
        
        
        PPVACM.info.setStyle("display","none");
        
        PPVACM.initShowObj();
        PPVACM.initTip();
        
        PPVACM.initLeft();
        
        PPVACM.initContentImage();
        
        $(window).addEvent("resize",PPVACM.resize);
        PPVACM.resize();
    },
    resize:function(){        
        var right = $$("#main .help_right")[0]; 
        var left = $$("#main .help_left")[0]; 
        var width = left.getStyle("width").toInt();
        var lpos = PPVACM.getPos($$("div.header_banner_admins")[0]);
        //right.setStyles({"left":lpos.x + width }); 
        if(lpos.x !=null )
        left.setStyle("left",lpos.x);  
    },
    //没有跳转地址的图片不可点
    initContentImage:function(){
        var divs = $$("div.help_right_content .admin_table td div");
        divs.each(function(elmt,index){
            var imgs = elmt.getElements("img");
            var el = elmt.getElements("a")[0];
            var b = elmt.getElements("b")[0];
            
            if(imgs && imgs.length==1 &&( !el.get("href") || el.get("href").trim()==""))
            {
                el.setStyle("cursor","default");
                el.set("href","javascript:void(0)");
                el.set("target","");
                b.setStyles({"text-decoration":"none","cursor":"default"});                           
            }
            else
            {
                b.addEvent("click",function(){window.open(el.get("href").toString());});
                b.setStyles({"text-decoration":"underline","cursor":"pointer"});  
            }
        });        
    },
    initLeft:function(){
        //var titles = $$(".help_left li p.help_left_title");
        //titles.each(function(el,index){el.addEvent("click",PPVACM.shutCMBox);});
        
        var div = $$("#main .help_left")[0];         
        PPVACM.floatDivs["left"] = new FloatDiv(div,{"y":257});
    },
    initShowObj:function(){        
        //PPVACM.floatDivs["target"] = new FloatDiv(PPVACM.target,{"y":400});
        //var img = $$(".layer_wrap .layer_head img")[0];
        var btns = $$(".layer_wrap .layer_foot input");
        var selall = $("layer_content_box_all");
        var cancel = btns[1];
        var submit = btns[0];
        
        //img.addEvent("click",PPVACM.shutCMBox);
        cancel.addEvent("click",PPVACM.cancelCMBox);
        submit.addEvent("click",PPVACM.submitCMBox);
        selall.addEvent("click",PPVACM.selAll);
        
        var chkboxes = PPVACM.checkboxes.slice(0,PPVACM.checkboxes.length-1);        
        chkboxes.each(function(el,index){
           el.addEvent("click",PPVACM.clickChk);
        });
    },
    initTip:function(){         
         PPVACM.floatDivs["tip"] = new FloatDiv(PPVACM.tip,{"y":245});
         
         var img = $$("div.layer_prompt img")[0];
         var shut = $$("div.layer_prompt input")[0];
         
         img.addEvent("click",PPVACM.shutTip);
         shut.addEvent("click",PPVACM.shutTip);
    },
    showFloats:function(b){
        for(var key in PPVACM.floatDivs)
        {
            var div = PPVACM.floatDivs[key];
            div.isActive = b;
        }
    },
    selAll:function(){
        var chk = $("layer_content_box_all");
        var chkboxes = PPVACM.checkboxes;
        if(chk.get("checked"))
        {
            chkboxes.each(function(el,index){
                el.set("checked","checked");
            });
        }
        else
            chkboxes.each(function(el,index){
                el.set("checked",false);
            });
    },
    clickChk:function(){
        var all = true;
        var chkboxes = PPVACM.checkboxes;
        for(var i = 0 ;i<chkboxes.length;i++)
        {
            var chk = chkboxes[i];
            if(!chk.get("checked")){ all = false;break;}
        }
        if(!all)$("layer_content_box_all").set("checked",false);
        else $("layer_content_box_all").set("checked",true);
    },
    resetCMInput:function(){
        //left
        var txts = PPVACM.inputs;
        txts.each(function(el,index){
            el.set("value","");
        });
        
        var chkboxes = PPVACM.checkboxes;
        chkboxes.each(function(el,index){
            el.set("checked","");
        });
        
        var memo =  $$("div.layer_wrap .layer_content_left textarea")[0];
        memo.set("value","");        
    },
    startShutTip:function(){
        PPVACM.stopShutTip();
        PPVACM.tipTimer = setTimeout(PPVACM.shutTip,5000);
    },
    stopShutTip:function(){
        clearTimeout(PPVACM.tipTimer);
    },
    shutTip:function(){
        PPVACM.tip.setStyle("display","none");
    },
    showTip:function(){
       PPVACM.tip.setStyle("display","block");
    },
    
    shutCMBox:function(){
        //PPVACM.showFloats(true);
        $(document.body).setStyles({"overflow-y":"auto","overflow-x":"auto"});
        $$("#main .help_left")[0].setStyle("left",153); 
        $("id_light_wrap").setStyle("display","none");
        PPVACM.resetCMInput();
    },
    cancelCMBox:function(){
       PPVACM.shutCMBox();
    },
   
    submitCMBox:function(){
        PPVACM.info.setStyle("display","none");
        if(!PPVACM.checkValidate())
            return;
        // console.log("submitCMBox");
        var chkboxes = PPVACM.checkboxes;
        var arr = [];
        for(var i = 0 ;i<chkboxes.length;i++)
        {
            var chk = chkboxes[i];
            if(chk.get("checked"))arr.push(i);
        }
        $("id_cm").set("value",arr.join(","));
        //PPVACM.info.setStyle("display","none");
        //PPVACM.showTip();
        //PPVACM.startShutTip();
        //PPVACM.shutCMBox(); 
      
        $("id_form_cm").submit();       
    },    
    showErr:function(msg){
        PPVACM.info.setStyle("display","block");
        if(msg)PPVACM.info.set("html",msg);
    },
    checkValidate:function(){
        var inputs = PPVACM.inputs;       
        if(!PPVACM.checkNull()){PPVACM.showErr("请您将必填项填写完整！");return false;}
        if(!PPVACM.checkLength(inputs[0].get("value"),50)){PPVACM.showErr("网站名称过长！");return false;}
        
        if(!PPVACM.checkLength(inputs[1].get("value"),50)){PPVACM.showErr("网站主页地址过长！");return false;}
        if(!PPVACM.checkHome()){PPVACM.showErr("网站主页不合法！例子：http://ppva.pp.tv");return false;}
        
        if(!PPVACM.checkLength(inputs[2].get("value"),200)){PPVACM.showErr("网站口号过长！");return false;}
        if(!PPVACM.checkLength(inputs[3].get("value"),20)){PPVACM.showErr("姓名过长！");return false;}
        if(!PPVACM.checkLength(inputs[4].get("value"),20)){PPVACM.showErr("电话号码过长！");return false;}
        if(!PPVACM.checkLength(inputs[5].get("value"),50)){PPVACM.showErr("邮件地址过长！");return false;}
        if(!PPVACM.checkMail()){PPVACM.showErr("邮箱地址不合法！例子:mail@pplive.com");return false;}        
        if(!PPVACM.checkQQ()){PPVACM.showErr("QQ号码应该是5-13位的数字！");return false;}
        if(!PPVACM.checkLength(inputs[7].get("value"),50)){PPVACM.showErr("MSN地址过长！");return false;}
        if(!PPVACM.checkLength($$("div.layer_wrap .layer_content_left textarea")[0].get("value"),1000)){PPVACM.showErr("备注过长！");return false;}
        
        return true;
    },
    checkLength:function(str,length){
        if(!str || str.trim()=="")return true;
        return str.length<=length;
    },
    checkMail:function(){        
        var reg = /[0-9a-zA-Z_]+@[0-9a-zA-Z_]+\..+/;///w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/;
        var mail = PPVACM.inputs[5].get("value");
        return !mail || reg.test(mail);
    },
    checkQQ:function(){
        var reg = /[1-9][0-9]{4,12}/;        
        var qq = PPVACM.inputs[6].get("value");
        return reg.test(qq);
    },
    
    checkHome:function(){
        var reg = /^http(s)?:\/\/()[0-9a-zA-Z]+\..+/;
        var home = PPVACM.inputs[1].get("value");
        return reg.test(home);
    },
    
    checkNull:function(){
        var inputs = PPVACM.inputs;
        for(var i = 0;i<inputs.length;i++)
        {
            var txt = inputs[i];
            var parent = txt.getParent();
            if(parent)parent = parent.getParent();
            
            if(parent && parent.get("tag")=="tr")
            {
                var red = parent.getElements("i");               
                if(red  && red.length == 1)
                {
                    var v = txt.get("value");
                    if(!v || v.trim() == "")
                    {
                        //console.log("<>"+red.get("html"));
                        return false;
                    }
                }               
            }
        }        
        var chkboxes = PPVACM.checkboxes;
        for(var i = 0;i<chkboxes.length;i++)
        {
            var chk = chkboxes[i];
            if(chk.get("checked"))return true;
        }
        return true;
    },
    showCMBox:function(){
        PPVACM.info.setStyle("display","none");
        
        // PPVACM.showFloats(false);
         $(document.body).setStyles({"overflow-y":"hidden","overflow-x":"hidden"});
         $("id_light_wrap").setStyle("display","block");
        
         var t = $("id_light");
         t.setStyle("display","block");
         t.set("opacity",0);
         t.fade(0.5);
         
         var box = PPVACM.target;
         box.setStyle("display","block");
         box.set("opacity",0);
         box.fade(1);
    },
    getPos:function (obj){
        var pos = new Object();
        pos.x = obj.offsetLeft;
        pos.y = obj.offsetTop;
        while(obj = obj.offsetParent){
             pos.x += obj.offsetLeft;
             pos.y += obj.offsetTop;
        }
        return pos;
    }
};

window.addEvent("domready",function(){      
    PPVACM.init();
});


