/* draggy.js

	it's not perfect, but it gets the job done.
	a little too self-referential for my tastes, but it's a quick solution to
	get it working in all of the browsers again until such time as we implement
	better lightbox-type scripts
	
	relies on:
	- $()				object finder from base.js
	- displayObject()	show/hide function from base.js
	
	usage:
	- Draggy(object_reference,pixels_from_left,pixels_from_top)
	- js before </head>
		window.onload = function() {
			var drag = $("Drag");
			drag.movement = Draggy(drag,120,40);
			drag.movement.init();
		};
	- xhtml somewhere as direct descendant of <body>
		<div id="Drag"></div>
	- css to style it 

*/

var Draggy = function(toMove,left,top){
	return {
		variable: toMove,
		isClicked: false,
		
		defX: left,
		defY: top,
		
		difX: -1,
		difY: -1,
		bWidth: -1,
		
		init: function() {
			this._bW();
			this.move(this.defX,this.defY);
			this.variable.onmousedown = function(e) {
				var e = e || window.event;
				var et = e.target || e.srcElement;
				if(et.tagName != "A")
				{
					var x = e.clientX;
					var y = e.clientY;
					
					this.movement.difX = x - this.offsetLeft;
					this.movement.difY = y - this.offsetTop;
					this.movement.isClicked = true;
					return false;
				}
			};
			this.variable.onmousemove = function(e) {
				if(this.movement.isClicked)
				{
					var e = e || window.event;
					var et = e.target || e.srcElement;
					var x = e.clientX;
					var y = e.clientY;
					this.movement.move(x-this.movement.difX,y-this.movement.difY);
					
					return false;
				}
			};
			this.variable.onmouseup = function(e) {
				var e = e || window.event;
				var et = e.target || e.srcElement;
				if(et.tagName != "A")
				{
					this.movement.isClicked = false;
					this.movement.difX = -1;
					this.movement.difY = -1;
				}
				return false;
			};
			this.variable.onmouseout = this.variable.onmouseup;
			return true;
		},
		move: function(x,y) {
			//if(x > 0 && x < (this.bWidth-this.variable.offsetWidth))
				this.variable.style.left = x + "px";
			//if(y > 0)
				this.variable.style.top = y + "px";
			return false;
		},
		show: function() {
			displayObject(this.variable,true);
			return false;
		},
		hide: function() {
			displayObject(this.variable,false);
			return false;
		},
		_bW: function() {
			this.bWidth = (window.innerWidth) ? window.innerWidth : document.body.clientWidth;
			return true;
		}
	}
};