$(document).ready(function(){
	
	$('img.thumbnails').live('mouseenter', function(){
		
		$(this).attr("src", $(this).attr("simg"));
		
	}).live('mouseleave', function(){
		
		if (!$(this).hasClass('thumbnails_active')) {
			$(this).attr("src", $(this).attr("blimg"));
		}
	}).live('click', function() {
		
		showBig( $(this) );
		
	});
	
});

function showBig( dom_id ) {
	
	if ($('img.thumbnails.thumbnails_active').length) {
		$('img.thumbnails.thumbnails_active').attr("src", $('img.thumbnails.thumbnails_active').attr("blimg")).removeClass("thumbnails_active");
	}
	
	$('.load_photo_process').fadeTo(10, 1).show();
	var new_img = $('<img>').attr("src", dom_id.attr('bimg'));
	$('#big_img img').remove();
	new_img.appendTo('#big_img').load(function(){
		$('.load_photo_process').fadeTo('fast', 0, function(){
			$(this).hide();
		});
	});
	dom_id.addClass('thumbnails_active').attr("src", dom_id.attr('simg'));
	
}

function changePage( cat, page_num, show_last_photo) {
	
	$.ajax({
		url: '/site/showcat',
		type: 'POST',
		dataType: 'html',
		data: ({ is_ajax: 'true', cat_id: cat, p: page_num, show_last_photo: show_last_photo }),
		beforeSend: function() {
			$('.ajaxload_layer.albottom').fadeTo('fast', 1);
		},
		success: function( response ) {
			$('#content').html( response );
			$('.ajaxload_layer.albottom').fadeTo('fast', 0);
		},
	});
	
}

function changeBg(css_class) {
	$('body').removeClass();
	$('body').addClass(css_class);
	$.cookie('site_style', css_class);
}

function prevPhoto()
{
	if ($('.prev_photo').hasClass('unactive')) {
		return;
	}
	$('.next_photo').removeClass('unactive');
	
	var el = $('.thumbnails.thumbnails_active').parent().prevAll('td.pt:first').children();
	if (!el.length) {
		if ($('.thumbnails.thumbnails_active').parent().parent().prevAll('tr:not(.ns):first').length) {
			
			var el = $('.thumbnails.thumbnails_active').parent().parent().prevAll('tr:not(.ns):first').children('td.pt:last').children();
			
		} else if ( $('.minus_blck').length ) {
			
			changePage( parseInt($('#current_cat_id').val()), parseInt($('#current_page_num').val())-1, true );
			return;
		} else {
			$('.prev_photo').addClass('unactive');
			return;
		}
	}
	if (el.length) showBig( $(el) );
}

function nextPhoto()
{	
	if ($('.next_photo').hasClass('unactive')) {
		return;
	}
	$('.prev_photo').removeClass('unactive');
	
	var el = $('.thumbnails.thumbnails_active').parent().nextAll('td.pt:first').children();
	if (!el.length) {
		if ($('.thumbnails.thumbnails_active').parent().parent().nextAll('tr:not(.ns):first').length) {
			var el = $('.thumbnails.thumbnails_active').parent().parent().nextAll('tr:not(.ns):first').children('td.pt:first').children();
		} else if ( $('.plus_blck').length ) {
			
			changePage( parseInt($('#current_cat_id').val()), parseInt($('#current_page_num').val())+1 );
			return;
		} else {
			$('.next_photo').addClass('unactive');
			return;
		}
	}
	if (el.length) showBig( $(el) );
}
