/*********************************************************************
********************************************************************************
Copyright 2005-2008 Dyna Framework by Luis Majano and Ortus Solutions, Corp
www.Dynaframework.com | www.luismajano.com | www.ortussolutions.com
********************************************************************************
Author 	 :	Ernst van der Linden ( evdlinden@gmail.com | http://evdlinden.behindthe.net )
Date     :	7/31/2008
Description : JS Functions for the Dyna Sidebar 
		
Modification History:
08/10/2008 evdlinden : FireFox problem solved
* 08/13/2008 evdlinden : Total renew; Now you can create a sidebar in the Dyna namespace.
**********************************************************************/

/* ColdBox Namespace */
var isExtended = 0;
var Dyna;
if (!Dyna) Dyna = {};

// SideBar					
Dyna.SideBar = function(arguments){
	this.init(arguments);
}

Dyna.SideBar.prototype.init = function(arguments)
{
	// Needed for FireFox display problem
	var NS6CorrectionX = 0;
	
	// Used to store time out functions. Needs to be in global scope
	this.moving = setTimeout('null',1);

	this.setIsNS6( (document.getElementById&&!document.all) );
	this.setIsIE( (document.all) );
	this.setLastWindowY(0); 
	this.setSlideSpeed(arguments.slideSpeed); 
	this.setWaitTimeBeforeClose(arguments.waitTimeBeforeClose);   
	this.setYOffset(arguments.yOffset);   
	this.setIsScroll(arguments.isScroll);   
	this.setWidth(arguments.width);   
	
	// Set document element objects used by the side bar
	if ( this.getIsNS6() ){
		this.setSideBarElement( document.getElementById(arguments.elementId) );
		this.setContainerElement( document.getElementById(arguments.containerElementId) );
		// Left correction
		this.getSideBarElement().style.left = parseInt(this.getSideBarElement().style.left) + NS6CorrectionX + "px"; 
		// Width correction
		this.setWidth( (this.getWidth()-NS6CorrectionX) );
		this.setYPositionElement( document.getElementById(arguments.YPositionElementId) );
		
	} else if ( this.getIsIE() ) {
		this.setSideBarElement( document.all(arguments.elementId) );
		this.setContainerElement( document.all(arguments.containerElementId) );
		this.setYPositionElement( document.all(arguments.YPositionElementId) );

	}
	
	// set y position
	this.getContainerElement().style.top=findPosY(this.getYPositionElement());
	// Set Container clipping	
	this.getContainerElement().style.clip="rect(0 "+( this.getSideBarElement().offsetWidth )+ " " + this.getSideBarElement().offsetHeight + " 0)";
	// Show side bar
	this.getSideBarElement().style.visibility = "visible";	
	
	  
	// Scroll enabled?
	if(this.getIsScroll()){
		this.scrollSideBar();
	}	
}
// SideBar Element 
Dyna.SideBar.prototype.setSideBarElement = function(sideBarElement){ this.sideBarElement = sideBarElement; }
Dyna.SideBar.prototype.getSideBarElement = function(){ return this.sideBarElement}
// Container element
Dyna.SideBar.prototype.setContainerElement = function(containerElement){ this.containerElement = containerElement; }
Dyna.SideBar.prototype.getContainerElement = function(){ return this.containerElement}
// YPositionElement element
Dyna.SideBar.prototype.setYPositionElement = function(YPositionElement){ this.YPositionElement = YPositionElement; }
Dyna.SideBar.prototype.getYPositionElement = function(){ return this.YPositionElement}

// IE?
Dyna.SideBar.prototype.setIsIE = function(isIE){ this.isIE = isIE; }
Dyna.SideBar.prototype.getIsIE = function(){ return this.isIE;}
// NS6?
Dyna.SideBar.prototype.setIsNS6 = function(isNS6){ this.isNS6 = isNS6; }
Dyna.SideBar.prototype.getIsNS6 = function(){ return this.isNS6;}
// Slide Speed
Dyna.SideBar.prototype.setSlideSpeed = function(slideSpeed){ this.slideSpeed = slideSpeed; }
Dyna.SideBar.prototype.getSlideSpeed = function(){ return this.slideSpeed;}
// Wait Time
Dyna.SideBar.prototype.setWaitTimeBeforeClose = function(waitTimeBeforeClose){ this.waitTimeBeforeClose = waitTimeBeforeClose; }
Dyna.SideBar.prototype.getWaitTimeBeforeClose = function(){ return this.waitTimeBeforeClose;}
// Y Offset
Dyna.SideBar.prototype.setYOffset = function(yOffset){ this.yOffset = yOffset; }
Dyna.SideBar.prototype.getYOffset = function(){ return this.yOffset;}
// Scroll?
Dyna.SideBar.prototype.setIsScroll = function(isScroll){ this.isScroll = isScroll; }
Dyna.SideBar.prototype.getIsScroll = function(){ return this.isScroll;}
// Last Window Y
Dyna.SideBar.prototype.setLastWindowY = function(lastWindowY){ this.lastWindowY = lastWindowY; }
Dyna.SideBar.prototype.getLastWindowY = function(){ return this.lastWindowY;}
// Width
Dyna.SideBar.prototype.setWidth = function(width){ this.width = width; }
Dyna.SideBar.prototype.getWidth = function(){ return this.width;}
// Move Out
Dyna.SideBar.prototype.moveOut = function(){ 
	var self = this;	
	if ( (this.getIsNS6() )&& parseInt( this.getSideBarElement().style.left)<0 || this.getIsIE() && this.getSideBarElement().style.pixelLeft<0){
		clearTimeout(this.moving);
		var selfMoveOut = function moveOut(){ self.moveOut(); }
		this.moving = setTimeout(selfMoveOut, this.getSlideSpeed());
		this.slide(10);
		
	}else {
		clearTimeout(this.moving);
		this.moving=setTimeout('null',1);
	}
}
// Move Back
Dyna.SideBar.prototype.moveBack = function(){
	var self = this;	
	clearTimeout(this.moving);
	var selfMoveBack1 = function moveBack1(){ self.moveBack1(); }
	this.moving = setTimeout(selfMoveBack1, this.getWaitTimeBeforeClose());
}
// Move Back 1
Dyna.SideBar.prototype.moveBack1 = function(){
	var self = this;
	if ((this.getIsNS6()) && parseInt(this.getSideBarElement().style.left)>(-this.getWidth()) || this.getIsIE() && this.getSideBarElement().style.pixelLeft>(-this.getWidth())) {
		clearTimeout(this.moving);
		var selfMoveBack1 = function moveBack1(){ self.moveBack1(); }
		this.moving = setTimeout(selfMoveBack1, this.getSlideSpeed());
		this.slide(-10);
	} else {
		clearTimeout(this.moving);
		this.moving=setTimeout('null',1);
	}
}
// Slide
Dyna.SideBar.prototype.slide = function(num){
	if (this.getIsIE()) {
		this.getSideBarElement().style.pixelLeft += num;
	}
	if (this.getIsNS6()) {
		this.getSideBarElement().style.left = parseInt(this.getSideBarElement().style.left)+num+"px";
	}
}
// Scroll
Dyna.SideBar.prototype.scrollSideBar = function(){
	var self = this;
	var smooth = 0;
	var windowY = getScrollXY().y;

	if (windowY!=this.getLastWindowY()&&windowY > this.getYOffset()) {
		smooth = .2 * (windowY - lastWindowY - this.getYOffset());
	}else if (gthis.etYOffset()+lastWindowY>this.getYOffset()) {
		smooth = .2 * (windowY - lastWindowY - (this.getYOffset()-(this.getYOffset()-windowY)));
	} else {
		smooth=0;
	}
	if(smooth > 0){
		 smooth = Math.ceil(smooth);
	}else {
		smooth = Math.floor(smooth);
	}	
	if (this.getIsIE()){
		this.getSideBarElement().style.top=parseInt(this.getSideBarElement().style.top)+smooth+"px";
	} 
	if (this.isNS6()){
		this.getSideBarElement().style.top=parseInt(this.getSideBarElement().style.top)+smooth+"px";	
	} 
	this.setLastWindowY( (this.getLastWindowY()+smooth));
	var selfScroll = function scroll(){ self.scroll(); }
	setTimeout(selfScroll, 1);
	
	if (this.getIsIE()) {
		this.getSideBarElement().style.pixelLeft += num;
	}
	if (this.getIsNS6()) {
		this.getSideBarElement().style.left = parseInt(this.getSideBarElement().style.left)+num+"px";
	}
}

function getScrollXY() {
  var scrollXY = {x:0,y:0};
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrollXY.y = window.pageYOffset;
    scrollXY.x = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrollXY.y = document.body.scrollTop;
    scrollXY.x = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrollXY.y = document.documentElement.scrollTop;
    scrollXY.x = document.documentElement.scrollLeft;
  }
  return scrollXY;
}

Dyna.SideBar.prototype.toggleSidebar = function(){ 
  
	if (!isExtended) {
	 	this.moveOut();
		isExtended = 1;
	} 	
	else { 
		this.moveBack()	;
		isExtended = 0;
	}	
		
	//isExtended ? this.moveOut(): this.moveBack();
}

 function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

