var RImageRotatorObjects = new Array();

function addRImageRotatorObject(rotator)
{
	RImageRotatorObjects.push(rotator);
}

function onRImageRotatorMouseOut(id)
{
	var i;
	for(i = 0; i < RImageRotatorObjects.length; i++)
	{
		if(RImageRotatorObjects[i].divId == id)
			RImageRotatorObjects[i].activate();
	}
}

function onRImageRotatorMouseOver(id)
{
	var i;
	for(i = 0; i < RImageRotatorObjects.length; i++)
	{
		if(RImageRotatorObjects[i].divId == id)
			RImageRotatorObjects[i].deactivate();
	}
}

function RImageRotatorRotateObjects()
{
	var i;
	for(i = 0; i < RImageRotatorObjects.length; i++)
	{
		RImageRotatorObjects[i].rotate();
	}
	window.setTimeout(RImageRotatorRotateObjects, 100);
}

window.setTimeout(RImageRotatorRotateObjects, 100);

function RImageRotator(divId, imageWidth, imageHeight, visibleCount, 
	speed, delay, direction, layout, space)
{
	this.g = 0;
	if(speed < 100)
		speed = 100;
	if(delay < 0)
		delay = 10;
	if(visibleCount < 0)
		visibleCount = 1;
	delay *= 10;
	this.borderX = space;
	this.borderY = space;
	this.reposition_counter = 5;
	this.direction = direction;
	this.stopped_state = 0;
	this.rotating_state = 1;
	this.current_state = this.stopped_state;
	this.current_absx = -1;
	this.current_absy = -1;
	this.divId = divId;
	this.imageWidth = imageWidth;
	this.imageHeight = imageHeight;
	this.visibleCount = visibleCount;
	this.speed = speed;
	this.delay = delay ;
	this.current_delay = delay;
	this.layout = layout;
	this.offset = 0;
	this.rot_offset = 0;
	this.active = false;
	this.images = new Array();
	this.rot_steps = speed / 100;
	this.objectImages = new Array();
	this.current_rot_steps = this.rot_steps;
	var targetDiv = document.getElementById('rimagerotator_scroll_div' + this.divId);
	this.targetDivWidth = targetDiv.offsetWidth;
	this.targetDivHeight = targetDiv.offsetHeight;
	if(this.layout == 'horizontal')
	{
		this.borderY = (targetDiv.offsetHeight - this.imageHeight) / 2;
		this.delta = (this.imageWidth + this.borderX) / this.rot_steps;
		
	}
	else
	{
		this.borderX = (targetDiv.offsetWidth - this.imageWidth) / 2;
		this.delta = (this.imageHeight + this.borderY) / this.rot_steps;
	}
	this.showcounter = 0;
}

RImageRotator.prototype.addImage = function(imageUrl, link, alt)
{
	var objImage = new Image();
	objImage.src = imageUrl;
	this.objectImages.push(objImage);
	var image = new Array(2);
	image[0] = imageUrl;
	image[1] = link;
	image[2] = alt;
	this.images.push(image);
}

RImageRotator.prototype.activate = function()
{
	this.active = true;
}

RImageRotator.prototype.deactivate = function()
{
	this.active = false;
}

RImageRotator.prototype.debug = function(text)
{
	var web_debug = document.getElementById('web_debug' + this.divId);
	if(web_debug)
		web_debug.innerHTML = text;
}

RImageRotator.prototype.show = function()
{
	if(this.images.length <= 0)
		return;
	var targetDiv = document.getElementById('rimagerotator_scroll_div' + this.divId);
	var divWidth = targetDiv.offsetWidth;
	var divHeight = targetDiv.offsetHeight;
	if(this.layout == 'horizontal')
	{
		this.borderY = (divHeight - this.imageHeight) / 2;
		this.delta = (this.imageWidth + this.borderX) / this.rot_steps;
		
	}
	else
	{
		this.borderX = (divWidth - this.imageWidth) / 2;
		this.delta = (this.imageHeight + this.borderY) / this.rot_steps;
	}
	var clipWidth = this.visibleCount * (this.imageWidth + 2 * this.borderX);  //targetDiv.offsetWidth; 
	var absx = this.getAbsXCoord(targetDiv);
	var absy = this.getAbsYCoord(targetDiv);
	this.current_absx = absx;
	this.current_absy = absy;
	var i;
	
	var startx = absx + this.borderX;
	if(this.layout == 'horizontal')
	{
		startx = absx + this.borderX + (divWidth - clipWidth) / 2;
	}
	var starty = absy + this.borderY;
	var incrX = 0;
	var incrY = 0;
	var deltaX = 0;
	var deltaY = 0;
	if(this.layout == 'horizontal')
	{
		incrX = this.imageWidth + 2 * this.borderX;
		deltaX = this.rot_offset;
	}
	else
	{
		incrY = this.imageHeight + 2 * this.borderY;
		deltaY = this.rot_offset;
	}
	targetDiv.innerHTML = '';
	var html = '';
	for(i = -1; i < this.visibleCount + 1; i++)
	{
		var x = startx + i * incrX + deltaX;
		var y = starty + i * incrY + deltaY;
		var width = this.imageWidth;
		var height = this.imageHeight;
		var posX = 0;
		var posY = 0;
		var opacity = 100;
		var divId = 'rimage_inner_div_' + (i + 1);
		if(x > startx + clipWidth || y > absy + divHeight || x + width < startx || y + height < starty)
			continue;
		if(x + width > startx + clipWidth)
			width = startx + clipWidth - x;
		if(y + height > absy + divHeight)
			height = absy + divHeight - y;
		if(x < startx)
		{
			width -= startx - x;
			posX = startx - x;
			x = startx;
		}
		if(y < starty)
		{
			height -= starty - y;
			posY = starty - y;
			y = starty;
		}
		var index = (i + this.visibleCount + this.offset) % this.images.length;
		
		html += '<div style="';
		html += 'position:absolute;left:' + x + 'px;top:' + y + 'px;';
		html += 'width: ' + width + 'px; height: ' + height + 'px; overflow: hidden;';
		if(width < this.imageWidth)
		{
			opacity = width * 100 / this.imageWidth;
		}
		else if(height < this.imageHeight)
		{
			opacity = height * 100 / this.imageHeight;
		}
		if(opacity < 100)
		{
			html += 'filter: alpha(opacity = ' + opacity + ');';
			html += '-moz-opacity:' + (opacity / 100) + ';';
		}
		html += '">';
//		html += '<a style="border-width: 0px;" ';
//		html += 'href="' + this.images[index][1] + '">';
		html += '<img id="r_portrait_image_' + index + '" src="' + this.images[index][0] + '" width="' + 
					this.imageWidth + 'px" height="' + this.imageHeight + '" border="0"';
		html += 'onmouseout="onRImageRotatorMouseOut(\'' + this.divId + '\')" ';
		html += 'onmouseover="onRImageRotatorMouseOver(\'' + this.divId + '\')" ';
		html += '/>';
//		html += '</a>';
		html += '</div>';
	}
	targetDiv.innerHTML += html;
}

RImageRotator.prototype.imagesLoaded = function()
{
	if(this.images.length <= 0)
		return true;
	for(i = -1; i < this.visibleCount + 1; i++)
	{
		var index = (i + this.visibleCount + this.offset) % this.images.length;
		var img = document.getElementById('r_portrait_image_' + index);
		if(img != null && !img.complete)
			return false;
	}
	return true;
}

RImageRotator.prototype.rotate = function()
{
	if(this.images.length > 0)
	{
		if(this.active)
		{
			if(this.current_state == this.stopped_state)
			{
				this.current_delay--;
				if(this.current_delay <= 0)
				{	
					if(this.imagesLoaded())
					{
						this.current_state = this.rotating_state;
						this.current_rot_steps = this.rot_steps;
						this.rot_offset = 0;
					}
					else
					{
						this.current_delay = this.delay;
					}
				}
				if(this.reposition_required() && this.imagesLoaded())
					this.show();
			}
			else if(this.current_state == this.rotating_state)
			{
				this.current_rot_steps--;
				this.rot_offset += this.direction * this.delta;
				if(this.current_rot_steps <= 0)
				{
					this.current_state = this.stopped_state;
					this.current_delay = this.delay;
					this.rot_offset = 0;
					this.offset += -this.direction;
					this.offset += this.images.length;
					this.offset %= this.images.length;
					this.current_rot_steps = 0;
				}
				this.show();
			}
		}
		else if(this.current_state == this.stopped_state)
		{
			if(this.reposition_required() && this.imagesLoaded())
				this.show();
		}
	}
}

RImageRotator.prototype.reposition_required = function()
{
	if(--this.reposition_counter <= 0)
	{
		this.reposition_counter = 10;
		var targetDiv = document.getElementById('rimagerotator_scroll_div' + this.divId);
		var absx = this.getAbsXCoord(targetDiv);
		var absy = this.getAbsYCoord(targetDiv);
		
		return (absx != this.current_absx || absy != this.current_absx || 
			targetDiv.offsetWidth != this.targetDivWidth || 
			targetDiv.offsetHeight != this.targetDivHeight);
	}
	else
		return false;
}

RImageRotator.prototype.getPositionType = function(element)
{
	if(window.getComputedStyle)
		return window.getComputedStyle(element,null).position;
	else if(element.currentStyle)
		return element.currentStyle.position;
	else
		return 'static';
}

RImageRotator.prototype.getAbsXCoord = function(element)
{
    var x = 0;
	var node = element;
    while(node != null && this.getPositionType(node) == 'static')
    {
        x += node.offsetLeft;
        node = node.offsetParent;
    }
	this.g++;
    return x;
}

RImageRotator.prototype.getAbsYCoord = function(element)
{
    var y = 0;
	var node = element;
    while(node != null && this.getPositionType(node) == 'static')
    {
        y += node.offsetTop;
        node = node.offsetParent;
    }
    return y;
}

