var thumbs=new Array();
var stat=new Array();
var thumb=new Array();

$(document).ready(function() {
  $('.hSprite').bind('mouseover',rota.start).bind('mouseout',rota.stop);
});

var rota = {'timer':0,'cache':{},'id':0,'curr':false, 'stopped':true};

rota.start2 = function(el) {
    var t = $(el);
    t.bind('mouseout',rota.stop);
    rota.start.call(el);
}
 
rota.start = function(e) {
    var t = $(this);
    rota.stop();
    rota.curr = t;
    rota.id = t.attr('id');
    rota.curr.css('background-position','0 0');
    //console.log('start: '+rota.id);
    if (!rota.cache[rota.id]) {
        img = new Image();
        img.loaded = false;
        rota.cache[rota.id] = img;
        img.vid = rota.id;
    } else img = rota.cache[rota.id];
    rota.stopped = false;
    if (!img.loaded) {
        t.parent().append('<span></span>');
        rota.loader = $('span',t.parent());
        $(img).bind('load',rota.onLoad);
        img.src = t.attr('sprite');
    } else {
        rota.begin();
    }
    //console.log('start: done');
}

rota.stop = function() {
    //console.log('stop: '+rota.id+' stopped('+rota.stopped+')');
    rota.stopped = true;
    clearTimeout(rota.timer);
    if (rota.curr) {
        rota.curr.css({'background-image':''});
        if (rota.loader) {
            rota.loader.remove();
            rota.loader = false;
        }
    }
    rota.id = false; rota.curr = false; rota.loader = false;
}

rota.onTime =  function(num) {
    clearTimeout(rota.timer);
    if (rota.stopped) {
        rota.stop();
        return true;
    }
    num++;
    if (num>9) num=0;
    //console.log('onTime: '+num);
    pos = (num*-235)+'px 0px';
    rota.curr.css({'background-position':pos});
    rota.timer = setTimeout('rota.onTime('+num+')',500);
}

rota.onLoad = function(e) {
    //console.log('onload: '+this.src);
    this.loaded = true;
    if (this.vid != rota.id) {
        //console.log('onload error: id='+rota.id+' img.id='+this.vid);
        return true;
    }
    rota.begin();
}

rota.begin = function() {
    if (rota.loader) {
        rota.loader.remove();
        rota.loader = false;
    }
    rota.curr.css({'background-position': '0 0'});
    rota.curr.css({'background-image':'url('+rota.curr.attr('sprite')+')'});
    rota.onTime(-1);
}
