﻿var ChinaEC = {};
ChinaEC.Config={
    Domain:"anibook.cn",
    Url:"http://www.anibook.cn",
    XUrl:"/ServerUpdate/",
    WebName:"ANI图书网",
    Spread:"SpreaderId"
};
ChinaEC.Head = {
	WebHeadMenu:function(){
		var Links = document.GetElementsByTagName("WebRootMenu","A");
		if(Links.length > 0){
			Links.Each(function(a){
				ChinaEC.Head.changeHeadMenu.Attach(a,"mouseover");
				ChinaEC.Head.changeHeadMenu.Attach(a,"mouseout");
			});
		}
		Links = null;
		ChinaEC.Head.InitBestBook();
	},
	changeHeadMenu:function(){
	    var event = Event.Current();
		var menu = Event.Element(event);
		Event.Stop(event);
		var css = null;
		if(!Object.isNull(menu)){
			css = menu.className;
			try{
				if(css.Contains("Out")){menu.className = css.replace("Out","On");}
				else{menu.className = css.replace("On","Out");}
			}
			catch(zhj){}
			css = null;
		}
		menu = null;
		return(false);
	},
	searchInputFocus:function(Input){
		if(Input.value == "请输入关键字"){
			Input.value = "";
		}
		Input.style.color = "";
		return(false);
	},
	searchInputBlur:function(Input){
		if(Input.value == "请输入关键字" || Input.value == ""){
			Input.value = "请输入关键字";
			Input.style.color = "#999";
		}
		else{
			Input.value = Input.value.StripBads();
		}
		return(false);
	},
	checkSearch:function(){},
	theFormSubmit:function(){
        var theForm = document.forms['aniForm'];
        if (!theForm) theForm = document.aniForm;
        theForm.submit();
    },
    InitBestBook: function(){
        var ads = document.GetElementsByTagName("HeadBestBook","IMG");
        var rand = Object.Rand(0, (ads.length - 1));
        ads.EachIndex(
            function(ad, i){
                if(i == rand) ad.style.display = 'block';
                else ad.style.display = 'none';
                return true;
            }
        );
    }
};
ChinaEC.Member = {
    Validate: function(){new Request.Update({Url:(ChinaEC.Config.Url + ChinaEC.Config.XUrl + AniBookXPath.MemberValidate),Params:{Validate:Object.Rand(100000000,999999999)},Element:"UserLoginState"}).Execute();},
    LoginOut: function(){new Request.Update({Url:(ChinaEC.Config.Url + ChinaEC.Config.XUrl + AniBookXPath.MemberLoginOut),Params:{LoginOut:Object.Rand(100000000,999999999)},Element:"UserLoginState"}).Execute();},
    Spreader:function(memberId){
        if(!Regex.Register(Regex.String.Int,'').test(memberId)) return;
        var paras = {SpreaderId:memberId,Referrer:(((document.referrer != null) && (document.referrer != '')) ? document.referrer.UrlEncode() : ''),PageUrl:document.location.href.UrlEncode()};
        new Request.Update({Url:(ChinaEC.Config.Url + ChinaEC.Config.XUrl + AniBookXPath.SpreaderCount),Params:paras,Element:"EmptyElement"}).Execute();
    }
};
ChinaEC.MoveLayer = Class.Create();
ChinaEC.MoveLayer.prototype = {
    initialize:function(options){
        if(Object.isNull(options.element) || Object.isNull(options.topelement)){return;}
        this.element = $(options.element);
        this.TopElement = $(options.topelement);
        this.LeftPosition = 0;
        this.LastPosition = 0;
        this.element.style.top = "40px";
        this.ChangPageSize();
        this.ChangPageSize.Bind(this).Attach(window,"resize");
        this.timer = new Execute(this.ChangeLayer.Bind(this),0.05);
    },
    ChangPageSize:function(){
        this.LeftPosition = Event.Position(this.TopElement).X;
        this.element.style.left = (this.LeftPosition + 965) + "px";
    },
    ChangeLayer:function(){
        var top = document.Extend.scrollTop();
        var percent = 0;
		if (top != this.LastPosition) {
			percent = 0.2 * (top - this.LastPosition);
			percent = (percent > 0)?Math.ceil(percent):Math.floor(percent);
			this.element.style.top = ((this.element.style.top).toFloat() + percent) + "px";
			this.LastPosition += percent;
		}
    }
};
ChinaEC.AjaxError = function(err){
    if(!err) return;
    switch(err){
        case 403 :
            ChinaEC.Effects.ModalPopup.Alert("你请求的资源没有权限访问！错误代码："+ err +"，请联系管理员解决！");
            break;
        case 404 :
            ChinaEC.Effects.ModalPopup.Alert("你请求的资源不存在或已经删除！错误代码："+ err +"，请联系管理员解决！");
            break;
        case 500 :
            ChinaEC.Effects.ModalPopup.Alert("你请求的资源存在程序内部错误！错误代码："+ err +"，请联系管理员解决！");
            break;
        case 505 :
            ChinaEC.Effects.ModalPopup.Alert("你请求的资源存在严重错误！错误代码："+ err +"，请联系管理员解决！");
            break;
        default :
            ChinaEC.Effects.ModalPopup.Alert("你请求的资源发生未知错误！错误代码："+ err +"，请联系管理员解决！");
            break;
    }
};
ChinaEC.Effects={}
ChinaEC.Effects.ProductSlide = Class.Create()
ChinaEC.Effects.ProductSlide.prototype = {
    initialize: function(parentElement, titleStyle, contentStyle){
        var pElement = $(parentElement);
        this.titleElements = $A(document.getElementsByClassName(pElement, titleStyle));
        this.contentElements = $A(document.getElementsByClassName(pElement, contentStyle));
        this.titleIdRegex = null;
        this.contentIdRegex = null;
    },
    Execute: function(){
        if((this.titleIdRegex == null) || (this.contentIdRegex == null)) return;
        var This = this;
        this.titleElements.Each(function(element){if(element) This.BindSlide.Bind(This).Attach(element,"mouseover"); return true;});
    },
    BindSlide: function(){
        var element = Event.Element(Event.Current());
        if((!element) || (element.tagName != "DIV")) 
            element = Event.FindElement(element,"DIV");
        if(!element) return;
        var tid = element.getAttribute("id").replace(this.titleIdRegex,"$1");
        var cid = "";
        var This = this;
        this.contentElements.Each(
            function(content){
                cid = content.getAttribute("id").replace(This.contentIdRegex,"$1");
                if(cid == tid) content.style.display = "block";
                else content.style.display = "none";
            }
        );
    }
};
ChinaEC.Effects.ModalPopup = {
    Background:{
        Element: "ModalPopupBackground",
        Open: function(){$(this.Element).FillFullScreen().Show();},
        Close: function(){$(this.Element).Hide();}
    },
    Element:{
        WindowID : "aNiWindow",
        Title    : "WindowTitleText",
        Content  : "WindowContent",
        Close    : "WindowCloseButton"
    },
    isRun:false,
    Alert:function(msg){
        this.Write({Title:"温馨提示",Content:msg});
        if(!this.isRun) this.Open();
    },
    Write:function(HTMLcollection){
        $(this.Element.Title).HTML(HTMLcollection.Title);
        $(this.Element.Content).HTML(HTMLcollection.Content);
    },
    Attach:function(){ChinaEC.Effects.ModalPopup.Position.Attach(window, "scroll");},
    Detach:function(){while(!Object.isNull(window.onscroll)) ChinaEC.Effects.ModalPopup.Position.Detach(window, "scroll");},
    Open:function(){
        this.isRun = true;
        $A(document.getElementsByTagName('select')).Each(function(element){element.style.visibility='hidden';return true;});
        this.Position();
        this.Attach();
    },
    Close:function(x){
        $(this.Element.WindowID).Style({left:"-1024px", visibility:"hidden"});
		this.Background.Close();
		$A(document.getElementsByTagName('select')).Each(function(element){element.style.visibility='visible';return true;});
		this.isRun = false;
		this.Detach();
		return x;
    },
    Position:function(){
        if(ChinaEC.Effects.ModalPopup.isRun){
            var element = $(ChinaEC.Effects.ModalPopup.Element.WindowID);
            var bound = Object.Bounds();
            var size = element.Size();
            element.Style({
                left:(Math.min(bound.Width / 2 - size.Width / 2) - 30) + "px",
                top:(document.Extend.scrollTop() + Math.min(bound.Height / 2 - size.Height / 2)) + "px",
                visibility:'visible'
            });
            ChinaEC.Effects.ModalPopup.Background.Open();
        }
    }
};
//var ClosePopup = ChinaEC.Effects.ModalPopup.Close;
ChinaEC.Effects.ProgressBar = {
    ElementId:'ProgressBar',
    Opened:false,
    Timer:null,
    Open: function(){
        if(ChinaEC.Effects.ProgressBar.Opened) return;
        else ChinaEC.Effects.ProgressBar.Opened = true;
        ChinaEC.Effects.ProgressBar.Timer = window.setInterval(ChinaEC.Effects.ProgressBar.SetPosition,1);
    },
    Close: function(){
        if(ChinaEC.Effects.ProgressBar.Timer != null) window.clearInterval(ChinaEC.Effects.ProgressBar.Timer);
       $(ChinaEC.Effects.ProgressBar.ElementId).Style({left:"-1000px"});
       ChinaEC.Effects.ProgressBar.Opened = false;
    },
    SetPosition: function(){
        var element = $(ChinaEC.Effects.ProgressBar.ElementId);
        var bound = Object.Bounds();
        var size = element.Size();
		element.Style({
            left:(bound.Width - (size.Width + 40)) + "px",
            top:(document.Extend.scrollTop() + 15) + "px"
        });
    },
    HTML:function(){
        return "<div id=\""+ ChinaEC.Effects.ProgressBar.ElementId +"\" style=\"width:120px; top:15px; left:-1000px; position:absolute; z-index:9999;\" class=\"border\">"+ ChinaEC.Effects.ProgressBar.HtmlText +"</div>";
    },
    HtmlText: "<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" style=\"background-color:White;\"><tr><td align=\"center\"><img src=\""+ChinaEC.Config.Url+"/Images/Icon/loading_small.gif\" border=\"0\" /></td><td align=\"left\" style=\"color:Red; font-size:13px; font-family:Arial, '宋体', sans-serif;\">数据处理中...</td></tr></table>"
};
ChinaEC.Shopping = {
    Type:{Cart:1, Wish:2},
    Insert: function(params){
        new Request.Update({
            Url      : (ChinaEC.Config.Url + ChinaEC.Config.XUrl + AniBookXPath.Shopping), 
            Params   : Request.Query.ToHash(params), 
            Element  : ChinaEC.Effects.ModalPopup.Element.Content, 
            Method   : Request.Enums.Method.Post, 
            Begin    : ChinaEC.Effects.ProgressBar.Open, 
            Finish   : ChinaEC.Effects.ProgressBar.Close, 
            OnError  : ChinaEC.AjaxError
        }).Execute();
    }
};