/*
Creator : Copyright (C) 2001 Luis Navalpotro.
Distributed under the terms of the GNU Library General Public License
Avalaible at http://sourceforge.net/projects/junciona/
Thanks to Oscar Caraballo and Emiliano Benitez for their support
questions&contact: lnavalpotro@terra.es
*/

function junciona(obj,parent,x,y,w,h,bgColor,visib,stack,content) {
  junciona.setNav();
  junciona.setMath();
  objeto = new objLayer(obj,parent,x,y,w,h,bgColor,visib,stack,content);
  eval(obj+"=objeto");
  return objeto;
}

junciona.setNav = function(){
  if (document.all) junciona.ie = true;
  else if(document.layers) junciona.ns4 = true;
  else if(document.getElementById) junciona.ns6 = true;
  return true;
}

junciona.setMath = function(){
  if (!junciona.sins){
    radianes = Math.PI/180;
    junciona.sins = new Array();
    junciona.coss = new Array();
    for (i=0;i<=360;i++){
      var angulo = i*radianes;
      junciona.sins[i] = Math.sin(angulo);
      junciona.coss[i] = Math.cos(angulo);
    }
  }
}

function objLayer(obj,parent,x,y,w,h,bgColor,visib,stack,content) {
  this.id = this.strObj = obj;
  this.layerId = obj+"Layer";
  this.initArgs = arguments;
  this.node = null;
  this.parent = (parent!=null)?eval(parent):null;
  this.getTreeNode();
  this.mkLayer();
}

objLayer.prototype.getTreeNode = function(){
  if (junciona.ie) this.getIeNode();
  else if (junciona.ns4) this.getNs4Node();
  else if (junciona.ns6) this.getNs6Node();
}

objLayer.prototype.getIeNode = function(){
  this.node = (this.parent==null)?document.body:document.all[this.parent.layerId];
  this.strNode = (this.parent==null)?"document.all":"document.all["+this.layerId+"]";
}

objLayer.prototype.getNs4Node = function(){
 if (this.parent==null) this.node = "document.layers['" + this.layerId +"']"; 
 else this.node = this.parent.node + ".document.layers['" + this.layerId +"']";
}

objLayer.prototype.getNs6Node = function(){
  this.node = (this.parent==null)? document.getElementsByTagName("BODY").item(0): document.getElementById(this.parent.layerId);
}

objLayer.prototype.mkLayer = function() {
  var layer;
  var content = this.initArgs[9];
  if (junciona.ie){
    var aux = '<DIV id=' + this.layerId + ' style="position:absolute;left:' + this.initArgs[2] + ';top:' + this.initArgs[3] + ';width:' + this.initArgs[4] + '"></div>';
    this.node.insertAdjacentHTML("BeforeEnd",aux);
    layer = this.node.children[this.node.children.length-1];
  }else if (junciona.ns4){
    (this.parent==null)? layer = document.layers[this.layerId] = new Layer(this.initArgs[4]) : layer = eval(this.node +'= new Layer('+this.initArgs[4]+','+this.parent.node+')');
  }else if (junciona.ns6){
    layer = document.createElement("DIV");
    layer.style.position = "absolute";
    layer.id = this.layerId;
    this.node.appendChild(layer);
  }
  this.layer = layer;
  this.style = this.layer.style;
  if(junciona.ns4) this.style = this.layer;
  this.move(this.initArgs[2],this.initArgs[3]);
  this.resize(this.initArgs[4],this.initArgs[5]);
  if (content!=null) this.setContent(content);
  if (this.initArgs[6]!=null)this.setBgColor(this.initArgs[6]);
  if (this.initArgs[8]!=null) this.setZindex(this.initArgs[8]);
  (this.initArgs[7]=="show")?this.show():this.hide();
}

objLayer.prototype.move = function(x,y) {
	this.x = this.style.left = x;
	this.y = this.style.top = y;
}

objLayer.prototype.resize = function(w,h){
 this.w = this.style.width = w;
 this.h = this.style.height = h;
 (junciona.ns4)?this.layer.resizeTo(w,h):this.clip(w,h,0,0);
}

//antiguo fill
objLayer.prototype.setContent = function(cont) {
	if (junciona.ns4){
		this.layer.document.open();
		this.layer.document.write(cont);
		this.layer.document.close();
	} else this.layer.innerHTML = cont;
}

//antiguo paint
objLayer.prototype.setBgColor = function(colH) {
  (junciona.ns4)? this.style.bgColor = colH : this.style.backgroundColor = colH;
}

objLayer.prototype.setZindex = function(zI) {
	this.style.zIndex = zI;
}

objLayer.prototype.show = function() {
	this.style.visibility = "inherit";
}

objLayer.prototype.hide = function() {
	this.style.visibility = (junciona.ns4) ? "hide" : "hidden";
}

objLayer.prototype.getLayerHeight = function(){
  return ((junciona.ns4)?this.layer.height:this.layer.offsetHeight);
}

objLayer.prototype.getLayerWidth = function(){
  return ((junciona.ns4)?this.layer.width:this.layer.offsetWidth);
}

objLayer.prototype.slide = function(xf,yf,resolution,speed,kine,nextEv) {
	this.whatToMove(this.x,this.y,xf,yf,resolution,speed,Math.round(this.x+(xf-this.x)/20*(10+kine)),Math.round(this.y+(yf-this.y)/20*(10+kine)),'this.move',nextEv);
}

objLayer.prototype.beizer = function(xf,yf,resolution,speed,xbeizer,ybeizer,nextEv) {
	this.whatToMove(this.x,this.y,xf,yf,resolution,speed,xbeizer,ybeizer,'this.move',nextEv);
}

objLayer.prototype.cut = function(xi,yi,xf,yf,resolution,speed,kine,nextEv) {
	this.whatToMove(xi,yi,xf,yf,resolution,speed,Math.round(xi+(xf-xi)/20*(10+kine)),Math.round(yi+(yf-yi)/20*(10+kine)),'this.clip',nextEv);
}

objLayer.prototype.circle = function(radius, angle0, angle1, steps, speed, nEv) {
  this.datGoo = new Array();
	this.datGoo[0] = steps;
	this.datGoo[1] = speed;
	this.datGoo[2] = 1;
	this.datGoo[3] = 0;
	this.datGoo[4] = 1/steps;
	this.datGoo[5] = "this.move";
	this.datGoo[6] = nEv;
	this.datGoo[7] = new Array();
	this.datGoo[8] = new Array();
  var sangle = (angle1 - angle0) / steps;
  var cx = this.x - radius * junciona.coss[angle0];
  var cy = this.y + radius * junciona.sins[angle0];
  for (var i=1; i<=steps; i++) { //steps
    angle0 += Math.round(sangle);
    var newAngle = (angle0<0)? angle0*-1:angle0;
    this.datGoo[7][i] = Math.ceil(cx + radius * junciona.coss[newAngle]);
    this.datGoo[8][i] = Math.ceil(cy - radius * junciona.sins[newAngle]);
    if (!isNaN(this.datGoo[7][i])) this.datGoo[0] = i;
  }
  this.runGoo();
}

objLayer.prototype.whatToMove = function(xi,yi,xf,yf,resolution,speed,xbeizer,ybeizer,func,nextEv) {
	this.datGoo = new Array();
	this.datGoo[0] = resolution+1;
	this.datGoo[1] = speed;
	this.datGoo[2] = 1;
	this.datGoo[3] = 0;
	this.datGoo[4] = 1/resolution;
	this.datGoo[5] = func;
	this.datGoo[6] = nextEv;
	this.datGoo[7] = new Array();
	this.datGoo[8] = new Array();
	for (i=1;i<=this.datGoo[0];i++){
		var auxS0 = 1-this.datGoo[3];
		var auxS1 = auxS0 * auxS0;
		var auxS2 = this.datGoo[3] * this.datGoo[3];
		var auxS3 = 2 * this.datGoo[3] * auxS0; 
		this.datGoo[7][i] = Math.round((auxS1*xi)+(auxS3*xbeizer)+(auxS2*xf));
		this.datGoo[8][i] = Math.round((auxS1*yi)+(auxS3*ybeizer)+(auxS2*yf));
		this.datGoo[3] = i * this.datGoo[4];	
	}
	this.runGoo();
}

objLayer.prototype.runGoo = function() {
	if (++this.datGoo[2] <= this.datGoo[0]) {
		eval(this.datGoo[5]+'('+this.datGoo[7][this.datGoo[2]]+','+this.datGoo[8][this.datGoo[2]]+')');
		setTimeout(this.id+".runGoo()",this.datGoo[1]);		
	} else { 
		if (this.datGoo[6]) eval(this.datGoo[6]);
	}	
}

objLayer.prototype.clip = function(r,b,t,l) {
	if (junciona.ns4) {
		if (t) this.layer.clip.top = t;
		this.layer.clip.right = r;
 		this.layer.clip.bottom = b;
 		if (l) this.layer.clip.left = l;
 	} else {
		if (!t) t=0;
		if (!l) l=0;
 		this.style.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
 	}
}

objLayer.prototype.getClipValues = function(which){
     if (!junciona.ns4) var clipv = this.style.clip.split("rect(")[1].split(")")[0].split("px");
     if (which == 't') return((junciona.ns4)? this.layer.clip.top : Number(clipv[0]));
     if (which == 'r') return((junciona.ns4)? this.layer.clip.right : Number(clipv[1]));
     if (which == 'b') return((junciona.ns4)? this.layer.clip.bottom : Number(clipv[2]));
     if (which == 'l') return((junciona.ns4)? this.layer.clip.left : Number(clipv[3]));   
}