/**
 * Various javascript effects used on site
 *
 * @author XShady
 * @project itzone
 */


/**
 * Class for making effect of scrolling element's content.
 *
 */
scrollElement = function(elementId, scroll, orientation, delay)
{
	// Find the element
    this.id = elementId;
    this.element = document.getElementById(this.id);

    if (!this.element)
    {
        if (this.debug)
    		window.alert('scrollElement.initialize(): DOM element ' + this.id + ' does not exist, but is required for this effect to operate');
        return false;
	}

	// Prepare properties ...
	this.orientation = (orientation == 'y' || orientation == 'Y') ? 'y' : 'x';
	this.delay = delay;
	this.scroll = scroll;
	// And set default values to the rest ...
	this.timer = false;
	this.progress = 0;
	this.objName = 'scrollElement';
	this.onFinish = '';
	this.startOn = 0;
	this.finishOn = 0;
	this.maxFPS = 10;

	// Get actual position - size of relevant margin
	var marginSize = (this.orientation == 'x')
		? this.element.style.marginLeft.replace('px', '')
		: this.element.style.marginTop.replace('px', '');
	this.marginSize = (marginSize == '') ? 0 : parseInt(marginSize, 10);
	this.originalMarginSize = this.marginSize;

	/**
	 * Starts scrolling of specified element
	 *
	 */
	this.startScroll = function()
	{
        this.startOn = new Date().getTime();
        this.finishOn = this.startOn + (this.delay * 1000);
        
        this.timer = setInterval(this.objName + '.loop()', 1000 / this.maxFPS);
        // XS: TODO: find out nicer way how to call our methods from interval
	};
	
	this.loop = function()
	{
	    // Check the time ;)
	    var timestamp = new Date().getTime();
	    var scrollStep = 0;
	    
	    // Calculate required position of the element at this moment
	    if (timestamp >= this.finishOn)
	    {
	        clearInterval(this.timer);
	        scrollStep = this.scroll;
	        this.progress = 100;
		}
		else
		{
		    var part = (timestamp - this.startOn) / (this.delay * 1000);
		    scrollStep = this.scroll * part;
		    this.progress = 100 * part;
		}
	    this.marginSize = this.originalMarginSize + scrollStep;
	    
	    // Let's set new element's position
	    if (this.orientation == 'x')
			this.element.style.marginLeft = this.marginSize + 'px';
		else
			this.element.style.marginTop = this.marginSize + 'px';
			
		if (this.progress == 100 && this.onFinish != '')
		    eval(this.onFinish);
	};
};


/**
 * Class for creating type-writer effect
 *
 * // XS: One day... need some randomizer...
typeWriter = function(elementId, text)
{
    // Find the element
    this.id = elementId;
    this.element = document.getElementById(this.id);
    this.text =

    if (!this.element)
    {
        if (this.debug)
    		window.alert('typeWriter.initialize(): DOM element ' + this.id + ' does not exist, but is required for this effect to operate');
        return false;
	}
}
*/