﻿
HomePageFeatures = {

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    properties
    
*/ 


/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    methods
    
*/ 


//function to set the z-indexes of the images
    resetZIndexes: function()
    {
        var li = this.module.getElementsByTagName('li');
        for(var i = 0; i < li.length; i++)
        {
            if(i == 0)
            {
                var zIndex = '2';
            } else {
                var zIndex = '1';
            }
            li[i].style.zIndex = zIndex;
        }     
    },
    
//Function to validate tiles
    validate: function()
    {
        var count = 0;
        var li = this.module.getElementsByTagName('li');
        for(var i = 0; i < li.length; i++)
        {
            var a = li[i].getElementsByTagName('a');
            var img = li[i].getElementsByTagName('img');
            if(a.length == 1 && img.length == 1)
            {
                count++;
            }
        }  
        
        if(count == li.length)
        {
            return true;
        } else {
            return null;
        }
    },
    
//Function to get number of tiles
    getTilesCount: function()
    {
        var li = this.module.getElementsByTagName('li');
        return li.length;
    },

//Function to get current tile
    getCurrentTileNum: function()
    {
        var li = this.module.getElementsByTagName('li');
        for(var i = 0; i < li.length; i++)
        {
            if(li[i].style.zIndex == '2')
            {
                return i;
            }
        }  
        return -1;
    },

//Function which returns next tile
    getNext: function()
    {
        var current = this.getCurrentTileNum();
        if((current + 1) >= this.getTilesCount())
        {
            return 0;
        } else {
            return (current + 1);
        }
    },
    
//Function which returns previous tile
    getPrevious: function()
    {
        var current = this.getCurrentTileNum();
        if(current == 0)
        {
            return (this.getTilesCount() - 1);
        } else {
            return (current - 1);
        }
    },

//Function to set current tile
    setCurrent: function(x)
    {
        var li = this.module.getElementsByTagName('li');
        for(var i = 0; i < li.length; i++)
        {
            if(i == x)
            {
                var zIndex = '2';
            } else {
                var zIndex = '1';
            }
            li[i].style.zIndex = zIndex;
        }   
    },
    
//Function to set counter
    setCounter: function(x)
    {
        var counter = document.getElementById('featuresCounter');
        counter.innerHTML = '';
        counter.appendChild(document.createTextNode((x+1) + '/' + this.getTilesCount()));
    },    
        
//Function to attach navigation buttons    
    attachNavigation: function()
    {
        if(this.getTilesCount() < 2)return;
        
        var div = document.createElement('div');
        
        var h3 = document.createElement('h3');
        h3.appendChild(document.createTextNode('Features'));
        div.appendChild(h3);
        
        var a = document.createElement('a');
        a.className = 'previous';
        a.setAttribute('href','#');
        a.setAttribute('title','previous');
        a.appendChild(document.createTextNode('previous'));
        DH.addEvent(a,'click',HomePageFeatures.move);
        div.appendChild(a);
        
        var a = document.createElement('a');
        a.className = 'next';
        a.setAttribute('href','#');
        a.setAttribute('title','next');
        a.appendChild(document.createTextNode('next'));
        DH.addEvent(a,'click',HomePageFeatures.move);
        div.appendChild(a);
        
        var span = document.createElement('span');
        span.setAttribute('id','featuresCounter');
        span.appendChild(document.createTextNode('1/' + this.getTilesCount()));
        div.appendChild(span);
        
        this.module.appendChild(div);
    },
    
    move: function(oEvent)
    {
        var target = DH.stopEvent(oEvent);
        
        switch(target.className)
        {
            case 'previous':
                var num = HomePageFeatures.getPrevious();
                break;
            case 'next':
                var num = HomePageFeatures.getNext();
                break;   
            default:          
        }
        HomePageFeatures.setCurrent(num);
        HomePageFeatures.setCounter(num);
    },


    init: function()
    {
        if(!HomePageFeaturesCount)var HomePageFeaturesCount = 0;
		
		//Get reference to holder element
		this.module = document.getElementById('features');
		if(!this.module)
		{
			//If exceeded the number of determined iterations
			if(HomePageFeaturesCount > 10)
			{
				throw new Error('HomePageFeatures.init() exceeded its limit of iterations');
			}
			//Try again in 100 miliseconds
			HomePageFeaturesCount++;
			setTimeout('HomePageFeatures.init()',100);
			return;
		}
		
		//If structure is valid
		if(this.validate())
		{
            this.resetZIndexes();
            
            this.attachNavigation();
        }
    }


}

