/* Copyright © 2008-2012, USA2BG.COM */

var g_idIbsTimer = 0;
var g_objIBS = null;
var g_iIbsTick = 0;
var g_iIbsSteps = 31;
var g_iIbsSlowSteps = 5;
var g_iIbsDirection = 0;
var g_iIbsLen = 864;
var g_iIbsH = 124;
var g_iIbsImgH = 248;
var g_fIbsStepLen = (g_iIbsLen - Math.pow(2,g_iIbsSlowSteps)) / (g_iIbsSteps - g_iIbsSlowSteps);

function _fnIbs()
{
  if( g_idIbsTimer ) {
    clearTimeout( g_idIbsTimer );
    g_idIbsTimer = 0;
  }
  if( g_objIBS )
  {
    if( g_iIbsTick <= g_iIbsSteps ) {
      if( g_iIbsTick < (g_iIbsSteps - g_iIbsSlowSteps) ) {
        var iOff = (g_iIbsDirection < 0 ? 0 : -g_iIbsLen) + Math.round( g_fIbsStepLen * (g_iIbsTick + 1) ) * g_iIbsDirection;
        var iTimeout = 12;
      }
      else {
        var iOff = Math.round( Math.pow(2, g_iIbsSteps - g_iIbsTick - 1) ) * g_iIbsDirection + parseInt( g_objIBS.style.left );
        var iTimeout = 50;
      }
      g_objIBS.style.left = iOff + "px";
      g_idIbsTimer = setTimeout( _fnIbs, iTimeout );
      g_iIbsTick++;
    }
    else {
      g_objIBS = null;
    }
  }
}

function _IbsGetNextImgIndex( objImg, iNext )
{
  var i = Math.round( Math.abs( parseInt( objImg.style.top ) ) / g_iIbsH );
  var len = Math.round( g_iIbsImgH / g_iIbsH );
  return iNext>0? ( (i+1)>=len? 0 : (i+1) ) :  ( (i-1)<0? (len-1) : (i-1) );
}

function IbsRotate( iDirection )
{
  if( !document.getElementById ) {
    return false;
  }
  if( !g_objIBS )
  {
    g_iIbsTick = 0;
    g_iIbsDirection = iDirection > 0 ? 1 : -1;

    g_objIBS = document.getElementById("ibs-slider");
    var objImg1 = document.getElementById("ibs-img1");
    var objImg2 = document.getElementById("ibs-img2");

    if( g_objIBS.style.left == '' ) { g_objIBS.style.left = "0px"; }
    if( objImg1.style.top == '' ) { objImg1.style.top = "0px"; }
    if( objImg2.style.top == '' ) { objImg2.style.top = "0px"; }

    if( g_iIbsDirection < 0 ) {
      if( parseInt( g_objIBS.style.left ) != 0 ) {
        objImg1.style.top = objImg2.style.top;
        g_objIBS.style.left = "0px";
      }
      var iIndex = _IbsGetNextImgIndex( objImg1, 1 );
      objImg2.style.top = (-iIndex * g_iIbsH) + 'px';
    }
    else {
      if( parseInt( g_objIBS.style.left ) == 0 ) {
        objImg2.style.top = objImg1.style.top;
        g_objIBS.style.left = g_iIbsLen + "px";
      }
      var iIndex = _IbsGetNextImgIndex( objImg2, -1 );
      objImg1.style.top = (-iIndex * g_iIbsH) + 'px';
    }

    _fnIbs();
  }
  else /* fast anim end on second call */
  {
    if( g_iIbsDirection < 0 ) {
      g_objIBS.style.left = -g_iIbsLen + "px";
    }
    else {
      g_objIBS.style.left = "0px";
    }
    g_objIBS = null;
  }
  return false;
}

