DivvyUtils = new Object();

DivvyUtils.makeSampleDiv = function(className)
{
    var sampleDiv = document.createElement("div");
    sampleDiv.innerHTML = "monkey";
    sampleDiv.className = className;
    sampleDiv.style.width = "75px";
    sampleDiv.style.height = "75px";
    sampleDiv.style.textAlign = "center";
    sampleDiv.style.lineHeight = "75px";
    return sampleDiv;
}


/**
 * Layer object
 */

function DivvyLayer(layout)
{
    this.elementArray = new Array();
    this.uid = Object.generateUID();
    this.layout = (layout)?(layout):(null);
}

DivvyLayer.prototype.getUID = function()
{
    return this.uid;
}

DivvyLayer.prototype.addElement = function(elem)
{
    this.elementArray.push(elem);
}

DivvyLayer.prototype.setLayout = function(layout)
{
    this.layout = layout;
}

DivvyLayer.prototype.render = function()
{
    if(this.layout != null)
        this.layout.renderLayout(this.elementArray);
}

DivvyLayer.prototype.moveToTop = function(currTopZIndex)
{
    if(this.elementArray != null)
    {
        for(var i=0; i < this.elementArray.length; i++)
        {
            this.elementArray[i].style.zIndex = currTopZIndex++;
        }
    }
    
    return currTopZIndex;
}

/**
 * Simple layout object
 */

function SimpleLayout(offsetX, offsetY)
{
    this.offsetX = offsetX;
    this.offsetY = offsetY;
}
    
SimpleLayout.prototype.renderLayout = function(elementArray)
{
    if(elementArray != null)
    {
        for(var i=0; i < elementArray.length; i++)
        {
            elementArray[i].style.top = (this.offsetX * (i+1)) + "px";
            elementArray[i].style.left = (this.offsetY * (i+1)) + "px";
        }
    }
}

/**
 * Circle layout object
 */

function CircleLayout(radius, centerX, centerY)
{
    this.radius = radius;
    this.centerX = centerX;
    this.centerY = centerY;
}

CircleLayout.prototype.generatePoints = function(pointCount)
{
	var points = new Array();
	var x,y,angle;
	var arc = 360/pointCount;
	var i = 0;

	for(degrees=0;degrees<360;degrees=degrees+arc)
	{
		angle = degrees * (Math.PI/180);
		var point = new Object();
		point.x = Math.round(this.centerX + (this.radius*Math.cos(angle)));
		point.y = Math.round(this.centerY + (this.radius*Math.sin(angle)));
		points[i++] = point;
	}
	return points;
}    
    
CircleLayout.prototype.renderLayout = function(elementArray)
{
    if(elementArray != null)
    {
    		var points = this.generatePoints(elementArray.length);
        for(var i=0; i < elementArray.length; i++)
        {
            elementArray[i].style.top = points[i].y + "px";
            elementArray[i].style.left = points[i].x + "px"; 		
        }
    }
}

/**
 * Point layout object
 */

function PointLayout(centerX, centerY)
{
    this.centerX = centerX;
    this.centerY = centerY;
}  
    
PointLayout.prototype.renderLayout = function(elementArray)
{
    if(elementArray != null)
    {
        for(var i=0; i < elementArray.length; i++)
        {
            elementArray[i].style.top = this.centerY + "px";
            elementArray[i].style.left = this.centerX + "px";
        }
    }
}

/**
 * Shortcut method for document.getElementById().
 *
 * If a single id argument is passed in, the result is the single result of the 
 * document.getElementById() call on that argument.  If more than one id argument 
 * is passed in, the result is an array of html elements as returned by calling
 * document.getElementById on each of the parameters.
 */
function $() 
{
    var results = [];
    var element;
  
    for (var i = 0; i < arguments.length; i++) 
    {
        element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        results.push(element);
    }
    
    return results.length < 2 ? results[0] : results;
}

/**
 * Generates a unique ID that is based on Math.random() and the current time
 *
 * Useful for creating unique IDs for html elements
 */
Object.generateUID = function()
{
    var random = new String(Math.random()).split('.')[1];
    var currentTime = new Date().getTime();
    return random + "_" + currentTime;
}

