$(function() {
	$(window).resize(function(){
		var image_width = $('.home-banner img').width();
		var image_height = $('.home-banner img').height();
		var max_width = $('.home-banner img').data('width');
		var max_height = $('.home-banner img').data('height');

		if( image_width && image_height ){
			var new_height = $(window).height() - ( $('#header').outerHeight() + $('#footer').outerHeight() );
			var new_width = Math.min( max_width, $(window).width() );

			$('.home-banner').css({
				height: new_height + 'px',
				width: new_width + 'px'
			});
			$('.home-banner img').css({
				width: new_width+'px'
			});
		}
		
		/* INNER PAGE GALLERY */
		var container_width = $('#preview').width();
		var image_width = $('#preview').children('img').width();
		if( image_width > container_width ){
			var margin = (image_width - container_width)/2;
			$('#preview').children('img').css({
				marginLeft: '-'+margin+'px'
			});
		}else{
			$('#preview').children('img').css({
				marginLeft: 'auto'
			});
		}
	});
	
	$('#preview .preloader').show();
	$('#preview').children('img').load(function(){
		$(window).trigger('resize');
		$('#preview .preloader').hide();
		$(this).show();
	});
	if( $('#preview').children('img').height() > 449 ){
		$(window).trigger('resize');
		$('#preview .preloader').hide();
		$(this).show();
	}else{
		$('#preview .preloader').show();
	}

	$('.home-banner img').load(function(){
		$(this).data('width', $(this).width()).data('height', $(this).height());

		$('.home-banner').css('width', $(this).width()+'px');

		$(window).trigger('resize');
	});

	$('#preview .img-title').html('<span>'+$('.thumb-cnt .thumb:first').attr('title')+'</span>');

	$('.thumb-cnt .thumb').click(function(){
		if( !$(this).hasClass('active') && $(this).attr('rel').length > 0 ){
			var path = $(this).attr('rel');
			var title = $(this).attr('title');
			$('.thumb-cnt .thumb.active').removeClass('active');
			$(this).addClass('active');

			$('#preview .preloader').show();
			$('#preview .main-image').fadeOut(function(){
				$('#preview .img-title').html('<span>'+title+'</span>');
				$(this).attr('src',  path);
				//if( $(this).width() && $(this).height() ){
				//	$('#preview .preloader').hide();
				//	$(this).show();
				//}else{
					$(this).unbind().load(function(){
						$('#preview .preloader').hide();
						$(this).show();
					});
				//}
			});
		}

		return false;
	});

	$('#preview .arrow-right').click(function(){
		if( $('.thumb-cnt .thumb.active').closest('.thumb-cnt').next('.thumb-cnt').length > 0 ){
			$('.thumb-cnt .thumb.active').closest('.thumb-cnt').next('.thumb-cnt').children('a').trigger('click');
		}else{
			$('.thumb-cnt .thumb:first').trigger('click');
		}

		return false;
	});

	$('#preview .arrow-left').click(function(){
		if( $('.thumb-cnt .thumb.active').closest('.thumb-cnt').prev('.thumb-cnt').length > 0 ){
			$('.thumb-cnt .thumb.active').closest('.thumb-cnt').prev('.thumb-cnt').children('a').trigger('click');
		}else{
			$('.thumb-cnt .thumb:last').trigger('click');
		}

		return false;
	});
	
	$('.wrapper').find('.view-hidden-list').click(function(){
		$(this).next('.hidden-list').slideToggle();
	});

});

