﻿function hideCalEventDiv(){
	if(window['calEventFac_popupDiv']){
		window['calEventFac_popupDiv'].style.visibility = 'hidden';
		window['calEventFac_popupDiv'].eventId = null;
	}
}
function buildCalendarEventDiv(){
	var div = document.createElement('div');
	div.id = 'calEventFac_popupDiv';
	div.className = 'popupDiv_event';
	div.eventId = null;
	myAttachEvent(div,'click',cancelBubble);
	var subDiv = document.createElement('div');
	subDiv.className = 'loadingCon';
	//subDiv.style.display = 'none';
	div.loadingDiv = subDiv;
	div.appendChild(subDiv);
	subDiv = document.createElement('div');
	subDiv.className = 'ajaxCon';
	subDiv.style.display = 'none';
	div.ajaxDiv = subDiv;
	div.appendChild(subDiv);
	subDiv = document.createElement('div');
	subDiv.className = 'floatRight closeEvent';
	subDiv.title = '关闭';
	myAttachEvent(subDiv,'click',hideCalEventDiv);
	div.ajaxDiv.appendChild(subDiv);
	subDiv = document.createElement('div');
	subDiv.className = 'clearAll';
	div.ajaxDiv.appendChild(subDiv);
	subDiv = document.createElement('div');
	div.ajaxDiv_sub = subDiv;
	div.ajaxDiv.appendChild(subDiv);
	document.body.appendChild(div);
	return div;
}

function calEventFac(h){
	var s = this;
	s.html = h;
	s.id = s.html.id;
	s.html.style.cursor = 'pointer';
	if(!window['calEventFac_popupDiv']) window['calEventFac_popupDiv'] = buildCalendarEventDiv();
	s.div = window['calEventFac_popupDiv'];
	s.divW = parseFloat(getCurrentStyle(s.div,'width'));
	s.divH = parseFloat(getCurrentStyle(s.div,'height'));
	s.loadingDiv = s.div.loadingDiv;
	s.ajaxDiv = s.div.ajaxDiv;
	s.ajaxDiv_sub = s.div.ajaxDiv_sub;
	s.eventId = s.html.getAttribute('eventId')||null;
	s.ajaxState = 'ok';
	s.ajaxResObj = null;
	if(s.eventId){
		s.fnC = function(e){s.fnClick(e);};
		myAttachEvent(s.html,'click',s.fnC);
		if(s.html.parentNode.className.indexOf('greyBg')>-1) s.fnOver = function(e){s.fnMouseOver_g(e);};
		else if(s.html.parentNode.className.indexOf('yellowBg')>-1) s.fnOver = function(e){s.fnMouseOver_y(e);};
		s.fnOut = function(e){s.fnMouseOut(e);};
		myAttachEvent(s.html,'mouseover',s.fnOver);
		myAttachEvent(s.html,'mouseout',s.fnOut);
	}
}
calEventFac.prototype = {
	fnClick : function(e){
		var s = this;
		cancelBubble(e);
		if(s.div.eventId!=s.eventId){
			if(s.div.eventId!=null) hideCalEventDiv();
			s.showEvent(e);
		}
		else hideCalEventDiv();
	},
	showEvent : function(e){
		var s = this;
		var pointer = s.fixXY();
		if(s.loadingDiv.style.display=='none') s.loadingDiv.style.display = '';
		if(s.ajaxDiv.style.display!='none') s.ajaxDiv.style.display = 'none';
		//showPopup(s.div.id,e,pointer.x,pointer.y+s.html.offsetHeight);
		s.div.style.top = pointer.y + 'px';
		s.div.style.left = pointer.x + 'px';
		s.div.style.visibility = 'visible';
		s.div.eventId = s.eventId;
		s.writeEventHTML();
	},
	fixXY : function(){
		var s = this;
		var bgV = 'Top';
		var bgH = 'Left';
		var fixParScr = s.html.parentNode.parentNode.scrollTop;
		var pointer = getXY(s.html);
		pointer.y += s.html.offsetHeight - 10 - fixParScr; 
		var winWH = getWindowWH();
		var wH = winWH.h,wW = winWH.w;
		var scrollTop = document.documentElement.scrollTop||document.body.scrollTop,scrollLeft = document.documentElement.scrollLeft||document.body.scrollLeft;
		if(pointer.x + s.divW > wW + scrollLeft){
			pointer.x = pointer.x + s.html.offsetWidth - s.divW;
			bgH = 'Right';
		}
		if(pointer.y + s.divH > wH + scrollTop){
			pointer.y = pointer.y - s.html.offsetHeight + 20 - s.divH;
			bgV = 'Bottom';
		}
		pointer.x = pointer.x<0?0:pointer.x;
		pointer.y = pointer.y<0?0:pointer.y;
		s.div.className = 'popupDiv_event bgImg' + bgV + bgH;
		return pointer;
	},
	writeEventHTML : function(){
		var s = this;
		if(s.ajaxResObj!=null){
			if(s.ajaxState=='ajax') return;
			s.ajaxDiv_sub.innerHTML = s.ajaxResObj;
			if(s.loadingDiv.style.display!='none') s.loadingDiv.style.display = 'none';
			if(s.ajaxDiv.style.display=='none') s.ajaxDiv.style.display = '';
		}
		else s.gotoAjax();
	},
	gotoAjax : function(){
		var s = this;
		if(s.ajaxState=='ajax') return;
		s.ajaxState = 'ajax';
		var t=new Date().getTime();
		var data = 'eventid='+s.eventId + '&t=' + t;
		submitCallback(data,'ajax_ShowEvent.aspx',handleCalEventResponse,'get',s);
		//test(data,'xxx.asp',handleCalEventResponse,'get',s);
	},
	fnMouseOver_g :function(e){
		var s = this;
		s.html.className = 'greyEve';
	},
	fnMouseOver_y : function(e){
		var s = this;
		s.html.className = 'yellowEve';
	},
	fnMouseOut : function(e){
		var s = this;
		s.html.className = '';
	}
}
function test(a,b,c,d,e){
	c(true,"<div class='contentDiv'><b>策略咨询</b><br>4月31日（周一）<br>在此您可选择感兴趣的SAP会员视频讲座系列以及会员专享活动，详细解读SAP 解决方案将<br>在此您可选择感兴趣的SAP会员视频讲座系列以及会员专享活动，详细解读SAP <br>在此您可选择感兴趣的SAP会员视频讲座系列以及会员专享活动，详细解读SAP <br>在此您可选择感兴趣的SAP会员视频讲座系列以及会员专享活动，详细解读SAP <br>在此您可选择感兴趣的SAP会员视频讲座系列以及会员专享活动，详细解读SAP <br>在此您可选择感兴趣的SAP会员视频讲座系列以及会员专享活动，详细解读SAP </div><div class='greyLineH'></div><div><div class='blueButton floatLeft' style='margin-right:10px;'><div class='leftBorder'></div><div class='center'><a href='download.asp?id=10250' target='_blank'>资料下载</a></div><div class='rightBorder'></div></div><div class='blueButton floatLeft'><div class='leftBorder'></div><div class='center'><a href='javascript:fnCalEventAJAX(&quot;"+e.id+"&quot;,&quot;&quot;)'>我要申请</a></div><div class='rightBorder'></div></div></div>",e);
}
function handleCalEventResponse(success,responseText,objPar){
	objPar.ajaxState = 'ok';
	//if(success) objPar.ajaxResObj = responseText;
	if(window['calEventFac_popupDiv'].eventId==objPar.eventId){
		if(success){
			objPar.ajaxDiv_sub.innerHTML = responseText;
		}
		else{//ajax error
			objPar.ajaxDiv_sub.innerHTML = 'ajax出错，请稍后再试！';
		}
		if(objPar.loadingDiv.style.display!='none') objPar.loadingDiv.style.display = 'none';
		if(objPar.ajaxDiv.style.display=='none') objPar.ajaxDiv.style.display = '';
	}
}

function fnCalEventAJAX(objId,ajaxArgu){
	var obj = window['sapModuleIns'][objId];
	if(obj){
		obj.loadingDiv.style.display = '';
		obj.ajaxDiv.style.display = 'none';
		var data = 'eventid='+ajaxArgu;
		obj.ajaxState = 'ajax';
		submitCallback(data,'ajax_ApplEvent.aspx',handleCalEventAJAXResponse,'get',obj);
		//test2(data,'xxx.asp',handleCalEventAJAXResponse,'get',obj);
	}
}
function test2(a,b,c,d,e){
	c(true,"<div class='contentDiv'><b>参加成功</b><br>确认信息<br>提供URL密码等参加信息<br>已经成功加载到我的日历中</div><div class='greyLineH'></div><div><div class='yellowButton' style='margin-left:110px;'><div class='leftBorder'></div><div class='center'><a href='#' onclick='hideCalEventDiv();return false;'>确定</a></div><div class='rightBorder'></div><div class='clearAll'></div></div></div>",e);
}
function handleCalEventAJAXResponse(success,responseText,objPar){
	objPar.ajaxState = 'ok';
	if(window['calEventFac_popupDiv'].eventId==objPar.eventId){
		if(success){
		    if (responseText.substring(0,5)=="reg_T")
		    {
		        alert('请先补充填写相关信息，再继续参加您感兴趣的活动。'); 
		        document.location.href=responseText;
		    }
		    else
		    {
			    objPar.ajaxDiv_sub.innerHTML = responseText;
			}
		}
		else{//ajax error
			objPar.ajaxDiv_sub.innerHTML = 'ajax出错，请稍后再试！';
		}
		if(objPar.loadingDiv.style.display!='none') objPar.loadingDiv.style.display = 'none';
		if(objPar.ajaxDiv.style.display=='none') objPar.ajaxDiv.style.display = '';
	}
}

myAttachEvent(document,'click',hideCalEventDiv);
