ScrollingLogo = function(src, width) {
    this.src = src;
    this.width = width;
}

ScrollingLogo.prototype.getSrc = function() {
    return this.src;    
}

ScrollingLogo.prototype.getWidth = function() {
    return this.width;
}

ScrollingLogoList = function() {}

ScrollingLogoList.DESIRED_FRAME_RATE = 20.0;
ScrollingLogoList.WIDGET_DIV_ID = "logowrapper";
ScrollingLogoList.IMAGE_DIVS = new Array("scrollingImage1", "scrollingImage2");
ScrollingLogoList.LOGO_DATA = new Array(new ScrollingLogo("images/logos_1.png", 881),
					new ScrollingLogo("images/logos_2.png", 881),
					new ScrollingLogo("images/logos_3.png", 740),
                                        new ScrollingLogo("images/logos_4.png", 765)); 
ScrollingLogoList.PADDING = 20;
ScrollingLogoList.NUM_CHUNKS_TO_DISPLAY = 2;

ScrollingLogoList.currentWidgetWidth = 0;
ScrollingLogoList.currentLogoWidths = new Array();
ScrollingLogoList.currentOrdering;

ScrollingLogoList.initialize = function()
{
	ScrollingLogoList.currentOrdering = new RandomCyclicalIterator(ScrollingLogoList.LOGO_DATA);
	
	for (var i = 0; i < ScrollingLogoList.IMAGE_DIVS.length; i++)
	{
		var logoData = ScrollingLogoList.currentOrdering.next();
		
		ScrollingLogoList.currentWidgetWidth += logoData.getWidth();
		ScrollingLogoList.currentLogoWidths.push(logoData.getWidth());
		
		var imageContainer = document.getElementById(ScrollingLogoList.IMAGE_DIVS[i]);
		imageContainer.style.width = logoData.getWidth() + "px";
		
		if (i > 0)
		{
			imageContainer.style.left = (ScrollingLogoList.currentWidgetWidth - logoData.getWidth()) + (ScrollingLogoList.PADDING * i+1) + "px";
		}
		
		var image = new Image();
		image.src = logoData.getSrc();
		
		var onload = "";
		
        if (i == 0)
        {
            onload = "onload=\"javascript:ScrollingLogoList.scrollLoop();\"";
        }
		
		// once again, we have to do it this way instead of using a nice clean
		// method like appendChild() because internet explorer is stupid
		imageContainer.innerHTML = "<img src=\"" + image.src + "\" " + onload + "/>";
	}
}

ScrollingLogoList.scrollLoop = function()
{
    var logoContainer = document.getElementById(ScrollingLogoList.WIDGET_DIV_ID);
    
    var children = logoContainer.childNodes;
    
    for (var i = 0; i < children.length; i++)
	{
		var child = children[i];
		
        var currentLeftOffset = parseInt(child.style.left);
        // have this as a hardcoded array lookup because accessing the width property through the DOM is SLOWWWW
		var imageWidth = ScrollingLogoList.currentLogoWidths[i];	
			
        if (currentLeftOffset != -imageWidth)
        {
            child.style.left =  currentLeftOffset - 1 + "px";
        }
        else
        {
            child.style.left = ScrollingLogoList.currentWidgetWidth - imageWidth + (ScrollingLogoList.PADDING * ScrollingLogoList.IMAGE_DIVS.length) + "px";
            ScrollingLogoList.replaceImage(child, i);
        }
	}
	
	setTimeout(ScrollingLogoList.scrollLoop, 1000 / ScrollingLogoList.DESIRED_FRAME_RATE);
}

ScrollingLogoList.replaceImage = function(element, childPosition)
{
	var oldWidth = ScrollingLogoList.currentLogoWidths[childPosition];
	var newLogoData = ScrollingLogoList.currentOrdering.next();
	
    var nextImage = new Image();
    nextImage.src = newLogoData.getSrc();
    
    // we have to generate the HTML here instead of just replacing the node's
    // child cause internet explorer is retarded and retains the old node's
    // height and width    

    var newImageHtml = "<img src='" + nextImage.src + "'/>";

    element.innerHTML = newImageHtml;
    element.style.width = newLogoData.getWidth() + "px";
    
    ScrollingLogoList.currentWidgetWidth += (newLogoData.getWidth() - oldWidth);
    ScrollingLogoList.currentLogoWidths[childPosition] = newLogoData.getWidth();
}

RandomCyclicalIterator = function(array) {
	this.array = array;
	this.position = 0;
	this.permutedIndices = RandomCyclicalIterator.getPermutedIndices(this.array.length);
}

RandomCyclicalIterator.getPermutedIndices = function(arrayLength) 
{
	var permutedIndices = new Array();
	var possibleIndices = new Array(arrayLength);
	
	for (var i = 0; i < arrayLength; i++)
	{
		possibleIndices[i] = i;
	}
	
	while (possibleIndices.length != 0)
	{
		var randomIndex = Math.round(Math.random() * (possibleIndices.length - 1));
		var chosenIndex = possibleIndices[randomIndex];
		possibleIndices.splice(randomIndex,1);
		permutedIndices.push(chosenIndex);
	}
	
	return permutedIndices;
}

RandomCyclicalIterator.prototype.next = function()
{
	var nextElement = this.array[this.permutedIndices[this.position]];
	
	this.position++;
	
	if (this.position >= this.array.length)
	{
		this.position = 0;
	}
	
	return nextElement;
}

ScrollingLogoList.initialize();
