/* ******************************************************************************************************************************
 * BuxGallery - jQuery Plugin
 * http://buxtom.ru
 *
 * Copyright (c) 2011 Buxtom
 * 
 * Version: 0.1 (16/10/2011)
 * Requires: jQuery v1.3+   
 ****************************************************************************************************************************** */

(function($) {



	$.fn.bux_gallery = function(bux_gallery_group) {

		var bux_gallery_index;
		var bux_gallery_arr;
		var bux_gallery_arr_ln;	
		
	
	
		//Создаём массив, определяем текущую картинку и общее количество элементов
		 bux_gallery_arr = jQuery.makeArray($("a." + bux_gallery_group));
		 bux_gallery_index = $("a." + bux_gallery_group).index(this);
		 bux_gallery_arr_ln = $(bux_gallery_arr).length;
		 
	
		 
		var bux_gallery_scroll = $(window).scrollTop();
		$("#bux_gallery_over").css("top", bux_gallery_scroll + "px");
		
		$("#bux_gallery_content").html("<img src='" + $(bux_gallery_arr).eq(bux_gallery_index).attr('href') + "' />");
		$("span.bux_gallery_status").html(bux_gallery_index+1 + "/" + bux_gallery_arr_ln);
		
		
		$("#bux_gallery_muar").fadeIn(50);
        $("#bux_gallery_over").fadeIn(300);
		
	
		$(".bux_gallery_but_next").click(function(){
		
			bux_gallery_index = bux_gallery_index + 1;
			if (bux_gallery_index == bux_gallery_arr_ln) 
			{
				bux_gallery_index = 0;
			}
			$("#bux_gallery_content").html("<img src='" + $(bux_gallery_arr).eq(bux_gallery_index).attr('href') + "' />");
			$("span.bux_gallery_status").html(bux_gallery_index+1 + "/" + bux_gallery_arr_ln);
		});	
		
		
		$(".bux_gallery_but_prew").click(function(){
		
			bux_gallery_index = bux_gallery_index - 1;
			if (bux_gallery_index < 0) 
			{
				bux_gallery_index = bux_gallery_arr_ln - 1;
			}
			$("#bux_gallery_content").html("<img src='" + $(bux_gallery_arr).eq(bux_gallery_index).attr('href') + "' />");
			$("span.bux_gallery_status").html(bux_gallery_index+1 + "/" + bux_gallery_arr_ln);
		});	
		
		
		$(".bux_gallery_close").click(function(){
			$("#bux_gallery_muar").fadeOut(150);
			$("#bux_gallery_over").fadeOut(300);
			
			$(document).unbind('keydown');
		});

		//Кнопки
		$(document).bind('keydown', function(e) {
			
			if (e.keyCode == 27) {
				$("#bux_gallery_muar").fadeOut(150);
				$("#bux_gallery_over").fadeOut(300);
				
				$(document).unbind('keydown');
			}
			
			if (e.keyCode == 39) {
				bux_gallery_index = bux_gallery_index + 1;
				if (bux_gallery_index == bux_gallery_arr_ln) 
				{
					bux_gallery_index = 0;
				}
				$("#bux_gallery_content").html("<img src='" + $(bux_gallery_arr).eq(bux_gallery_index).attr('href') + "' />");
				$("span.bux_gallery_status").html(bux_gallery_index+1 + "/" + bux_gallery_arr_ln);
			}
			
			if (e.keyCode == 37) {
				bux_gallery_index = bux_gallery_index - 1;
				if (bux_gallery_index < 0) 
				{
					bux_gallery_index = bux_gallery_arr_ln - 1;
				}
				$("#bux_gallery_content").html("<img src='" + $(bux_gallery_arr).eq(bux_gallery_index).attr('href') + "' />");
				$("span.bux_gallery_status").html(bux_gallery_index+1 + "/" + bux_gallery_arr_ln);
			}
			
		});		
	}
	
	
})(jQuery);



