/*
 * jQuery TanChu Power By City023.com
 */
(function($){

	// to track if the mouse button is pressed
	var isMouseDown    = false;

	// to track the current element being dragged
	var currentElement = null;

	// callback holders
	var dropCallbacks = {};
	var dragCallbacks = {};

	// global position records
	var lastMouseX;
	var lastMouseY;
	var lastElemTop;
	var lastElemLeft;
	
	// track element dragStatus
	var dragStatus = {};	

	// returns the mouse (cursor) current position
	$.getMousePosition = function(e){
		var posx = 0;
		var posy = 0;

		if (!e) var e = window.event;

		if (e.pageX || e.pageY) {
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
		}

		return { 'x': posx, 'y': posy };
	};

	// updates the position of the current element being dragged
	$.updatePosition = function(e) {
		var pos = $.getMousePosition(e);

		var spanX = (pos.x - lastMouseX);
		var spanY = (pos.y - lastMouseY);

		$(currentElement).css("top",  (lastElemTop + spanY));
		$(currentElement).css("left", (lastElemLeft + spanX));

	};

	// when the mouse is moved while the mouse button is pressed
	$(document).mousemove(function(e){
		if(isMouseDown && dragStatus[currentElement.id] == 'on'){
			// update the position and call the registered function
			$.updatePosition(e);
			if(dragCallbacks[currentElement.id] != undefined){
				dragCallbacks[currentElement.id](e, currentElement);
			}

			return false;
		}
	});

	// when the mouse button is released
	$(document).mouseup(function(e){
		if(isMouseDown && dragStatus[currentElement.id] == 'on'){
			isMouseDown = false;
			if(dropCallbacks[currentElement.id] != undefined){
				dropCallbacks[currentElement.id](e, currentElement);
			}

			return false;
		}
	});

	// register the function to be called while an element is being dragged
	$.fn.ondrag = function(callback){
		return this.each(function(){
			dragCallbacks[this.id] = callback;
		});
	};

	// register the function to be called when an element is dropped
	$.fn.ondrop = function(callback){
		return this.each(function(){
			dropCallbacks[this.id] = callback;
		});
	};
	
	// stop the element dragging feature
	$.fn.dragOff = function(){
		return this.each(function(){
			dragStatus[this.id] = 'off';
		});
	};
	
	
	$.fn.dragOn = function(){
		return this.each(function(){
			dragStatus[this.id] = 'on';
		});
	};

	// set an element as draggable - allowBubbling enables/disables event bubbling
	$.fn.easydrag = function(allowBubbling){

		return this.each(function(){

			// if no id is defined assign a unique one
			if(undefined == this.id || !this.id.length) this.id = "easydrag"+(new Date().getTime());

			// set dragStatus 
			dragStatus[this.id] = "on";
			
			// change the mouse pointer
			$(this).css("cursor", "move");

			// when an element receives a mouse press
			$(this).mousedown(function(e){

				// set it as absolute positioned
				$(this).css("position", "absolute");

				// set z-index
				$(this).css("z-index", "10000");

				// update track variables
				isMouseDown    = true;
				currentElement = this;

				// retrieve positioning properties
				var pos    = $.getMousePosition(e);
				lastMouseX = pos.x;
				lastMouseY = pos.y;

				lastElemTop  = this.offsetTop;
				lastElemLeft = this.offsetLeft;

				$.updatePosition(e);

				return allowBubbling ? true : false;
			});
		});
	};

	$.fn.runfloatwin=function() {
		this.hide();
		this.easydrag(true);
		//$("#floatwin").ondrop(function(e, element){ alert(element + " Dropped"); });

		mleft=(document.documentElement.clientWidth-parseFloat (this.width()))/2+document.documentElement.scrollLeft;
		mtop=(document.documentElement.clientHeight -parseFloat (this.height()))/2+document.documentElement.scrollTop;

		this.css({ 'left': mleft, 'top': mtop, 'cursor':'default' });

		this.show("normal");
	}

})(jQuery);

//ShowMsg City023.com

function showMsg(title,content,width,height,cssName,isclose){
  //背景屏幕
  var iWidth = $("body").width();
  var iHeight = $("body").height();        
  var temp_float=new String;
  temp_float="<div id=\"ShowMsgBoxBg\" style=\"width:"+iWidth+"px;height:"+iHeight+"px;\"></div><div id=\"ShowMsg\">";
  temp_float+="<div id=\"ShowMsg_title\">";
  temp_float+="<div class=\"left\"></div>";
  if(isclose=='1'){
  temp_float+="<div class=\"right\"></div>"; 
  }else{
  temp_float+="<div class=\"right TanChu_close\"><a href=\"javascript:\">关闭</a></div>";
  }
  temp_float+="</div>";
  temp_float+="<div id=\"ShowMsg_body\"></div>";
  temp_float+="</div>";
$("body").append(temp_float);
$("#ShowMsg").css({width:width,height:height});
$("#ShowMsg_body").html(content);
$("#ShowMsg_title div.left").html(title);

$("#ShowMsg .TanChu_close a").click(function(){
  closeMsg();
});
function closeMsg(){
  $("#ShowMsgBoxBg").animate({opacity:"0"},"normal",function(){$(this).hide();});
  $("#ShowMsg").hide();
  $("#ShowMsgBoxBg").remove();
  $("#ShowMsg").remove();
}
contentType=content.substring(0,content.indexOf(":"));
content=content.substring(content.indexOf(":")+1,content.length);
switch(contentType){
  case "url":
  var content_array=content.split("|");
  $.ajax({
    type:content_array[0],
    url:content_array[1],
    data:content_array[2],
    beforeSend:function(){
      $("#ShowMsg #ShowMsg_body").html("<img src='../templates/image/loader.gif' />");
    },
	error:function(){
	  $("#ShowMsg #ShowMsg_body").html("出错了... , 请重新刷新页面");
	},
    success:function(html){
      $("#ShowMsg #ShowMsg_body").html(html);
    }
  });
  break;
  case "text":
  $("#ShowMsg #ShowMsg_body").html(content);
  break;
  case "id":
  $("#ShowMsg #ShowMsg_body").html($("#"+content+"").html());
  break;
  case "iframe":
  $("#ShowMsg #ShowMsg_body").html("<iframe src=\""+content+"\" width=\"100%\" height=\""+(parseInt(height)-30)+"px"+"\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
}
$("#ShowMsgBoxBg").show();
$("#ShowMsg_body").addClass(cssName);
$("#ShowMsgBoxBg").animate({opacity:"0.5"},"normal");
$("#ShowMsg").runfloatwin();
}