$(document).ready(function() {
	
	var clickedElement;
	
	/*
	if($('#sale').length) {
		$('#sale').fancybox().click();
	}
	*/
	
	if($('#sidebar').length) {
		$('#sidebar ul ul li:last-child h2').css('padding-bottom', '20px');
	}
	
	if($('#slideshow').length) {
		$('#slideshow').cycle({
			fx: 'scrollHorz',
			next: '#next-button',
			prev: '#prev-button',
			speed: 800,
			sync: true,
			timeout: 8000
		});
	}
		
	$('#gallery a').fancybox();
	
	$('a.inline').fancybox({
		frameWidth: 300,
		frameHeight: 50,
		hideOnContentClick: false,
		callbackOnShow: function() {
			clickedElement = $.fn.fancybox.item.elem;
			$('#fancy_div').find('#image-src').val($.fn.fancybox.item.href.match(/\d+.jpg$/));
			$('#fancy_div').find('#image-title').val($.fn.fancybox.item.title);
		}
	});
	
	if($('.sf-menu').length) {
		$('.sf-menu').superfish();
	}
	
	if ($('#sidebar').length) {
		var active = $('#main').attr('class');
		$('#sidebar a.' + active).addClass('active');
	}
	
	$('.pager').click(function() {		
		var page = $(this).attr('href');
		if(page == 0) return false;
		
		refresh(page);
		
		return false;	
	});
	
	$('a.image-box').hover(function() {
		$('.edit-clear').appendTo(this).show();
	});
	
	$('.pager, #top').live('mouseover', function() {
		$('.edit-clear').hide().appendTo('#main');
	});
	
	$('.clear').click(function() {
		var answer = confirm('Naozaj chcete vymazať obrázok?');
		
		if(answer) {
			var container = $(this).parent().parent();
			imageSrc = $(container).attr('href');
			imageSrc = imageSrc.match(/\d+.jpg$/);
						
			$.ajax({
				type: 'POST',
				url: 'delete_image.php',
				data: 'src=' + imageSrc,
				success: function(response) {
					if(response) {						
						$(container).fadeOut(500, function() {
							$('.edit-clear').hide().appendTo('#main');
							$(container).remove();
							
							var count = $('#gallery').find('a').length;
							var nextPage = parseInt($('#next').attr('href'), 10);
							var prevPage = parseInt($('#prev').attr('href'), 10);
							
							if(count <= 15 && nextPage)
								refresh(prevPage + 1);
							else if(!count && prevPage)
								refresh(prevPage);
						});	
					}
				}
			});
		}
		return false;
	});
	
	$('#submit').live('click', function() {
		var src = $(this).prev().prev().val();
		var title = $(this).prev().val();
		
		$.ajax({
			type: 'POST',
			url: 'edit_image.php',
			data: 'src=' + src + '&title=' + title,
			success: function() {
				$(clickedElement).parent().parent().attr('title', title);
				$.fn.fancybox.close();
			}
		});	
	});
		
	$('#image-title').live('keypress', function(e) {
		if(e.which == 13)
			$('#fancy_div').find('#submit').trigger('click');
	});

	
	if($('#fileInput').length) {
		$('#fileInput').fileUpload({
			uploader: '../admin/uploader.swf',
			script: '../admin/upload.php',
			cancelImg: '../admin/img/cancel.png',
			folder: '../temp',
			multi: true,
			fileDesc: '*.jpg, *.png, *.gif',
			fileExt: '*.jpg; *.png; *.gif'
		});
	}
	
	$('#start-upload').click(function() {
		$('#fileInput').fileUploadStart();
		return false;
	});
	
	$('#clear-list').click(function() {
		$('#fileInput').fileUploadClearQueue();
		return false;
	});
	
	$("#pageflip").hover(function() { //On hover...
		$("#pageflip img , .msg_block").stop()
			.animate({ //Animate and expand the image and the msg_block (Width + height)
				width: '320px',
				height: '320px'
			}, 500);
		$(".msg_block").stop()
			.animate({ //Animate and expand the image and the msg_block (Width + height)
				width: '305px',
				height: '305px'
			}, 500);
		} , function() {
		$("#pageflip img").stop() //On hover out, go back to original size 50x52
			.animate({
				width: '57px',
				height: '57px'
			}, 220);
		$(".msg_block").stop() //On hover out, go back to original size 50x50
			.animate({
				width: '50px',
				height: '50px'
			}, 200); //Note this one retracts a bit faster (to prevent glitching in IE)
	});
	
});

function refresh(page) {
	
	$('#gallery').fadeOut(500, function() {
		$('#gallery a').remove();
		$('#loader').fadeIn('fast');
						
		if(/admin/.test(window.location))
			var pagerUrl = '../pager.php';
		else
			var pagerUrl = 'pager.php';
			
		$.ajax({
			type: 'POST',
			url: pagerUrl,
			data: 'page=' + page,
			dataType: 'xml',
			success: function(result) {
				var imageCount = $(result).find('image').length;
				var nextPage = $(result).find('nextPage').text();
				var loaded = 0;
					
				$(result).find('image').each(function() {
					var img = new Image();
					var imageSource = $(this).attr('imageSource');
					var thumbSource = $(this).attr('thumbSource');
					var imageTitle = $(this).attr('imageTitle');
					var showTitle = $(this).attr('showTitle');
					
					var anchor = $('<a class="image-box" rel="group"></a>').attr('href', imageSource);
					if(showTitle == 1 || $('.admin').length) $(anchor).attr('title', imageTitle);
					
					$(anchor).hover(function() {
						$('.edit-clear').appendTo(this).show();
					}).append(img);
					
					$(img).load(function() {
						loaded += 1;
						$('#gallery').append(anchor);
						if(loaded == imageCount) {
							$('#loader').fadeOut('fast', function() {
								$('#gallery').fadeIn(500);
								$('#gallery a').fancybox();
								$('#prev').attr('href', page - 1);
								$('#next').attr('href', nextPage);
							});
						}
					}).attr('src', thumbSource);
				});
			}
		});
	});
			
}
