var bookIndex = {};
bookIndex.fun = (function(){
	var oVersion = oMail = oSubscribe = oUnsubscribe = oInfoBox = oDiv = null,oForm=null,oUnInfoBox=null;
			 url = '', off = false,oExit=null;
			 
	var setCloseDivs=function()
	{
		var oCloses = [oInfoBox.getElementsByTagName('h3')[0], oConfirmInfoBox.getElementsByTagName('h3')[0], oUnInfoBox.getElementsByTagName('h3')[0]],
			oClose_len = oCloses.length;
			
		for(var i=0;i<oClose_len;i++)
		{
			if(oCloses[i].getElementsByTagName('a')[0]){
				oCloses[i].getElementsByTagName('a')[0].onclick = function(event)
				{
					var evt = event || window.event;
					
					//找到离onClose最近的接点，为div.就可以设置事件关闭。
					oDiv = this.parentNode.parentNode;
					
					oDiv.style.left = '-99999px';
					oDiv.style.top = '-99999px';
					evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
				}
			}
		}
		
	}
					
			 
	// 显示订阅或退订信息
	var showSubscribeInfo = function(btn,text,status){
			var oClose = document.getElementById('close'),
				oExit = document.getElementById('exit'),
				
				parent = btn.parentNode,
				x = parent.offsetLeft,
				y = parent.offsetTop;

		if(!oMail.value){
			alert(SPS.text('MAIL_SUBSCRIBE_NOVALUE'));
		}else{
			
				if(document.all){
					//if(parent.className != 'version'){
						x+= parent.offsetLeft;
						y+= btn.offsetTop + parent.offsetTop + 10;
					//}else{
						//break;
					//}
				}else{
					while(parent = parent.parentNode){
					if(parent.className != 'version'){
						x+= parent.offsetLeft;
						y+= parent.offsetTop;
					}else{
						break;
					}
					}
				}
			
			if(status === 2){	// 当前选中版本已经订阅过
				oInfo = oInfoBox.getElementsByTagName('span')[0],
				oInfoBox.style.left = x + 'px';
				oInfoBox.style.top = y + btn.offsetHeight + 5 + 'px';
				oInfo.innerHTML = text;
			}else if(status === 1){	// 已经订阅另外一个版本，是否确认订阅当前版本
				oInfo = oConfirmInfoBox.getElementsByTagName('span')[0];
				oOK = document.getElementById('OK');
				oCancel = document.getElementById('cancel');
				oConfirmInfoBox.style.left = x + 'px';
				oConfirmInfoBox.style.top = y + btn.offsetHeight + 5 + 'px';
				oInfo.innerHTML = text;

				
				oOK.onclick = function(){
					oForm.submit();
					oConfirmInfoBox.style.left = '-99999px';
					oConfirmInfoBox.style.top = '-99999px';
					
					
				}
				oCancel.onclick = function(){
					oConfirmInfoBox.style.left = '-99999px';
					oConfirmInfoBox.style.top = '-99999px';					
				}
			}
		}
		
		oExit = document.getElementById('exit');
		oExit.onclick = function(){
			oInfoBox.style.left = '-99999px';
			oInfoBox.style.top = '-99999px';
		}
	};
	
	var showUnsubscribeInfo = function(btn,text,status)
	{
		
		var  oExit = document.getElementById('unOk'),
		parent = btn.parentNode,
		x = parent.offsetLeft,
		y = parent.offsetTop;

		if(!oMail.value)
		{
			alert(SPS.text('MAIL_SUBSCRIBE_NOVALUE'));
		}else
		{
			if(document.all){
				//if(parent.className != 'version'){
					x+= parent.offsetLeft;
					y+= btn.offsetTop + parent.offsetTop + 10;
			}else{
				while(parent = parent.parentNode){
				if(parent.className != 'version'){
					x+= parent.offsetLeft;
					y+= parent.offsetTop;
				}else{
					break;
				}
				}
			}
			if(status == 0)
			{	// 当前选中版本已经取消过了。
			    oUnInfo = oUnInfoBox.getElementsByTagName('span')[0],
				oUnInfoBox.style.left = x + 'px';
			   // console.log(oUnInfo);
				oUnInfoBox.style.top = y + btn.offsetHeight + 5 + 'px';
				oUnInfo.innerHTML = text;
			}
			var  oExit = document.getElementById('unOk');
			oExit.onclick = function(){
				oUnInfoBox.style.left = '-99999px';
				oUnInfoBox.style.top = '-99999px';
			}
		}
	};

	// 订阅
	var setSubscribe = function(){
	
		var vMail = oMail.value,
				oVersion_len = oVersion.length,
				vVersion = '', 
				oBtn = this,
				formAction = oBtn.getAttribute('act'),
				url = oBtn.getAttribute('ajax'),
				trim = /(^\s*)|(\s*$)/g,
				mailPatten =  /^\w+([-+.]\w+)*@\w+\.\w+(\.\w+)*$/;
				
				oForm.action=formAction;
				vMail = vMail.replace(trim,'');
		if(!vMail || vMail == '请输入您的邮箱' || vMail == 'Enter your email'){
			alert(SPS.text('MAIL_SUBSCRIBE_NOVALUE'));
			return;
		}
		
		if(!mailPatten.test(vMail)){
			alert(SPS.text('MAIL_ADDRESS_FORMAT'));
			return;
		}
		
		for(var i = 0; i < oVersion_len; i++){
			if(oVersion[i].checked){
				vVersion = oVersion[i].value;
				break;
			}
		}
		var ajax = $Ajax(url,{
			type: 'post',
			onload : function(originalRequest)
			{
					//console.log(originalRequest);
					var info = originalRequest.text();
					if(parseInt(info) == 0){
						oForm.submit();
					}else if(parseInt(info) === 1){
						showSubscribeInfo(oBtn,SPS.text('MAIL_SUBSCRIBE_DIFFERENTVERSION'),parseInt(info));
					}else if(parseInt(info) === 2){
						showSubscribeInfo(oBtn,SPS.text('MAIL_SUBSCRIBE_SAMEVERSION'),parseInt(info));
					}
				
		}

		});
		//window.alert("kiashi request");
		ajax.request({version:vVersion,email:vMail});//传递参数.
		
	};
	// 退订
	var setUnsubscribe = function()
	{
		var vMail = oMail.value,
			oVersion_len = oVersion.length,
			vVersion = '',
			oBtn = this,
			formAction = oBtn.getAttribute('act'),
			url = oBtn.getAttribute('ajax'),
			trim = /(^\s*)|(\s*$)/g,
			mailPatten = /^\w+([-+.]\w+)*@\w+\.\w+(\.\w+)*$/;
		
		oForm.action=formAction;
		vMail = vMail.replace(trim,'');
		if(!vMail || vMail == '请输入您的邮箱' || vMail == 'Enter your email'){
			alert(SPS.text('MAIL_SUBSCRIBE_NOVALUE'));
			return;
		}else if(!mailPatten.test(vMail)){
			alert(SPS.text('MAIL_ADDRESS_FORMAT'));
			return;
		}
		for(var i = 0; i < oVersion_len; i++){
			if(oVersion[i].checked){
				vVersion = oVersion[i].value;
				break;
			}
		}
		
		var ajax = $Ajax(url,{
			type: 'post',
			onload : function(originalRequest)
			{
					var info = originalRequest.text();
					//alert(info);
					if(parseInt(info)==0)//没有要取消的订阅信息
					{
						
						showUnsubscribeInfo(oBtn,SPS.text('MAIL_UNSUBSCRIBE_NOMAIL'),parseInt(info));
					}else
					{
						oForm.submit();
					}
				
		}

		});
		ajax.request({version:vVersion,email:vMail});//传递参数.
	};
		
	return{
		init: function(version_name,mail_id,subscribe_id,unsubscrib_id,infoBox_id,confirmInfoBox_id,form_id,unInfoBox_id,ViewVersion_id){
		oVersion = document.getElementsByName(version_name);
		oMail = document.getElementById(mail_id);
		oSubscribe = document.getElementById(subscribe_id);
		oUnsubscribe = document.getElementById(unsubscrib_id);
		oInfoBox = document.getElementById(infoBox_id);
		oConfirmInfoBox = document.getElementById(confirmInfoBox_id);
		oForm=document.getElementById(form_id);
		oUnInfoBox=document.getElementById(unInfoBox_id);
		
	},
		subscribe: function()
		{
			oSubscribe.onclick = setSubscribe;		
				
			oMail.onfocus = function()
			{
				oInfoBox.style.left = '-99999px';
				oInfoBox.style.top = '-99999px';
				oConfirmInfoBox.style.left = '-99999px';
				oConfirmInfoBox.style.top = '-99999px';
				if(!off){
					this.value = '';
					off = true;
				}
			}
		},
		unsubscribe : function()
		{
			oUnsubscribe.onclick = setUnsubscribe;
		},
		setClose:function()
		{
			setCloseDivs();
		}
		
	}
})();