$(document).ready(function() {
	
	/* Work Font-Sizes */
	var fontSizeCookie = $.cookie('fontSize');
	if(fontSizeCookie != null) {
		switch(fontSizeCookie) {
			case 'normal':
				$('#primary').css('font-size','100%');
			break;
			case 'big':
				$('#primary').css('font-size','110%');
			break;
			case 'bigger':
				$('#primary').css('font-size','120%');
			break;
		}
	} else {
		$.cookie('fontSize', '', { expires: -1 });
	}
	
	$('.normalize').click(function() {
		$('#primary').css('font-size','100%');
		$.cookie('fontSize', 'normal', { expires: 1 });
	});
	
	$('.big').click(function() {
		$('#primary').css('font-size','110%');
		$.cookie('fontSize', 'big', { expires: 1 });
	});
	
	$('.bigger').click(function() {
		$('#primary').css('font-size','120%');
		$.cookie('fontSize', 'bigger', { expires: 1 });
	});
	/* =================== */
	
	/* Severin heute */
	$('.heute-top li').mouseover(function() {
		$(this).find('p').addClass('hover');
		
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace(".jpg", "_in.jpg");
		element.attr('src', newSource);

	});
	$('.heute-top li').mouseout(function() {
		$(this).find('p').removeClass('hover');
		
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace("_in.jpg", ".jpg");
		element.attr('src', newSource);
	});
	
	/* Severin heute
	$('li.first').mouseover(function() {
		$('span.first').addClass('first-active');
		$('span.first-active').removeClass('first');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace(".jpg", "_in.jpg");
		element.attr('src', newSource);

	});
	$('li.first').mouseout(function() {
		$('span.first-active').addClass('first');
		$('span.first').removeClass('first-active');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace("_in.jpg", ".jpg");
		element.attr('src', newSource);
	});
	
	$('li.second').mouseover(function() {
		$('span.second').addClass('second-active');
		$('span.second-active').removeClass('second');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace(".jpg", "_in.jpg");
		element.attr('src', newSource);
	});
	$('li.second').mouseout(function() {
		$('span.second-active').addClass('second');
		$('span.second').removeClass('second-active');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace("_in.jpg", ".jpg");
		element.attr('src', newSource);
	});
	
	$('li.third').mouseover(function() {
		$('span.third').addClass('third-active');
		$('span.third-active').removeClass('third');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace(".jpg", "_in.jpg");
		element.attr('src', newSource);
	});
	$('li.third').mouseout(function() {
		$('span.third-active').addClass('third');
		$('span.third').removeClass('third-active');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace("_in.jpg", ".jpg");
		element.attr('src', newSource);
	});
	
	$('li.fourth').mouseover(function() {
		$('span.fourth').addClass('fourth-active');
		$('span.fourth-active').removeClass('fourth');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace(".jpg", "_in.jpg");
		element.attr('src', newSource);
	});
	$('li.fourth').mouseout(function() {
		$('span.fourth-active').addClass('fourth');
		$('span.fourth').removeClass('fourth-active');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace("_in.jpg", ".jpg");
		element.attr('src', newSource);
	});
	
	$('li.fifth').mouseover(function() {
		$('span.fifth').addClass('fifth-active');
		$('span.fifth-active').removeClass('fifth');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace(".jpg", "_in.jpg");
		element.attr('src', newSource);
	});
	$('li.fifth').mouseout(function() {
		$('span.fifth-active').addClass('fifth');
		$('span.fifth').removeClass('fifth-active');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace("_in.jpg", ".jpg");
		element.attr('src', newSource);
	});
	
	$('li.sixth').mouseover(function() {
		$('span.sixth').addClass('sixth-active');
		$('span.sixth-active').removeClass('sixth');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace(".jpg", "_in.jpg");
		element.attr('src', newSource);
	});
	$('li.sixth').mouseout(function() {
		$('span.sixth-active').addClass('sixth');
		$('span.sixth').removeClass('sixth-active');
		element = $(this).find('img');
		source = element.attr('src');
		newSource = source.replace("_in.jpg", ".jpg");
		element.attr('src', newSource);
	});
	
	 =================== */
	
	$('.chronicle-out').mouseover(function() {
		var src = $(this).attr('src');
		if(src.match('_in')) {
			var correct = src.replace(/_in.jpg/,".jpg");
			$(this).attr('src', correct);
		}
		var correct = src.replace(/.jpg/,"_in.jpg");
		$(this).attr('src', correct);
	});
	$('.chronicle-out').mouseout(function() {
		var src = $(this).attr('src');
		var correct = src.replace(/_in.jpg/,".jpg");
		$(this).attr('src', correct);
	});
		
	/* Chronik */
	$('a.next').mouseover(function() {
		$('.timeline').everyTime(10, 'controlled', function() {
			var fullLength = 2000;
			var pos = $('.timeline').position();
			var common = fullLength-980;
			if(Math.abs(pos['left']) >= common) {
				var nextStep = pos['left'];
			} else {
				var nextStep = pos['left']-3;
			}
			$('.timeline').css('left', nextStep);
		});

	});
	
	$('a.next').mouseout(function() {
		$('.timeline').stopTime('controlled');
	});
	
	$('a.previous').mouseover(function() {
		$('.timeline').everyTime(10, 'controlled', function() {
			var pos = $('.timeline').position();
			if(pos['left'] >= 0) {
				var nextStep = pos['left'];
			} else {
				var nextStep = pos['left']+3;
			}
			$('.timeline').css('left', nextStep);
		});

	});
	
	$('a.previous').mouseout(function() {
		$('.timeline').stopTime('controlled');
	});
	
	$('#chronik-header').mouseover(function() {
		 $('#chronikyearimage').hide();
	});
	$('#chronik-header').mouseout(function() {
		 $('#chronikyearimage').show();
	});
	
	
	/* =================== */
		
	/* Rezepte */
	$('#recipe-selector').change(function() { 
		document.location.href=$('#recipe-selector :selected').val();
	});
	
	/* =================== */
	
	/* Shop */
	
	$('#cart-checkout').click(function() {
		document.location.href='/shop/customer'
	});
	
	$('#edit-cart').click(function() {
		document.location.href='/shop/cart'
	});
	
	$('#full-checkout').click(function() {
		document.location.href='/shop/send'
	});
	
	$('#edit-personal').click(function() {
		document.location.href='/shop/customer'
	});
	
	$('#check-shopping').submit(function() {
		return false;
	});
	
	/* =================== */
	
	/* Lexikon */
	/*
	$.getJSON("/index/live-lexikon", { val: $('#primary').html() },
		function(data){
			$.each(data, function(i,item){
				var tmp = $('#primary').html().replace(item.keyword, "<a class='inline-lexikon' href='/service/technik-lexikon/"+item.result.callname+"'>"+item.keyword+"</a>");
				$('#primary').empty();
				$('#primary').html(tmp);
			});
		}
	);
	*/
	
	/* =================== */
	
	/* Routenplaner */
	$('#routing').submit(function() {
		startingpoint = encodeURIComponent($('#start').val());
		parent.document.location.href='http://maps.google.de/maps?f=d&saddr=' + startingpoint + '&daddr=R%C3%B6hre+27,+59846+Sundern+(Severin+Elektroger%C3%A4te+GmbH)&hl=de&geocode=%3BCbji4P2q32taFer8DgMd30V6ACGnM09GMIXMBg&mra=pe&mrcr=0&sll=51.691288,7.726135&sspn=1.185023,3.570557&ie=UTF8&ll=51.704906,7.624512&spn=1.184667,3.570557&z=9';
		return false;
	});
	
	/* =================== */

	/* Produkte */
	
	/* categorys */
	$('.categoryoverview').click(function() {
		document.location.href=$(this).find('a').attr('href');
	});
	
	/* Zoom */
	$('.zoomDetailImage').click(function() {
		$('.zoomDetailImageArea').remove();
		var url = $(this).attr('href');
		if(url.length > 4) {
			
			if(window.location.host == 'www.severin.de') {
				closeName = 'schliessen';
			} else if(window.location.host == 'www.severin.com') {
				closeName = 'close';
			} else if(window.location.host == 'pl.severin.dimento.com') {
				closeName = 'zamknąć';
			} else if(window.location.host == 'ita.severin.dimento.com') {
				closeName = 'chiuso';
			}
			
			$('.sidebar').append('<div class="zoomDetailImageArea"><img onclick="$(\'.zoomDetailImageArea\').remove();" src="'+url+'" /><p><a href="javascript:;" onclick="$(\'.zoomDetailImageArea\').remove();">' + closeName + '</a></p></div>');
		}
		return false;
	});
	
	/* Product Scroller */
	$('.singlescrollerproduct').mouseover(function() {
		$('#tooltip').remove();
		pos = $(this).offset();
		
		$('body').append('<span style="top:' + (pos['top']-34) + 'px;left:' + (pos['left']+18) + 'px;" id="tooltip">' + $(this).find('.description').html() + '</span>');
	});
	
	if($('.scroll-product') && $('.products')) {
		totalWidthOfBox = 0;     
		widthOfVisibleBox = $(".scroll-product").width();   
		$('.scroll-product .product').each(function() {
			totalWidthOfBox += $(this).outerWidth(true);   
		});                              
        
		if (totalWidthOfBox > widthOfVisibleBox) {
			$('.scroll-product .product').wrapAll('<div id="imageSlide_Wrapper" style="position: relative; left: 0px; width: '+totalWidthOfBox+'px"></div>');

			outerToInnerRelation = (totalWidthOfBox - widthOfVisibleBox) / widthOfVisibleBox;

			$('.scroll-product').mousemove(function(event) {
			  $('#imageSlide_Wrapper').stop();
			  currentMouseposition = event.clientX - $('.scroll-product').offset()['left'];
			  interpolatedPosition = currentMouseposition * outerToInnerRelation;                    

			  if (interpolatedPosition <= (totalWidthOfBox - widthOfVisibleBox)) {
			    $('#imageSlide_Wrapper').css('left', '-'+interpolatedPosition+'px');
			  }
			});                                                   
		}










		
		completewidth = totalWidthOfBox;  
		
		if(completewidth > 420) {
			// click jump 			
			$('a.nextProductx').click(function() {
				$('#imageSlide_Wrapper').stopTime('controlled');
				$('#tooltip').remove();
				
				var fullLength = $('#imageSlide_Wrapper').width();
				var pos = $('#imageSlide_Wrapper').position();
				var common = fullLength-450;
				
				var jump = pos['left']-450;
				
				if(Math.abs(jump) >= common) {
					/*var nextStep = pos['left'];*/
				} else {
					jump = jump + 'px';
					$("#imageSlide_Wrapper").animate({"left": "+=" + jump}, "slow");
				}
		
			});
			
			// scrollen start
			$('a.nextProduct').mouseover(function() {
				$('#imageSlide_Wrapper').everyTime(50, 'controlled', function() {
					$('#tooltip').remove();
					var fullLength = $('#imageSlide_Wrapper').width();
					var pos = $('#imageSlide_Wrapper').position();
					var common = fullLength-450;
					if(Math.abs(pos['left']) >= common) {
						/*var nextStep = pos['left'];*/
					} else {
						var nextStep = pos['left']-7;
						$('#imageSlide_Wrapper').css('left', nextStep);
					}
				});
		
			});
			
			// scrollen stop
			$('a.nextProduct').mouseout(function() {
				$('#imageSlide_Wrapper').stopTime('controlled');
			});
			
			// scrollen start
			$('a.previousProduct').mouseover(function() {
				$('#imageSlide_Wrapper').everyTime(50, 'controlled', function() {
					$('#tooltip').remove();
					var pos = $('#imageSlide_Wrapper').position();
					if(pos['left'] >= 0) {
						var nextStep = pos['left'];
					} else {
						var nextStep = pos['left']+7;
					}
					$('#imageSlide_Wrapper').css('left', nextStep);
				});
		
			});
			
			// scrollen stop
			$('a.previousProduct').mouseout(function() {
				$('#imageSlide_Wrapper').stopTime('controlled');
			});	
			
	
			// aktuelles Produkt in die mitte des headers "schieben" und den entsprechenden Tooltip als callback öffnen
			$.each($('.singlescrollerproduct'), function() {
				if(document.location.pathname.search($(this).find('a').attr('href')) > 0) {
					$('#tooltip').remove();				
					var obj = $(this);				
					posRelative = obj.position();		
					
					if(posRelative['left'] > 420) {
						elmWidth = $(this).find('img').css('width');
						diff = ((450-parseInt(elmWidth))/2);
						walkLeft = Math.abs(posRelative['left']-diff);	
						
						$("#imageSlide_Wrapper").css('left', '-' + walkLeft + "px", function() {
							posAbsolute = obj.offset();
							// Tooltip setzen
							$('body').append('<span style="top:' + (posAbsolute['top']-34) + 'px;left:' + (posAbsolute['left']+18) + 'px;" id="tooltip">' + obj.find('.description').html() + '</span>');
						});
						/*$(".scroll-product").animate({left: "-=" + walkLeft + "px"}, 1450, function () {
						});*/
					}
					
				}
			});		
		} else {
			//alert(completewidth);
			$('a.nextProduct').hide();
			$('a.previousProduct').hide();
			var sidespace = (450-completewidth);
			if(sidespace > 0) {
				var median = Math.abs((sidespace/2));
				$('#imageSlide_Wrapper').css('left', median + 'px');
			}
			
			// aktuelles Produkt in die mitte des headers "schieben" und den entsprechenden Tooltip als callback öffnen
			$.each($('.singlescrollerproduct'), function() {
				if(document.location.pathname.search($(this).find('a').attr('href')) > 0) {
					$('#tooltip').remove();				
					posAbsolute = $(this).offset();

					// Tooltip setzen
					$('body').append('<span style="top:' + (posAbsolute['top']-34) + 'px;left:' + (posAbsolute['left']+18) + 'px;" id="tooltip">' + $(this).find('.description').html() + '</span>');				
				}
			});	
			
		}
	}
});


function ArticleToBasketByImageMap(artno) {
	$('#row-' + artno).addClass('bold');
	var currentValue = $('#part-' + artno).val();
	if(isNaN(currentValue)) {
		$('#part-' + artno).val(1)
	} else {
		var newValue = parseInt(currentValue);
		if(isNaN(newValue)) {
			$('#part-' + artno).val(1);
		} else {
			$('#part-' + artno).val(newValue+1);
		}
		//document.location.href='#row-' + artno;
	}
	
	$('#row-' + artno).oneTime(1200, 'controlled', function() {
		$(this).removeClass('bold');
	});
}
