/**
 * This file is part of project - Powerviawindow
 *
 * please reserving all of the comment, otherwise you should not use those code
 *
 * @Author    muqiao
 * @Version   1.2
 * @Copyright muqiao (http://hi.baidu.com/emkiao)
 * @Revision  $Id$
 */
(function($){
var namespace = arguments[1] || 'Model';
var pureModel = window[namespace] = function(options){
	var o = this.options = $.extend({
		left:'auto',
		top:'auto'
	},options || {});
	this.id = "model_" + new Date().getTime();
	this.model = $('<div id="'+this.id+'" class="puremodel"></div>').prependTo(document.body);
	o.content == undefined && (o.content = '&nbsp;');
	this.html(o.content);
	if(o.event && o.event.target){
		var dim = $(o.event.target).getDimensions(),
			wsize = _getWindowScroll(),
			rightedge = wsize.width  - o.event.clientX,
			bottomedge = wsize.height - o.event.clientY;
		o.left = wsize.left + o.event.clientX;
		o.top = wsize.top  + o.event.clientY;
		if(rightedge < dim.width){
			o.left -= dim.width;
		}
		if(bottomedge < dim.height){
			o.top -= dim.height;
		}
	}else if(o.top == 'auto' || o.left == 'auto'){
		var wS = _getWindowScroll();
    	var pS = _getPageSize();
		if(o.top == 'auto'){
			o.top  = (pS.windowHeight - this.height)/2 + wS.top;
		}
		if(o.left == 'auto'){
			o.left = (pS.windowWidth -  this.width)/2  + wS.left;
		}
	}
	this.locate(o.top,o.left).toFront();
	this.startDrag = _startDrag.bindE(this);
	this.model.bind("mousedown",this.startDrag);
};
var maxZIndex = 51;
pureModel.prototype = {
	close : function(){
		this.model.remove();
		this.model.unbind('mousedown',this.startDrag);
		return this;
	},html: function(content){
		if(content==undefined){
			return this.options.content||'';
		}
		content == '' && (content = "&nbsp;");
		this.model.empty().append(content);
		this.options.content = content;
		var dim = this.model.getDimensions();
		this.width  = dim.width;
		this.height = dim.height;
		return this;
	},locate:function(top,left){
		top  = parseInt(top);
		left = parseInt(left);
		isNaN(top) && (top = this.top || 0);
		isNaN(left) && (left = this.left || 0);
		this.model.css({top:top,left:left});
		this.top = top;
		this.left = left;
		return this;
	},toFront:function(){
		var zIndex = parseInt(this.model.css('zIndex'));
		(isNaN(zIndex) || zIndex < maxZIndex) && this.model.css('zIndex',++maxZIndex);
		return this;
	}
};
var _startDrag  = function(e){
	var target = e.target;
	this.toFront();
	this.pointer = {X: e.pageX,Y: e.pageY};
	this.dowhat = 'dragging';
	this.endDrag  = _endDrag.bindE(this);
	this.updateDrag = _updateDrag.bindE(this);
	$(document).bind('mouseup',this.endDrag).bind('mousemove',this.updateDrag);
	document.body.ondrag = function(){return false;};
	document.body.onselectstart = function(){return false;};
};
var _updateDrag = function(e){
	if(this.dowhat == null) return;
	var pointer  = {X: e.pageX,Y: e.pageY};
	var dx = pointer.X - this.pointer.X;
	var dy = pointer.Y - this.pointer.Y;
	var left =  parseFloat(this.model.css('left')) + dx;
	var top  =  parseFloat(this.model.css('top'))  + dy;
	this.pointer = pointer;
	this.locate(top,left);
};
var _endDrag  = function(e){
	this.dowhat = null;
	$(document).unbind('mouseup',this.endDrag).unbind('mousemove',this.updateDrag);
	document.body.ondrag = null;
	document.body.onselectstart = null;
};
var _getWindowScroll = function(){
	var T, L, W, H,win = window;
	with (win.document) {
		if (win.document.documentElement && documentElement.scrollTop) {
			T = documentElement.scrollTop;
			L = documentElement.scrollLeft;
		} else if (win.document.body) {
			T = body.scrollTop;
			L = body.scrollLeft;
		}
		if (win.innerWidth) {
			W = win.innerWidth;
			H = win.innerHeight;
		} else if (win.document.documentElement && documentElement.clientWidth) {
			W = documentElement.clientWidth;
			H = documentElement.clientHeight;
		} else {
			W = body.offsetWidth;
			H = body.offsetHeight
		}
	}
	return { top: T, left: L, width: W, height: H };
};
var _getPageSize = function(){
	var windowWidth, windowHeight,pageHeight, pageWidth,xScroll, yScroll, win = window, doc = document.body;
	if (win.innerHeight && win.scrollMaxY) {
		xScroll = doc.scrollWidth;
		yScroll = win.innerHeight + win.scrollMaxY;
	}else if (doc.scrollHeight > doc.offsetHeight){
		xScroll = doc.scrollWidth;
		yScroll = doc.scrollHeight;
	}else {
		xScroll = doc.offsetWidth;
		yScroll = doc.offsetHeight;
	}
	if(win.innerHeight) {
		windowWidth = win.innerWidth;
		windowHeight = win.innerHeight;
	}else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}else if (doc) {
		windowWidth = doc.clientWidth;
		windowHeight = doc.clientHeight;
	}
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
};
})(jQuery,'Model');