﻿// Lee dialog 1.0 http://www.xij.cn/blog/?p=68

function getPosLeft(obj) 
{ 
	var l = obj.offsetLeft; 
	while(obj = obj.offsetParent) 
	{ 
		l += obj.offsetLeft; 
	} 
	return l; 
}

function getPosTop(obj) 
{ 
	var l = obj.offsetTop; 
	while(obj = obj.offsetParent) 
	{ 
		l += obj.offsetTop; 
	} 
	return l; 
}

function stateChangeIE(_frame)
{ 
 if (_frame.readyState=="interactive")//state: loading ,interactive, complete
 {
    $("#load").remove();
    _frame.style.visibility = "visible";
 }   
}

function stateChangeFirefox(_frame)
{ 
    $("#load").remove();
    _frame.style.visibility = "visible";
}

function closeDialog()
{
  $("#floatBoxBg").animate({opacity:"0"},"fast",function(){$(this).hide();});
  $("#floatBox").hide();
}

var dialogFirst=true;
function dialog(title,content,width,height,cssName,src){

var left = getPosLeft(src)+src.offsetWidth;
if ( (left+Number(width.replace("px",""))) > screen.width )
{
    left = left-Number(width.replace("px",""))-src.offsetWidth-4;
}
var top = (getPosTop(src)-Number(height.replace("px",""))-20);
if (top < 0){ top = 0; }

if(dialogFirst==true){
  var temp_float=new String;
  temp_float="<div id=\"floatBoxBg\" style=\"height:"+$(window).height()+"px;filter:alpha(opacity=0);opacity:0;\"></div>";
  //temp_float="<div id=\"floatBoxBg\" style=\"top:"+(top-20)+"px;left:"+(left-20)+"px;width:"+(Number(width.replace("px",""))+40)+"px;height:"+(Number(height.replace("px",""))+70)+"px;filter:alpha(opacity=0);opacity:0;\"></div>";
  temp_float+="<div id=\"floatBox\" class=\"floatBox\">";
  temp_float+="<div class=\"title\"><h4></h4><span>关闭</span></div>";
  temp_float+="<div class=\"content\"></div>";
  temp_float+="</div>";
  $("body").append(temp_float);
  dialogFirst=false;
}
$("#floatBox .title").css("cursor","move");
$("#floatBox .title span").click(closeDialog);

$("#floatBox .title h4").html(title);
contentType=content.substring(0,content.indexOf(":"));
content=content.substring(content.indexOf(":")+1,content.length);

switch(contentType){
  case "url":
  var content_array=content.split("?");
  $("#floatBox .content").ajaxStart(function(){
    $(this).html("loading...");
  });
  $.ajax({
    type:content_array[0],
    url:content_array[1],
    data:content_array[2],
	error:function(){
	  $("#floatBox .content").html("error...");
	},
    success:function(html){
      $("#floatBox .content").html(html);
    }
  });
  break;
  case "text":
  $("#floatBox .content").html(content);
  break;
  case "id":
  $("#floatBox .content").html($("#"+content+"").html());
  break;
  case "iframe":
  $("#floatBox .content").html("<div align='center' style='position: absolute; width: 100px; height: 100px; z-index: 1; left:140px; top:100px' id='load'><img src='/images/ajax-loader.gif' />&nbsp;载入中...</div>");
  $("#floatBox .content").append("<iframe style='visibility:hidden;' onreadystatechange=stateChangeIE(this) onload=stateChangeFirefox(this) name='callframe' id='callframe' width=\"100%\" height=\""+(parseInt(height)-30)+"px"+"\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
  callframe.location.href=content;
}

$("#floatBoxBg").show();
$("#floatBoxBg").animate({opacity:"0.4"},"fast");
$("#floatBox").attr("class","floatBox "+cssName);
$("#floatBox").css({display:"block",left:left+"px",top:top+"px",width:width});


(function($){
$.fn.jqDrag=function(h){return i(this,h,'d');};
$.fn.jqResize=function(h){return i(this,h,'r');};
$.jqDnR={dnr:{},e:0,
drag:function(v){
 if(M.k == 'd')E.css({left:M.X+v.pageX-M.pX,top:M.Y+v.pageY-M.pY});
 else E.css({width:Math.max(v.pageX-M.pX+M.W,0),height:Math.max(v.pageY-M.pY+M.H,0)});
  return false;},
stop:function(){$().unbind('mousemove',J.drag).unbind('mouseup',J.stop);}
};
var J=$.jqDnR,M=J.dnr,E=J.e,
i=function(e,h,k){return e.each(function(){h=(h)?$(h,e):e;
 h.bind('mousedown',{e:e,k:k},function(v){var d=v.data,p={};E=d.e;
 // attempt utilization of dimensions plugin to fix IE issues
 if(E.css('position') != 'relative'){try{E.position(p);}catch(e){}}
 M={X:p.left||f('left')||0,Y:p.top||f('top')||0,W:f('width')||E[0].scrollWidth||0,H:f('height')||E[0].scrollHeight||0,pX:v.pageX,pY:v.pageY,k:d.k};
 $().mousemove($.jqDnR.drag).mouseup($.jqDnR.stop);
 return false;
 });
});},
f=function(k){return parseInt(E.css(k))||false;};
})(jQuery);

$('#floatBox').jqDrag();
}