//***************************************************************************************
// apitools.js - common classes for DHTML pages
//---------------------------------------------------------------------------------------
// (C) 2007 - Gabor Finta (alias Fincy GO.) infoTRADE ltd. | HUNGARY Budapest |
//---------------------------------------------------------------------------------------
//
// Thank you for Travis Beckham (http://www.squidfingers.com | http://www.podlob.com)
// who made the base version.
//
//***************************************************************************************



//***************************************************************************************
//
// COSDetect class - Contains th OS independed functions.
//

//=======================================================================================
COSDetect = function()
//---------------------------------------------------------------------------------------
//
{
	var agent = navigator.userAgent.toLowerCase(); 
	this._mac = agent.indexOf('mac') != -1;
	this._win = !this._mac;
	this._w3c = document.getElementById;
	this._iex = document.all;
	this._ns4 = document.layers;
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.GetObjNS4 = function( obj, name )
//---------------------------------------------------------------------------------------
// Netscape szerinti elemkiválasztás, rekurzív módon történik
//
{
	var ObjList = obj.layers;
	var result, temp;
	for (var i=0; i<ObjList.length; i++)
	{
		if ( ObjList[i].id == name )
		 	return ObjList[i];

		if( ObjList[i].layers.length )
			temp = this.getObjNS4( ObjList[i], name );

		if(temp)
			return temp;
	}
	return null; //result;
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.GetObj = function( name )
//---------------------------------------------------------------------------------------
// A <name> nevű elemhez tartozó objektummal tér vissza, az elem kiválasztását
// az osztály konstruktorában definiált, megfelelő függvények meghívásával végzi.
// A NetScape szerinti kiválasztást csak bonyolultabb, rekurzív segédfüggvénnyel
// lehet megoldani.
//
{
	if(this._w3c)
		return document.getElementById( name );

	if(this._iex)
		return document.all[ name ];

	if(this._ns4)
		return this.getObjNS4( document, name );

	return null;
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.GetStyle = function( obj )
//---------------------------------------------------------------------------------------
//
{
	return (this._ns4) ? obj : obj.style;
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.GetWindowWidth = function()
//---------------------------------------------------------------------------------------
// width of the window
{
	return this._iex ? document.body.clientWidth : window.innerWidth;
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.GetWindowHeight = function()
//---------------------------------------------------------------------------------------
// height of the window
{
	return this._iex ? document.body.clientHeight : window.innerHeight;
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.GetScrollTop = function()
//---------------------------------------------------------------------------------------
// top scroll position of the window
{
	return this._iex ? document.body.scrollTop : window.pageYOffset;
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.GetScrollLeft = function()
//---------------------------------------------------------------------------------------
// left scroll position of the window
{
	return this._iex ? document.body.scrollLeft : window.pageXOffset;
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.SetScrollTop = function( n )
//---------------------------------------------------------------------------------------
// set the vertical scroll position of the window
{ 
	window.scrollTo(this.getScrollLeft() ,n );
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.SetScrollLeft = function( n )
//---------------------------------------------------------------------------------------
// set the horizontal scroll position of the window
{ 
	window.scrollTo( n, this.getScrollTop() );
}
//
//=======================================================================================



//=======================================================================================
COSDetect.prototype.SetScroll = function( x, y )
//---------------------------------------------------------------------------------------
// set the x,y scroll position of the window
{ 
	window.scrollTo( x, y );
}
//
//=======================================================================================


//
// End of COSDetect class
//
//***************************************************************************************







//***************************************************************************************
//
// CHTMLObj class - for a specified HTML element. You can use them to change
// the parameters of the HTML elemets easily.
//

//=======================================================================================
CHTMLobj = function( name )
//---------------------------------------------------------------------------------------
//
{
	if( name )
	{
		this._inherit      = COSDetect; 
		this._inherit( name );
		this._id           = name;
		this._el           = this.GetObj( this._id );
		this._css          = this.GetStyle( this._el );
		this._obj          = name+'Object'; 

		eval( this._obj + '=this' );	

		this._timer        = null;
		this._glideRunning = false;
		this._tweenRunning = false;
		this._fadeRunning  = false;	// Added by SU, Couloir
		this._randNum      = null;	// Added by SU, Couloir
		this._startFade    = false;	// Added by SU, Couloir
		return this;
	}
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype = new COSDetect();
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.GetLeft = function()
//---------------------------------------------------------------------------------------
// left position of the element
{ 
	return parseInt( this._css.left || 0 );
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.GetTop = function()
//---------------------------------------------------------------------------------------
// top position of the element
{
	return parseInt( this._css.top || 0 );
}		
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.GetWidth = function()
//---------------------------------------------------------------------------------------
// width of the element
{
	if ( this._ns4 )
		 return this._el.document.width;
	return this._el.offsetWidth;
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.GetHeight = function()
//---------------------------------------------------------------------------------------
// height of the element
{
	if ( this._ns4 )
		 return this._el.document.height;
	return this._el.offsetHeight;
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.GetClipWidth = function()
//---------------------------------------------------------------------------------------
// clip width of the element
{
	if ( this._ns4 )
		 return this._el.clip.width;
	return this._el.offsetWidth;
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.GetClipHeight = function()
//---------------------------------------------------------------------------------------
// clip height of the element
{
	if ( this._ns4 )
		 return this._el.clip.height;
	return this._el.offsetHeight;
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.SetStyle = function(prop, val)
//---------------------------------------------------------------------------------------
// change the value of any css property
{
	if ( !this._ns4 )
	{
		this._el.style[prop] = val;
		if ( this._iex && this._mac )
			this._el.innerHTML = this._el.innerHTML;
	}
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.Show = function()
//---------------------------------------------------------------------------------------
// show the visibility of the element
{
	this._css.visibility = 'visible';
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.Hide = function()
//---------------------------------------------------------------------------------------
// hide the visibility of the element
{
	this._css.visibility = 'hidden';
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.ShowHide = function()
//---------------------------------------------------------------------------------------
// toggle the visibility of the element
{
	if (this._css.visibility == 'hidden' || this._css.visibility == 'hide')
		this._css.visibility = 'visible';
	else
		this._css.visibility = 'hidden';
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.SetInner = function(html)
//---------------------------------------------------------------------------------------
// change the contents of the element
{
	if ( this._ns4 )
	{
		this._el.document.open();
		this._el.document.write(html);
		this._el.document.close();
	}
	else
		this._el.innerHTML = html;
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.MoveTo = function(x,y)
//---------------------------------------------------------------------------------------
// move the element to a new position
{
	if ( this._ns4 )
		this._el.MoveTo( x, y );
	else
	{
		this._css.left = x + 'px';
		this._css.top  = y + 'px';
	}
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.MoveBy = function(x,y)
//---------------------------------------------------------------------------------------
// move the element to a new position relative to it's current position
{
	if ( this._ns4 ) 
		this._el.MoveBy( x, y );
	else
	{
		this._css.left = this.getLeft() + x  + 'px';
		this._css.top  = this.getTop()  + y  + 'px';
	}
}
//=======================================================================================




//=======================================================================================
CHTMLobj.prototype.SizeTo = function(w,h)
//---------------------------------------------------------------------------------------
// set the size of the element
{
	if ( !this._ns4 )
	{
		this._css.width  = w + 'px';
		this._css.height = h + 'px';
	}
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.SizeBy = function(w,h)
//---------------------------------------------------------------------------------------
// set the size of the element relative to it's current size
{
	if ( !this._ns4 )
	{
		this._css.width  = this.GetWidth()  + w + 'px';
		this._css.height = this.GetHeight() + h + 'px';
	}
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.GlideTo = function( x, y, callback )
//---------------------------------------------------------------------------------------
// ease-out animation, callback function optional
{
	if ( this._glideRunning )
	{
		var left = this.GetLeft();
		var top  = this.GetTop();
		if ( Math.abs(left-x) <= 1 && Math.abs(top-y) <= 1)
		{
			this.MoveTo( x, y );
			this.CancelGlide();
			if ( callback )
				eval( this._obj+'.'+callback+'()' );
		}
		else
			this.MoveTo(left+(x-left)/2, top+(y-top)/2);
	}
	else
	{
		var c = (callback) ? ',\"'+callback+'\"' : '' ;
		this._timer  = setInterval( this._obj+'.GlideTo('+x+','+y+c+')', 100 );
		this._glideRunning = true;
	}
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.CancelGlide = function()
//---------------------------------------------------------------------------------------
// ease-out animation, callback function optional
// cancel the glideTo method
{
	clearInterval( this._timer );
	this._timer        = null;
	this._glideRunning = false;
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.SwapDepth = function(obj)
//---------------------------------------------------------------------------------------
// swap the z-index of 2 elements
{
	var temp = this._css.zIndex;
	this._css.zIndex = obj._css.zIndex;
	obj._css.zIndex  = temp;
}
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.TweenTo = function( method, start, end, time )
//---------------------------------------------------------------------------------------
// * Modified by SU, Uptonic.com *
// time-based animation, with multiple easing methods
// method: a function that takes 4 arguments: time, start, change, and duration
// start: array of starting width, height dimensions [w, h]
// end: array of ending width, height dimensions [w, h]
// time: number of 'frames' it takes to get to the end position
{
	if ( !this._tweenRunning )
	{
		this._tweenTime = 0;
		var s = '['+start.toString()+']';
		var e = '['+end.toString()+']';
		this._timer = setInterval(this._obj+'.TweenTo('+method+','+s+','+e+','+time+')', 33);
		this._tweenRunning = true;
	}

	if ( ++this._tweenTime > time )
		this.CancelTween();
	else
	{
		var w = method(this._tweenTime, start[0], end[0]-start[0], time);
		var h = method(this._tweenTime, start[1], end[1]-start[1], time);
		this.SizeTo(w,h);
	}
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.CancelTween = function()
//---------------------------------------------------------------------------------------
// cancel the tweenTo method
{
	clearInterval( this._timer );
	this._timer        = null;
	this._tweenRunning = false;
	this._startFade    = true;
}
//
//=======================================================================================



//***************************************************************************************
//
// Easing Equations by Robert Penner - robertpenner.com -
//

//.......................................................................................
LinearTween = function(t, b, c, d)
//.......................................................................................
{
	return c*t/d + b;
}
//
//.......................................................................................


//.......................................................................................
EaseInQuad = function(t, b, c, d)
//.......................................................................................
{
	t /= d;
	return c*t*t + b;
}
//
//.......................................................................................


//.......................................................................................
EaseOutQuad = function(t, b, c, d)
//.......................................................................................
{
	t /= d;
	return -c * t*(t-2) + b;
}
//
//.......................................................................................


//.......................................................................................
EaseInOutQuad = function(t, b, c, d)
//.......................................................................................
{
	t /= d/2;
	if (t < 1) return c/2*t*t + b;
	t--;
	return -c/2 * (t*(t-2) - 1) + b;
}
//
//.......................................................................................


//.......................................................................................
EaseInExpo = function(t, b, c, d)
//.......................................................................................
{
	return c * Math.pow( 2, 10 * (t/d - 1) ) + b;
}
//
//.......................................................................................


//.......................................................................................
EaseOutExpo = function(t, b, c, d)
//.......................................................................................
{
	return c * ( -Math.pow( 2, -10 * t/d ) + 1 ) + b;
}
//
//.......................................................................................

//
// Easing Equations by Robert Penner - robertpenner.com -
//
//***************************************************************************************



//=======================================================================================
CHTMLobj.prototype.GetRandom = function( start, end )
//---------------------------------------------------------------------------------------
// generate new random number
// Added by SU, Uptonic.com
// December 2004 - January 2005
{
    this._randNum = Math.round( start + ((end-start) * Math.random()) );
    return this._randNum;
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.SetOpacity = function( opacity )
//---------------------------------------------------------------------------------------
// set opacity of the element
{
	
	opacity = ( opacity == 100 ) ? 99.999 : opacity;			// Fix for math error in some browsers
	this._css.filter       = "alpha(opacity:"+opacity+")";		// IE/Windows
	this._css.KHTMLOpacity = opacity/100;						// Safari < 1.2, Konqueror
	this._css.MozOpacity   = opacity/100;						// Older Mozilla and Firefox
	this._css.opacity	   = opacity/100;						// Safari 1.2, newer Firefox and Mozilla, CSS3   
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.FadeOut = function(opacity, change, speed)
//---------------------------------------------------------------------------------------
// gradually decrease the opacity of the element
// opacity: starting opacity of element
// change: the size of the increments between steps
// speed: the rate of the animation
{
	if ( opacity >= 0 )
	{
	  this._fadeRunning = true;
	  this.SetOpacity( opacity );
	  opacity -= change;
	  setTimeout(this._obj+'.FadeOut('+opacity+','+change+','+speed+')', speed);
	}
	else
	{
		this._fadeRunning = false;
		this.Hide();
	}
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.FadeIn = function(opacity, change, speed)
//---------------------------------------------------------------------------------------
// gradually increase the opacity of the element
// opacity: starting opacity of element
// change: the size of the increments between steps
// speed: the rate of the animation	
{
	if ( opacity <= 100 )
	{
		this.Show();
		this._fadeRunning = true;
		this.SetOpacity(opacity);
		opacity += change;
		setTimeout(this._obj+'.FadeIn('+opacity+','+change+','+speed+')', speed);
	}
	else
	{
		this._fadeRunning = false;
		this.SetOpacity(100);
	}
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.DisplayShow = function()
//---------------------------------------------------------------------------------------
// display the element as 'block'
{
	this._css.display = 'block';
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.DisplayHide = function()
//---------------------------------------------------------------------------------------
// do not display the element
{
	this._css.display = 'none';
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.SetSrc = function(target)
//---------------------------------------------------------------------------------------
// set the element's source to target
{
	this._el.src = target;
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.SetHref = function(target)
//---------------------------------------------------------------------------------------
// set the element's link to target
{
	this._el.href = target;
}
//
//=======================================================================================



//=======================================================================================
CHTMLobj.prototype.SetInnerHtml = function(content)
//---------------------------------------------------------------------------------------
// set the element's inner HTML to content
{
	this._el.innerHTML = content;
}
//
//=======================================================================================

//
// End of CHTMLObj class
//
//***************************************************************************************

