// Beehive II - V2.00.030906b, september 7, 2003
// ----------------------------------------------
// Peterned - http://www.xs4all.nl/~peterned/
// (c) 2003 - Peter Nederlof

var activeObj,dragObj, mouseX=0,mouseY=0, dynTopZ=100;
// Collection & css
// ----------------------------------------------
function Collection(base) { this.set(base); }
Collection.prototype = {
	set:function(list) { for(var i in list) this[i] = list[i]; return this; },
	copy:function() { return new Collection(this); },
	using:function(list) { return this.copy().set(list); }
}

var css = {addRule:function(type, rule) {this[type] = new Collection(rule);}}
css.addRule('ABSOLUTE', {position:'absolute', visibility:'visible', left:0, top:0});
css.addRule('RELATIVE', {position:'relative', left:0, top:0, right:0, bottom:0});

// Various
// ----------------------------------------------
if(!Function.apply) 
Function.prototype.apply = function(obj, args) {
	var sArg = [], l = args.length;
	for(var i=0; i<l; i++) { sArg[i] = 'arg['+i+']'; }
	var action = new Function('obj','f','arg',
		'obj.$apply = f; obj.$apply(' + sArg.join(',') + ')');
		return action(obj, this, args);
}

function strict(n) { return (typeof n == 'number')? (n+'px'):n; }

function pushProperties(list, into) {
	
	for(var i in list) { 
	try {into[i] = strict(list[i]);} catch(e){} }
}

// DynObject core
// ----------------------------------------------
function DynObject(parent) {
	this.parentElement = parent.element || document.body;
	this.parent = parent;
	this.x = this.y = 0;
	this.children = [];
	this.events = [];
	this.threads = [];
}
	function attachObject(el) {
		return (new DynObject(document)).attach(el);
	}

dynProto = DynObject.prototype
document.createObject = dynProto.createObject = function(type, attributes, css) {
	var obj = new DynObject(this);
	obj.create(type, attributes, css);
	if(this.children) this.children[this.children.length] = obj;
	return obj;
}

dynProto.create = function(type, attributes, css) {
	var el = document.createElement(type);
	pushProperties(attributes, el);
	pushProperties(css, el.style);
	this.registerElement(this.parentElement.appendChild(el));
}

dynProto.attach = function(element) {
	if(element.parentNode) this.parentElement = element.parentNode;
	this.registerElement(element); return this;
}

dynProto.registerElement = function(element) {
	(this.element = element).dynObject = this;
	this.css = this.element.style;
	if(this.css.position == 'absolute') {
		this.x = parseInt(this.css.left);
		this.y = parseInt(this.css.top);
		this.getDimensions();
	}
}

dynProto.moveElement = function(other, type) {
	var p = other.parentElement, o = other.element;
	this.registerElement(this.parentElement.removeChild(this.element));
	this.parent = (this.parentElement = (type == 'append'? o:p)).dynObject;
	switch(type) {
		case 'append': other.element.appendChild(this.element); break;
		case 'before': p.insertBefore(this.element, o); break;
		case 'replace': p.replaceChild(this.element, o); break;
	}
}

dynProto.remove = function() {
	this.parentElement.removeChild(this.element);
	var pc = this.parent.children;
	for(var i in pc) {
		if(pc[i] == this) {
			pc[i] = pc[pc.length-1]; pc.length --; break;
		}
	}
}

// Basics
// ----------------------------------------------

//JC added ______________________________________________________

dynProto.centre=function(D){  
var W,H;
	if(document.body==this.parentElement){W=doc.w;H=doc.h;
	}else{W=this.parent.w;H=this.parent.h;}
	
var x=(W-this.w)/2;
var y=(H-this.h)/2;
D = D || "B"

 if (D=="Y"){x=this.x}
 if (D=="X"){y=this.y}
this.moveTo(Math.floor(x),Math.floor(y));
}

dynProto.setBackground = function(to) {
	if(to.indexOf('.') > 0) this.css.backgroundImage = 'url('+to+')';
	else this.css.backgroundColor = to;
}

dynProto.slideTo = function(xx, yy, tt) {
tt= tt ? tt : 500;
xx = xx||this.x;
yy = yy||this.y;
this.run('moveTo', {from:[this.x, this.y], to:[xx, yy], time:tt})
}

dynProto.opacity_gradual = function(fromop,toop, tt) {
tt= tt ? tt : 500;
//alert(op+"=op 138 o="+o);
this.run('setOpacity', {from:[fromop], to:[toop], time:tt})
}


dynProto.resizeToTimed = function(ww, hh, tt) {
tt= tt ? tt : 500;
this.run('resizeTo', {from:[this.w, this.h], to:[ww, hh], time:tt})
}

dynProto.cardReSizeMove = function(w,h,x,y,tt){
	tt= tt ? tt : 500;
	var z= (w==cardw) ? 1:10;
	this.setZIndex(z);
	this.resizeToTimed(w,h,tt)
	this.slideTo(x,y,tt);
}


dynProto.props = function(){
alert(this.css.display);
}

//this.run('resizeTo', {from:[this.w, this.h], to:[ww, hh], time:tt})
//this.timer = setInterval(function(){ self.tick(); }, specs.delay || 40);

dynProto.doAfter = function() {
	var el=this;
	var d=el.donefunc;
	var t=typeof d;
	//INFO.write(a+"=a d="+d);
	if ('function'==t){d(el);
		}else{ eval (d);} 
	el.donefunc=null;
	}


//end of JC added ______________________________________________________




dynProto.moveTo = function(x, y) {
	this.x = x; this.y = y;
	if(this.limits) {
		var l = this.limits;
		this.x = (x < l.mx)? l.mx:((x + this.w > l.mw)? l.mw-this.w:x);
		this.y = (y < l.my)? l.my:((y + this.h > l.mh)? l.mh-this.h:y);
	}	
	
	this.css.left = strict(x); 
	this.css.top = strict(y);
	this.getDimensions();
}


dynProto.limitMovement = function(x, y, w, h) {
		this.limits = { mx:x, my:y, mw:w, mh:h };
		this.moveTo(this.x, this.y);
	}


dynProto.moveBy = function(dx, dy) {
	this.moveTo(this.x + dx, this.y + dy);
}

dynProto.setVisible = function(vis){
	this.css.visibility=vis?'visible':'hidden';
	//this.css.visibility = (this.visible = vis)?
		//((this.parent)? 'inherit':'visible'):'hidden';
}

dynProto.setStyle = function(attribute, value) {
	if(typeof attribute == 'object') pushProperties(attribute, this.css);
	else try { this.css[attribute] = strict(value); } catch(e) {}
}

dynProto.setZIndex = function(z) {
	this.css.zIndex = this.z = z;
	if(z > dynTopZ) dynTopZ = z;
}

dynProto.bringToFront = function(){ this.setZIndex(dynTopZ + 1); }

dynProto.resizeTo = function(w, h) {
	this.setStyle('width', this.w = w);
	this.setStyle('height', this.h = h);
	this.getDimensions();
	this.executeEvents('onresize');
}

dynProto.relateToParent = function(behavior) {
	var self = this;
	this.parent.attachEvent('onresize', function(){
		var res, func;
		for(var i in behavior) {
			res = new Function('parent', 'self', 'return ' + behavior[i]);
			self.setStyle(i, res(self.parent, self));
			self.getDimensions();
		}	self.executeEvents('onresize');
	});	this.parent.executeEvents('onresize');
}

dynProto.write = function(html) { 
	html=html.replace(/\n/g,"<br />");
	this.element.innerHTML = this.html = html; 
}

dynProto.add = function(html) { this.write((this.html || '') + html); }

dynProto.getDimensions = function() {
	this.w = this.element.clientWidth || this.element.offsetWidth;
	this.h = this.element.clientHeight || this.element.offsetHeight;
	this.r = this.x + this.w;
	this.b = this.y + this.h;
}

dynProto.setOpacity = function(to) {
	this.opacity = to;
	this.css.filter = 'alpha(opacity='+to+')';
	this.css.MozOpacity = to/100;
}

dynProto.run = function(ref, specs) {
	specs.scope = this;	
	if(this.threads[ref]&&!this.donefunc) {this.threads[ref].stop()}
	//clearInterval(this.timer);
	var thread = this.threads[ref] = new Thread();
	thread.run(ref, specs);
}

// Thread
// ----------------------------------------------
function DynCounter(a, b) {	return function(pos) { return a + (b - a)*pos; }}
function Thread() {	this.counters = []; }
Thread.prototype = {
	
	stop:function()	{ clearInterval(this.timer);
							if (this.scope.donefunc){
								var el=this.scope;
								clearTimeout(el.timerout);
								el.timerout = setTimeout(function(){ el.doAfter(); }, 0);
								}
	},
	
	run:function(ref, specs) {		
		var a = specs.value || specs.from, b = specs.value || specs.to, l = a.length;
		this.steps = specs.time? Math.floor(specs.time / 40):false;
		this.scope = specs.scope || window;
		this.ref = ref;
		this.step = 0;

		if(l) { for(var i=0; i<l; i++) this.counters[i] = new DynCounter(a[i], b[i]);
		} else this.counters[0] = new DynCounter(a, b);
		var self = this; if(this.timer) this.stop();
		this.timer = setInterval(function(){ self.tick(); }, specs.delay || 40);
	},

	tick:function() {
		var pos = this.steps? (this.step++)/this.steps:1;
		var res = [], len = this.counters.length;
		for(var i=0; i<len; i++) res[i] = this.counters[i](pos);
		this.scope[this.ref].apply(this.scope, res);
			if(this.step > this.steps) { this.stop();}
	}



}

// Document properties
// ----------------------------------------------
var doc = {
	getXScroll:function() { return document.body.scrollLeft || (window.pageXOffset || 0); },
	getYScroll:function() { return document.body.scrollTop || (window.pageYOffset || 0); },
	refresh:function() {
		this.w = this.availWidth = document.body.clientWidth || window.innerWidth;
		this.availHeight  = document.body.clientHeight || window.innerHeight;
	
		if( document.documentElement &&
	      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    this.availWidth = document.documentElement.clientWidth;
    	this.availHeight = document.documentElement.clientHeight;
		}
	this.w = this.availWidth;this.h = this.availHeight;
	}
}

// Events
// ----------------------------------------------
dynProto.attachEvent = document.attachEvent = function(type, reference) {
	if(!this.events) this.events = [];
	if(!this.events[type]) this.events[type] = [];
	var pos = this.events[type].length, eObj = [type, pos];
	this.events[type][pos] = reference;
	if(this.element) { this.element[type] = function(e) {
		activeObj = this.dynObject
		activeObj.executeEvents(type, e);
		if(dragObj) return false; }
	} else { this[type] = function(e) {
		this.executeEvents(type, e);
		if(dragObj) return false; }
	}	return eObj;
}

dynProto.removeEvent = document.removeEvent = function(eObj) { 
	if(this.events[eObj[0]]) delete this.events[eObj[0]][eObj[1]]; }
dynProto.removeEvents = document.removeEvents =	function(type) { 
	if(type) this.events[type] = []; else this.events = []; }

dynProto.executeEvents = document.executeEvents = function(type, e) {
	if(!this.events[type]) return;
	if(this.eventType != type) this.eventType = type;
	if(this.element && type == 'onmousedown') {
		this.clickX = (e)? e.layerX:event.offsetX;
		this.clickY = (e)? e.layerY:event.offsetY;
	}	
	if (this.element && !e) {e=this} //added by JC
	//if (!e) {e=this} //added by JC
	for(var i in this.events[type]) { this.events[type][i](e); }
}

window.onload = function() { document.executeEvents('onload'); }
document.attachEvent('onload', function(){
	window.onresize = function() { doc.refresh(); document.executeEvents('onresize'); }
	document.attachEvent('onmousemove', function(e) {
		mouseX = (e)? e.pageX:(event.clientX + document.body.scrollLeft);
		mouseY = (e)? e.pageY:(event.clientY + document.body.scrollTop);
		dynHandleDrag();
	});
	document.attachEvent('onmouseup', dynStopDrag);
	window.onresize();
});

// Drag & drop
// ----------------------------------------------
dynProto.enableDragDrop = function(settings) {
	this.attachEvent('onmousedown', dynStartDrag);
	this.attachEvent('onmouseup',   dynStopDrag);
	function contains(arg) { return settings? (settings.indexOf(arg) > -1):0 }
	this.drag = {
		dragParent:contains('dragParent'),
		dragH:contains('horizontal'),
		dragV:contains('vertical')
	};
}

function dynStartDrag() {
	dragObj = activeObj;
	var target = dragObj.drag.dragParent? dragObj.parent:dragObj;
	dragObj.xAnch = mouseX - target.x;
	dragObj.yAnch = mouseY - target.y;
	dragObj.executeEvents('ondragstart');
}

function dynStopDrag() {
	if(!dragObj) return;
	dragObj.executeEvents('ondragstop');
	dragObj = null;
}

function dynHandleDrag() {
	if(!dragObj) return;
	var set = dragObj.drag, target = set.dragParent? dragObj.parent:dragObj;
	var tx = set.dragV? target.x : (mouseX - dragObj.xAnch);
	var ty = set.dragH? target.y : (mouseY - dragObj.yAnch);
	target.moveTo(tx, ty);
	dragObj.executeEvents('ondrag');
}