var mainURL = 'http://dsk77.ru/';
var curstart = 0;
(function($){
	$.fn.getGallery = function(imagesArr){
		var gallery_cont = this;
		var firstimg = $("<img/>");				
		$(firstimg)			
			.addClass ("galleryimage")						
			.attr ("id", 0)
			.appendTo ($(this));

		$("<div/>")
			.addClass ("smallcont")
			.appendTo (this)
			.css ("position", "absolute")			
			.css ("z-index", 100)
			.css ("padding", "0")
			.width (362)
			.height (49)		
			.css ("left", 0)
			.css ("top", 223)
			.fadeTo(0,0);
			
		$("<img/>")
			.attr ("src", mainURL + "public/img/left.gif")
			.css ("float", "left")
			.css ("position", "absolute")
			.css ("left", 0)
			.height (49)
			.width (15)			
			.addClass ("leftstrelka")
			.css ("cursor", "pointer")
			.click (function(){
				$(".smlimg").html("");
				if (curstart - 10 < 0){
					curstart = 10;
				}
				for (var i = curstart - 10; i < curstart; i++){							
					if (i<imagesArr.length){
						$("<img/>")
							.attr ("src", mainURL + "public/img/small/" + imagesArr[i])
							.height (49)
							.attr ("id", i)
							.addClass ("smallimg")
							.css ("cursor", "pointer")
							.appendTo (".smlimg[id=" + (i-curstart + 10) + "]")
							.click (function(){
								$("img.galleryimage")
									.attr("src", mainURL + "public/img/big/" + imagesArr[$(this).attr("id")]);
								if ($(this).height()/$(this).width() < 3/4){
									$("img.galleryimage").width (362);
									$("img.galleryimage").height ("");
								}
								else {						
									$("img.galleryimage").height (272);
									$("img.galleryimage").width ("");
								}
							})	
							.load (function(){
								if ($(this).width()<65){
									var ratio = $(this).height()/$(this).width();
									$(this).width(65);
									$(this).height (ratio*65);
								}								
								$(this).css ("margin-left", -Math.round(($(this).width()-65)/2));
								$(this).css ("margin-top", -Math.round(($(this).height()-49)/2));
							})						
					}
				}
				curstart = curstart - 5;
				$(".smallimg")
					.css ("opacity", 0)
					.animate ({opacity: 1}, 500);					
				
								
			})
			.appendTo ($(".smallcont"));
		$("<div/>")		
			.addClass ("smimcont")
			.css ("float", "left")	
			.css ("z-index", 300)
			.css ("position", "absolute")
			.css ("left", 13)
			.width (336)
			.css ("overflow", "hidden")
			.appendTo ($(".smallcont"));	
		
		$("<img/>")
			.attr ("src", mainURL + "public/img/right.gif")
			.css ("float", "left")	
			.css ("position", "absolute")
			.css ("left", 347)
			.height (49)
			.width (15)	
			.addClass ("rightstrelka")
			.css ("cursor", "pointer")
			.click (function(){
				$(".smlimg").html("");
				if (curstart + 5 > imagesArr.length){
					curstart = imagesArr.length - 5;
				}
				if (curstart<0)
					curstart = 0;
				for (var i = curstart; i < curstart + 5; i++){	
					if (i< imagesArr.length){
						$("<img/>")
							.attr ("src", mainURL + "public/img/small/" + imagesArr[i])
							.height (49)
							.attr ("id", i)
							.addClass ("smallimg")
							.css ("cursor", "pointer")
							.appendTo (".smlimg[id=" + (i-curstart) + "]")
							.click (function(){
								$("img.galleryimage")
									.attr("src", mainURL + "public/img/big/" + imagesArr[$(this).attr("id")]);
								if ($(this).height()/$(this).width() > 3/4){
									$("img.galleryimage").height (272);
									$("img.galleryimage").width ("");
								}
								else {						
									$("img.galleryimage").width (362);
									$("img.galleryimage").height ("");
								}
							})
							.load (function(){
								if ($(this).width()<65){
									var ratio = $(this).height()/$(this).width();
									$(this).width(65);
									$(this).height (ratio*65);
								}								
								$(this).css ("margin-left", -Math.round(($(this).width()-65)/2));
								$(this).css ("margin-top", -Math.round(($(this).height()-49)/2));
							})	
					}
				}
				curstart = curstart + 5;
				$(".smallimg")
					.css ("opacity", 0)
					.animate ({opacity: 1}, 500);					
				
								
			})			
			.appendTo ($(".smallcont"));
		
		for (var i = 0; i < 5; i++){
			
			$("<div/>")
				.width (65)				
				.height (49)
				.css ("margin", "0px 1px 0px 1px")
				.css ("overflow", "hidden")
				.css ("float", "left")
				.addClass("smlimg")
				.attr("id", i)
				.appendTo (".smimcont");				
		}
			
		$(this)
			.mouseover (function(){
				if (imagesArr.length >1)
					$(".smallcont")
						.stop()
						.fadeTo("slow", 0.7);
			})
			.mouseout (function(){
				if (imagesArr.length >1)			
					$(".smallcont")
						.stop()
						.fadeTo("slow", 0);
			});			
			
		$.each (imagesArr, function(i, item){

		})
		$(".rightstrelka").click();
		$(".smallimg[id=0]").click();
	}	
	

	
})(jQuery);

$(document).ready(function(){
	


});
