Function.prototype.bind=function(object){
  var __method=this;
  return function(){
    return __method.apply(object, arguments);
  }
};

Function.prototype.extend=function(obj){for(var prop in obj){this.prototype[prop]=obj[prop]}};

Array.prototype.each=function(fn){
	var el=null,i=0,newArr=[],l=this.length;
	while(i<l){newArr.push(fn(this[i],i++))};
	return newArr;
};

try{document.execCommand('BackgroundImageCache',false,true);}catch(er){};
var ua=navigator.userAgent.toLowerCase();
var isOpera=ua.indexOf('opera/9')!=-1,isIE=(ua.indexOf('msie')!=-1)&&(!isOpera),isGecko=ua.indexOf('gecko')!=-1;
function details(obj){var str='';for(var prop in obj){str+=prop+ '=> '+obj[prop]+'\n';}return str;}
function debug(data){try{ge('debug').innerHTML=data;}catch(er){};}
function tag(n){return(n.nodeName||'').toLowerCase();}

if(!Array.prototype.push){
	Array.prototype.push=function(e){this[this.length]=e;};
	Array.prototype.pop=function(){var e=this[this.length-1];delete this[this.length-1];return e;};
}

function getXY(n) {var w=n.offsetWidth,h=n.offsetHeight;for(var x=0,y=0;x+=n.offsetLeft,y+=n.offsetTop,n=n.offsetParent;);return{x:x,y:y,w:w,h:h};}
function ce(t){return document.createElement(t);}
function ct(t){return document.createTextNode(t);}
function ge(id){return document.getElementById(id);}
function ges(tag){return document.getElementsByTagName(tag);}
function sendQuery(url){var s=ce('script');s.type='text/javascript';s.src=url;document.documentElement.firstChild.appendChild(s);}

/* $hortDOM */
function cn(name){return $(ce(name))}
function _short(o){return $(o)}
function $(o){'ac,att,cls,ac2,sp,add,Eadd,s,doc,ce,before,after,ss,chain'.split(',').each(function(p){this[p]=window['__'+p]}.bind(o));o.tag=tag(o);return o;}
function __before(){this.add=__ib;return this;};
function __after(){this.add=__ia;return this;};
function __ib(tag,text){var n=this.ce(tag,text);this.parentNode.insertBefore(n,this);this.add=__add;return n;}
function __ia(tag,text){var n=this.ce(tag,text),s;(s=this.nextSibling)?this.parentNode.insertBefore(n,s):this.parentNode.appendChild(n);this.add=__add;return n;}
function __ac(n){return this.appendChild(n);};
function __att(n,v){if(arguments.length==2){this.setAttribute(n,v);return this;}else{return this.getAttribute(n)}};
function __cls(name,mode){
	var cls=this.className;
	var ret=this;
	switch (arguments.length){
		case 1:cls=name;break;
		case 0:ret=cls;break;
		case 2:
			switch(mode){
				case true:cls+=' '+name;break;
				case false:cls=cls.replace(/\w+/g,function(m){return m==name?'':m+' '});break;
			}
		break;
	}
	this.className=cls;
	return ret;
};

function __ss(s){for(var prop in s){this.style[prop]=s[prop];}return this;}
function __sp(n){return n.appendChild(this);};
function __s(){return this.style};
function __Eadd(type,fn){window.EvMan.Add(this,type,fn);return this;};
function __doc(){return this.ownerDocument||this.document;};
function __ac2(tag){return this.ac(this.ce(tag));};
function __add(tag,text){return this.ac(this.ce(tag,text));};
function __ce(tag,text){var n=$(this.doc().createElement(tag));if(arguments.length==2){n.ac(n.doc().createTextNode(text));}return n;};
function __chain(tag,lastNodeText){
var n=this,tags=tag.split(' '),i=0;
while(tag=tags[i++])tag.replace(/(\w+)(\.|#)?(\w+)?/,function($1,$2,$3,$4){n=n.ac2($2);if ($3=='.') n.cls($4); else if($3=='#') n.att('id',$4);});
if(lastNodeText){
	n.ac(ct(lastNodeText));
}
return n;
};


function getDomEntry(){var ss=ges('script');return $(ss[ss.length-1]);}
function clearNode(n){
//	n.innerHTML='';
	while(n.firstChild){
		if(n.firstChild.firstChild){
			clearNode(n.firstChild);
		}else{
			n.removeChild(n.firstChild);
		}
	}
}

/* events */
function stopEvent(e){
	try{e.preventDefault();e.stopPropagation();}catch(er){e.returnValue=false;e.cancelBubble=true;}
}

if(!EvMan){

var EvMan={

	data:[],
	tmp:{},
	addMethod:'',
	removeMethod:'',
	typePrefix:'',
	InitDone:false,

	Init:function(){
		if(this.InitDone){return};
		if(window.addEventListener){

			this.addMethod='addEventListener';
			this.removeMethod='removeEventListener';

		}else if(window.attachEvent){

			this.addMethod='attachEvent';
			this.removeMethod='detachEvent';
			this.typePrefix='on';
		}
		EvMan.Add(window,'unload',this.CleanUp);
		this.InitDone=true;
	},

	Add:function(obj,type,fn,useCapture){

		if(typeof obj=='string'){
			obj=document.getElementById(obj);
		}
		if(obj===null||fn===null){
			return;
		}
		if(this.addMethod){
			obj[this.addMethod](this.typePrefix+type,fn,false);
			this.data.push({obj:obj,type:type,fn:fn,useCapture:useCapture});
		}
	},

	AddTmp:function(scope,obj,type,fn){

			obj[this.addMethod](this.typePrefix+type,fn,false);
			if(!this.tmp[scope]){
				this.tmp[scope]=[];
			}
			this.tmp[scope].push({obj:obj,type:type,fn:fn,useCapture:false});
	},

	CleanTmp:function(scope){
		var data,ev;
		if((data=this.tmp[scope])){
			for(var i=0;i<data.length;i++){
				ev=data[i];
				ev.obj[this.removeMethod](this.typePrefix+ev.type,ev.fn,ev.useCapture);
				ev.obj=null;
				ev.fn=null;
				ev=null;
			}
			this.tmp[scope]=[];
		}
	},

	Rem:function(obj,type,fn){
		obj[this.removeMethod](this.typePrefix+type,fn,null);
	},

	CleanUp:function(){

		if (!EvMan||!EvMan.data) return;

		var ev=null;
		var removeMethod=EvMan.removeMethod;
		var typePrefix=EvMan.typePrefix;

		for(var i=(EvMan.data.length-1);i>=0;i--){
			ev=EvMan.data[i];
			ev.obj[removeMethod](typePrefix+ev.type,ev.fn,ev.useCapture);
			delete ev.obj;
		}
	}
};
EvMan.Init();
}

function intVal(prop) {
	return (prop||'0').replace(/[^\d\-\.]/g,'')*1;
}

var $E={

	data:[],
	tmp:{},
	addMethod:'',
	removeMethod:'',
	typePrefix:'',
	InitDone:false,

	Init:function(){
		if(this.InitDone){return;};
		if(window.addEventListener){

			this.addMethod='addEventListener';
			this.removeMethod='removeEventListener';

		}else if(window.attachEvent){

			this.addMethod='attachEvent';
			this.removeMethod='detachEvent';
			this.typePrefix='on';
		}

		$E.Add(window,'unload',this.CleanUp);
		this.InitDone=true;
	},

	_mutateEvent:function(e){
		var x=y=0;
		if(e.pageY){
			x=e.pageX;y=e.pageY;
		}else{
			x=e.clientX+(document.documentElement||document.body).scrollLeft;
			y=e.clientY+(document.documentElement||document.body).scrollTop;
		}
		return {el:e.target||e.srcElement,x:x,y:y,event:e,stop:function(){stopEvent(this.event)}};
	},

	_mutateHandler:function(fn){
		return function(e){
			fn($E._mutateEvent(e));
		}
	},

	Add:function(obj,type,fn,useCapture){

		if(typeof obj=='string'){
			obj=document.getElementById(obj);
		}

		if(obj===null||fn===null){
			return;
		}

		fn=this._mutateHandler(fn);

		if(this.addMethod){
			obj[this.addMethod](this.typePrefix+type,fn,false);
			this.data.push({obj:obj,type:type,fn:fn,useCapture:useCapture});
		}
	},

	AddTmp:function(scope,obj,type,fn){

			fn=this._mutateHandler(fn);
			obj[this.addMethod](this.typePrefix+type,fn,false);
			if(!this.tmp[scope]){
				this.tmp[scope]=[];
			}
			this.tmp[scope].push({obj:obj,type:type,fn:fn,useCapture:false});
	},

	CleanTmp:function(scope){
		var t=this;
		t.tmp[scope]&&t.tmp[scope].each(function(ev){
			ev.obj[t.removeMethod](t.typePrefix+ev.type,ev.fn,ev.useCapture);
			ev=null;
		});
		t.tmp[scope]=[];
	},

	Rem:function(obj,type,fn){
		obj[this.removeMethod](this.typePrefix+type,fn,null);
	},

	CleanUp:function(){
		if (!$E||!$E.data) return;
		var ev=null;
		var removeMethod=$E.removeMethod;
		var typePrefix=$E.typePrefix;

		$E.data.each(function(ev){ev.obj[removeMethod](typePrefix+ev.type,ev.fn,ev.useCapture)});
		for(var scope in $E.tmp){
			$E.tmp[scope].each(function(ev){ev.obj[removeMethod](typePrefix+ev.type,ev.fn,ev.useCapture)});
		}
	},

	DOMready:(function(){
		var load_events = [],load_timer,script,done,exec,old_onload,
			init=function(){
				done=true;
				clearInterval(load_timer);
				while (exec = load_events.shift()) exec();
	    };

		return function(func){
			if(done) return func();

	    if(!load_events[0]){
				if (document.addEventListener) {
					document.addEventListener("DOMContentLoaded", init, false);
				}
				/*@cc_on @*/
				/*@if (@_win32)
					if(script = document.getElementById("__ie_onload")){
							script.methods&&script.methods.push(init);
					}else{
						document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
						script = document.getElementById("__ie_onload");
						script.methods=[];
						script.methods.push(init);
						script.onreadystatechange = function(){
							if (this.readyState == "complete"){
								for(var i=0;i<this.methods.length;i++){
									this.methods[i]();
								}
								this.methods=null;
								this.onreadystatechange=null;
							}
						};
					}
				/*@end @*/

        /* for Safari*/
				if(/WebKit/i.test(navigator.userAgent)){
					load_timer = setInterval(function() {
						if (/loaded|complete/.test(document.readyState))
							init(); /*call the onload handler*/
					}, 10);
				}
			}
			load_events.push(func);
		}
	})()

};

$E.Init();

var $DD={

	x:0,
	y:0,
	moveElement:null,
	Sxy:{},
	Dxy:{x:0,y:0},

	init:function(){
		$E.CleanTmp('drag');
		var docs=document.getElementsByTagName('iframe');
		$DD.documentHandler(document,{x:0,y:0});
		for(var i=0;i<docs.length;i++){
			$DD.documentHandler(docs[i].contentWindow.document,getXY(docs[i]));
		}
	},

	documentHandler:function(d,xy){

		$E.AddTmp('drag',d,'mousemove',function(e){
			$DD.Dxy=xy;
			$DD._trace(e)}
		);

		$E.AddTmp('drag',d,'mousedown',function(e){

			$DD.Dxy=xy;
			var el=e.el;

			if((el.className||'').indexOf('draggable')!=-1){
				d.onselectstart=function(){return false};
				$DD.Start(el);
				e.stop();
			}else if((el.className||'').indexOf('trigger')!=-1){
				var trigger=el;
				do{
					if((el.className||'').indexOf('draggableParent')!=-1){
						d.onselectstart=function(){return false};
						$DD.Start(el,trigger);
						e.stop();
						break;
					}
				}while(el=el.parentNode);
			}
		});

		$E.AddTmp('drag',d,'mouseup',function(){
			if($DD.moveElement){
				$DD.Stop();
			}
		});		
	},

	_trace:function(e){
		$DD.x=e.x;
		$DD.y=e.y;
		if($DD.moveElement!=null){
			$DD.Move(e);
		}
	},

	Move:function(e){
		e.stop();
		var xy=$DD.Sxy;
		var ss={top:($DD.y-xy.y+$DD.Dxy.y)+'px',left:($DD.x-xy.x+$DD.Dxy.x)+'px'};
		$DD.moveElement.ss(ss);
		$DD.moveElement.onDragMove&&$DD.moveElement.onDragMove();
	},

	Start:function(el,trigger){
		var s=el.style;
		$DD.Sxy={x:$DD.x-intVal(s.left),y:$DD.y-intVal(s.top)};
		$DD.moveElement=el;
		if($DD.moveElement.onDragStart){
			$DD.moveElement.onDragStart();
		}
	},

	Stop:function(){
		if($DD.moveElement==null){
			return false;
		}
		if($DD.moveElement.onDragEnd){
			$DD.moveElement.onDragEnd();
		}

		$DD.moveElement=null;
		document.onselectstart=null;
		$DD.Sxy={};
	},

	Bind:function(trigger,moveable){
		if(moveable){
			// trigger must be a child of moveable
			trigger.cls('trigger',true);
			moveable.cls('draggableParent',true);
		}else{
			trigger.cls('draggable',true);
		}
	}
};

$E.Add(window,'load',$DD.init);

DragDrop=$DD;

window['_shared_core_loaded']=true;