﻿var prepath = "http://www.155.com";
var isCanDrag = false;
var isMoveing = false;
var popLeft = 0;
var popTop = 0;
var dragObject;
var popType = 0;
var popWidth = 430;
var popHeight = 160; 
var popTitle = "提示信息";
var popUrl = "";
var popContent = "";
var popParams = "";
var hiddenTags = new Array();
var reloadContent = true;
var popCallback;
var popFocusId;

try {document.execCommand("BackgroundImageCache", false, true);}catch(ex){}

function popInit() 
{
  document.onmousemove = popMouseMove;
  document.onmousedown = popMouseDown;
  document.onmouseup = popMouseUp;
  document.ondragstart = popMouseStop;
  window.onresize = popResize;
}

function popMouseDown() 
{
 if (isCanDrag) 
  {
    popLeft = window.event.x - parseInt(dragObject.style.left);
    popTop = window.event.y - parseInt(dragObject.style.top);
    isMoveing = true;
  }
}

function popMouseStop() 
{
  window.event.returnValue = false;
}
function popMouseMove() 
{
  if (isMoveing) 
  {
    dragObject.style.left  = window.event.x - popLeft;
    dragObject.style.top = window.event.y - popTop;
  }
}

function popMouseUp() 
{
  isMoveing = false;
  /*
  document.onmousemove = null;
 document.onmousedown = null;
  document.onmouseup = null;
  document.ondragstart = null;
 dragObject = null;
  window.event.returnValue = true;
  */
}

function popResize()
{
  var bg = $("popDialogBg");
  if (bg) 
  {
    bg.style.height = document.body.scrollHeight;
    bg.style.width = document.body.scrollWidth;
  }
}

function objMouseOut()
{
  isCanDrag  = false;
}


function objMouseOver()
{ 
  dragObject = $("windowstishi");
  isCanDrag  = true;
}
function $(uid)
{ 
  return document.getElementById(uid);
}

function initBuild()
{
  $('popDialogCase')?$('popDialogCase').parentNode.removeChild(G('popDialogCase')):function(){};
  var oDiv = document.createElement('DIV');
  oDiv.id = 'popDialogCase';
  document.body.appendChild(oDiv);
}
function build()
{ 
   var cover = '<div id="popDialogBg" style="display:none;position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:65530;min-height:100%;"></div>';
   var mainBox = '<div id="windowstishi" style="display:none;left:0px;top:0px;z-index:65532;position:absolute;width:'+popWidth+'px;height:' + popHeight + 'px;">'; 
   mainBox += '<div id="w_top_titbj" style="cursor:move;" onmouseover="objMouseOver();" onmouseout="objMouseOut();">' + popTitle + '<span id="w_top_left"></span><a href="javascript:void(0)" onFocus="this.blur()" title="点击关闭窗口" onclick="popClose();">关闭窗口</a><span id="w_top_right"></span></div>'
   switch(popType)
   {
     //alert
     case 0:
       mainBox  += '<div id="w_neirong" style="height:' + parseInt(popHeight - 72)  + 'px;"><div id="w_info">';
       mainBox += '<table width="0" border="0" cellspacing="0" cellpadding="0"><tr><td><div id="pidy"></div></td><td>' + popContent + '</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>';
      mainBox += '</div></div>';
       mainBox += '<div id="w_bottom"><span id="w_bottom_left"></span><div id="w_b_input"><input name="popDialogOK" id="popDialogOK" type="button"  value="确认"  title="确认" onclick="popClose();"/></div><span id="w_bottom_right"></span></div>';
       break;
     //confirm
     case 1: 
       mainBox  += '<div id="w_neirong" style="height:' + parseInt(popHeight - 72) + 'px;"><div id="w_info">';
      mainBox += '<table width="0" border="0" cellspacing="0" cellpadding="0"><tr><td><div id="pidy"></div></td><td>' + popContent + '</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>';
       mainBox += '</div></div>';
       mainBox += '<div id="w_bottom"><span id="w_bottom_left"></span><div id="w_b_input"><input name="popDialogOK" id="popDialogOK" type="button"  value="确认"  title="确认" /><input name="popDialogCancel" id="popDialogCancel" type="button" value="取消" onclick="popClose();" /></div><span id="w_bottom_right"></span></div>';
       break;
     //content
     case 2:  
       mainBox  += '<div id="w_neirong" style="height:' + parseInt(popHeight - 28) + 'px;"><div id="w_info">';
       mainBox += '</div></div>';
     //iframe
     case 3:   
       mainBox  += '<div id="w_neirong" style="height:' + parseInt(popHeight - 28) + 'px;"><div id="w_info">';
      mainBox += '<iframe src=""></iframe>';
       mainBox += '</div></div>';
       break; 
     //custom confirm  
     case 4:
      mainBox  += '<div id="w_neirong" style="height:' + parseInt(popHeight) - 72 + 'px;"><div id="w_info">';
       mainBox += '<table width="0" border="0" cellspacing="0" cellpadding="0"><tr><td>' + popContent + '</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>';
      mainBox += '</div></div>';
       mainBox += '<div id="w_bottom"><span id="w_bottom_left"></span><div id="w_b_input"><input name="popDialogOK" id="popDialogOK" type="button"  value="确认"  title="确认" /><input name="popDialogCancel" id="popDialogCancel" type="button" value="取消" /></div><span id="w_bottom_right"></span></div>';   
   } 
   mainBox += '</div>';    
   initBuild();
   $("popDialogCase").innerHTML = cover + mainBox;
}
function createPop(type,width,height,title,content,url1,para,reload,okclick,focusid)
{
  popType = type;
  if (width) popWidth = width;
  if (height) popHeight = height;
  popTitle = title;
  popContent = content;
  popUrl = url1;
  popParams = parseQuery(para);
  reloadContent = reload;
  popCallback = okclick;
  popFocusId = focusid;
  popHiddenTags();
  build();
  popInit();
  bindCallback();
  popShow();
}

function calcPos(width,height)
{
 var pageSize = getPageSize();
  var pageScroll = getPageScroll();
 var fullSize = getFullSize();
  var iLeft = pageScroll[0] + (pageSize[0] - popWidth) / 2 + "px";
  var iTop = pageScroll[1] + (pageSize[1] - popHeight) / 2 + "px";
  var iWidth = fullSize[0] + "px";
  var iHeight = fullSize[1] + "px";
  
  var ret = new Array(iLeft,iTop,iWidth,iHeight);
  return ret;
}
function getPageSize()
{
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	var aPageSize = new Array(w,h) 
	return aPageSize;
}
function getPageScroll()
{
	var xScrollLeft,yScrolltop;
	if (self.pageYOffset) 
	{
	  xScrollLeft  = self.pageXOffset;
		yScrolltop = self.pageYOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop)
	{
	  xScrollLeft = document.documentElement.scrollLeft;
		yScrolltop = document.documentElement.scrollTop;
	} 
	else if (document.body) 
	{
	  xScrollLeft = document.body.scrollLeft;
		yScrolltop = document.body.scrollTop;
	}
	var aPageScroll = new Array(xScrollLeft,yScrolltop) 
	return aPageScroll;
}

function  getFullSize()
{
  var fullWidth,fullHeight;
  var contentWidth,contentHeight;

 if(typeof(window.innerWidth)=='number')
  {fullWidth = window.innerWidth;fullHeight = window.innerHeight;}
  else if(document.documentElement && document.documentElement.clientWidth)
  {fullWidth=document.documentElement.clientWidth;fullHeight=document.documentElement.clientHeight;}
  else if(document.body&&document.body.clientWidth)
  {fullWidth=document.body.clientWidth;fullHeight=document.body.clientHeight;}
  if(document.documentElement&&(document.documentElement.scrollHeight||document.documentElement.offsetHeight))
  {if(document.documentElement.scrollHeight>document.documentElement.offsetHeight)
  {contentWidth=document.documentElement.scrollWidth;contentHeight=document.documentElement.scrollHeight;}
  else
  {contentWidth=document.documentElement.offsetWidth;contentHeight=document.documentElement.offsetHeight;}}
  else if(document.body&&(document.body.scrollHeight||document.body.offsetHeight))
  {if(document.body.scrollHeight>document.body.offsetHeight)
  {contentWidth=document.body.scrollWidth;contentHeight=document.body.scrollHeight;}
  else
  {contentWidth=document.body.offsetWidth;contentHeight=document.body.offsetHeight;}}
  else {contentWidth=fullWidth;contentHeight=fullHeight;}

  if(fullWidth<contentWidth) fullWidth=contentWidth;
  if(fullHeight<contentHeight) fullHeight=contentHeight;
   
  var aFullSize = new Array(fullWidth,fullHeight);
  return aFullSize;
}
function closePop()
{
   var oPop = $('popDialogCase');
   if (oPop) oPop.parentNode.removeChild(oPop);
   popShowTags(); 
   if (popFocusId != null)
 {
$(popFocusId).focus();}
   if (reloadContent == true) {top.location.reload();}
}
function popClose()
{
   var oPop = $('popDialogCase');
   if (oPop) oPop.parentNode.removeChild(oPop);
   popShowTags(); 
   if (popFocusId != null) { $(popFocusId).focus(); }
}

function popHiddenTag(tagName)
{
   var ele = document.getElementsByTagName(tagName);
   if(ele !=null)
   {
	 for(var i=0;i<ele.length;i++)
	 {
	   if(ele[i].style.display!="none"&&ele[i].style.visibility!='hidden')
	   {
	     ele[i].style.visibility = 'hidden';
	     hiddenTags[i] = ele[i];
	   }
	 }

  }
}

function popHiddenTags()
{
    var someHiddenTag = "select,object,embed";
	var tags = someHiddenTag.split(",");
	for(var i = 0; i < tags.length; i++)
	{
	   popHiddenTag(tags[i]);
	}
}

function popShowTags()
{
   if (hiddenTags && hiddenTags.length)
   {
      for (var i = 0; i < hiddenTags.length; i ++)
      {
         hiddenTags[i].style.visibility = 'visible';
      }
  }
}
function popShow()
{
  var popBg = $("popDialogBg");
  var popBox = $("windowstishi");
  popBg.style.display = "";
  popBox.style.display = ""; 
  var ret = calcPos(popWidth, popHeight);
  popBox.style.left = ret[0];
  popBox.style.top = ret[1]; 
  //popBg.style.width = ret[2];
  popBg.style.height = ret[3];
}

function bindCallback()
{
  if (this.popCallback)
  {
      var btnok = $("popDialogOK");
	  if (document.addEventListener)
	    btnok.addEventListener('click',this.popCallback,false);
	  else
	    btnok.attachEvent('onclick',this.popCallback);
   }
}

function parseQuery(query) 
{
  var para = new Object ();
   if ( ! query ) return para; // return empty object
   var pairs = query.split(/[;&]/);
   for ( var i = 0; i < pairs .length; i++ ) 
   {
      var keyVal = pairs[i].split('=');
     if ( ! keyVal || keyVal.length != 2 ) continue;
      var key = unescape( keyVal [0] );
     var val = unescape( keyVal [1] );
      val = val.replace(/\+/g, ' ');
      para[key] = val;
   }
   return para;
}
function getParameter(name)
{
   return popParams[name];
}
function popAlert(content,isReload,focusId)
{
   createPop(0,420,160,"食客网温馨提示",content,"","",isReload,null,focusId);
}
function popConfirm(content,isReload,para,cb,focusId)
{
   createPop(1,420,160,"食客网温馨提示",content,"",para,isReload,cb,focusId);
}
function popEdit(isReload,para,cb)
{
  content = '<div style="height:40px;padding-top:40px;font-size:18px;">分类名称：<input type="text" name="popDialogInput" id="popDialogInput" onfocus="popFocus();"></div>';
  content += '<div style="height:25px;text-align:center;"><div id="popEditTiShi" name="popEditTiShi" style="display:none;color:red;font-size:14px;"></div></div>';
 createPop(1,420,160,"食客网温馨提示",content,"",para,isReload,cb);
  var gid = getParameter("uuid");
  var name = getParameter("name");
  var obj = $("popDialogInput");
  obj.value = name;
  obj.gid = gid;
  obj.gname = name;
  $("popDialogInput").focus();
}
function popFocus()
{ 
  var obj1 = $("popEditTiShi");
  if (obj1) {obj1.innerText = "";obj1.style.display = "none";popLast(event.srcElement);}
}
function popLast(obj)   
{     
 var range = obj.createTextRange();   
  range.moveStart('character',15);   
  range.collapse(true);   
  range.select();  
}