var mouseX=0,mouseY=0,dragTag;

var LoginState  =   CAS_LOGIN_STATE;

var MyGameArea = -9999;
var MyGameGroup = -9999;

if(typeof(top.GameGroup)!="undefined"){
		MyGameGroup = top.GameGroup;
}

if(typeof(top.GameArea)!="undefined"){
		MyGameArea = top.GameArea;
}

window.onload=function(){
    
    initializeLinkPara();

    var Para_game   =   0;
    touchPara("game");
    
    var APPPath = "default";
	var Cas	=	"https://cas.sdo.com";

    if(Para_game==569){
    		APPPath = "warlord";
    }
   
    function touchPara(val){
        var isExist =false;
        var   rl=   "isExist = (typeof("+val+")!='undefined')?true:false;";
        eval(rl);
         if(isExist){
            var runingstring    =   "Para_"+val+"   =   "+val+";";
            eval(runingstring);
        }
    }
    function initializeLinkPara(){
        var selftag = document.getElementById("sndawidget");
	    //debugger;
        QueryString= selftag.src.substring(selftag.src.lastIndexOf("?")+1,selftag.src.length); 
        tmpArr=QueryString.split("&"); 
        //alert("QueryString:"+QueryString);
        for (i=0;i<=tmpArr.length;i++){
            try{
                eval (tmpArr[i]);
            }catch(e){ 
                var re = new RegExp("(.*)=(.*)","ig"); 
                re.exec(tmpArr[i]); 
                try{eval (RegExp.$1+"="+"\""+RegExp.$2+"\"");} 
                catch(e){} 
            }
        }    
    }
  
  var mouseX=0,mouseY=0,dragTag;//3
  var newdiv    =   document.createElement("div");
  newdiv.setAttribute("id","move");
  
  //var currentUrl	=	this.location;
	//remove cas ticket.using page redirect
	var currentUrl	=	"http://webqft.sdo.com/redirect.asp?type="+Para_game;
	//var currentUrl	=	"http://pt.qftdev.sdo.com/webwidget/redirect.asp?type="+Para_game;


/*Items Define*/
//var login		= "<div class=\"login\"><a href=\""+Cas+"/cas/login?service="+currentUrl+"\" title=\"\u767B\u5F55\" class=\"link\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32login.gif\"></a></div><div class=\"hr_1\"></div>";
//var register	= "<div class=\"register\"><a href=\"javascript:void(0);\" title=\"\u6CE8\u518C\" class=\"link\" onclick=\"winopen('http://register.sdo.com/sdo/reg/inputinfo1.asp?from="+Para_game+"')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32register.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
//
//var cc		="<div class=\"cc\"><a href=\"javascript:void(0);\" title=\"\u5145\u503C\" class=\"link\" onclick=\"winopen('http://dev.pay.sdo.com/index.aspx')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32cc.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
//var check	="<div class=\"check\"><a href=\"javascript:void(0);\" title=\"\u4F59\u989D\u67E5\u8BE2\" class=\"link\" onclick=\"window.open('http://61.172.247.108/PayNet/PtPay/PtBalance.aspx','','width=570,height=360,scrollbars=yes,resizable=yes');\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32check.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
//var passport="<div class=\"passport\"><a href=\"javascript:void(0);\" title=\"\u901A\u884C\u8BC1\" class=\"link\" onclick=\"winopen('http://pwd.sdo.com')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32passport.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
//var novel	= "<div class=\"novel\"><a href=\"javascript:void(0);\" title=\"\u8D77\u70B9\" class=\"link\" onclick=\"winopen('http://www.qidian.com/CASIndex.aspx')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32XS.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
//var jf		="<div class=\"jf\"><a href=\"javascript:void(0);\" title=\"\u79EF\u5206\" class=\"link\" onclick=\"winopen('http://pay.qftdev.sdo.com/ptpay/ptpay/TokenOrder.aspx')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32SC.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
//var kf		="<div class=\"kf\"><a href=\"javascript:void(0);\" title=\"\u5BA2\u670D\" class=\"link\" onclick=\"winopen('http://kf.sdo.com/sdch/index.asp')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32kf.png\" class=\"transparent\"/></a></div>";
//var zz		="<div class=\"zz\"><a href=\"javascript:void(0);\" title=\"\u5212\u8D26\" class=\"link\" onclick=\"window.open('http://61.172.247.108/OldDirectPay/ingame/ptbpay.aspx?game="+Para_game+"&gamearea=-1&gameserver=-1','','width=570,height=360,scrollbars=yes,resizable=yes');\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32zz.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";

var login		= "<div class=\"login\"><a href=\"https://cas.sdo.com/cas/login?service="+currentUrl+"\" title=\"\u767B\u5F55\" class=\"link\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32login.gif\"></a></div><div class=\"hr_1\"></div>";
var register	= "<div class=\"register\"><a href=\"javascript:void(0);\" title=\"\u6CE8\u518C\" class=\"link\" onclick=\"winopen('http://register.sdo.com/sdo/reg/inputinfo1.asp?from="+Para_game+"')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32register.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";

var cc		="<div class=\"cc\"><a href=\"javascript:void(0);\" title=\"\u5145\u503C\" class=\"link\" onclick=\"winopen('http://pay.sdo.com/index.aspx')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32cc.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
var check	="<div class=\"check\"><a href=\"javascript:void(0);\" title=\"\u4F59\u989D\u67E5\u8BE2\" class=\"link\" onclick=\"window.open('http://pay.16288.com/PtPay/PtBalance.aspx','','width=570,height=360,scrollbars=yes,resizable=yes');\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32check.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
var passport="<div class=\"passport\"><a href=\"javascript:void(0);\" title=\"\u901A\u884C\u8BC1\" class=\"link\" onclick=\"winopen('http://pwd.sdo.com')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32passport.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
var novel	="<div class=\"novel\"><a href=\"javascript:void(0);\" title=\"\u8D77\u70B9\" class=\"link\" onclick=\"winopen('http://www.qidian.com/CASIndex.aspx')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32XS.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
var jf		="<div class=\"jf\"><a href=\"javascript:void(0);\" title=\"\u79EF\u5206\" class=\"link\" onclick=\"winopen('http://jf.sdo.com/jfIndex.aspx')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32SC.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
var kf		="<div class=\"kf\"><a href=\"javascript:void(0);\" title=\"\u5BA2\u670D\" class=\"link\" onclick=\"winopen('http://kf.sdo.com/sdch/index.asp')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32kf.png\" class=\"transparent\"/></a></div>";
//var zz		="<div class=\"zz\"><a href=\"javascript:void(0);\" title=\"\u5212\u8D26\" class=\"link\" onclick=\"window.open('http://direct.sdo.com/PayWeb/InGame/PTBPay.aspx?game="+Para_game+"','','width=570,height=360,scrollbars=yes,resizable=yes');\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32zz.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";
var zz		="<div class=\"zz\"><a href=\"javascript:void(0);\" title=\"\u5212\u8D26\" class=\"link\" onclick=\"window.open('http://direct.sdo.com/PayWeb/InGame/PTBPay.aspx?game="+Para_game+"&gamearea="+MyGameArea+"&gameserver="+MyGameGroup+"','','width=570,height=360,scrollbars=yes,resizable=yes');\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/32zz.png\" class=\"transparent\"/></a></div><div class=\"hr_1\"></div>";

var ItemShowArr = "";
if(LoginState==1){
	/*User is Login*/
	if(Para_game==573){
		ItemShowArr = check+cc+passport+novel+jf+kf;
	}else if(Para_game==581){
		ItemShowArr = check+cc+passport+novel+jf+kf;
	}else if(Para_game==574){
		ItemShowArr = check+cc+passport+novel+jf+kf;
	}else{
		ItemShowArr = zz+check+cc+passport+novel+jf+kf;
	}
}else{
	/*User not Login */
	ItemShowArr = login+register+cc+novel+jf+kf;
}

newdiv.innerHTML = "<div class=\"sidebar\">"+
  "<div class=\"logo\"><a id=\"switch\" href=\"javascript:void(0);\" class=\"link\" onclick=\"switchbar()\" title=\"\u5355\u51FB\u6536\u8D77/\u653E\u4E0B\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/logo.png\" class=\"transparent\"/></a></div>"+
  "<div class=\"content\" id=\"WidgetContent\">"+
    "<div class=\"top\">"+
	"<div class=\"btn_ask\"><a href=\"javascript:void(0);\" title=\"\u5E2E\u52A9\" class=\"link\" onclick=\"winopen('http://webqft.sdo.com/Webwidget_help.htm')\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/btn_ask.png\" class=\"transparent\"/></a></div>"+
	"<div class=\"btn_close\"><a href=\"javascript:void(0);\" title=\"\u5173\u95ED\" class=\"link\" onclick=\"switchbar()\"><img src=\"http://webqft.sdo.com/skin/"+APPPath+"/images/btn_close.png\" class=\"transparent\"></a></div>"+
	"<div class=\"clearboth\"></div></div>"+
    "<div class=\"mid\" id=\"SndaWidgetBar\">"+
			ItemShowArr+
    "</div>"+
	"<div class=\"bottom\"></div>"+
  "</div>"+
"</div>";


  newdiv.style.position     ="absolute";//relative
  newdiv.style.width        ="50px";
  newdiv.style.height       =0;
  newdiv.style.top          =320;

  var offLeft = (navigator.appName!="Microsoft Internet Explorer")?50:parseInt(newdiv.style.width);
  newdiv.style.left         =(document.body.clientWidth-offLeft)+"px";
  
  newdiv.style.cursor       ="move";
  newdiv.setAttribute("z-index",10001);
  document.body.appendChild(newdiv);
  
  objDiv = document.getElementById("move");
  
  Drag.init(objDiv); 
  if(navigator.appName=="Microsoft Internet Explorer"){
        //odrag(objDiv);
				LoadJsCssFile("http://webqft.sdo.com/skin/"+APPPath+"/common/TEST.css","css"); 
  }else{
				LoadJsCssFile("http://webqft.sdo.com/skin/"+APPPath+"/common/TEST2.css","css");
  }

};


function switchbar(){
    if(document.getElementById("WidgetContent").style.display=="")
        document.getElementById("WidgetContent").style.display="None";
    else
        document.getElementById("WidgetContent").style.display="";
}
function openbar(){
        document.getElementById("WidgetContent").style.display="";
}
function closebar(){
        document.getElementById("WidgetContent").style.display="None";
}

function odrag(dv){

  dv.onmousedown=function(e){
        e = e || window.event;
        mouseX = e.clientX - parseInt(dv.style.left);
        mouseY = e.clientY - parseInt(dv.style.top);
        dragTag =   true;

      
      if(dv.setCapture){
          dv.setCapture();
      }else if(window.captureEvents){
          window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
      }else{
          
      }

      dv.onmousemove=function(e){
           
           e = e || window.event;
           if(dragTag){                                  
                    dv.style.left   = e.clientX - mouseX + "px";
		            dv.style.top    = e.clientY - mouseY + "px";
		        }                                          
      };

      dv.onmouseup=function(){
           dragTag = false;
           if(dv.releaseCapture){
              dv.releaseCapture();
           }else if(window.captureEvents){
              window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
           }
          dv.onmousemove=null;
          dv.onmouseup=null;
        
          
      };
   };
}

function LoadJsCssFile(filename,filetype){
    if(filetype=="js"){
        var fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src",filename);
    }else if(filetype=="css"){
        var fileref=document.createElement("link");
        fileref.setAttribute("rel","stylesheet");
        fileref.setAttribute("type","text/css");
        fileref.setAttribute("href",filename);
    }
    if(typeof(fileref)!="undefined")document.getElementsByTagName("head")[0].appendChild(fileref)
}


function debugInfo(info) {
  var debugWindow = document.getElementById("debug_window");
  debugWindow.value = debugWindow.value + "\r\n" + info;
}

//*********************************************
function makeDraggable (what) {
	what.onmousedown = startDrag;
	what.onmousemove = draging;
	what.onmouseup = endDrag;
}

function startDrag (e) {
	if (!e) e = window.event;
	
  mouseX = e.clientX - parseInt(this.style.left);
  mouseY = e.clientY - parseInt(this.style.top);
	dragTag = true;
	
	if(e.target)
	{
		objMoved = e.target;
		e.preventDefault();
	}else{
		objMoved = e.srcElement;
		e.cancelBubble();
	}
}
function draging (e) {
	if ((dragTag)) {
		if (!e) e = window.event;
		objMoved.style.left = e.clientX - mouseX + "px";
		objMoved.style.top = e.clientY - mouseY + "px";
	}
}

function endDrag (e) {
	dragTag = false;
	
	if(e) 
		e.preventDefault();
	else
		e.cancelBubble();
}


//***********************************************************************************************************************///

var Drag = {

obj : null,
//for onclick() and Drag() Clash
Ismousemoved	:	false,

init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
{
   o.onmousedown = Drag.start;

   o.hmode    = bSwapHorzRef ? false : true ;
   o.vmode    = bSwapVertRef ? false : true ;

   o.root = oRoot && oRoot != null ? oRoot : o ;

   if (o.hmode && isNaN(parseInt(o.root.style.left ))) o.root.style.left   = "0px";
   if (o.vmode && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
   if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right = "0px";
   if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

   o.minX = typeof minX != 'undefined' ? minX : null;
   o.minY = typeof minY != 'undefined' ? minY : null;
   o.maxX = typeof maxX != 'undefined' ? maxX : null;
   o.maxY = typeof maxY != 'undefined' ? maxY : null;

   o.xMapper = fXMapper ? fXMapper : null;
   o.yMapper = fYMapper ? fYMapper : null;

   o.root.onDragStart = new Function();
   o.root.onDragEnd = new Function();
   o.root.onDrag   = new Function();
},

start : function(e)
{
   var o = Drag.obj = this;
   e = Drag.fixE(e);
   var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
   var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
   o.root.onDragStart(x, y);

   o.lastMouseX = e.clientX;
   o.lastMouseY = e.clientY;

   if (o.hmode) {
    if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
    if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
   } else {
    if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
    if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
   }

   if (o.vmode) {
    if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
    if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
   } else {
    if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
    if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
   }

   document.onmousemove = Drag.drag;
   document.onmouseup   = Drag.end;

   return false;
},

drag : function(e)
{
   e = Drag.fixE(e);
   var o = Drag.obj;

   var ey = e.clientY;
   var ex = e.clientX;
   var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
   var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
   var nx, ny;

   if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
   if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
   if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
   if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

   nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
   ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

   if (o.xMapper)   nx = o.xMapper(y) 
   else if (o.yMapper) ny = o.yMapper(x) 

   Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
   Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
   Drag.obj.lastMouseX = ex;
   Drag.obj.lastMouseY = ey;
   
	 //output("Drag.Ismousemoved="+Drag.Ismousemoved);
	 //output("nx="+nx+" | x="+x);
	 //output("ny="+ny+" | y="+y);
	 
	 
	 if(Math.abs(x-nx)>0){
	 		//output("x"+Math.abs(nx-ex));
	 		Drag.Ismousemoved	=	true;
	 }
	 if(Math.abs(y-ny)>0){
	 		//output("y"+Math.abs(ny-ey));
	 	  Drag.Ismousemoved	=	true;
	 }
	 
   Drag.obj.root.onDrag(nx, ny);
   
   return false;
},

end : function()
{
   document.onmousemove = null;
   document.onmouseup   = null;
   Drag.obj.root.onDragEnd( parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
          parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
   Drag.obj = null;
   
   //output(Drag.Ismousemoved);
   if(!Drag.Ismousemoved){
   		document.getElementById("switch").onclick=switchbar;
   	}else{
   		document.getElementById("switch").onclick=function(){};
   	}
   Drag.Ismousemoved	=	false;
},

fixE : function(e)
{
   if (typeof e == 'undefined') e = window.event;
   if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
   if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
   return e;
}
};

/*************************************************************************/
/*other function
/*************************************************************************/
function GetCookie(v_name)
{
	var lowerCookieName = v_name.toLowerCase();
	var cookieStr = document.cookie;
	
	if (cookieStr == "")
			return "";
	
	var cookieArr = cookieStr.split("; ");
	var pos = -1;
	for (var i=0; i<cookieArr.length; i++)
	{
		pos = cookieArr[i].indexOf("=");
		if (pos > 0)
		{
				if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName)
				{
						return unescape(cookieArr[i].substring(pos+1, cookieArr[i].length));
				}
		}
	}
	return "";
}

function openweb(theURL,winName){
  window.open(theURL,winName,'fullscreen');
}
function winopen(targeturl){
  var newwindow	=	window.open(targeturl,"","width="+screen.width+",height="+screen.height+",scrollbars=yes,resizable=yes");
}
function winopenwithsize(targeturl,width,height){
	if(width!=null && height!=null){
		window.open(targeturl,"","width="+width+",height="+height+",scrollbars=yes,resizable=yes");
	}else{
		window.open(targeturl,"","width="+screen.width+",height="+screen.height+",scrollbars=yes,resizable=yes");
	}
}
function alertshut(){
    alert("\u8BE5\u529F\u80FD\u6682\u672A\u5F00\u653E\uFF01");
}


//function output(val){
//			$("#debug").val($("#debug").val()+"\r\n"+val);
//		}
/********************/
/*After Load
/********************/

