/**                     Sirocco -- Administation Office
 *                             -- DivObject.js --
 * 
 * An abstraction layer to map div properties above browsers non-unified CSS renderings
 * Requires EnvInspector tool to be loaded
 *
 * Javascript version : 1.5
 *
 * @category	Tool
 * @author	Luc Thibault <luc@suhali.net>
 * @copyright	2005 - Luc Thibault - SUHALI WEB DESIGN
 * @license	GNU General Public License
 * @version	2.1
 *
 * GNU General Public License 
 * 	This program is free software ; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation; either version 2 of the License,
 *	or (at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful, but
 *	WITHOUT ANY WARRANTY ; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *	See the GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program ; if not, write to the
 *	Free Software Foundation, Inc.,
 *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA ;
 *	or visit http://www.gnu.org/copy_nLeft/gpl.html
 */


function DivObject (id) {
	if (!EnvInspector.DOM) {
		alert("Sorry, your browser is not supported with this framework...\nWe suggest you upgrade to a fully W3C compliant browser\nWe recomend usage of Firefox, Safari, Konqueror or Opera");
		return null;
	}
	this.id = id;
	this.elem = document.getElementById(id);
	this.css = this.elem.style;
	this.obj = this.id + "DivObject";
	eval(this.obj + "=this");


	this.left = function (x) {
		if (x != null)
			this.css.left = x+'px';
		else	return parseInt(this.css.left.substring(0,this.css.left.length-2))
	}
	this.top = function (y) {
		if (y != null)
			this.css.top = y+'px';
		else	return parseInt(this.css.top.substring(0,this.css.top.length-2))
	}
	this.width = function (w) {
		if (w != null)
			this.css.width = w+'px';
		else	return parseInt(this.elem.offsetWidth);
	}
	this.height = function (h) {
		if (h != null)
			this.css.height = h+'px';
		else	return parseInt(this.elem.offsetHeight);
	}

	this.show = function () {
		this.css.visibility = "visible";
	}
	this.hide = function () {
		this.css.visibility = "hidden";
	}

	this.setBg = function (bg) {
		if (bg.substring(0,1) == '#')
			this.css.backgroundColor = bg;
		else	this.css.backgroundImage = 'url('+bg+')';
	}

	this.moveTo = function (x, y) {
		this.left(x);
		this.top(y);
	}
	this.moveBy = function (dx, dy) {
		this.css.left = this.left()+dx;
		this.css.top = this.top()+dy;
	}

	this.clip = function (l, t, r, b) {
		if (this.width()<r)
			this.css.width = r;
		if (this.height()<b)
			this.css.height = b;
		this.css.clip = 'rect(' + t + 'px ' + r + 'px ' + b + 'px ' + l + 'px)';
	}
	this.getClip = function (which) {
		var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px");
		if (which=="t")
			return Number(clipv[0]);
		if (which=="r")
			return Number(clipv[1]);
		if (which=="b")
			return Number(clipv[2]);
		if (which=="l")
			return Number(clipv[3]);
	}

	this.setEvent = function (event, action) {
		if (EnvInspector.browser == 'ie')
			this.elem.attachEvent('on'+event, action);
		else	this.elem.addEventListener(event, action, true);
	}

	this.write = function (html) {
		this.elem.innerHTML = html;
	}

	/************ SLIDE FUNCTIONS ***********/

	this.slideTo = function (endx, endy, inc, speed, fn) {
		if (endx == null)
			endx = this.left();
		if (endy == null)
			endy = this.top();
		var distx = endx-this.left();
		var disty = endy-this.top();
		this.slideStart(endx, endy, distx, disty, inc, speed, fn);
	}
	this.slideBy = function (distx, disty, inc, speed, fn) {
		var endx = this.left() + distx;
		var endy = this.top() + disty;
		this.slideStart(endx, endy, distx, disty, inc, speed, fn);
	}
	this.slideStart = function (endx, endy, distx, disty, inc, speed, fn) {
		if (this.slideActive)
			return;
		if (!inc)
			inc = 10;
		if (!speed)
			speed = 20;
		var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc;
		if (num==0)
			return;
		var dx = distx/num;
		var dy = disty/num;
		if (!fn)
			fn = null;
		this.slideActive = true;
		this.slide(dx, dy, endx, endy, num, 1, speed, fn)
	}
	this.slide = function (dx, dy, endx, endy, num, i, speed, fn) {
		if (!this.slideActive)
			return;
		if (i++ < num) {
			this.moveBy(dx,dy);
			setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed);
		} else {
			this.slideActive = false;
			this.moveTo(endx,endy);
			eval(fn);
		}
	}

	/************ WIPE FUNCTIONS ************/

	this.wipeTo = function (endt, endr, endb, endl, num, speed, fn) {
		var distt = (endt!=null) ? endt-this.getClip('t') : 0;
		var distr = (endr!=null) ? endr-this.getClip('r') : 0;
		var distb = (endb!=null) ? endb-this.getClip('b') : 0;
		var distl = (endl!=null) ? endl-this.getClip('l') : 0;
		this.wipeStart(distt, distr, distb, distl, endt, endr, endb, endl, num, speed, fn);
	}
	this.wipeBy = function (distt, distr, distb, distl, num, speed, fn) {
		this.wipeStart(distt, distr, distb, distl, distt+this.getClip('t'), distr+this.getClip('r'), distb+this.getClip('b'), distl+this.getClip('l'), num, speed, fn);
	}
	this.wipeStart = function (distt, distr, distb, distl, endt, endr, endb, endl, num, speed, fn) {
		if (this.wipeActive)
			return;
		if (!fn)
			fn = null;
		this.wipeActive = true;
		this.wipe(distt/num, distr/num, distb/num, distl/num, endt, endr, endb, endl, this.getClip('t'), this.getClip('r'), this.getClip('b'), this.getClip('l'), num, 1, speed, fn);
	}
	this.wipe = function (dt, dr, db, dl, endt, endr, endb, endl, st, sr, sb, sl, num, i, speed, fn) {
		if (!this.wipeActive)
			return;
		if (i++ < num) {
			this.clip(sl+i*dl, st+i*dt, sr+i*dr, sb+i*db);
			setTimeout(this.obj+".wipe("+dt+","+dr+","+db+","+dl+","+endt+","+endr+","+endb+","+endl+","+st+","+sr+","+sb+","+sl+","+num+","+i+","+speed+",\""+fn+"\")",speed);
		} else {
			this.wipeActive = false;
			this.clip(endl, endt, endr, endb);
			eval(fn);
		}
	}

}


