function mtn_resize() {
    var ww=mtn_ww();
    mtn_bsess('screen',ww); 
    mtn_screen=ww;
    if (typeof(qp) != 'undefined') {
        qp.resize(ww);
    }
}
function browse_iconview(on) {
    setActiveStyleSheet(on ? 'cd_iconview.css' : 'cd_textview.css');
    mtn_bsess('cdbrowse_iconview', on);
    if (on) qp.initnextall();   
}
function setActiveStyleSheet(title) {
    var i, a, main;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
        a.disabled = a.getAttribute("title") == title ? false : true;
        }
    }
}
// browseQP start -----------------------------------------------------------
function browseQP (request_url, cdbundles, iconview) {
    if (request_url == undefined) request_url = 'browse_qpreview.php';
    if (cdbundles == undefined) cdbundles = 0;
    if (iconview == undefined) iconview = true; 
    this.iconview = iconview;
    this.imagewidth = 160;
    this.leftmargin = 250;
    this.rightmargin = 10;
    this.cdbundles = cdbundles;
    this.request_url = request_url;
    this.perpage = 4;
    this.initnext = iconview;

    this.qp_prdc = new Array();
    this.qp_rid = new Array();
    this.qp_maxpage = new Array();
    this.qp_actpage = new Array();
    this.qp_imagecount = new Array();
    this.qp_cdbundles = new Array();
    this.inslide = false;
    this.navig_text =lrs['br_qp_images'];
    this.qp_navig_text = new Array();
}

browseQP.prototype.add = function (prdc, imagecount, cdbundles, navig_text) {
    if (cdbundles == undefined) cdbundles = this.cdbundles;
    if (navig_text == undefined) navig_text = this.navig_text;
    this.qp_prdc.push(prdc);
    this.qp_cdbundles[prdc] = cdbundles;
    this.qp_imagecount[prdc] = imagecount;
    this.qp_navig_text[prdc] = navig_text;
    if (this.iconview) {
        this.init(prdc);
    }
}

browseQP.prototype.initnextall = function (prdc) {
    if (this.initnext) return false;
    for (var i=0;i<this.qp_prdc.length;i++) {
        this.init(this.qp_prdc[i]);
    }
    this.initnext = true;
}

browseQP.prototype.init = function (prdc) {
    var url = this.request_url + '?next=1' + (this.qp_cdbundles[prdc] ? '&cdbundles=1' : '');
    this.nav(prdc, url);
}

browseQP.prototype.nav = function (prdc, url) {
    if (this.qp_maxpage[prdc] == undefined) {
        this.qp_maxpage[prdc] = 0;
        this.qp_actpage[prdc] = -1;
    } else if (this.inslide) return false;
    if ((url.indexOf('next=1') != -1)) {

        if (this.qp_actpage[prdc]+2 > this.qp_maxpage[prdc]) {
            this.qp_maxpage[prdc] = this.qp_actpage[prdc]+2;
            if (this.perpage != undefined) url += '&by='+this.perpage;                    
            url += '&o='+this.qp_actpage[prdc];
            url += '&cd='+prdc;
            var rn=Math.floor(Math.random()*301234);
            url += '&rn='+rn;
            var rid = xmlreq(url, this.nav_resp, this);
            this.qp_rid[rid] = prdc;
        } else {
            this.pos(prdc, this.qp_actpage[prdc],1);
        }
    } else {
        this.pos(prdc, this.qp_actpage[prdc],-1);
    }
}

browseQP.prototype.nav_resp = function(rid, o) {
//    var resp = mtn_request[rid].response;
    var resp = o.response;

    var prdc = this.qp_rid[rid];
    var aresp = resp.split('_::_');
    if (aresp.length == 1) {
        page = parseInt(aresp[0]);
        if (page >=0) {
            this.pos(prdc, page, 1);
        }
    } else if (aresp.length > 2) {
        var page = parseInt(aresp[1]);
        var count = parseInt(aresp[2]);
//        alert('page: '+page+', count: ' + count);
        var actpage = page+1;
        if (page) {
            this.pos(prdc, page-1, 1);
        } else {
            this.qp_actpage[prdc] = 0;
        }
        var o;
        var tr;
        if ((o = find_object('table_'+prdc)) && (tr = find_object('images_'+prdc))) {
            o.innerHTML = '<table cellspacing="0" ><tr id="images_'+prdc+'">'+tr.innerHTML+aresp[0]+'</tr></table>';
        } 
        if (h = find_handle('table_'+prdc)) {
            var w = parseInt(h.width.replace("px", "").replace(";", ""));
//                alert('table width: '+w + ', added images: '+count);
            h.width = (w + count * this.imagewidth) +'px';
        }
    }
} 

browseQP.prototype.pos = function(prdc, page, dir) {
    var actpage = page+dir;
    this.qp_actpage[prdc] = actpage;
    this.set_navlinks(prdc);
    var ht;
    if (ht = find_handle('table_'+prdc)) {
        var fr = parseInt(ht.left.replace("px", "").replace(";", ""));
        if (isNaN(fr)) fr = 0;
        this.inslide = true;
        this.slide(prdc, fr, 0, parseInt(fr -dir*this.imagewidth*this.perpage), 10);
    }
}
browseQP.prototype.set_navlinks= function(prdc) {
    var o;
    if (o = find_object('navlinks_'+prdc)) {
        var html = '';
        if (this.qp_actpage[prdc]>0) {
            html += '<a class="naviglink ql" onclick="qp.nav(\''+prdc+'\', \''+ this.request_url+'?prev=1&cdbundles='+this.qp_cdbundles[prdc]+'\');">&lt;</a>';
        } else {
            html += '<span class="navigdead">-</span>';  
        } 
        html += '&nbsp;' + this.qp_navig_text[prdc]+ '&nbsp;';//+this.qp_actpage[prdc];
        if ((this.qp_actpage[prdc]+1)*this.perpage < this.qp_imagecount[prdc]) {
            html += '<a class="naviglink ql" onclick="qp.nav(\''+prdc+'\', \''+ this.request_url+'?next=1&cdbundles='+this.qp_cdbundles[prdc]+'\');">&gt;</a>';
        } else {
            html += '<span class="navigdead">-</span>';  
        }
//        if (prdc == 'COR110') alert(html);
		if (html != '&lt;&nbsp;undefined&nbsp;&gt;') o.innerHTML = html;
    }
}
browseQP.prototype.slide = function(prdc,from,curr,to,steps) {
    var dir=from > to ? -1 : 1;
    var s=find_handle('table_'+prdc);
    if (!s) { return; }
    curr+=Math.abs(to-from)/steps;
    if (curr > Math.abs(from-to)) { curr=Math.abs(from-to); }
    var x=2*Math.abs((curr-from)/(to-from));
    var fx=Math.round((2-(x*x-3*x+2))/2*Math.abs(to));
//    alert('left:'+s.left+', dir:'+dir+', from:'+from+', fx:'+fx+', curr:'+curr+', to:'+to+', steps:'+steps);
    var l  = from + dir*curr;
    s.left=l+'px';
//    ss.clip='rect(auto 0px auto 300px)';
    if (dir*from +curr<dir*to) {
        var self = this;
        setTimeout(function(){ self.slide(prdc,from,curr,to,steps)},38); 
    } else {
        this.inslide = false;
    }
}
browseQP.prototype.resize = function(ww) {
//    return true;
    var prev_perpage = this.perpage;
    this.perpage = Math.floor((ww-this.leftmargin+this.rightmargin)/this.imagewidth);
    if (this.perpage < 1) this.perpage = 1;
    if (this.perpage == prev_perpage) return true;
    var divw = this.perpage * this.imagewidth-this.rightmargin;
    var ds;
    for (var i=0;i<this.qp_prdc.length;i++) {
        var prdc = this.qp_prdc[i];
        if (ds = find_handle('div_'+prdc)) {
            ds.width = divw+'px';
        }
        var prev_actpage = this.qp_actpage[prdc];
        this.qp_actpage[prdc] = Math.floor(prev_actpage*prev_perpage/this.perpage);
        if (this.qp_actpage[prdc] < 0) this.qp_actpage[prdc] =0;
        if (h = find_handle('table_'+prdc)) {
            h.left = '-'+ (this.qp_actpage[prdc]*this.perpage*this.imagewidth)+'px';
        }
        var prev_maxpage = this.qp_maxpage[prdc];
        var imagecount = (prev_maxpage + 1) * prev_perpage;
        var maxpage = Math.floor(imagecount/this.perpage);
        var url = '';
        if ((maxpage+1)*this.perpage != imagecount) {
            var by = (maxpage+1)*this.perpage;
            if (by > this.qp_imagecount[prdc]) by = this.qp_imagecount[prdc];
            if (by < 1) by=1;
            url = this.request_url + '?next=1&cd='+prdc+'&o=-2&by='+by+'&cdbundles='+this.qp_cdbundles[prdc];
            var rid = xmlreq(url, this.part_resp, this);
            this.qp_rid[rid] = prdc;
        }
        this.qp_maxpage[prdc] = maxpage;
        this.set_navlinks(prdc);
//        if (!i) alert('maxpage before: '+prev_maxpage+'\nafter'+this.qp_maxpage[prdc]+'\nurl: '+url+'\nactpage: '+this.qp_actpage[prdc]);
    }
}
browseQP.prototype.part_resp = function(rid) {
	if (typeof(mtn_request[rid])!="undefined")
    	var resp = mtn_request[rid].response;    
    else
    	var resp="";
    var prdc = this.qp_rid[rid];
    //if (prdc=='COR110') alert(resp);
    var aresp = resp.split('_::_');
    var o;
    var h;
    if (o = find_object('table_'+prdc)) {
        o.innerHTML = '<table cellspacing="0" ><tr id="images_'+prdc+'">'+aresp[0]+'</tr></table>';
    } 
    if (h = find_handle('table_'+prdc)) {
        if (typeof(aresp[2])!=undefined) h.width = (parseInt(aresp[2]) * this.imagewidth) +'px';
    }
}
// browseQP end
