var newClass = function() {
	return function() {
		return this.init.apply(this, arguments);
	}
}


var Smena = newClass();

Smena.prototype = {

init: function() {
 var t = this;
 keyCode = 0;
 
 

     document.onkeydown = function(e) {

        if (e == null) { // ie
        	keyCode = event.keyCode;
        } else { // mozilla
        	keyCode = e.which;
        }

        if (keyCode == 0x25){
		   t.leftGalleryCP();
		};
		
		if (keyCode == 0x27){
		   t.rightGalleryCP();
		};
		
		if (keyCode == 0x1B){
		   t.closeGallery();
		};
     };
 
	
	$(".Price img").mouseenter(function() {
	$(".Price").addClass("PriceSel");
	});
	
	$(".Price img").mouseleave(function() {
	$(".Price").removeClass("PriceSel");
	});
	
	$(".LeftLP, .LeftCt").click(function() {
		t.leftGalleryCP();
	
});


$(".RightLP, .RightCt").click(function() {
		t.rightGalleryCP();
	});

	/***OnlywebBox***/
	
	$(".OnlywebBox img").click(function() {
		$(this).parents(".TopGallery").find(".OnlywebBox img").removeClass("SelImg");
		$(this).addClass("SelImg");
		var src_foto = $(this).parents('.OnlywebBox').find('input').val();
		var src_big = $(this).parents('a').attr("href");
		$(this).parents('.PhotoBlock').find(".BigPhotoPB a").addClass("DisplayNone");
		$(this).parents('.PhotoBlock').find(".BigPhotoPB a img").attr("src",src_foto).load(function () {
		$(this).parents('.PhotoBlock').find(".BigPhotoPB a").removeClass("DisplayNone");
		$(this).parents('.PhotoBlock').find(".BigPhotoPB a").attr("href",src_big);
		});
		return false;
		
	 }); 
	
	$(".TopGallery .BigPhotoPB img").click(function() {
		var src_bigPhoto = $(this).parents('a').attr("href");
		var destination = $(this).offset().top;
		$(".ContBP").css("top",destination - 150)
		$('.BgPage, .LoaderBigPhoto, .ContBP').fadeIn(0);		
		
		arr = [];
		$(this).parents(".TopGallery").find(".OnlywebBox a").each(function(i){
			cur_href_ci = $(this).attr("href");															   
			arr.push(cur_href_ci);
         });

		a = arr.length;
		$(".AllBP").text(a);
		$('.ImgCBP').html('<img src="" />');
		numberImgStart = jQuery.inArray(src_bigPhoto, arr);	
		$('.CurrentBP').text(numberImgStart+1);

		showImgStart = $(".ImgCBP img").attr("src",arr[numberImgStart]);
		showImgStart.load(function () {
			widthCurImg = showImgStart.width();
			heightCurImg = showImgStart.height();
			$(".WidthBP").animate({width: widthCurImg, height: heightCurImg}, 500);
			
			$(".WidthBP").queue(function  () {
			$(".WidthBP img").fadeIn(500);
			$(this).dequeue();
			});
	
		});
		
		

		return false;
		
		});

	$('.CloseCBP').click(function() {
		t.closeGallery();
	});

	$('.JsClick .ToLeftImgCont').live("click", function() {
				
		a = $(".ImgCBP img").attr("src");
		b = jQuery.inArray(a, arr);
		if (b == 0) {
			c = arr.length -1;
		} else {
		c = b - 1;
		}
		
		
		t.changeShowPhoto();
	});
	
	$('.JsClick .ToRightImgCont').live("click", function() {
		a = $(".ImgCBP img").attr("src");
		b = jQuery.inArray(a, arr);
		if (b == arr.length-1) {
			c = 0;
		} else {
		c = b + 1;
		}
		
		t.changeShowPhoto();
	});
	
	$('.WidthBP').mouseenter (function () {
		$(".ToLeftImgCont, .ToRightImgCont, .TextCBP, .CloseCBP").fadeIn("fast");
	});
	
	$('.WidthBP').mouseleave (function () {
		$(".ToLeftImgCont, .ToRightImgCont, .TextCBP, .CloseCBP").fadeOut("fast");
	});

	/***OnlywebBox***/

},

	leftGalleryCP: function() {
		
		if ($(".ListProducts").find("div.CurImg").is(":first-child")) {
				$(".ListProducts div").removeClass("CurImg");
				$(".ListProducts img").fadeOut(0);
				src_hs = $(".ListProducts div:last-child").find("input").val();
				$(".ListProducts").find("div:last-child input").replaceWith("<img src='"+src_hs+"' />");
				$(".ListProducts").find("div:last-child").addClass("CurImg");
				$(".CurImg img").fadeIn("slow");
				div_hs = $(".ListSP div:last-child");
				 $(".ListSP .ShowProduct").removeClass("CurSP");
				 div_hs.addClass("CurSP");
			}
			else {
				 src_hs = $(".CurImg").prev("div").find("input").val();
				 $(".CurImg").prev("div").find("input").replaceWith("<img src='"+src_hs+"' />");
				 $hsEdit = $(".CurImg").prev("div");
				 $(".ListProducts div").removeClass("CurImg");
				 $(".ListProducts").find("img").fadeOut(0);
				 $hsEdit.addClass("CurImg");
				 $(".CurImg img").fadeIn("slow");
				 div_hs = $(".ListSP .CurSP").prev("div");
				 $(".ListSP .ShowProduct").removeClass("CurSP");
				 div_hs.addClass("CurSP");
			}
	},
	
	rightGalleryCP: function() {
	if ($(".ListProducts").find("div.CurImg").is(":last-child")) {
				$(".ListProducts div").removeClass("CurImg");
				$(".ListProducts img").fadeOut(0);
				src_hs = $(".ListProducts div:first-child").find("input").val();
				$(".ListProducts").find("div:first-child input").replaceWith("<img src='"+src_hs+"' />");
				$(".ListProducts").find("div:first-child").addClass("CurImg");
				$(".CurImg img").fadeIn("slow");
				div_hs = $(".ListSP div:first-child");
				 $(".ListSP .ShowProduct").removeClass("CurSP");
				 div_hs.addClass("CurSP");
			}
			else {
				 src_hs = $(".CurImg").next("div").find("input").val();
				 $(".CurImg").next("div").find("input").replaceWith("<img src='"+src_hs+"' />");
				 $hsEdit = $(".CurImg").next("div");
				 $(".ListProducts div").removeClass("CurImg");
				 $(".ListProducts").find("img").fadeOut(0);
				 $hsEdit.addClass("CurImg");
				 $(".CurImg img").fadeIn("slow");
				 div_hs = $(".ListSP .CurSP").next("div");
				 $(".ListSP .ShowProduct").removeClass("CurSP");
				 div_hs.addClass("CurSP");
			}
	},
	
	changeShowPhoto: function() {
		$(".WidthBP").removeClass("JsClick");
		$('.CurrentBP').text(c+1);
		$(".ImgCBP img").fadeOut(0);
		showImg = $(".ImgCBP img").attr("src",arr[c]);
		widthCurImg = showImg.width();
		heightCurImg = showImg.height();
		$(".WidthBP").animate({width: widthCurImg, height: heightCurImg}, 500);
		
		showImg.load(function() {
		$(".WidthBP").queue(function  () {
			$(".WidthBP img").fadeIn(500);
			$(".WidthBP").addClass("JsClick");
			$(this).dequeue();
			});				  
		});
		
	},
	
	closeGallery: function() {
		$(".ContBP, .BgPage").fadeOut("fast");
	}
	
};


$(document).ready(function(){
	new Smena();
	
	$(function(){
	    $('._blank').click(function(){
	        window.open(this.href);
	        return false;
	    });
	});

});
