﻿// Website Common Functions

function getObjectsByTagAndClass(tag, cls)
{
   var array = document.getElementsByTagName(tag);
   var output = new Array();
   var matches = 0;
   for(var i = 0; i < array.length; i ++ )
   {
      var parts = array[i].className.split(' ');
      for(var j = 0; j < parts.length; j ++ )
      {
         if(parts[j] == cls)
         {
            output[matches ++ ] = array[i];

         }
      }
   }
   return output;
}

function selectTab(cls, index)
{
    var tabIndex = 0;
    var tabs = new Array();
    tabs = getObjectsByTagAndClass('li', cls);
    
    for (var i = 0; i <= tabs.length - 1; i++)
    {
        if (tabs[i])
        {
            tabIndex = (index <= tabs.length-1) ? index : 0;
            tabs[i].className = (i == tabIndex) ? cls + " selected" : cls;
        }
    }
}

var slidePanel = new Object();
slidePanel = function (tag, cls)
{
    // Properties
    this.AUTO_PLAY = true;
    this.AUTO_DIRECTION = "next";
    this.INTERVAL = 10;
    this.PLAY_HTML = "Play";
    this.PAUSE_HTML = "Pause";    
    // Varables
    var index = 0;
    var timer = null;
    var panels = new Array();
    panels = getObjectsByTagAndClass(tag, cls);
    // Functions
    this.renderPanel = function()
    {
        for (var i = 0; i <= panels.length - 1; i++)
        {
            if (panels[i])
            {
                panelIndex = (index <= panels.length-1) ? index : 0;
                panels[i].className = (i == panelIndex) ? cls + " selected" : cls;
            }
        }
        
        if (this.AUTO_PLAY == true)
        {
            var panelInstance = this;
            timer = setInterval(function(){panelInstance.cyclePanel(panelInstance.AUTO_DIRECTION)}, (this.INTERVAL * 1000));
        }
    }
    
    this.cancelAutoRun = function()
    {
        if(timer != "undefined")
        {
            clearInterval(timer);
        }
    }
    
    this.cyclePanel = function(dir)
    {
        if (dir == "next")
        {
             this.AUTO_DIRECTION = "next";
             index = (index < panels.length - 1) ? index + 1 : 0;
        }
        else if (dir == "prev")
        {
             this.AUTO_DIRECTION = "prev";
             index = (index > 0) ? index - 1 : panels.length - 1;
        }
        
        this.cancelAutoRun();
        this.renderPanel();
    }
    
    this.autoRun = function(idx)
    {
        var autoElement = document.getElementById(idx);
        
        if(this.AUTO_PLAY)
        {
            this.AUTO_PLAY = false;
            autoElement.innerHTML = this.PLAY_HTML;
        }
        else
        {
            this.AUTO_PLAY = true;
            autoElement.innerHTML = this.PAUSE_HTML;
        }
        
        this.cancelAutoRun();
        this.renderPanel();
    }
    
    this.load = function()
    {
        this.renderPanel();
    }
}
