    

function add_photo(photo_id, product_id, photo_url) {
    el = document.forms['product_editor_' + product_id].elements['photos'];
    if (el.value.length != 0) {
        photos = el.value.split(',');
    } else {
        photos = Array();
    }
    photos.push( photo_id );
    el.value = photos.join(',');
    
    patchi( 'product_editor_'+product_id+'_photolist', photo_url+'?photos='+encodeURIComponent(el.value));
}

function remove_photo(position, product_id, photo_url) {
    el = document.forms['product_editor_' + product_id].elements['photos'];
    if (el.value.length != 0) {
        photos = el.value.split(',');
    } else {
        photos = Array();
    }
    photos = el.value.split(',');
    photos.splice(position,1);
    el.value = photos.join(',');
    
    patchi( 'product_editor_'+product_id+'_photolist', photo_url+'?photos='+encodeURIComponent(el.value));
}


function absLeft(el) {
     return (el.offsetParent) ? 
         el.offsetLeft+absLeft(el.offsetParent) : el.offsetLeft;
}


function panners_handle_click_dom(ev) {
    ele = ev.target;
    while (! panners[ele.id]) {
        if (! ele.parentNode) return;
        ele = ele.parentNode;
    }
    rel_x = (ev.clientX-ele.offsetLeft)/ele.offsetWidth;
    panners[ele.id].click(rel_x);
}

function panners_handle_click_microsoft(ele) {
    ev = window.event;
    if (!ev) return;
    rel_x = (ev.clientX-absLeft(ele))/ele.offsetWidth;
    panners[ele.id].click(rel_x);
}


function panners_idle() {
    for (id in panners) {
        panners[id].animate();
    }
    window.setTimeout("panners_idle()",50);
}


function Panner(id, nphot) {
    element = document.getElementById(id);
    element_ofs = document.getElementById(id+'_mover');
    this.element = element;
    this.element_ofs = element_ofs;
    this.offset = 0;
    this.smooth_offset = 0;
    this.nphot = nphot;
    if (this.element.addEventListener) {
        this.element.removeAttribute('onclick');
        this.element.addEventListener('click',panners_handle_click_dom, false);
    }    
    this.click = panner_click;
    this.animate = panner_animate;
}

function panner_click(rel_x) {
    m = 0;
    if (rel_x > 0.66) m = 157;
    if (rel_x < 0.33) m = -157;
    this.offset -= m;
}

function panner_animate() {
    if (this.smooth_offset != this.offset) {
        mr = (this.offset-this.smooth_offset)/2.;
        m = Math.round(mr);
        if (Math.abs(m)==0) {
            if (mr<0) m = -1;
            else m = +1;
        }
        this.smooth_offset += m;
        this.element_ofs.style.marginRight = (-this.smooth_offset)+'px';
	this.element_ofs.style.marginLeft = this.smooth_offset+'px';
    }
}
function register_panner( id, nphot ) {
    if (panners[id]) {
        delete panners[id];
    }
    panners[id] = new Panner(id, nphot);
}

var panners = new Object();
panners_idle();



