var section='images';
var timeout = 600;
var timeout_over = 100;
var timeout_out = 600;
var act_publisher = 0;
var def_bigimage = '';
var def_description = '';
var scandinavian = false;
var debugdiv;
var brand_samples_img = new Array();
var brand_samples_link = new Array();
var brand_timer = null;
function add_sample(publisher_id, idx,img, link) {
    if (!idx) {
        brand_samples_img[publisher_id] = new Array();
        brand_samples_link[publisher_id] = new Array();
    }
    brand_samples_img[publisher_id].push(img);
    brand_samples_link[publisher_id].push(link);
    
    var o;
    if (o = find_object('brandname_'+publisher_id)) {
        o.onmouseover = function() {brand_over(publisher_id);};
        o.onmouseout = function() {brand_over(publisher_id, 0);};
    }

}
function brand_over(id, over) {
    if (over == undefined) over = 1;
    clearTimeout(brand_timer);
    if (id || !over) {
        act_publisher = over ? id : 0;
        brand_timer = setTimeout('brand_highlight('+id+','+over+')', over ? timeout_over : timeout_out);
    } else if (!over) {
//        alert('over');
    }
}
function brand_highlight(id, over) {
    if (over && (act_publisher != id)) return false;
    brand_highlight_css(id, over);
    if (!over && act_publisher) return false;
    if (typeof(scandf) != 'undefined') {
        if (over) {
            scandf.goto(id);
        } else {
            scandf.play();
        }
    }
}
function brand_highlight_css(id, over) {
    var on;
    if (on = find_object('brandname_'+id)) {
    //    on.className = over ? 'bsp re' : 'bsp a7';
    }
    var o;
    if (o = find_object('brand_samples')) {
        o.style.display = 'none';
        o.innerHTML = '';
        if (over && brand_samples_img[id]) {
            for(var i=0;i < brand_samples_img[id].length;i++) {
                var a = document.createElement('A');
                a.href = brand_samples_link[id][i];
                a.className = i ? 'brand_sample' : 'brand_sample_first';
                var img = document.createElement('IMG');
                img.src = brand_samples_img[id][i];
                a.appendChild(img);
                o.appendChild(a);
            }
            o.style.display = 'block';
            var oWidth = brand_samples_img[id].length * (188 +10) + 10;
            if (oWidth < o.offsetWidth) oWidth = o.offsetWidth;
            
            var l = findPosX(on) + on.offsetWidth + 10;
            var ww = mtn_ww();
            var m = false;
            if ((l + oWidth + 10) > ww) {
                var l1 = mtn_ww() - oWidth -10;
                l = l1>=0 ? l1 : 0;
                m = true;
            }
            o.style.left = l + 'px';

            var oHeight = 188 + 20;
            var posY = findPosY(on);
            var t = posY + on.offsetHeight;
            var scrolly=document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
            var hh = scrolly + mtn_hh();
            if ((t + oHeight + 10) > hh) {
                if (m) {
                    t = posY - 20 - oHeight;
                } else {
                    t = hh - oHeight - on.offsetHeight - 10;
                }
            }
            o.style.top = t + 'px';
        }
    }
}
function scandFader() {
    this.publishers = new Array();
    this.stopped = false;
    this.timeout = 5000;
    this.fade_speed = 8;
    this.fade_increment = 0.2
    this.fader = new CrossFade(this.fade_speed, this.fade_increment, this.timeout);
    this.fader.deleteFromImage = false;
    this.prev_idx = 0;
    this.idx = 1;
    this.fadeFrom = find_object('bigimage_0');
    this.fadeTo;
    this.infade = false;
}
scandFader.prototype.start = function(publishers) {
    //debugdiv = find_object("debug");
    this.publishers = publishers;
    var self = this;
    if (this.timer) {
        clearTimeout(this.timer); 
    }
    this.timer = setTimeout(function() {self.fade(self.prev_idx, self.idx);}, self.timeout);
}
scandFader.prototype.fade = function(from_idx, to_idx, force) {
    if (force == undefined) force = false;
    var ex = false;
    if (from_idx == to_idx) ex = true;
    if (this.stopped && !force)  ex = true;
    if (ex) {
        if (debugdiv)  debugdiv.innerHTML += 'fade exited: stopped-'+this.stopped+', force: '+force+', from: '+from_idx+', to:'+to_idx+'<br>';
        return false;
    }
    if ((this.fadeFrom = find_object('bigimage_'+this.publishers[from_idx])) && (this.fadeTo = find_object('bigimage_'+this.publishers[to_idx]))) {
        var self = this;
        if (debugdiv) debugdiv.innerHTML += 'fade start: prev_idx: '+from_idx +'idx: ' + to_idx + '<br>';
        this.infade = true;
        self.fader.fade(self.fadeFrom, self.fadeTo, function() {self.next();});
        brand_highlight_css(this.publishers[this.prev_idx], 0);
        brand_highlight_css(this.publishers[this.idx], 1);
    }
}
scandFader.prototype.next = function() {
    if (debugdiv) debugdiv.innerHTML += 'next : prev_idx: '+this.prev_idx +'idx: ' + this.idx + '<br>';
    this.infade = false;
    if (this.stopped) {
        if (debugdiv) debugdiv.innerHTML += 'next exited (stopped)<br>';
        return false;
    }
    var idx = this.idx+1;
    if (idx >= this.publishers.length) idx = 0;

    this.prev_idx = this.idx;
    this.idx = idx;
    var self = this;
    if (this.timer) {
        clearTimeout(this.timer); 
    }
    this.timer = setTimeout(function() {self.fade(self.prev_idx,self.idx);}, this.timeout);
    if (debugdiv) debugdiv.innerHTML += 'next fade after timeout: prev_idx: '+this.prev_idx +'idx: ' + this.idx + '<br>';
}
scandFader.prototype.play = function() {
    if (debugdiv) debugdiv.innerHTML += 'play! stopped: '+this.stopped+' infade: '+this.infade+'<br>';
    if (!this.stopped) return;
    if (debugdiv) debugdiv.innerHTML += '--- play calls next, stopped: ' + this.stopped+'<br>';

    this.stopAnimation();
    this.infade = false;
    this.stopped = false;
    this.next();
}
scandFader.prototype.goto = function(id) {
    var idx = -1;
    this.stopAnimation();
    for (var i=0;(i<this.publishers.length) && (idx == -1);i++) {
        if (this.publishers[i] == id) {
            idx = i;
        }
    }
    this.infade = false;
    this.stopped = true;
    if (this.idx != idx) {
        if (debugdiv) debugdiv.innerHTML += 'goto! stopped: ' + this.stopped+'<br>';
        this.prev_idx = this.idx;
        this.idx = idx;
        if (this.timer) {
            clearTimeout(this.timer); 
        }
        this.fade(this.prev_idx, this.idx, true);
    }
}   
scandFader.prototype.stopAnimation = function() {
    this.fader.stopAnimation();
    for (var i=0;i<this.publishers.length;i++) {
        if ((h = find_handle('bigimage_'+this.publishers[i])) && (i != this.idx)) {
            h.display = 'none';
        }
    }
}


