﻿/*
* hardcoded css class selectors - rotation-box / show-1 / button-thumb / button-text
* should ideally separate?!
*/

function RotationBox(objId)
{
	this.id = objId;
	this.counter = 1;
	this.rotateTime = 0; 
	this.rotateTimerId = null;
}
RotationBox.prototype.show = function(styleClass) {
    
	obj = document.getElementById(this.id);
	obj.className = 'rotation-box '+styleClass;			
	
	//check styleClass ends in 1...4)
	var slideSelected = styleClass.substr(-1);
	if (!isNaN(parseInt(slideSelected)))
	    this.counter = parseInt(slideSelected); 
}
RotationBox.prototype.init = function() {
	
	//center buttons		
	obj = document.getElementById(this.id);		
	this.centreButtonContent(obj);
	
	//show first tiem
	this.show('show-1');
	
	// setup timer
	this.enableRotator(true);
}
RotationBox.prototype.enableRotator = function(bEnable) 
{
    if (bEnable && this.rotateTime)
    {    
        var _this = this;
        this.rotateTimerId  = setInterval(function(){_this.rotateSlide();}, this.rotateTime);     
    }
    else 
    if(this.rotateTimerId)
    {
        clearInterval(this.rotateTimerId);
        this.rotateTimerId = null;
    }
}
RotationBox.prototype.rotateSlide = function() 
{
    if (this.counter++ > 3) 
        this.counter = 1;
        
    this.show("show-"+this.counter);     
}
RotationBox.prototype.centreButtonContent = function(parent)
{    
	if (parent.className && parent.nodeType == 1) {
		if (parent.className.search(/\b(button-thumb|button-text)\b/) != -1)
			RotationBox.verticalCentreChild(parent);
	}
	for(var i=0; i < parent.childNodes.length; i++) 
		this.centreButtonContent(parent.childNodes[i]);         
}
RotationBox.verticalCentreChild = function(parent)
{	
	parent.style.marginTop = Math.floor((parent.offsetParent.offsetHeight / 2) - (parent.offsetHeight/2)) + "px";
}

