﻿/*
* 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";
}
/***/
function ExpandableList(listId, toggleId) {

    this.list = document.getElementById(listId);
    this.toggler = document.getElementById(toggleId);
	this.maxItems = 4;
	this.state = "collapsed";	
}
ExpandableList.prototype.display = function(maxItems) {

    var li = this.list.getElementsByTagName("li");
    var m = isNaN(maxItems) ? this.maxItems : maxItems;

    for (var i = 0; i < li.length; i++)
        li.item(i).style.display = "none";
    for (var i = 0; i < li.length && i < m; i++)
        li.item(i).style.display = "block";

    if (li.length < m )
        this.toggler.style.display = "none";    
}
ExpandableList.prototype.toggle = function() {

    if (this.state == "collapsed") {
        this.display(9999);
        this.state = "expanded";
        this.toggler.innerHTML = "Show Less";
        this.toggler.className = "collapse-link";
    } else {
        this.display(this.maxItems);
        this.state = "collapsed";
        this.toggler.innerHTML = "Show More";
        this.toggler.className = "expand-link";
    }
    return false;
}
