var Home = (function(){
	var element = '.banner';
	return {
		init: function()
		{
			Home.handlers();
			Home.timer();
		},
		handlers: function()
		{
			
			$(element + ' .thumbs a').click(function(){
				Home.chooseImage($(this));
			});
			
			$(element + ' .nav .next').click(function(){
				if ($('ul:animated').length <= 0) {
					if (typeof(window.timing) != 'undefined') {
						clearTimeout(window.timing);
					}
					Home.go();
				}
			});
			
			$(element + ' .nav .prev').click(function(){
				if ($('ul:animated').length <= 0) {
					if (typeof(window.timing) != 'undefined') {
						clearTimeout(window.timing);
					}
					Home.go();
				}
			});
			
		},
		timer: function()
		{
			window.timing = setTimeout(Home.go,4000);
		},
		go: function()
		{
			var active = $('.crop .img img').attr('rel');
			var next_image = $("#" + active).next();
			if (next_image.length) {
				next_image.click();
			} else {
				$("#id-1").click();
			}
			Home.timer();
		},
		chooseImage: function(element)
		{
			clearTimeout(window.timing);
			var id = element.attr('id');
			var title = element.attr('title');
			var full_size = element.find('.full-size').html();

			$('.crop .loading').show();
			var img = new Image();
			$(img)
		    .load(function () {
		      $(this).hide();
		      $('.crop .img').html(this);
					$('.crop .img img').attr('rel', id);
					$('.banner .infos p').text(title);
					$('.crop .loading').hide();
		      $(this).fadeIn();
		
		    })
		    .error(function () {
		    })
		    .attr('src', full_size);
		
		}
	}
})();
$(function(){
	Home.init();
});
