/* Author:Kalle
*/
var Gallery = {
	width : null,
	animating : false,
	currentPage : 1,
	$current : null,
	timer : null,
	timerSeconds : 10,
	pageCount : null,
	init : function() {
		if($('#gallery').size() == 0) return;
		Gallery.pageCount = $('#gallery .gallery-image').size();
		Gallery.width = $('#gallery').css('width');
		Gallery.startTimer()

		if(Gallery.pageCount > 1) {
			$current = $('#gallery .gallery-image:eq(0)');
			$('#gallery').append('<a href="#" id="gallery-next"></a><a href="#" id="gallery-previous"></a>')
			$('#gallery-next').css('right', '-40px')
			$('#gallery-previous').css('left', '-40px')
		} else {
			return;
		}

		$('#gallery-actions').css('bottom', '-36px')

		$('#gallery').hover(function() {
			$('#gallery-actions').stop().animate({'bottom' : 0}, 'fast')
			$('#gallery-next').stop().animate({'right' : 0}, 'fast')
			$('#gallery-previous').stop().animate({'left' : 0}, 'fast')
		},function() {
			$('#gallery-actions').stop().animate({'bottom' : '-36px'}, 'slow')
			$('#gallery-next').stop().animate({'right' : '-40px'}, 'fast')
			$('#gallery-previous').stop().animate({'left' : '-40px'}, 'fast')
		})

		$('#gallery .gallery-image:gt(0)').css('left', Gallery.width)
		
		$('#gallery-next').click(function(e) {
			e.preventDefault();
			clearInterval(Gallery.timer)
			Gallery.next();
			
		})
		$('#gallery-previous').click(function(e) {
			e.preventDefault();
			clearInterval(Gallery.timer)
			if (Gallery.animating) return;
			
			Gallery.animating = true;
			Gallery.currentPage--;
			if (Gallery.currentPage == 0) {
				Gallery.currentPage = Gallery.pageCount;
				$prev = $('#gallery .gallery-image:eq('+(Gallery.pageCount-1)+')');
			} else {
				$prev = $current.prev();
			}

			$prev.css('left', '-'+Gallery.width);
			$current.stop().animate({'left' :  Gallery.width}, 'slow', 'easeInSine')
			$prev.stop().animate({'left' : 0}, 'slow', 'easeInSine', function() {
				$current = $(this);
				Gallery.animating = false;
				Gallery.setPaging();
				
			});
		})
		$('.gallery-pagination li a').click(function(e) {
			e.preventDefault();
			clearInterval(Gallery.timer)
			if (Gallery.animating) return;
			var page = parseInt($(this).data('page'));
			if (Gallery.currentPage == page) return;
			
			Gallery.animating = true;
			Gallery.currentPage = page;
			$next = $('#gallery .gallery-image:eq(' + (Gallery.currentPage-1) + ')');
			$next.css('left', Gallery.width);
			$current.stop().animate({'left' : '-' + Gallery.width}, 'slow', 'easeInSine')
			$next.stop().animate({'left' : 0},  'slow', 'easeInSine', function() {
				$current = $(this);
				Gallery.animating = false;
				Gallery.setPaging();
				
			})
		});
	},
	next : function() {
		if (Gallery.animating) return;
		
		Gallery.animating = true;
		Gallery.currentPage++;

		if (Gallery.currentPage > Gallery.pageCount) {
			Gallery.currentPage=1
			$next = $('#gallery .gallery-image:eq(0)');
		} else {
			$next = $current.next();
		}
		$next.css('left', Gallery.width);
		$current.stop().animate({'left' : '-' + Gallery.width}, 'slow', 'easeInSine')
		$next.stop().animate({'left' : 0},  'slow', 'easeInSine', function() {
			$current = $(this);
			Gallery.animating = false;
			Gallery.setPaging();
			
		})
	},
	setPaging : function() {
		$('#slideshow-read-more').attr('href', $current.data('link'))
		$('#aside h1, #aside h2, #aside .content').fadeOut('fast', function() {
			$('#aside h1').text($current.data('title'))
			$(this).fadeIn();
		})
		$('#aside h2').fadeOut('fast', function() {
			$('#aside h2').text($current.data('sub-title'))
			$(this).fadeIn();
		})
		$('#aside .content').fadeOut('fast', function() {
			$('#aside  .content').html($current.data('description'))
			$(this).fadeIn();
		})
		
		$('.gallery-pagination li').removeClass('current');
		$('.gallery-pagination li:eq(' + (Gallery.currentPage-1) + ')').addClass('current');
	},
	startTimer : function() {
		Gallery.timer = setInterval(function() {
			Gallery.next();
		}, Gallery.timerSeconds * 1000)
	}
	
}

$(function() {
	$("select").uniform();
	$("#contact-search select").change(function() {
		// location.href = '?' + $(this).attr('name') + '=' + $(this).val();
		$(this).closest('form').submit()
	});
	$('input[placeholder], textarea[placeholder]').placeholder();
	
	Gallery.init();
})










