jQuery.noConflict();
jQuery(document).ready(function($){
	var chwidth = 0;
	var chheight = 0;
	var timerId;
	//var maxScroll = $(".content-scroll").attr("scrollWidth") - $(".content-scroll").width();
	var maxScroll = $(".content-scroll .content-holder").width() - $(".content-scroll").width();
	var left_begin = 0;
	var left_end = 100;
	var right_begin = $(".content-scroll").width() - 100;
	var right_end = $(".content-scroll").width();

	var top_begin = 0;
	var top_end = 100;
	var bottom_begin = $(".content-scroll-vert").height() - 100;
	var bottom_end = $(".content-scroll-vert").height();

	$('.product').each(function(i,e){
		chwidth += $(this).outerWidth()+10;
	});

	var wert = $("#menu_searchsubmit").val();
	$("#menu_searchsubmit").focus(function(){
		if ($(this).val() == wert) {
			$(this).val("");
		}
	});
	$("#menu_searchsubmit").blur(function(){
		if ($(this).val() == "") {
			$(this).val(wert);
		}
	});



	/*horizontal scroller*/
	if(chwidth==0) {
		chwidth = $('.content-holder:first-child').outerWidth();
		if(chwidth==$('.content-scroll').width()) {
			chwidth = 0;
			$('.content-holder').children().each(function() {
				if($(this).children().length > 0) {
					$(this).children().each(function() {
						chwidth += $(this).outerWidth();
					});
				} else {
					chwidth += $(this).outerWidth();
				}
			});		
		}
	}

	if(navigator.userAgent.toLowerCase().indexOf("msie 7") || navigator.userAgent.toLowerCase().indexOf("msie 6")) {
		chwidth += 100;
	}

	$('.content-holder').width(chwidth);

	if(chwidth<=$('.content-slider-holder').width()) {
		/*$('.content-slider-holder').hide();*/
	} else {
		$('.content-slider').slider({
		  animate: true,
		  change: handleSliderChange,
		  slide: handleSliderSlide
		});
	}

	$('.content-scroll').bind('mousemove',function(e){
			clearInterval(timerId);
			var mousepos = e.pageX - $('.content-scroll').offset().left;
			//var maxScroll = $(".content-scroll").attr("scrollWidth") - $(".content-scroll").width();
			var maxScroll = $(".content-scroll .content-holder").width() - $(".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);
					var slpos = pos/maxScroll * 100;
					if(slpos>100) slpos = 100;
					else if(slpos<0) slpos = 0;
		  			$('.ui-slider-handle').css({left:slpos+'%'});
				} else if(mousepos >= right_begin && mousepos <= right_end && $('.content-scroll').scrollLeft()<maxScroll) {
					var pos = $('.content-scroll').scrollLeft() + 3;
					$('.content-scroll').scrollLeft(pos);
					if($('.content-scroll').scrollLeft()>=maxScroll-3) pos = maxScroll;
					var slpos = pos/maxScroll * 100;
					if(slpos>100) slpos = 100;
					else if(slpos<0) slpos = 0;
		  			$('.ui-slider-handle').css({left:slpos+'%'});
				} else clearInterval(timerId);
			}, 10);
		}
	);

	$('.content-scroll').hover(function(e) { }, function(e) { clearInterval(timerId); });


	/*vertical scroller*/
	if(chheight==0) {
		chheight = $('.content-holder-vert:first-child').height();
	}

	$('.content-holder-vert').height(chheight);

	if(chheight<=$('.content-slider-holder-vert').height()) {
		$('.content-slider-holder-vert').hide();
	} else {
		$('.content-slider-vert').slider({
		  animate: true,
		  change: handleSliderChangeVertical,
		  slide: handleSliderSlideVertical,
		  orientation: 'vertical',
		  value: 100
		});
	}

	$('.content-scroll-vert').bind('mousemove',function(e){
			clearInterval(timerId);
			var mousepos = e.pageY - $('.content-scroll-vert').offset().top;
			var maxScroll = $(".content-scroll-vert").attr("scrollHeight") - $(".content-scroll-vert").height();
	    	timerId = setInterval(function() {
				if(mousepos >= top_begin && mousepos <= top_end && $('.content-scroll-vert').scrollTop()>0) {
					var pos = $('.content-scroll-vert').scrollTop() - 3;
					$('.content-scroll-vert').scrollTop(pos);
					var slpos = 100 - (pos/maxScroll * 100);
					if(slpos>100) slpos = 100;
					else if(slpos<0) slpos = 0;
		  			$('.content-slider-vert .ui-slider-handle').css({bottom:slpos+'%'});
				} else if(mousepos >= bottom_begin && mousepos <= bottom_end && $('.content-scroll-vert').scrollTop()<maxScroll) {
					var pos = $('.content-scroll-vert').scrollTop() + 3;
					$('.content-scroll-vert').scrollTop(pos);
					if($('.content-scroll-vert').scrollTop()>=maxScroll-3) pos = maxScroll;
					var slpos = 100 - (pos/maxScroll * 100);
					if(slpos>100) slpos = 100;
					else if(slpos<0) slpos = 0;
		  			$('.content-slider-vert .ui-slider-handle').css({bottom:slpos+'%'});
				} else clearInterval(timerId);
			}, 10);
		}
	);

	$('.content-scroll').hover(function(e) { }, function(e) { clearInterval(timerId); });


	/*Productinfo Popup*/
	var api = $('.productlist .product').tooltip({
		effect: 'slide',
		events: {
		  def:     'click,blur',
		  input:   'click,blur',
		  widget:  'click,blur',
		  tooltip: 'blur,blur'
		},
		relative: true,
		position: 'center right',
		offset: [-25,-100],
		onBeforeShow: function() {
			clearInterval(timerId);
			/*
			$('.productlist .product').each(function(e){
				$(this).tooltip().hide();
			});
			*/
			$('.productlist .tooltip').hide();
		},
		onShow: function() {
			//var elem = this.getTrigger().attr('id');			
			//var pos = $('#'+elem).position().left+$('#'+elem).width()-150;
			var img = this.getTrigger().children('img');
			var pos = img.position().left + img.width() - 150;
			var maxScroll = $(".content-scroll").attr("scrollWidth") - $(".content-scroll").width();
  			$(".content-scroll").animate({scrollLeft: pos }, 250);
  			var slpos = $('.content-slider-holder').width() * ((pos+$('.content-slider-holder').width()-25)/$(".content-scroll").attr("scrollWidth"));
  			if(slpos >= $('.content-slider-holder').width()) slpos = $('.content-slider-holder').width();
  			$(".ui-slider-handle").animate({left: slpos }, 250);
		},
		api: true
	});

	$('.tooltip').hover(function(e) { clearInterval(timerId); }, function(e) { clearInterval(timerId); });

	$('.closetip').click(function(e){
		//$('#'+$(this).attr('id').replace('close','product')).tooltip({ fadeOutSpeed: 10, delay: 0, api: true}).hide();
		 //$('#'+$(this).attr('id').replace('tooltip-close','product')).tooltip().hide();
		 $(this).parent('.tooltip').hide();
	});
});


function  handleSliderChange(e, ui)
{
  //var maxScroll = jQuery(".content-scroll").attr("scrollWidth") - jQuery(".content-scroll").width();
  var maxScroll = jQuery(".content-scroll .content-holder").width() - 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();
  var maxScroll = jQuery(".content-scroll .content-holder").width() - jQuery(".content-scroll").width();
  jQuery(".content-scroll").animate({scrollLeft: ui.value * (maxScroll / 100) }, 0);  //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();
  var maxScroll = jQuery(".content-scroll .content-holder").width() - 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();
  var maxScroll = jQuery(".content-scroll .content-holder").width() - jQuery(".content-scroll-vert").height();
  //jQuery(".content-scroll-vert").attr({scrollTop: (100-ui.value) * (maxScroll / 100) });
  jQuery(".content-scroll-vert").animate({scrollTop: (100-ui.value) * (maxScroll / 100) }, 0);
}

