var IE = navigator.userAgent.indexOf("MSIE")>0? 1: 0;
var HOST = "http://www.epopos.com";
//var HOST = "http://localhost:81";
var getArgs=(function()
{
    var sc=document.getElementsByTagName('script');
    var paramsArr=sc[sc.length-1].src.split('?')[1].split('&');
    var args={},argsStr=[],param,t,name,value;
    for(var ii=0,len=paramsArr.length;ii<len;ii++)
    {
            param=paramsArr[ii].split('=');
            name=param[0],value=param[1];
            if(typeof args[name]=="undefined")
            { //参数尚不存在
                args[name]=value;
            }
            else if(typeof args[name]=="string")
            { //参数已经存在则保存为数组
                args[name]=[args[name]]
                args[name].push(value);
            }
            else
            {  //已经是数组的
                args[name].push(value);
            }
    }
    return function()
    {
    	return args;
    } //以json格式返回获取的所有参数
})();

var ep = getArgs()["ep"];
var styleType = getArgs()["style"];
var position = getArgs()["position"];
var url = window.location.href;
document.write("<style>");
document.write(".epopos_loading_div {");
	document.write("position: absolute;");
	document.write("z-index:9001;");
	document.write("width: 100%;");
	document.write("height: 100%;");
	document.write("top: 0;");
	document.write("left: 0;");
	document.write("min-height:100%;");
	document.write("background-color:#000000;");
	document.write("filter:alpha(opacity=60);");
	document.write("-moz-opacity: 0.6;");
	document.write("opacity: 0.6;");
	document.write("visibility:visible;");
	document.write("display:block;");
document.write("}");
document.write("</style>");
document.write('<div id="epoposLoadDiv" class="epopos_loading_div" style="display:none;z-index:1000;">&nbsp; </div>');

function startChatWindow()
{
	createEPOPOSWindow("chatWindowWindow",596,488,HOST+"/web800/chatWindow.os?ep="+ep+"&url="+url);
}
var picURL = "";
if(styleType=="one")
{
	picURL = "/web800/pic/EP800_01.png";
}
else if(styleType=="two")
{
	picURL = "/web800/pic/EP800_02.png";
}
else if(styleType=="three")
{
	picURL = "/web800/pic/EP800_03.png";
}
else if(styleType=="four")
{
	picURL = "/web800/pic/EP800_04.png";
}
//左边
var left_img='<a href="javascript:startChatWindow()"><img src='+HOST+picURL+' border=0></a>';

//右边
var right_img='<a href="javascript:startChatWindow()"><img src='+HOST+picURL+' border=0></a>';
var delta=0.05
var collection;
function floaters() 
{
	this.items= [];
	this.addItem= function(id,x,y,content)
	{
		document.write('<DIV id='+id+' style="Z-INDEX: 10; POSITION: absolute;  width:80px; height:60px;left:'+x+';top:'+y+'">'+content+'</DIV>');

		var newItem= {};
		newItem.object= document.getElementById(id);
		newItem.x= x;
		newItem.y= y;

		this.items[this.items.length]= newItem;
	};
	this.play= function()
	{
		collection= this.items
		setInterval('play()',10);
	};
}
function play()
{
	
	for(var i=0;i<collection.length;i++)
	{
		var followObj= collection[i].object;
		var followObj_x= (typeof(collection[i].x)=='string'?eval(collection[i].x):collection[i].x);
		var followObj_y= (typeof(collection[i].y)=='string'?eval(collection[i].y):collection[i].y);
		var scrollTop =document.documentElement.scrollTop||document.body.scrollTop;  
		if(followObj.offsetLeft!=(document.body.scrollLeft+followObj_x))
		{
			
			var dx=(document.body.scrollLeft+followObj_x-followObj.offsetLeft)*delta;
			dx=(dx>0?1:-1)*Math.ceil(Math.abs(dx));
			followObj.style.left=followObj.offsetLeft+dx+"px";
		}

		if(followObj.offsetTop!=(scrollTop+followObj_y)) 
		{
			
			var dy=(scrollTop+followObj_y-followObj.offsetTop)*delta;
		
			dy=(dy>0?1:-1)*Math.ceil(Math.abs(dy));
				
			followObj.style.top=followObj.offsetTop+dy+"px";
		
		}
		if(position=="right")
		{
			followObj.style.left = document.body.clientWidth-220;
		}
		followObj.style.display= '';
	}
	
	setDivCenter(document.getElementById("chatWindowWindow"));
	setCloseDivPosition();
	//setDivCenter(document.getElementById("closeDiv"));
}
var theFloaters= new floaters();
if(position=="left")
	theFloaters.addItem('followDiv2',20,230,''+left_img+'');
else 
	theFloaters.addItem('followDiv1',document.body.clientWidth-220,230,''+right_img+'');
theFloaters.play();


//设置Div居中
function setDivCenter(obj)
{
	if(obj==null||obj==undefined)
		return;
	var cw  =document.documentElement.clientWidth||document.body.clientWidth;
    var ch = document.documentElement.clientHeight||document.body.clientHeight;
	var bodyTopHeight =document.documentElement.scrollTop||document.body.scrollTop;  
	var objWidth = obj.offsetWidth||obj.clientWidth;
	var objHeight = obj.offsetHeight||obj.clientHieght;
	obj.style.top =bodyTopHeight+(ch-objHeight)/2+"px";
	obj.style.left = (cw-objWidth)/2+"px";
	obj.style.visibility="visible";
}

//设置关闭按钮Div
function setCloseDivPosition()
{
	var closeDiv = document.getElementById("epoposCloseDiv");
	if(closeDiv==null)
		return;
	var chatWindowWindow = document.getElementById("chatWindowWindow");
	var st =document.documentElement.scrollTop||document.body.scrollTop;

	closeDiv.style.left = (parseInt(chatWindowWindow.offsetLeft)+553)+"px";
	closeDiv.style.top = (parseInt(chatWindowWindow.offsetTop))+"px";
}
/**
*windId 窗口ID
*width,窗口宽度
*height 窗口高度
*winTitle 窗口标题
*winUrl 窗口链接
*关闭窗口时需要执行的命令,不执行 为空
**/

var zIndex = 5000;
function createEPOPOSWindow(winId,width,height,winUrl)
{
	var myWindow = document.getElementById(winId);
	if(myWindow!=null)
		return;
	var loadDiv = document.getElementById("epoposLoadDiv");
	
   var cw  =document.documentElement.clientWidth||document.body.clientWidth;
    var ch = document.documentElement.clientHeight||document.body.clientHeight;
	//var cw = document.documentElement.clientWidth;
	//var ch = document.documentElement.clientHeight;
	var st =document.documentElement.scrollTop||document.body.scrollTop;
	loadDiv.style.top=0;
	loadDiv.style.zIndex = zIndex++;
	loadDiv.style.height = document.documentElement.scrollHeight+"px";
	loadDiv.style.display = "block";
	this.startX = (cw-width)/2;
	if(startX<0)
		startX=0;
	this.startY= (ch-height)/2;
	if(startY<=0)
		startY=0;
	var newWindow=document.createElement("div");
	newWindow.style.position ="absolute";
	newWindow.id = winId;	
	newWindow.style.zIndex = zIndex++;
	newWindow.onmouseover=function()
	{
		//drag(winId,1)	
	}
	
	 document.body.appendChild(newWindow);
	

	 newWindow.style.left = startX+"px";
	 newWindow.style.top = (startY+st)+"px";
	 newWindow.style.height = height+100;
	 newWindow.style.width = width;

	var htmlStr='<iframe src="'+winUrl+'" id="PF'+winId+'" name="PF'+winId+'" width="'+width+'" height="'+height+'" frameborder="0" scrolling="no" allowTransparency="true"></iframe>';

	newWindow.innerHTML = htmlStr;
	
	/***窗口关闭按钮****/
	var closeDiv = document.createElement("div");
	closeDiv.style.position ="absolute";
	closeDiv.style.zIndex = zIndex++;
	closeDiv.innerHTML='<img src="'+HOST+'/web800/images/but_close.png" style="cursor:pointer" title="Close" onclick="closeEPOPOSWindow(\'chatWindowWindow\')"/>';
	closeDiv.width = 36;
	closeDiv.height = 19;
	closeDiv.style.left = (startX+553)+"px";
	closeDiv.style.top = (startY+st-50)+"px";
	closeDiv.id = "epoposCloseDiv";
	 document.body.appendChild(closeDiv);
	//if(IE)
		//enablePngImages();
}
//关闭窗口
function closeEPOPOSWindow(winId)
{
	var myWindow = document.getElementById(winId);
	myWindow.style.display = "none";
	var popFrame = document.getElementById("PF"+winId);
	popFrame.src = "";
	var loadDiv = document.getElementById("epoposLoadDiv");
	loadDiv.style.display = "none";
	var epoposCloseDiv = document.getElementById("epoposCloseDiv");
	document.body.removeChild(myWindow);
	document.body.removeChild(epoposCloseDiv);
	
}

