function x_promotrack(id) {xmlreq('x_promotrack.php?link_id='+id, mtn_noresp);}
function bind_to_promo(pobj, section_id, ids) {
    if (pobj.params[section_id].length <2) return true;
    for (var i=0;i<ids.length;i++) {
        var o;
        if (o = find_object(ids[i])) {
            o.pobj = pobj;
            o.section_id = section_id;
            o.onmouseover = promoover;
            o.onmouseout = promoout;
        }
    }
    if (o = find_object('ehs')) {
        o.pobj = pobj;
        o.section_id = section_id;
    }
    if (o = find_object('sok')) {
        o.pobj = pobj;
        o.section_id = section_id;
    }
}
function promostate(pobj, section_id, stop) {
    
    if (pobj.params[section_id].length <2) return true;

    if (pobj.waitforstop[section_id] != stop) return false;
    if (pobj.stopped[section_id] == stop) return false;

    if (stop) {
        pobj.pause(section_id,false);
    } else {
        pobj.play(section_id, true, false);
    }
}
function Slider(div_id, speed, steps, pauseAfter) {
    this.div_id = div_id;
    this.div = find_handle(div_id);
    this.speed = speed;
    this.steps = steps;
    this.pauseAfter = pauseAfter;
    this.continueSlide = true;
}

Slider.prototype.slide = function(prev_id, id, from, to, afterSlide) {
    if (afterSlide == undefined) afterSlide = "";
    this.afterSlide = afterSlide;
    this.prev_id = prev_id;
    this.id = id;
    this.s = find_handle(this.id);
    this.prev_s = find_handle(this.prev_id);
    this.from = from;
    this.to = to;
    this._slide(0);
}
Slider.prototype._slide = function(curr) {
    var dir=this.from > this.to ? -1 : 1;
    curr+=Math.abs(this.to-this.from)/this.steps;
    if (curr > Math.abs(this.from-this.to)) { curr=Math.abs(this.from-this.to); }
    var x=2*Math.abs((curr-this.from)/(this.to-this.from));
    var fx=Math.round((2-(x*x-3*x+2))/2*Math.abs(this.to));
//    alert('left:'+this.div.left+', dir:'+dir+', this.from:'+this.from+', fx:'+fx+', curr:'+curr+', this.to:'+this.to+', this.steps:'+this.steps);
    var l  = this.from + dir*curr;
    this.div.left=l+'px';
//    ss.clip='rect(authis.to 0px authis.to 300px)';
    if (dir*this.from +curr<dir*this.to) {
        var self = this;
        this.timer = setTimeout(function(){self._slide(curr)},this.speed); 
    } else {
        this._finishSlide();
    }
}
Slider.prototype._finishSlide = function() {
    if (this.timer) { 
        clearTimeout(this.timer); 
    }
    if (this.div) this.div.left = this.to+'px';

    if (false && this.prev_id) {
        var o = find_object(this.prev_id);
        if (o) {
            var p = o.parentNode;
            if (p) p.removeChild(o);
        }
    }
    
    if (this.continueSlide && this.afterSlide != "") {
        setTimeout(this.afterSlide, this.pauseAfter);
        this.afterSlide = "";
    }
}

Slider.prototype.stopAnimation = function() {
    this.continueSlide = false;
    this._finishSlide();
}
function promoPlayer(params, effect) { 
    if ((effect == undefined) || (effect == '')) effect = 'fade';
    this.effect = effect;
    this.params = params;
    this.active = new Array();
    this.iter = new Array();
    this.html = new Array();
    this.animate = new Array();
    this.stopped = new Array();
    this.fader = new Array();
    this.slider = new Array();
    this.fade_speed = 8;
    this.fade_increment = 0.2;
    this.timeout = 10000;
    this.slide_speed = 100;
    this.slide_steps = 5;
    this.waitforstop = new Array();
    this.locked = new Array();
    this.manual = new Array();
}
promoPlayer.prototype.start = function() {
    for (var section_id in this.params) {
        if (this.params[section_id].length > 1) {
            this.html[section_id] = new Array();
            if (this.effect == 'fade') {
                this.fader[section_id] = new CrossFade(this.fade_speed, this.fade_increment, this.timeout);
            } else if (this.effect == 'slide') {
                this.slider[section_id] = new Slider('promo_body_'+section_id, this.slide_speed, this.slide_steps, this.timeout);
            }
            var self = this;
            var idx = first_index[section_id];
            var o;
            if (o = find_object('promo_body_'+section_id)) {
                this.html[section_id][idx] = o.innerHTML;
                o.promoobj = self;
            }
            this.animate[section_id] = true;      
            this.stopped[section_id] = false;      
            this.waitforstop[section_id] = false;
            this.locked[section_id] = false;
            this.set_active(section_id, idx);
            var next_idx = parseInt(this.iter[section_id])+1;
            if (next_idx >= this.params[section_id].length) next_idx = 0;
            var sid = section_id;
            setTimeout(function() {self.getver(sid,next_idx);}, this.timeout);
        }
    }
}
promoPlayer.prototype.set_active = function(section_id, idx) {
    var o;
    if (typeof(this.active[section_id]) == 'object') {
        if (o = find_object('promolink_'+section_id+'_'+this.active[section_id].idx)) {
            o.className = o.className.replace(/ re/, '');
        }
    }
    this.iter[section_id] = idx;
    this.active[section_id] = this.params[section_id][this.iter[section_id]];
    if (o = find_object('promolink_'+section_id+'_'+this.active[section_id].idx)) {
        o.className += ' re';
    }
    
}
promoPlayer.prototype.getver = function (section_id, idx, force) {
    if (force == undefined) force = this.animate[section_id] && !this.stopped[section_id] ? true : false;
    if (force) {
        if (idx == undefined) idx = parseInt(this.iter[section_id])+1;
        if (idx >= this.params[section_id].length) idx = 0;
        if (idx != this.iter[section_id]) {
            if (typeof(this.html[section_id][idx]) == 'undefined') {
                var url = 'x_promo.php?section_id='+section_id+'&idx='+idx+'&'+this.params[section_id][idx].param;
                xmlreq(url, this.getver_resp,this);
            } else {
                this.replace_div(section_id, idx, this.html[section_id][idx]);
            }
        }
    }
}
promoPlayer.prototype.getver_resp = function(rid) {
    if (mtn_request[rid]) {
        var a = mtn_request[rid].response.split('_::::_');
        if (a.length == 3) {
            var section_id =  a[0];
            var id = 'ver_'+section_id+'_'+this.params[section_id][a[1]].ver_id;
            a[2] = a[2].replace(id, id+'_'+a[1]);
            this.html[section_id][a[1]] = a[2]
            this.replace_div(section_id, a[1], this.html[section_id][a[1]]);
        }
    }
}
promoPlayer.prototype.replace_div = function(section_id, idx, html) {
    var prev_id = 'ver_'+section_id+'_'+this.active[section_id].ver_id+'_'+this.iter[section_id];
    var id = 'ver_'+section_id+'_'+this.params[section_id][idx].ver_id+'_'+idx;
    var o = find_object(prev_id);
    if (!o) {
        prev_id = 'ver_'+section_id+'_'+this.active[section_id].ver_id;
        o = find_object(prev_id);
    }
    if (o) {
        var p = o.parentNode;
        if (this.effect == 'fade') {
            var div = document.createElement('div');
            div.innerHTML = html;
            this.hide_content(div);
            p.innerHTML +=  div.innerHTML; 
            o = find_object(prev_id);
            var o1 = find_object(id);
            if (o && o1) {
                this.swap_fade(section_id, o, o1);
                this.set_active(section_id, idx);
            }
        } else if (this.effect == 'slide') {
            p.innerHTML +=  html; 
            this.swap_slide(section_id, prev_id, id);
            this.set_active(section_id, idx);
        }
    }
}
promoPlayer.prototype.hide_content = function(o) {
    for (var i=0;i<o.childNodes.length;i++) {
        var c = o.childNodes[i];
        if (c.style) {
            c.style.display = 'none';
        }
    }
}
promoPlayer.prototype.swap_fade = function(section_id, o1, o2) {
    this.fader[section_id].continueFade = this.animate[section_id];
    var self = this;
    this.fader[section_id].fade(o1, o2, function(){self.getver(section_id);});
}
promoPlayer.prototype.swap_slide= function(section_id, prev_id, id) {
    this.slider[section_id].continueSlide = this.animate[section_id];
    var self = this;
    this.slider[section_id].slide(prev_id,id, 0, -600, function(){self.after_slide(section_id, prev_id, id);});
}
promoPlayer.prototype.after_slide = function(section_id, prev_id, id) {
    this.getver(section_id);
}
promoPlayer.prototype.click = function(section_id, idx) {
    this.play(section_id, false);
    this.getver(section_id, idx, true);
}
promoPlayer.prototype.pause = function(section_id, man) {
    if (man == undefined) man = true;
    this.play(section_id,false, man);
}
promoPlayer.prototype.play = function(section_id, start, man) {
    if (man == undefined) man = true;
    if ((this.animate[section_id] == start) && (this.stopped[section_id] != start)) return false;
    if (start == undefined) start = true;
    if (start && this.locked[section_id]) return false;
    if (start && !this.stopped[section_id]) return false;
    if (start && this.stopped[section_id] && this.manual[section_id] && !man) {
        return false;
    }
    if (!this.stopped[section_id] || start) {
        this.manual[section_id] = man; 
    }
    if (this.effect == 'fade') {
        this.fader[section_id].stopAnimation();
    } else if (this.effect == 'slide') {
        this.slider[section_id].stopAnimation();
    }
    this.animate[section_id] = start;
    this.stopped[section_id] = !start;
    var o;
    if (o = find_object('promo_pause_button_'+section_id)) {
        o.src = 'g/promo/pause-'+(start ? 'off' : 'on') + '.gif';
    }
    if (o = find_object('promo_play_button_'+section_id)) {
        o.src = 'g/promo/play-'+(start ? 'on' : 'off') + '.gif';
    }
    if (start) {
        this.getver(section_id);
    }
}
