(function($) {
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(function() {
	$.preLoadImages('welcome/img/16.jpg', 'welcome/img/15.jpg', 'welcome/img/14.jpg', 'welcome/img/13.jpg', 'welcome/img/12.jpg', 'welcome/img/11.jpg', 'welcome/img/10.jpg', 'welcome/img/9.jpg', 'welcome/img/8.jpg', 'welcome/img/7.jpg', 'welcome/img/6.jpg', 'welcome/img/5.jpg', 'welcome/img/4.jpg', 'welcome/img/3.jpg', 'welcome/img/2.jpg', 'welcome/img/1.jpg');

	$('#videoPlay').fadeTo(0, 0.3);

	$('#videoBox iframe').attr({
		src: 'http://player.vimeo.com/video/27956829',
		width: 940,
		height: 400
	});

	$(function() {
		$('#videoBox').hover(function() {
			$('#videoPlay').stop().animate({
				opacity: 0.5
			}, 100);
		},

		function() {
			$('#videoPlay').stop().animate({
				opacity: 0.3
			}, 100);
		});
	});

	$('#videoBox').click(function() {
		$('#videoImg, #videoPlay').fadeOut(200, function() {
			$('#videoBox iframe').attr('src', 'http://player.vimeo.com/video/27956829?autoplay=1');
		});

		$(this).css('cursor', 'auto');
	});

	$('#welcomeThumbs img').fadeTo(0, 0.5);

	$(function() {
		$('#welcomeThumbs li').hover(function() {
			$(this).children('img').stop().animate({
				opacity: 1
			}, 100);
		},

		function() {
			$(this).children('img').stop().animate({
				opacity: 0.5
			}, 100);
		});
	});

	$('#welcomeThumbs li').click(function() {
		var imgZoomSrc = $(this).children('img').attr('src').replace('thumb', '');

		$('#imgZoom').attr('src', imgZoomSrc);

		$('#imgZoomWrapper').width($(document).width());
        $('#imgZoomWrapper, #imgZoom').height($(document).height());

		$('body').addClass('noOverflow');

		$('#imgZoomWrapper').fadeIn(200, function() {
			$('#imgZoomLeft, #imgZoomRight, #imgZoomClose').fadeTo(0, 0.5);

			$('#imgZoomLeft, #imgZoomRight').css({
				top: ($(document).height() - $('#imgZoomLeft').height()) / 2
			});

			$('#videoBox iframe').attr('src', 'http://player.vimeo.com/video/27956829');
			$('#videoImg, #videoPlay').show();
		});
	});

	$('#imgZoomClose').click(function() {
		$('#imgZoomWrapper').fadeOut(200, function() {
			$('#imgZoom').attr('src', 'http://player.vimeo.com/video/27956829');
			$('body').removeClass('noOverflow');
		});
	});

	$('#imgZoomLeft').click(function() {
		var currentIndex = parseInt($('#imgZoom').attr('src').replace(/\D/g,''), 10);

		if(currentIndex == 1) {
			var imgZoomSrc = $('#imgZoom').attr('src').replace(currentIndex, 16);
		} else {
			var imgZoomSrc = $('#imgZoom').attr('src').replace(currentIndex, currentIndex - 1);	
		}

		$('#imgZoom').attr('src', imgZoomSrc);
	});

	$('#imgZoom, #imgZoomRight').click(function() {
		var currentIndex = parseInt($('#imgZoom').attr('src').replace(/\D/g,''), 10);

		if(currentIndex == 16) {
			var imgZoomSrc = $('#imgZoom').attr('src').replace(currentIndex, 1);
		} else {
			var imgZoomSrc = $('#imgZoom').attr('src').replace(currentIndex, currentIndex + 1);	
		}

		$('#imgZoom').attr('src', imgZoomSrc);
	});

	$(function() {
		$('#imgZoomLeft, #imgZoomRight, #imgZoomClose').hover(function() {
			$(this).stop().animate({
				opacity: 1
			}, 100);
		},

		function() {
			$(this).stop().animate({
				opacity: 0.5
			}, 100);
		});
	});
});

$(window).resize(function() {
	$('#imgZoomWrapper:visible').width($(window).width());
    $('#imgZoomWrapper:visible, #imgZoom:visible').height($(window).height());

    $('#imgZoomLeft:visible, #imgZoomRight:visible').css({
		top: ($(window).height() - $('#imgZoomLeft').height()) / 2
	});
});
