var inMotion=false;
var currentTitle = "";
var scrollerId = "";
var divElem = "";

function scrollHorizontal (elementid,xDistance) {

	firstElem = $$(".scroller-bildblock")[0];
	lastElem = $$(".scroller-bildblock")[$$(".scroller-bildblock").length-1];
	if ($(elementid).style.left=="")
	  $(elementid).style.left="0px";

	if (!inMotion) {
		if (xDistance<0) { /* right */
			firstElemCopy = firstElem.cloneNode(true);
			$(elementid).appendChild(firstElemCopy);
			$(elementid).style.left=($(elementid).style.left.replace("px","")-xDistance)+"px";
			$(elementid).removeChild(firstElem);
		} else { /* left */
			lastElemCopy = lastElem.cloneNode(true);
			$(elementid).insertBefore(lastElemCopy, firstElem);
			$(elementid).style.left=($(elementid).style.left.replace("px","")-xDistance)+"px";
			$(elementid).removeChild(lastElem);
		} 
		new Effect.Move($(elementid), {
			x: xDistance,
			y: 0,
			mode: "relative",
			duration: 0.3,
			beforeStart : function (e) { 
				inMotion=true; 
			},
			afterFinish : function (e) { 
				ajaxGet('/fileadmin/php/storeScrollValue.php?a=set&scrollerStyle['+elementid+']='+escape($(elementid).innerHTML));
				inMotion=false;  
			}
		});
		
	}
}


function AddOnload(myfunc) {
	if(window.addEventListener)
		window.addEventListener('load', myfunc, false);
	else if(window.attachEvent)
		window.attachEvent('onload', myfunc);
}


function initScroller () {
	divElem = $$(".scroller-bilder")[0];
	scrollerId = divElem.id;
	currentTitle=$$("a.on")[0].title;
	divElem.style.width=($$(".scroller-bildblock").length*101)+"px";
	//this also inserts the session html into the right div
	ajaxGet('/fileadmin/php/storeScrollValue.php?a=get&scrollerId='+scrollerId);
}


function ajaxGet(action) {
	new Ajax.Request(action, { 
		asynchronous: true, 
		method: 'get',
		encoding: 	"UTF-8",
		evalScripts: false,
		onSuccess: function (t) {
			if (t.responseText!="") {
			  var html = t.responseText;

			  //mark the right link as active
			  html = html.replace('class="on"','');
			  html = html.replace('class="off"','');
			  html = html.replace('title="'+currentTitle+'"','title="'+currentTitle+'" class="on"');

			  //IE special:
			  html = str_replace('class=on', '', html);
			  html = str_replace('class=off', '', html);
			  html = str_replace('title='+currentTitle, 'title="'+currentTitle+'" class=on', html);

			  //inject modified HTML
			  divElem.update(html);
			}
			divElem.style.display = "block";
		},
		onFailure: function (t) {
	   	}
	  }
    )
}


function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
 
    var s = subject;
    var ra = r instanceof Array, sa = s instanceof Array;
    var f = [].concat(search);
    var r = [].concat(replace);
    var i = (s = [].concat(s)).length;
    var j = 0;
    
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    }
 
    return sa ? s : s[0];
}


AddOnload(initScroller);
