$(document).ready(function() {
	
	/* gebaut für den header um alle scroller zu aktivieren */
	
	function slider_moveto_h(item, position){
		item.animate({left: position }, 800);
	}
	function slider_moveto_v(item, position){
		item.animate({top: position }, 800);
	}
	
	function slider_doscroll_up(event){
		
		var value 			= parseInt($(this).parent().children('.scrollarea').attr('value'));
		if(typeof value !== 'number' || !isFinite(value)){
			value = 0;
		}
		
		var maxvalue = $(this).parent().children('.scrollarea').attr('maxvalue');
		var eintragcount = $(this).parent().parent().children('.content').children('.scrollbox').children('.eintrag').size();
		var targetvalue = (value-(maxvalue/eintragcount));
			
		if(targetvalue < 0 ){
			targetvalue = 0;
		}
		
		var scrollerposition = (100-(targetvalue/maxvalue*100))+"%";
		$(this).parent().children('.scrollarea').children('div').height(scrollerposition);
		$(this).parent().children('.scrollarea').children('a').css('bottom', scrollerposition);	
			
		$(this).parent().children('.scrollarea').attr('value', targetvalue);
		slider_moveto_v($(this).parent().parent().children('.content').children('.scrollbox').css('top', "-"+targetvalue+"px" ));
	}
	function slider_doscroll_down(event){
		
		var value 			= parseInt($(this).parent().children('.scrollarea').attr('value'));
		if(typeof value !== 'number' || !isFinite(value)){
			value = 0;
		}
		
		var maxvalue = $(this).parent().children('.scrollarea').attr('maxvalue');
		var eintragcount = $(this).parent().parent().children('.content').children('.scrollbox').children('.eintrag').size();
		var targetvalue = (value+(maxvalue/eintragcount));
		
		if(targetvalue > maxvalue ){
			targetvalue = maxvalue;
		}
		
		var scrollerposition = (100-(targetvalue/maxvalue*100))+"%";
		$(this).parent().children('.scrollarea').children('div').height(scrollerposition);
		$(this).parent().children('.scrollarea').children('a').css('bottom', scrollerposition);
		
		$(this).parent().children('.scrollarea').attr('value', targetvalue);
		slider_moveto_v($(this).parent().parent().children('.content').children('.scrollbox').css('top', "-"+targetvalue+"px" ));
		
	}
	
	function slider_doscroll_right(event){
		
		var value 			= parseInt($(this).parent().children('.scrollarea').attr('value'));
		if(typeof value !== 'number' || !isFinite(value)){
			value = 0;
		}
		var max 		= $(this).parent().children('.scrollarea').slider('option', 'max');
		var margins 	= parseInt($(this).parent().parent().children('.content').children().css('margin-left'))+parseInt($(this).parent().parent().children('.content').children().css('margin-right'));
		var padding 	= parseInt($(this).parent().parent().children('.content').children().css('padding-left'))+parseInt($(this).parent().parent().children('.content').children().css('padding-right'));
		var newvalue 	= value+(($(this).parent().parent().children('.content').children().width()+margins+padding)/2);
		
		if(value == newvalue){
			return;
		}
		if(newvalue >= max){
			slider_moveto_h($(this).parent().parent().children('.content'), "-"+max+"px");
			slider_moveto_h($(this).parent().children('.scrollarea').children('a'), "100%");
			$(this).parent().children('.scrollarea').attr('value', max);
		}else{
			slider_moveto_h($(this).parent().parent().children('.content'), "-"+Math.round(newvalue)+"px");
			slider_moveto_h($(this).parent().children('.scrollarea').children('a'), (newvalue/max*100)+"%");
			$(this).parent().children('.scrollarea').attr('value', newvalue);
		}
	}
	function slider_doscroll_left(event){
		
		var value 			= parseInt($(this).parent().children('.scrollarea').attr('value'));
		if(typeof value !== 'number' || !isFinite(value)){
			value = 0;
		}
		var max 		= $(this).parent().children('.scrollarea').slider('option', 'max');
		var min 		= $(this).parent().children('.scrollarea').slider('option', 'min');
		var margins 	= parseInt($(this).parent().parent().children('.content').children().css('margin-left'))+parseInt($(this).parent().parent().children('.content').children().css('margin-right'));
		var padding 	= parseInt($(this).parent().parent().children('.content').children().css('padding-left'))+parseInt($(this).parent().parent().children('.content').children().css('padding-right'));
		var newvalue 	= value-(($(this).parent().parent().children('.content').children().width()+margins+padding)/2);
		
		if(value == newvalue){
			return;
		}
		if(newvalue <= min){
			slider_moveto_h($(this).parent().parent().children('.content'), min+"px");
			slider_moveto_h($(this).parent().children('.scrollarea').children('a'), "0%");
			$(this).parent().children('.scrollarea').attr('value', min);
		}else{
			slider_moveto_h($(this).parent().parent().children('.content'), "-"+Math.round(newvalue)+"px");
			slider_moveto_h($(this).parent().children('.scrollarea').children('a'), (newvalue/max*100)+"%");
			$(this).parent().children('.scrollarea').attr('value', newvalue);
		}
	}
	
	$('.scrollcontainer_hor').each(function(){
		
		var margins = parseInt($('.content', this).children().css('margin-left'))+parseInt($('.content', this).children().css('margin-right'));
		var padding = parseInt($('.content', this).children().css('padding-left'))+parseInt($('.content', this).children().css('padding-right'));
		var itemwidth = $('.content', this).children().width();
		
		$('.content', this).width(($('.content', this).children().length * (itemwidth+margins+padding))+"px");
		
		$('.scrollbar .scrollarea', this).slider({
			animate: 	true,
			min:		0,
			max:		$('.content', this).width()-$('.scrollbar', this).width(),
			slide: 		function(event, ui){
				$(this).parent().parent().children('.content').css('left', "-"+ui.value+"px" );
				
				$(this).attr('value', ui.value);
			}
		});
	
		$('.scroll_right', this).unbind('mousedown.scroll_right').bind('mousedown.scroll_right', slider_doscroll_right);
		$('.scroll_left', this).unbind('mousedown.scroll_left').bind('mousedown.scroll_left', slider_doscroll_left);
		
	});
	$('.stepscrollcontainer').each(function(){
		
		var margins = parseInt($('.thumbs a img', this).css('margin-left'))+parseInt($('.thumbs a img', this).css('margin-right'));
		var padding = parseInt($('.thumbs a img', this).css('padding-left'))+parseInt($('.thumbs a img', this).css('padding-right'));
		
		var itemcount = $('.thumbs a', this).length;
		var gesammtwidth = itemcount * ($('.thumbs a img', this).width()+margins+padding);
		
		$('.thumbs', this).width(gesammtwidth+"px");
		
		$('a.zurueck', this).unbind('click.thumb_zurueck').bind('click.thumb_zurueck', function(){
			if($(this).parent().attr('running') !== "1"){
				$(this).parent().attr('running', "1");
				var margins = parseInt($(this).parent().children('.thumbs').children('a').children('img').css('margin-left'))+parseInt($(this).parent().children('.thumbs').children('a').children('img').css('margin-right'));
				var padding = parseInt($(this).parent().children('.thumbs').children('a').children('img').css('padding-left'))+parseInt($(this).parent().children('.thumbs').children('a').children('img').css('padding-right'));
				var itemwidth = $(this).parent().children('.thumbs').children('a').children('img').width();
				var left = parseInt($(this).parent().children('.thumbs').css('left'));
				var newleft = left+(itemwidth+margins+padding);
				
				if(newleft <= 0){
					$(this).parent().children('.thumbs').animate({left: newleft+"px"}, 'normal', 'linear', function(){
						$(this).parent().attr('running', "0");
					});
				}else{
					$(this).parent().attr('running', "0");
				}
			}
		});
		$('a.weiter', this).unbind('click.thumb_weiter').bind('click.thumb_weiter', function(){
			
			if($(this).parent().attr('running') !== "1"){
				$(this).parent().attr('running', "1");
				
				var margins = parseInt($(this).parent().children('.thumbs').children('a').children('img').css('margin-left'))+parseInt($(this).parent().children('.thumbs').children('a').children('img').css('margin-right'));
				var padding = parseInt($(this).parent().children('.thumbs').children('a').children('img').css('padding-left'))+parseInt($(this).parent().children('.thumbs').children('a').children('img').css('padding-right'));
				var itemwidth = $(this).parent().children('.thumbs').children('a').children('img').width();
				var left = parseInt($(this).parent().children('.thumbs').css('left'));
				var contentwidth = $(this).parent().children('.thumbs').width()+margins+padding;
				var restwidth = contentwidth-($(this).parent().width()-$(this).parent().children('a').width()*2);
				var newleft = left-(itemwidth+margins+padding);
				
				if(restwidth >= (newleft*-1)){
					
					$(this).parent().children('.thumbs').animate({left: newleft+"px"}, 'normal', 'linear', function(){
						$(this).parent().attr('running', "0");
					});
				}else{
					$(this).parent().attr('running', "0");
				}
			}
		});
	});
	
	
	$('.scrollcontainer_ver').each(function(){
		
		
		var maxboxheight = $('.content', this).height();
		var contentheight = $('.content .scrollbox', this).height();
		
		if(contentheight > maxboxheight){
			
			var contentheight = contentheight-maxboxheight,
				buttonUpHeight = $('.scrollbar .scroll_up', this).height(),
				buttonDownHeight = $('.scrollbar .scroll_down', this).height();
				
			$('.scrollbar .scrollarea', this).attr('maxvalue', contentheight);
			$('.content', this).height(maxboxheight);
			$('.scrollbar', this).height(maxboxheight);
		
			$('.scrollbar .scrollarea', this).height(maxboxheight-buttonUpHeight-buttonDownHeight).slider({
				orientation: "vertical",
				range: "min",
				min: 0,
				max: contentheight,
				value: contentheight,
				slide: function(event, ui) {
					var targetvalue = contentheight-ui.value;
					
					$(this).parent().parent().children('.content').children('.scrollbox').css('top', "-"+targetvalue+"px" );
									
					$(this).attr('value', targetvalue);
				}
			});
			$('.scrollbar .scroll_up', this).unbind('mousedown.scroll_up').bind('mousedown.scroll_up', slider_doscroll_up);
			$('.scrollbar .scroll_down', this).unbind('mousedown.scroll_down').bind('mousedown.scroll_down', slider_doscroll_down);
		}else{
			//var scrollbarwidth = $('.scrollbar', this).width();
			$('.scrollbar', this).remove();
			//$('.content', this).width($('.content', this).width()+scrollbarwidth);
		}
	});

});