var pic = 0;

jQuery(document).ready(function($){
	var chwidth = 0;
	var chheight = 0;
	var timerId;
	var maxScroll = $(".content-scroll").attr("scrollWidth") - $(".content-scroll").width();			
	var left_begin = 0;
	var left_end = 100;
	var right_begin = $(".scrollarea").width();
	var right_end = $(".scrollarea").width() + 100;

	$('.nip_image').each(function(i,e){
		chwidth += $(this).width()+21;
	});
	
	
	/*horizontal scroller*/
	if(chwidth==0) {
		chwidth = $('.content-holder:first-child').width();
	}
	
	$('.content-holder').width(chwidth);	

	if(chwidth<=$('.content-slider-holder').width()) {
		/*$('.content-slider-holder').hide();*/
	} else {
		$('.content-slider').slider({
		  animate: true,
		  change: handleSliderChange,
		  slide: handleSliderSlide
		});		
	}
	
	$('.scrollarea').bind('mousemove',function(e){
			clearInterval(timerId);
			var mousepos = e.pageX - $('.scrollarea').offset().left;	
			var maxScroll = $(".content-scroll").attr("scrollWidth") - $(".content-scroll").width();			
	    	timerId = setInterval(function() {
				if(mousepos >= left_begin && mousepos <= left_end && $('.content-scroll').scrollLeft()>0) {	
					var pos = $('.content-scroll').scrollLeft() - 3;
					$('.content-scroll').scrollLeft(pos);
				} else if(mousepos >= right_begin && mousepos <= right_end && $('.content-scroll').scrollLeft()<maxScroll) {
					var pos = $('.content-scroll').scrollLeft() + 3;
					$('.content-scroll').scrollLeft(pos);
				} else clearInterval(timerId);
			}, 10);
		}
	);
	
	$('.content-scroll').hover(function(e) { }, function(e) { clearInterval(timerId); });
	
	aktiv = window.setTimeout("nextpic()", 8000);
});

function  handleSliderChange(e, ui)
{
  var maxScroll = jQuery(".content-scroll").attr("scrollWidth") - jQuery(".content-scroll").width();  
  jQuery(".content-scroll").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = jQuery(".content-scroll").attr("scrollWidth") - jQuery(".content-scroll").width();
  jQuery(".content-scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function  handleSliderChangeVertical(e, ui)
{
  var maxScroll = jQuery(".content-scroll-vert").attr("scrollHeight") - jQuery(".content-scroll-vert").height();  
  jQuery(".content-scroll-vert").animate({scrollTop: (100-ui.value) * (maxScroll / 100) }, 1000);
}

function handleSliderSlideVertical(e, ui)
{
  var maxScroll = jQuery(".content-scroll-vert").attr("scrollHeight") - jQuery(".content-scroll-vert").height();
  jQuery(".content-scroll-vert").attr({scrollTop: (100-ui.value) * (maxScroll / 100) });
}


function switchimg(pic) {
	var newpic = jQuery("#img"+pic).attr('src');
	var newtext = jQuery("#img"+pic).attr('rel');
	var newlink = jQuery("#img"+pic).attr('rev');
	jQuery('#mainpic').ImageSwitch({
		Type: "FadeIn",
		NewImage: newpic,
		Speed: 2000
	});
	jQuery('.GrpEffectImg').remove();
	jQuery('#maintext').html(newtext);
	jQuery('#mainpiclink').attr('href',newlink);
}


function nextpic() {
	if(pic<5) pic = pic+1;
	else pic=0;
	window.clearTimeout(aktiv);
	switchimg(pic);
	aktiv = window.setTimeout("nextpic()", 8000);
}
