/* Functions for ursgrunder */


function mootools_slide() {


	if (!$('p7scroller1')) return;

	var current_image=parseInt($('current_image').getProperty('value'));
	var count_images=parseInt($('count_images').getProperty('value'));
	var width_image=parseInt($('length_imageset').getProperty('value'));
	var width_imagebox = width_image*count_images;
	var current_imageposition = (width_image+3)*(current_image-1);
	var offset = 1;		// HOW MUCH TO MOVE

	var periodical;
	var delay=10;

	var scroll = new Fx.Scroll('p7scroller1', {
		offset:{'x':0, 'y':0},
		transition: Fx.Transitions.linear
	});

	var scroller = function(goForward){

		var width_viewbox=parseInt($('p7scroller1').getStyle('width'));
		$('p7s1content1').setStyle('width',width_imagebox);
		var max_imageposition=(Math.ceil((width_imagebox-width_viewbox)/width_image)+1)*width_image;

		if(current_imageposition >= max_imageposition) {
			current_imageposition = max_imageposition;
		}

		if(current_imageposition <= 0) {
			current_imageposition = 0;
		}

		if(this.goForward || goForward) {
			if (goForward != "start") {
				current_imageposition += offset;
			} else {
				(function(){ scroll.set(current_imageposition, 0)}).delay(100);
				return;
			}

		} else {
			if (current_imageposition != 0) {
				current_imageposition += -(offset);
			}
		}
		scroll.start(current_imageposition);
	}

	$(document.body).getElements('a.child-left').addEvent('mouseover', function(event) {
		$clear(periodical);
		// scroller.run(false);
		periodical=scroller.periodical(delay,{goForward: false});
	});

	$(document.body).getElements('a.child-right').addEvent('mouseover', function(event) {
		$clear(periodical);
		// scroller.run(true);
		periodical=scroller.periodical(delay,{goForward:true});

	});

	$(document.body).getElements('a.child-left').addEvent('mouseout', function(event) {
		$clear(periodical);
	});

	$(document.body).getElements('a.child-right').addEvent('mouseout', function(event) {
		$clear(periodical);
	});

	scroller.run("start");

}



var resizeMain = function() {
    if(document.getElementById("fix").style.width != document.getElementById("menuTopUl").offsetWidth+"px")  {
    	document.getElementById("fix").style.width=document.getElementById("menuTopUl").offsetWidth+"px";
    	if ($("p7scroller1")) document.getElementById("p7scroller1").style.width=document.getElementById("menuTopUl").offsetWidth-20+"px";
    }
}

/* Main */

var sizeFix=false;
if (sizeFix) {
	window.addEvent('domready', function() {
		document.getElementById("fix").style.width=document.getElementById("menuTopUl").offsetWidth+"px"
		if ($("p7scroller1")) document.getElementById("p7scroller1").style.width=document.getElementById("menuTopUl").offsetWidth-20+"px";
	})
	resizeMain.periodical(100);
}

window.addEvent('domready', function() {
	mootools_slide();
});

