// JavaScript for the Westaro Website

var pictureGroups = new Object;
var activePictures = new Object;
var laufband;
var laufband_name;
var stuhlzahl;
var stuhlbreite;
var lb;

function westaro_laufband_init(lb_name, sz, sb) {
    stuhlzahl = sz;
    stuhlbreite = sb;
    laufband = 0;
    laufband_name = lb_name;
    var wn = window.name.split('_');
    if (wn.length == 2) {
	if (wn[0] == laufband_name) {
	    var alt = parseInt(wn[1]);
	    if (alt <= 0 && alt >= (5 - stuhlzahl) * stuhlbreite) {
		laufband = alt;
		document.writeln("<style type=\"text\/css\">div#all div#framed div#head div#head_right div#laufband div#band { left: " 
				 + String(laufband) + "px; }<\/style>");
	    }
	}
    }
    westaro_arrows();
}

function westaro_init() {
    lb = document.getElementById('band');
    if (pictureGroups) {
	var key, p, i, src;
	for (key in pictureGroups) {
	    try {
		activePictures[key] = 0;
		p = document.getElementById(key);
		p.firstChild.id = 'pic-' + key + '-0';
		var d = document.createElement('div');
		d.className = 'switch';
		var e = document.createElement('div');
		e.id = 'switch-' + key + '-0';
		e.setAttribute('onclick', "westaro_picture('"+key+"', '0');");
		e.className = 'active';
		d.appendChild(e);
		for (i = 0; i < pictureGroups[key].length; ++i) {
		    var b = document.createElement('div');
		    b.id = 'switch-' + key + '-' + String(i+1);
		    b.setAttribute('onclick', "westaro_picture('"+key+"', '"+String(i+1)+"');");
		    d.appendChild(b);
		}
		p.parentNode.appendChild(d);
		var c = document.createElement('div');
		c.style.clear = 'both';
		p.parentNode.appendChild(c);
		for (i = 0; i < pictureGroups[key].length; ++i) {
		    src = pictureGroups[key][i];
		    var img = document.createElement('img');
		    img.setAttribute('src', base_url+'/sites/default/files/'+src);
		    img.setAttribute('alt', 'Stuhl');
		    img.id = 'pic-' + key + '-' + String(i+1);
		    p.appendChild(img);
		}
	    } catch(e) {}
	}
    }
}

function westaro_picture(key, n) {
    try {
	if (n != activePictures[key]) {
	    document.getElementById('pic-' + key + '-' + String(activePictures[key])).style.display = 'none';
	    document.getElementById('pic-' + key + '-' + String(n)).style.display = 'inline';
	    document.getElementById('switch-' + key + '-' + String(activePictures[key])).className = 'normal';
	    document.getElementById('switch-' + key + '-' + String(n)).className = 'active';
	    activePictures[key] = n;
	}
    } catch(e) {}
}

var mover;
var moving = false;
var mousedown = false;
var target;

function westaro_stop() {
    mousedown = false;
}

function westaro_left() {
    mousedown = true;
    if (!moving && laufband > (6 - stuhlzahl) * stuhlbreite) {
	target = laufband - stuhlbreite;
	moving = true;
	mover = window.setInterval('westaro_move_left()', 5);
	document.getElementById('rightarrow').className = 'active';
    }
}

function westaro_move_left() {
    if (laufband > target) {
	laufband -= 2;
	lb.style.left = String(laufband) + 'px';
    } else {
	window.clearInterval(mover);
	westaro_arrows();
	moving = false;
	window.name = laufband_name + '_' + String(laufband);
	if (mousedown) {
	    westaro_left();
	} else {
	    document.getElementById('rightarrow').className = 'normal';
	}
    }
}

function westaro_right() {
    mousedown = true;
    if (!moving && laufband < 0) {
	target = laufband + stuhlbreite;
	moving = true;
	mover = window.setInterval('westaro_move_right()', 5);
	document.getElementById('leftarrow').className = 'active';
    }
}

function westaro_move_right() {
    if (laufband < target) {
	laufband += 2;
	lb.style.left = String(laufband) + 'px';
    } else {
	window.clearInterval(mover);
	westaro_arrows();
	moving = false;
	window.name = laufband_name + '_' + String(laufband);
	if (mousedown) {
	    westaro_right();
	} else {
	    document.getElementById('leftarrow').className = 'normal';
	}
    }
}

function westaro_arrows() {
    var left = document.getElementById('leftarrow');
    var right = document.getElementById('rightarrow');
    if (laufband > (6 - stuhlzahl) * stuhlbreite) {
	right.style.display = 'block';
    } else {
	right.style.display = 'none';
    }
    if (laufband < 0) {
	left.style.display = 'block';
    } else {
	left.style.display = 'none';
    }
}

