AutoRotator = function(params){
    var me = this;	
    this.element = $(params.element);
    this.timeout = params.timeout;
    this.longtimeout = params.longtimeout;
    this.tabactive = params.tabactive || "";
    
    this.images = this.element.find('.rotobanners');
    
    this.links = $("#main").find(".rotolinks a").click(function(){
        me.onLinkClick(this);
        return false;
    });
    this.i = 0;
    this.timer = setTimeout(function(){
        me.autoswitch()
    }, this.timeout);
}

AutoRotator.prototype = {
    onLinkClick: function(sender){
        var me = this;
        //		console.log(sender.hash);
        this.switchImage(sender.hash);
        clearTimeout(this.timer);
        this.timer = setTimeout(function(){
            me.autoswitch()
        }, this.longtimeout);
    },
    switchImage: function(hash){
        me = this;
        this.images.each(function(){
            var $this = $(this);			
            if ($this.is(hash)) {
                $this.fadeIn(500);
            }
            else {
                $this.hide();
            }
        })
        this.links.each(function(){
            var $this = $(this);
            var $activator = me.tabactive == 'self' ? $this : $this.parent();
            if ($this.attr('hash') == hash) {
                $activator.addClass('active');
            }
            else {
                $activator.removeClass('active');
            }
        })
    },
    autoswitch: function(){
        var me = this;
		//console.log(this, me, me.links);
		this.i = ++this.i % this.links.length;		
		$(this.links[this.i].hash).attr("rel",parseInt($(this.links[this.i].hash).attr("rel"))+1);
		if (parseInt($(this.links[this.i].hash).attr("rel")) < 2) {
			this.switchImage(this.links[this.i].hash);
			this.timer = setTimeout(function(){
				me.autoswitch()
			}, this.timeout);
		}		
    }
}
