$(document).ready(function() {

	var slider_timer = 6000;
	var ledionopto_timer = 7000;

	// Slider
	var slides = $('#slider .slides ul li').size();
	var next, current;
	
	$('#slider .nav li').click(function() {
		if (!$(this).hasClass('active')) {
			$(this).find('.hover').remove();
			$('#slider .nav li > .active').remove();
			$('#slider .nav li').removeClass('active');
			
			$(this).addClass('active');
			$(this).append('<span class="active"></span>');
			$(this).children('.active').fadeIn(500);
			
			next = $(this).index()*500;
			$('#slider .slides ul').stop(true, false);
			$('#slider .slides ul').animate({ 'top' : -next+'px' }, 500);
			
			clearInterval(slider_interval);
			slider_interval = setInterval(function() {  
				nextSlide();  
			}, slider_timer);
		}
	});
	
	function nextSlide() {
		current = $('#slider .nav li.active').index()+1;
		if (current+1 <= slides) {
			next = current+1;
		} else {
			next = 1;
		}

		$('#slider .nav li > .active').remove();
		$('#slider .nav li').removeClass('active');

		$('#slider .nav li:nth-child('+next+')').addClass('active');		
		$('#slider .nav li:nth-child('+next+')').append('<span class="active"></span>');
		$('#slider .nav li:nth-child('+next+') > .active').fadeIn(500);
		
		$('#slider .slides ul').stop(true, false);
		$('#slider .slides ul').animate({ 'top' : -((next-1)*500)+'px' }, 500);	
	}
	
	slider_interval = setInterval(function() {  
		nextSlide();
	}, slider_timer); 
	
	$('#slider .nav li').mouseenter(function() {
		if (!$(this).hasClass('active')) {
			$(this).append('<span class="hover"></span>');
			$(this).children('.hover').fadeIn(500);
		}
	});
	
	$('#slider .nav li').mouseleave(function() {
		$(this).children('.hover').fadeOut(250, function() {
			$(this).remove('.hover');
		});
	});
	
	$('#slider .nav li.active').append('<span class="active"></span>');
	$('#slider .nav li.active > .active').show();

	// Gallery
	var clicked = true;
	var title = true;
	var active = 1;
	var images = $('#gallery .images ul li').size();
	var next = true;
	var previous = true;
	var thumbnails_pages = Math.ceil($('#gallery .thumbnails ul li').size()/3)-1;
	var left = true;

	$('#gallery .thumbnails ul li:first-child a').addClass('active');
	$('#gallery .thumbnails strong').html($('#gallery .thumbnails ul li:first-child a img').attr('alt'));
	
	$('#gallery .thumbnails ul li a').click(function() {	
		if (!$(this).hasClass('active')) {	
			clicked = $(this).parent().index()*473;
			$('#gallery .images ul').stop(true, false);
			$('#gallery .images ul').animate({ 'top' : -clicked+'px' }, 500);
		
			title = $(this).children('img').attr('alt');
			$('#gallery .thumbnails strong').hide();
			$('#gallery .thumbnails strong').html(title);
			$('#gallery .thumbnails strong').fadeIn(500);
		}

		$('#gallery .thumbnails ul li a').removeClass('active');		
		$(this).addClass('active');
	});

	$('#gallery .images .nav').click(function() {
		active = $('#gallery .thumbnails ul li a.active').parent().index()+1;

		if ($(this).hasClass('next')) {
			next = active+1;
			if (next <= images) {
				$('#gallery .images ul').stop(true, false);
				$('#gallery .images ul').animate({ 'top' : -((next-1)*473)+'px' }, 500);
		
				title = $('#gallery .thumbnails ul li:nth-child('+next+') a img').attr('alt');
				$('#gallery .thumbnails strong').hide();
				$('#gallery .thumbnails strong').html(title);
				$('#gallery .thumbnails strong').fadeIn(500);

				if ((next-1) % 3 == 0) {
					$('#gallery .thumbnails ul').stop(true, false);
					left = parseInt($('#gallery .thumbnails ul').css('left'))-327;
					if (left >= -thumbnails_pages*327) {
						$('#gallery .thumbnails ul').animate({ 'left' : left+'px' }, 500);
					}	
				}
				
				$('#gallery .thumbnails ul li a').removeClass('active');		
				$('#gallery .thumbnails ul li:nth-child('+next+') a').addClass('active');
				active = next;
			}
		}
		
		if ($(this).hasClass('previous')) {
			previous = active-1;
			if (previous >= 1) {
				$('#gallery .images ul').stop(true, false);
				$('#gallery .images ul').animate({ 'top' : -((previous-1)*473)+'px' }, 500);
		
				title = $('#gallery .thumbnails ul li:nth-child('+previous+') a img').attr('alt');
				$('#gallery .thumbnails strong').hide();
				$('#gallery .thumbnails strong').html(title);
				$('#gallery .thumbnails strong').fadeIn(500);

				if ((previous) % 3 == 0) {
					$('#gallery .thumbnails ul').stop(true, false);
					left = parseInt($('#gallery .thumbnails ul').css('left'))+327;
					if (left <= 0) {
						$('#gallery .thumbnails ul').animate({ 'left' : left+'px' }, 500);
					}	
				}
				
				$('#gallery .thumbnails ul li a').removeClass('active');		
				$('#gallery .thumbnails ul li:nth-child('+previous+') a').addClass('active');
				active = previous;
			}
		}
	});

	$('#gallery .thumbnails .nav').click(function() {
		$('#gallery .thumbnails ul').stop(true, false);
	
		if ($(this).hasClass('next')) {
			left = parseInt($('#gallery .thumbnails ul').css('left'))-327;
			if (left >= -thumbnails_pages*327) {
				$('#gallery .thumbnails ul').animate({ 'left' : left+'px' }, 500);
			}	
		}
		
		if ($(this).hasClass('previous')) {
			left = parseInt($('#gallery .thumbnails ul').css('left'))+327;
			if (left <= 0) {
				$('#gallery .thumbnails ul').animate({ 'left' : left+'px' }, 500);
			}	
		}
	});

	// Hover effects
	$('#scroll-to-top, #back, #panel .distributor a').mouseenter(function() {
		$(this).append('<span class="hover"></span>');
		$(this).children('.hover').fadeIn(500);
		if ($(this).parent().hasClass('distributor')) {
			$(this).addClass('hovered');
			clearInterval(ledionopto_interval);
		}
	});
	
	$('#scroll-to-top, #back, #panel .distributor a').mouseleave(function() {
		$(this).children('.hover').fadeOut(500, function() {
			$(this).remove('.hover');
		});
		if ($(this).parent().hasClass('distributor')) {
			$(this).removeClass('hovered');
			ledionopto_interval = setInterval(function() {  
				ledionopto();
			}, ledionopto_timer);
		}
	});
	
	// Scroll to top
	$('#scroll-to-top').click(function() {
		$('html, body').animate({ scrollTop: 0 }, 'slow');
	});
	
	// Ledionopto logo
	function ledionopto() {
		if (!$('#panel .distributor a').hasClass('hovered')) {
			$('#panel .distributor a').append('<span class="hover"></span>');
			$('#panel .distributor a .hover').fadeIn(1000);
			$('#panel .distributor a .hover').delay(1000).fadeOut(1000, function() {
				$(this).remove('.hover');
			});
		}
	}
	
	ledionopto_interval = setInterval(function() {  
		ledionopto();
	}, ledionopto_timer);
	
	// Newsletter submit button hover
	$('#social .newsletter input[type="submit"]').mouseenter(function() {
		$(this).stop(true, false);
		$(this).animate({ 'opacity' : '0.6' }, 500);
	});
	
	$('#social .newsletter input[type="submit"]').mouseleave(function() {
		$(this).animate({ 'opacity' : '1' }, 500);
	});

	// Default input & textarea value
  $('input[type="text"], textarea').focus(function() { 
    if ($(this).val() == $(this).prop('defaultValue')) {
      $(this).val('');
    }
  });
  $('input[type="text"], textarea').blur(function() {
    if ($(this).val() == '') {
      $(this).val($(this).prop('defaultValue'));
    } 
  });

});
