var newClass = function() {
	return function() {
		return this.init.apply(this, arguments);
	}
}


var OB = newClass();

OB.prototype = {

init: function() {
 var t = this;
 
     document.onkeydown = function(e) {

        if (e == null) { // ie
        	keyCode = event.keyCode;
        } else { // mozilla
        	keyCode = e.which;
        }

		if (keyCode == 0x1B){
		   t.closeGallery();
		};
     };
 
	
	/***OnlywebBox***/
	
	
	$(".OnlywebBox img").click(function() {
										
		if ($(this).hasClass("SelImg")) {
			return false;
			
		} else {
			$(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('.TopGallery').find(".BigPhotoPB a").addClass("DisplayNone");
			$(this).parents('.TopGallery').find(".BigPhotoPB a img").attr("src",src_foto).load(function () {
			$(this).parents('.TopGallery').find(".BigPhotoPB a").removeClass("DisplayNone");
			$(this).parents('.TopGallery').find(".BigPhotoPB a").attr("href",src_big);
			$(this).parents('.TopGallery').find(".RedCross a").attr("href",src_big);
		});
		}
		
		return false;
		
	 });


    $("table.TableForImg a").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
       var src_bigPhoto = $(this).attr("href");
	   var destination = $(this).offset().top;

       $(".ContBP").css("top",destination - 300)
       $('.BgPage, .ContBP').fadeIn(0);
        $('.ImgCBP').html('<img src="" />');
        $(".WidthBP").addClass("NoNavArr");

       	showImgStart = $(".ImgCBP img").attr("src",src_bigPhoto);

		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();
			});
        });
    });
	
	$(".BigPhotoPB a, .RedCross a, .OnePicterBox").click(function() {
		var src_bigPhoto = $(this).attr("href");
		var destination = $(this).offset().top;
		
			
		$(".ContBP").css("top",destination - 0)
		$('.BgPage, .ContBP').fadeIn(0);
		
		arr = [];
		$(this).parents(".TopGallery").find(".OnlywebBox a").each(function(i){
			cur_href_ci = $(this).attr("href");															   
			arr.push(cur_href_ci);
         });
		
		if (arr.length <= 1) {
		arr.pop();
		arr.unshift(src_bigPhoto);
		$(".WidthBP").addClass("NoNavArr");
		}
		
		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;
		
		});
	
	
	$(".ElementSNP img").click(function() {
		var src_bigPhoto = $(this).parents('a').attr("href");
		var destination = $(this).offset().top;
		
		
		if ( $('.ContBP').length ) {
			
			$('.BgPage, .ContBP').fadeIn(0);	
			
		} else {
		
		$("body").append('<div class="BgPage"></div><div class="ContBP"><div class="WidthBP JsClick"><div class="ImgCBP"></div><div class="ToLeftImgCont"></div><div class="ToRightImgCont"></div><div class="TextCBP"><div class="VnTC">фото <span class="CurrentBP"></span> из <span class="AllBP"></span></div></div><div class="CloseCBP"></div><div class="B"></div></div><!-- WidthBP--><div class="B"></div></div><!--ContBP-->');
		}

		$(".ContBP").css("top",destination - 350)
		$('.BgPage, .LoaderBigPhoto, .ContBP').fadeIn(0);		
		
		arr = [];
		$(this).parents(".TopGallery").find(".ListNullImg input").each(function(i){
			cur_href_ci = $(this).val();															   
			arr.push(cur_href_ci);
         });
		
		if (arr.length < 1) {
		$(".WidthBP").addClass("NoNavArr");
		}
		
		arr.unshift(src_bigPhoto);

		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;
		
		});
	
	$("#video").click(function() {
		var srcVideoPage = $(this).attr("href");
		var destination = $(this).offset().top;
		$(".WidthBP").addClass("VideoWBP");
		
		
		$(".ContBP").css("top",destination - 350)
		$('.BgPage,  .ContBP').fadeIn(0);		
		
		$(".VideoBlockWBP").load(srcVideoPage, {limit: 25}, function(){
															   
		wWO = $(".WidthBP object").width();
		hWO = $(".WidthBP object").height();
		$(".WidthBP").animate({width: wWO, height: hWO}, 500);
		$(".CloseCBP").fadeIn("fast");
		$(".WidthBP").queue(function  () {
			$(".WidthBP object").fadeIn(500);
			$(this).dequeue();
			
		});
		
		});
		
		return false;
		
		});

	$('.CloseCBP').live("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 > *').live("mouseover", function () {
		clearTimeout(t.timer);
		$(".ToLeftImgCont, .ToRightImgCont, .TextCBP, .CloseCBP").fadeIn("fast");
	});
	
	$('.WidthBP > *').live("mouseout", function () {
		t.timeHideElImg();
	});

	/***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");
		$(".VideoBlockWBP").html("");
	},
	
	timeHideElImg :function() {
	var t = this;
    t.timer = setTimeout(function(){t.hideElImg()}, 1000);
	},
	
	hideElImg :function() {
		$(".ToLeftImgCont, .ToRightImgCont, .TextCBP, .CloseCBP").fadeOut("fast");
	}
	
};


$(document).ready(function(){
	new OB();
});
