Ext.ns('FW.Widgets.AdRotator');
FW.Widgets.AdRotator=function(config) {
    FW.Widgets.IBaseObj.fw_init.call(this,config);
    FW.Widgets.AdRotator.superclass.constructor.call(this,config);
}
Ext.extend(FW.Widgets.AdRotator, Ext.Panel, FW.Widgets.IBaseObj);
CURRENT_PROTO=FW.Widgets.AdRotator.prototype;
Ext.reg('fw.widgets.adrotator',FW.Widgets.AdRotator);

CURRENT_PROTO.initComponent=function() {    
    this.addEvents('rotated');
    this.current_image_idx = -1;
    this.image_prefix = this.image_prefix || '';
    this.image_prefix = this.image_prefix.replace('/$','');
    this._start_clock();
    FW.Widgets.AdRotator.superclass.initComponent.call(this);
}

CURRENT_PROTO._start_clock=function() {
    if(this.task)
        this.task.cancel();
    this.task = new Ext.util.DelayedTask();
    this.task.delay(this.delay*1000, this._rotate_now, this);
}


CURRENT_PROTO._rotate_now=function() {
    this.current_image_idx += 1;
    if(this.current_image_idx >= this.images.length)
        this.current_image_idx = 0;
    var image_url = this.image_prefix + '/' + this.images[this.current_image_idx];
    Ext.get(this.image_id).dom.setAttribute('src', image_url);
    this.fireEvent('rotated', this.current_image_idx, image_url);
    this._update_href(image_url);
    this._start_clock();
}


CURRENT_PROTO._update_href=function(image_url) {
    if(Ext.isEmpty(this.target_id) || Ext.isEmpty(this.hrefs))
        return;

    var image_file = image_url.split('/').last();
    //console.log('image-file:' + image_file);
    Ext.get(this.target_id).dom.setAttribute('href', this.hrefs[image_file]);
}
