//
// Copyright (c) 2002 Endum. All rights reserved.
//
// This program is the result of personal studies and other authors' free scripts
// selections; it's free software distribuited as is WITHOUT ANY WARRANTY and
// WITHOUT TECHNICAL SUPPORT. You can use it for any purpose, except reselling 
// even in cdrom archives. 
//
// I hope it could be useful in javascript learning.
//
// Author: Angelo Selvini <info@endum.net>
// Release: 0.1
// Date: 20020112
//
//
// Credits
// S.Champeon D.Fox, authors of the book Building Dynamic HTML Guis 
//					 (http://dhtml-guis.com)
// People working in that project
//
// Camels, they never stop.

// GLOBALS 

var objects;
var divSet = new Array();
var subDivSet = new Array();


// FUNCTIONS

// Called at the end of html page
// Create object for each layer in html page, then create methods for them
function createObjects() {
	if (document.all) {
		objects = document.all.tags("DIV"); 	// msiexplorer
	} else if (document.layers) {
		objects = document.layers		// netscape
	} else if (document.getElementById){
		objects = document.getElementsByTagName("DIV");
	}

	for( i = 0; i < objects.length; i++ ) {
		current_name = objects[i].id;
	    	divSet[ current_name ] = new defineMethods( objects[i] );
		// Endum update: 
		// check also for div inside div (1 sublevel only)
		// in Netscape 4.x
		// Unnecessary in ie4.x, already present in document.all
		if ((document.layers) && (objects[i].layers.length)){
//			if (objects[i].layers.length) {
			var num = eval(objects[i].layers.length);
			for(sub=0;sub<num;sub++) {
				current_name= objects[i].layers[sub].id;
				divSet[ current_name ] = new defineMethods(objects[i].layers[sub] );
//			alert("HO TROVATO IL BASTARDO!: "+objects[i].layers[0].name);
			}
		}
	}
}


// Called by createObjects 
// Define methods for each object created in createObjects function
function defineMethods(object) {
	if (document.getElementById) {
		this.isGK = object;
		this.isIE = object;
        } else if (object.style) {
		this.isIE = object;
	} else {
		this.style = object;
	}

	this.name = object.id;
	this.timeid = -1;
	// object.method = associated function
	// layer visibility

	this.conceal = conceal;
	this.reveal = reveal;
	this.isVisible = isVisible;
	this.zIndex = getZIndex;
	this.setZIndex = setZIndex;
	this.getLeft = getLeft;
	this.getTop = getTop;
	this.setLeft = setLeft;
	this.setTop = setTop;
	this.height = getHeight;
	this.width = getWidth;
	this.setHeight = setHeight;
	this.setWidth = setWidth;

	this.place = place;
	this.shove = shove;
	this.scroll = scrollInit;
	this.scrollTo = scrollToInit

	this.getText = getText;
	this.replace = replace;
}

// Defining functions

// hide divs
function conceal() {
  if( this.isIE ) {
    this.isIE.style.visibility = "hidden";
  } else {
    this.style.visibility = "hidden";
  }
}

// show divs
function reveal() {
  if( this.isIE ) {
    this.isIE.style.visibility = "visible";
  } else {
    this.style.visibility = "visible";
  }
}

// get div visibility state
function isVisible() {
  if( this.isIE ) {
    if( this.isIE.style.visibility == "visible" ) {
      return true;
    }
  } else {
    if( this.style.visibility == "visible" ) {
      return true;
    } else if( this.style.visibility == "show" ) {
      return true;
    }
  }
  return false;
}

// get div's z-index value 
function getZIndex() {
  if( this.isIE ) {
    var z = this.isIE.style.zIndex;
    return z;
  } else {
    var z = this.style.zIndex;
    return z;
  }
}

// set div's z-index
function setZIndex(z) {
  if( this.isIE ) {
    this.isIE.style.zIndex = z;
  } else {
    this.style.zIndex = z;
  }
}

// get div's left position
function getLeft() {
  if((this.isIE) || (this.isGK)) {
    if( this.isIE.style.pixelLeft ) {
      var l = this.isIE.style.pixelLeft;
    } else if( this.isIE.style.clientLeft ) {
      var l = this.isIE.style.clientLeft;
    } else if( this.isIE.style.posLeft ) {
      var l = this.isIE.style.posLeft;
    } else if( this.isIE.style.offsetLeft ) {
      var l = this.isIE.style.offsetLeft;
    } else if( this.isIE.offsetLeft ) {
      // detecting in NS6 first time it's executed
      var l = this.isIE.offsetLeft;
    }  else {
      var l = 0;
    }
  } else if (document.layers) {
    if( this.style.clip.left ) {
      var l = this.style.clip.left;
    } else if( this.style.left ) {
      var l = this.style.left;
    } else {
      var l = 0;
    } 
  }

  return l;
}

// get div's top positionu
function getTop() {
  if( this.isIE ) {
    if( this.isIE.clientTop ) {
      var t = this.isIE.clientTop;
    } else if( this.isIE.style.pixelTop ) {
      var t = this.isIE.style.pixelTop;
    } else if( this.isIE.style.posTop ) {
      var t = this.isIE.style.posTop;
    } else if( this.isIE.style.offsetTop ) {
      var t = this.isIE.style.offsetTop;
    } else if( this.isIE.offsetTop ) {
      var t = this.isIE.offsetTop;
    }  else {
      var t = 0;
    }
  } else {
    if( this.style.clip.top ) {
      var t = this.style.clip.top;
    } else if( this.style.top ) {
      var t = this.style.top;
    } else {
      var t = 0;
    }
  }
  return t;
}

// set div's left position
function setLeft(l) {
  if (this.isGK) {
	  if (typeof(this.isIE.pixelLeft) == 'number') {
	    this.isIE.left = l;
	  } else {
	    this.isIE.style.left= l + 'px';
	  }
  } else if( this.isIE ) {
    this.isIE.style.pixelLeft = l;
  } else {
    this.style.left = l;
  }
}

// set div's top position
function setTop(t) {
   if (this.isGK) {
	  if (typeof(this.isIE.pixeliTop) == 'number') {
	    this.isIE.left = t;
	  } else {
	    this.isIE.style.top= t + 'px';
	  }
  } else if( this.isIE ) {
    this.isIE.style.pixelTop = t;
  } else {
    this.style.top = t;
  }
}

// get div's height
function getHeight() {
  if( this.isIE ) {
    if( this.isIE.clientHeight ) {
      var h = this.isIE.clientHeight;
    } else if( this.isIE.style.pixelHeight ) {
      var h = this.isIE.style.pixelHeight;
    } else if( this.isIE.style.posHeight ) {
      var h = this.isIE.style.posHeight;
    } else if( this.isIE.style.height ) {
      var h = this.isIE.style.height;
      h = parseInt(h.substr(0, h.length - 2));
    } else {
      h = 0;
    }
  } else {
    if( this.style.clip.height ) {
      var h = this.style.clip.height;
    } else if( this.style.height ) {
      var h = this.style.height;
    } else {
      var h = 0;
    }
  }
  return h;
}

// get DIV's width
function getWidth() {
  if( this.isIE ) {
    if( this.isIE.clientWidth ) {
      var w = this.isIE.clientWidth;
    } else if( this.isIE.style.pixelWidth ) {
      var w = this.isIE.style.pixelWidth;
    } else if( this.isIE.style.posWidth ) {
      var w = this.isIE.style.posWidth;
    } else if( this.isIE.style.width ) {
      var w = this.isIE.style.width;
      w = parseInt(w.substr(0, w.length -2));
    } else {
      var w = 0;
    }
  } else {
    if( this.style.clip.width ) {
      var w = this.style.clip.width;
    } else if( this.style.width ) {
      var w = this.style.width;
    } else {
      var w = 0;
    }
  }
  return w;
}

// set DIV's width
function setWidth(w) {
  if( this.isIE ) {
    this.isIE.style.pixelWidth = w;
  } else {
    this.style.width = w;
  }
}

// set the object's height
function setHeight(h) {
  if( this.isIE ) {
    this.isIE.style.pixelHeight = h;
  } else {
    this.style.height = h;
  }
}

// place DIV absolutely
function place( x, y ) {
  if( this.isIE ) {
	this.setLeft(x);
	this.setTop(y);
  } else {
    this.style.moveTo( x, y );
  }
}

function shove( x, y ) {
    if ( ((this.getLeft()+x) < 2) && ((this.getLeft()+x) > -3850)) {
	    this.setLeft(this.getLeft() +x);
    } else {
	(x>0) ? (x=0) : (x=thumbsLim);
	this.setLeft(x);
    }
}

// replace contents of DIV dynamically
function replace(txt) {
  if( this.isIE ) {
    this.isIE.innerHTML = txt;
  } else {
//#alert(this);
    this.style.document.writeln(txt);
    this.style.document.close();
  }
}

// copy contents of DIV dynamically
function getText() {
  if( this.isIE ) {
    txt = this.isIE.innerHTML;
  } else {
//    this.style.document.writeln(txt);
//    this.style.document.close();
alert(this.style.document);
  }
  return txt;
}

//
// EVENTS
//

function disable_right_click(e)
{
    var browser = navigator.appName.substring ( 0, 9 );
    var event_number = 0;
    if (browser=="Microsoft") {
    	event_number = event.button;

    } else { if (browser=="Netscape")
        event_number = e.which;
    }
    if ( event_number==2 || event_number==3 ){ 
	window.alert("© ");    	
        return (false);
    }

    return (true);
}

function check_mousekey ()
{
    var mouse_key = 93;
    var keycode = event.keyCode;
 
    if ( keycode == mouse_key )
        alert ( "Mouse Key Is Disabled" );
}

function trap_page_mouse_key_events() {
    var browser = navigator.appName.substring ( 0, 9 );		
    document.onmousedown = disable_right_click;
    if ( browser == "Microsoft" )
        document.onkeydown = check_mousekey;
    else if ( browser == "Netscape" )
        document.captureEvents( Event.MOUSEDOWN );
}

//
// ANIMATION
//
// dir: 	scroll direction 	- up,dw,lf,rg
// speed: 	scroll speed 		- milliseconds
// step:	offset movement		- pixels
// layer:	moving layer 		- string

var intervalID;var dir; var step; var speed; var mup,mdw,mlf,mrg,stl; 
var args; var mstyle;
var inc=0.15;
var intervalID;

function scrollInit(){
//	if (buffer != this) {
//		if (this.timeid > 0) {
//			scrollStop();
//		}
//		buffer = this;
//	}
	args = scrollInit.arguments;
	whichDiv = this.name;
	obj = this;
	intervalID = setInterval('scrollLoop(whichDiv,args[0],args[2],args[3],args[4],args[5],args[6],args[7],obj)',args[1]);
	this.timeid = intervalID;
	if (whichDiv == "menuSectionsMenu") {divSet['moveShield'].reveal();}
}

function scrollToInit(){
	args = scrollToInit.arguments;
	whichDiv = this.name;
	obj = this
	step=0;
	//startDir=evalDirection();	
	intervalID = setInterval('scrollLoop(whichDiv,args[0],args[2],args[3],args[4],args[5],args[6])',args[1]);
	this.timeid = intervalID;
//	if (whichDiv == "menuSectionsMenu") {divSet['moveShield'].reveal();}
}

function evalDirection() {
//	leftPos=this.getLeft();
	if (obj.getLeft() <= args[7]) {direction='rg';};
	if (obj.getLeft() >  args[7]) {direction='lf';};
	return direction;
}

function scrollStop(objToStop) { 
	if (objToStop) {
		clearInterval(divSet[objToStop].timeid);
		divSet[objToStop].timeid = -1;
		if (objToStop == "menuSectionsMenu") {divSet['moveShield'].conceal();}
	} else {
		clearInterval(intervalID);
	}
	inc=0.15;

}
function scrollLoop(whichDiv,dir,step,mup,mdw,mlf,mrg,mstyle,movLayer,stl) {
	if (mstyle == "") {
		checkLimits(whichDiv,mup,mdw,mlf,mrg,movLayer);
		step = step+1;

	} else if (mstyle == "easeout") {
		checkLimits(whichDiv,mup,mdw,mlf,mrg,movLayer,stl,dir);
		step = step + Math.abs(divSet[whichDiv].getLeft()-mlf)*0.05;
	} else if (mstyle == "easein") {
		checkLimits(whichDiv,mup,mdw,mlf,mrg,movLayer,stl,dir);
		step = step+inc;
		inc= inc+0.15;
	}

	if ((dir == "dw") || (dir == "rg")) {
		step = Math.abs(step);
	}
	else if ((dir == "up" && step > 0) || (dir == "lf")) {
		step = -step;
	}

	if ((dir == "dw") || (dir == "up")) { divSet[whichDiv].shove(0,step);}
	else if ((dir == "lf") || (dir == "rg")) {divSet[whichDiv].shove(step,0);}
}

function checkLimits () {
	var args=checkLimits.arguments;
	whichDiv = args[0]; mup = args[1]; mdw = args[2]; mlf = args[3]; mrg = args[4]; movLayer = args[5];stl = args[6];dir = args[7]
	//upper limit
	if (divSet[whichDiv].getTop() < mup) {scrollStop(whichDiv);divSet[whichDiv].setTop(mup+1);}

	//bottom limit
	if (divSet[whichDiv].getTop() > mdw) {scrollStop(whichDiv);divSet[whichDiv].setTop(mdw-1);}

	//left limit
	if (divSet[whichDiv].getLeft() < mlf) {scrollStop(whichDiv);divSet[whichDiv].setLeft(mlf+1);}

	//right limit
	if (divSet[whichDiv].getLeft() > mrg) {scrollStop(whichDiv);divSet[whichDiv].setLeft(mrg-1);}

	//scrollTo limit
//	if ((divSet[whichDiv].getLeft() >= stl) && (args[7] == 'lf')) {scrollStop(whichDiv);/*alert('limite fw')*/}
//	if ((divSet[whichDiv].getLeft() <= stl) && (args[7] == 'rg')) {scrollStop(whichDiv);alert('limite bw');}
}

