/* shobject 1.0, copyright(c) 2010 inventive designs <www.inventivedesigns.cz>
   ! - requires AddEvent() and removeEvent() functions defined below.
*/
var d = document;
var w = window;

var news = function(id) {
    var t = this;
    t.items = $('#' + id + ' .items');
    t.itemArray = $('#' + id + ' .items .item');
    t.switchArray = $('#' + id + ' .switches .switch');
    if (t.itemArray.length > 0) {
        t.activeItem = t.itemArray.first();
        t.activeSwitch = t.switchArray.first();
        var noFirstItemArray = t.itemArray.not(':first');
        var noFirstSwitchArray = t.switchArray.not(':first');
        // immediately hide all items but first
        noFirstItemArray.css('visibility', 'hidden');
        noFirstItemArray.animate({ 
            opacity: t.hiddenItemAlpha
        }, 0);
        // immediately hide all switches but first
        noFirstSwitchArray.animate({ 
            opacity: t.hiddenSwitchAlpha
        }, 0);
        // assign click action to switch
        t.switchArray.click(function() {
            var index = t.switchArray.index($(this));;
            t.stopTimer();
            if (!t.animating) {
                t.changeItem(index);
            }
        });
        // assign hover pause action
        t.items.hover(function(){
            t.over = true;
            t.stopTimer();
        }, function(){
            t.over = false;
            t.startTimer(t.mouseOutWaitDurCoef);
        });
    }
}
news.prototype = {
    waitDur: 4300,
    mouseOutWaitDurCoef: 0.3,
    showItemDur: 450,
    hideItemDur: 200,
    shownItemAlpha: 1.0,
    hiddenItemAlpha: 0.0,
    showSwitchDur: 500,
    hideSwitchDur: 500,
    shownSwitchAlpha: 1.0,
    hiddenSwitchAlpha: 0.3,
    run: function() {
        var t = this;
        t.startTimer();
    },
    startTimer: function(coef) {
        var t = this;
        if (!t.animating && !t.over) {
            var dur = t.waitDur;
            if (coef) {
                dur *= coef;
            }
            t.stopTimer();
            t.timeout = setTimeout(function(){ t.changeItem(); }, dur);
        }
    },
    stopTimer: function() {
        var t = this;
        if (t.timeout) {
            clearTimeout(t.timeout);
        }
    },
    changeItem: function(index) {
        var t = this;
        t.animating = true;
        var nextItem;
        var nextSwitch;
        var activeIndex = t.itemArray.index(t.activeItem);
        if (index != null) {
            if (activeIndex == index) {
                t.animating = false;
                return;
            }
        } else {
            // index was not passed, get index of next item in the loop
            index = activeIndex + 1;
            if (index >= t.itemArray.length) {
                index = 0;
            }
        }
        nextItem = t.itemArray.eq(index);
        nextSwitch = t.switchArray.eq(index);
        // hide the actual item
        t.activeItem.animate({
            opacity: t.hiddenItemAlpha
        }, t.hideItemDur, function() {
            t.activeItem.css('visibility', 'hidden');
            nextItem.css('visibility', 'visible');
            // after animation finish show the next item
            nextItem.animate({
                opacity: t.shownItemAlpha
            }, t.showItemDur, function() {
                // after animation finish set the timer again
                t.activeItem = nextItem;
                t.animating = false;
                t.startTimer();
            });            
        });
        t.activeSwitch.animate({
            opacity: t.hiddenSwitchAlpha
        }, t.hideSwitchDur);
        nextSwitch.animate({
            opacity: t.shownSwitchAlpha
        }, t.hideSwitchDur, function() {
            t.activeSwitch = nextSwitch;
        });
    }
}

function shobject(settings,id) {
  for (var i in settings) this[i] = settings[i];
  if (id) this.init(id);
}
shobject.prototype = {
  a_shown:100,
  a_hidden:0,
  dur_setNextAlpha:60,
  step:15,
  init:function(id) {
    this.id = id;
    this.a = this.a_hidden;
    this.setAlpha(this.a);
  },
  setAlpha:function(a,id) {
    if (!id) {
      var id = this.id;
      this.a = a;
      clearInterval(this.int_setAlpha);
    }
    var s = d.getElementById(id).style;
    s.opacity = (a/100);
    s.MozOpacity = (a/100);
    s.KhtmlOpacity = (a/100);
    s.filter = 'Alpha(opacity='+a+')';
    s.visibility = 'visible';
  },
  setNextAlpha:function(sh,a_end) {
    if (sh.sign*sh.a<a_end) {
      sh.a += sh.step*sh.sign;
    } else {
      sh.a = a_end;
      clearInterval(sh.int_setAlpha);
    }
    sh.setAlpha(sh.a,sh.id)
  },
  show:function() {
    clearInterval(this.int_setAlpha);
    var t = this;
    this.sign = 1;
    this.int_setAlpha = w.setInterval(function(){shobject.prototype.setNextAlpha(t,t.a_shown)},t.dur_setNextAlpha);
  },
  hide:function() {
    clearInterval(this.int_setAlpha);
    var t = this;
    this.sign = -1;
    this.int_setAlpha = w.setInterval(function(){shobject.prototype.setNextAlpha(t,t.a_hidden)},t.dur_setNextAlpha);
  }
}
// ^ end of shobject
function changeImg(id_img,url) {
  d.getElementById(id_img).src = url;
}
function setClass(id,c) {
  d.getElementById(id).className = c;
}
function setMouseOverOut(activator,fncOver,fncOut) {
  var el;
  if (activator.nodeType==1) el = activator;
  else el = d.getElementById(activator);
  if (fncOver) addEvent(el,'mouseover',function(){eval(fncOver)});
  if (fncOut) addEvent(el,'mouseout',function(){eval(fncOut)});
}
// unification of browsers's functions
function addEvent(o,eventName,fnc) {
  if(o.addEventListener) {
    o.addEventListener(eventName,fnc,true);
    return true;
  } else if (o.attachEvent) {
    var r = o.attachEvent('on'+eventName,fnc);
    return r;
  } else {
    return false;
  }
}
function removeEvent(o,eventName,fnc) {
  if(o.removeEventListener) {
    o.removeEventListener(eventName,fnc,true);
    return true;
  } else if (o.detachEvent) {
    var r = o.detachEvent('on'+eventName,fnc);
    return r;
  } else {
    return false;
  }
}
