$(function() {

	// Förstasidan
	if ($('#firstpage').size() > 0) {
	
		var images = $('#firstpage-image img');
		
		images.eq(0).css('z-index', '2');
		
		images.eq(0).mouseenter(function() {
		
			images.eq(0).fadeOut(500);
			images.eq(1).fadeIn(500, function() {
			
				images.eq(0).css('z-index', '1');
				images.eq(1).css('z-index', '2');
			
			});
		
		});
		
		images.eq(1).mouseleave(function(e) {
		
			if ($(e.relatedTarget).is('area'))
				return false;
		
			images.eq(1).fadeOut(500);
			images.eq(0).fadeIn(500, function() {
			
				images.eq(0).css('z-index', '2');
				images.eq(1).css('z-index', '1');
			
			});
		
		});
	
	}

	// Förstasida & strategisida
	if ($('#strategi').size() > 0 || $('#firstpage').size() > 0) {
		
		var tooltips = $('.tooltips .tooltip');
	
		$('#illustration').find('area').each(function(index) {
		
			$(this).tooltip({
				bodyHandler: function() {
					return tooltips.eq(index).html();
				},
				delay: 0,
				showURL: false,
				track: true
			});
		
		});
	
	}

	// Produktsida
	if ($('#system').size() > 0) {
	
		var animating = false;
		var leftScroll = $('.scroll-left');
		var rightScroll = $('.scroll-right');
		var scroller = $('.system-list-scroller');
		var systemCount = $('.system-list .system').size();
		var visibleCount = 7;
		var systemWidth = 130;
		var overflow = systemCount - visibleCount;
		var offset = Math.abs(parseInt(scroller.css('left'))) / systemWidth;
		
		function scrollLeft() {
		
			if (animating)
				return false;
			
			if (offset === 0)
				return false;
			
			offset--;
				
			animating = true;
				
			scroller.animate({ left: '+='+systemWidth }, 250, function() { animating = false; });
			
			naviCheck();
		
		}
		
		function scrollRight() {
		
			if (animating)
				return false;
				
			if (offset == overflow)
				return false;
			
			offset++;
			
			animating = true;
				
			scroller.animate({ left: '-='+systemWidth }, 250, function() { animating = false; });
			
			naviCheck();
		
		}
		
		function naviCheck() {
		
			if (offset == overflow)
				rightScroll.hide();
			else
				rightScroll.show();
				
			if (offset === 0)
				leftScroll.hide();
			else
				leftScroll.show();
		
		}
		
		if (systemCount > visibleCount) {
		
			// Bind events
			leftScroll.find('a').click(function(e) { e.preventDefault(); scrollLeft(); });
			rightScroll.find('a').click(function(e) { e.preventDefault(); scrollRight(); });
			
			naviCheck();
		
		}
		
		// Remember offset and enable enhanced tooltip
		$('.system-list a').click(function(e) {
		
			e.preventDefault();
			
			window.location = $(this).attr('href')+'?offset='+offset;
		
		}).tooltip({
			delay: 0,
			showURL: false,
			track: true,
			extraClass: 'system'
		});
		
		// Check if active product is visible
		/*
		var activeIndex = $('#system-'+BOTEK.system).index();
		if (activeIndex > 6) {
		
			setTimeout(function() {
			
				animating = true;
				
				if ((systemCount-activeIndex) >= 7) {
					scroller.animate({ left: '-='+(activeIndex*systemWidth) }, 0, function() { animating = false; });
					offset = activeIndex;
				} else {
					var scrollIndex = activeIndex - (visibleCount-(systemCount-activeIndex));
					scroller.animate({ left: '-='+(scrollIndex*systemWidth) }, 0, function() { animating = false; });
					offset = scrollIndex;
				}
				
				naviCheck();
			
			}, 500);
			
		}
		*/
	
	}
	
	// Ajax links
	$('a.ajax-link').live('click', function() {
		
		var url = this.href;
		
		$('#popup-content').hide();
		$('#popup-wrapper').hide();
			
		window.location='#';
		
		$.get(url, function(data) {
			
			var closeHtml = $('<span>Stäng</span>').addClass('popup-close').click(function() {
				$('#popup-content').slideUp(250, function() { $('#popup-wrapper').fadeOut(150); }).html('');
			})
			
			$('#popup-content').html(data).append(closeHtml);
			$('#popup-wrapper').css('display', 'inline');
			$('#popup-content').slideDown(250);
			
		});
		
		return false;
		
	});

});
