/**
 * JavaScript Horizontal Scrolling List
 * Copyright (c) 2008. by MASSVision
 *
 * @version 1.0
 * @author Mladen Mijatov
 */

var hsl_timer = null;
var hsl_list = new Array();
var hsl_item_width = 0;

function hsl_Init(tagName, cName) {
   hsl_item_width = 120;
   hsl_GetItems(tagName, cName);

   hsl_InitialAlign();
   setTimeout("hsl_StartTimer();", 3000)
}

function hsl_GetItems(tagName, cName) {
   var list = document.getElementsByTagName(tagName);
   var eCount = 0;

   for(i = 0; i<list.length; i++) {
      var item = list[i];

      if (item.className == cName) {
         hsl_list[eCount] = item;
         eCount++;
      }
   }
}

function hsl_InitialAlign() {
   var xSpace = hsl_item_width;

   for(i = 0; i<hsl_list.length; i++) {
      var item = hsl_list[i];

      with (item.style) {
         position = 'absolute';
         left = (i * xSpace) + 'px';
         top = '0px';
      }
   }
}

function hsl_GetDim(obj) {
   if (obj.clientWidth)
      return [obj.clientWidth, obj.clientHeight]; else
      return [obj.width, obj.height];
}

function hsl_StartTimer() {
   hsl_timer = setInterval("hsl_MoveItems();", 10);
}

function hsl_GetLastItemPos() {
   var result = -1000;

   for (i=0; i<hsl_list.length; i++) {
      var item = hsl_list[i];

      with (item.style) {
         xPos = Number(left.substr(0, left.indexOf('px')));

         if (xPos > result) result = xPos;
      }
   }

   return result;
}

function hsl_MoveItems() {
   var xPos = 0;
   var shiftLeft = 5;

   startFrom = 0;
   for (i=0; i< hsl_list.length; i++) {
      var item = hsl_list[i];

      with (item.style) {
         xPos = Number(left.substr(0, left.indexOf('px')));
         if (xPos-shiftLeft < -hsl_item_width) {
            startFrom = i;
         }
      }
   }

   for (i=startFrom; i<hsl_list.length; i++) {
      var item = hsl_list[i];

      with (item.style) {
         xPos = Number(left.substr(0, left.indexOf('px')));

         if (xPos-shiftLeft < -hsl_item_width) {

            clearInterval(hsl_timer);
            setTimeout("hsl_StartTimer();", 3000)

            xPos = hsl_GetPosition(item);
            left = xPos + 'px';

         } else {
            xPos = xPos - shiftLeft;
            left = xPos + 'px';
         }

      }
   }

}

function hsl_GetPosition(obj) {
   var holder = document.getElementById("scroll_list");
   var item_count = hsl_list.length;
   var dim_holder = hsl_GetDim(holder);
   var last_item_pos = hsl_GetLastItemPos();
   var result = 0;

   if ((item_count * hsl_item_width) > dim_holder[0]) {
      result = last_item_pos + hsl_item_width;
   } else {
      result = dim_holder[0];
   }

   return result;
}


function switchToBelgrade() {
	document.getElementById('belgradeCell').className = 'selectorCellOn';
	document.getElementById('europeCell').className = 'selectorCell';
	document.getElementById('regionCell').className = 'selectorCell';
	
	// TODO: affect list
}
function switchToEurope() {
	document.getElementById('belgradeCell').className = 'selectorCell';
	document.getElementById('europeCell').className = 'selectorCellOn';
	document.getElementById('regionCell').className = 'selectorCell';
	
	// TODO: affect list
}
function switchToRegion() {
	document.getElementById('belgradeCell').className = 'selectorCell';
	document.getElementById('europeCell').className = 'selectorCell';
	document.getElementById('regionCell').className = 'selectorCellOn';
	
	// TODO: affect list
}