(function($){
	$(document).ready(function(){

		var siteThemePerDisplayCount = Number($('#hiddenData').attr('theme-display-count'));
		$('#thumbnail-list .site-theme-thumbnail').addClass(function(index){
			return (index >= siteThemePerDisplayCount) ? 'hide':'';
		});

		$('#btnPreview').click(function(){
			$('#preview-dialog').dialog("open");
		});

		$('#thumbnail-list .site-theme-thumbnail input[type=radio]').thumbnailRadioButton({
			radioGroupId: 'thumbnail-list',
			thumbnailClass:'radio-button-thumbnail image shadow',
			select:function($thumbnail, $radio){
				$thumbnail.css('border-style', 'inset');
				$thumbnail.parent().removeClass('shadow');

				$('#btnSave').removeAttr('disabled');
				$('#themeid').val($radio.val());
			},
			deselect:function(){
				$('#thumbnail-list .radio-button-thumbnail img').css('border-style', 'solid');
				$('#thumbnail-list .radio-button-thumbnail').addClass('shadow');
			}
		});

		$('.site-theme-thumbnail .image.selected img').addClass('inset');

		$('#preview-dialog').dialog({
			title:'Preview',
			autoOpen:false,
			modal:true,
			resizable:false,
			height:805,
			width:828,
			dialogClass:'sb-dialog-container preview-dialog-container shadow-dark',
			create:function(){
				$('#btnClose').click(function(){
					$('#preview-dialog').dialog("close");
				});
			}
		});

		$('.preview-button').click(function(){
			$('.image', $(this).prev()).click();
		});

		$('.preview-button button').click(function(){
			$('#preview-dialog .dialog-content .preview-image').hide();
			$('#preview-dialog .loading-icon').show();

			var previewTitle = $('label', $(this).closest('.site-theme-thumbnail')).text();
			$('#preview-dialog').dialog("option", "title", "Preview: " + previewTitle).dialog("open");

			var img = new Image(800, 700);
			img.onload = function(){
				$('#preview-dialog .loading-icon').hide();
				$('#preview-dialog .dialog-content .preview-image').html('<img src="' + this.src + '" alt="' + this.alt + '" title="" />').show();
			}
			img.onerror = function(){
				$('#preview-dialog .loading-icon').hide();
				$('#preview-dialog .dialog-content').html('<div class="error">Error loading the preview.</div>');
			}
			img.src = $(this).parent().prevAll('img').attr('src').replace('.png', '-large.png');

			img.alt = previewTitle;
		});

		$('.pager .more a').click(function(e){
			if ($(this).hasClass('disabled')) {
				e.stopPropagation();
				return false;
			}
			$('.site-theme-thumbnail-list .site-theme-thumbnail.hide').filter(function(index){
				return index < siteThemePerDisplayCount;
			})
			.hide()
			.removeClass('hide')
			.fadeIn(1500)

			var $allItems = $('.site-theme-thumbnail-list .site-theme-thumbnail');
			var shownItemCount = $allItems.not('.hide').length;

			if ($allItems.length == shownItemCount) {
				$(this).addClass('disabled').focus(function(){
					$(this).blur();
				});
				$(this).blur();
			}
			var scrollToIndex = (Math.floor(shownItemCount / siteThemePerDisplayCount) - 1) * siteThemePerDisplayCount;
			$('html,body').animate({scrollTop: $allItems.eq(scrollToIndex).offset().top}, 1000);

			e.stopPropagation();
			return false;
		});

		if ($('.site-theme-thumbnail-list .site-theme-thumbnail').length <= siteThemePerDisplayCount) {
			$('.pager .more a').addClass('disabled').focus(function(){
				$(this).blur();
			});
		}
	});
})(jQuery);