﻿// JavaScript Document
$(document).ready(function() {
	$('div.thumbs a').hover(function(){
		$('a.showbig img').attr({src: $(this).attr('href')});
	});
	/*$('a.showbig').click(function(e){
		e.preventDefault();
		console.log($('');
		$('div.thumbs a[href=' + $(this).children('img').attr('src') + ']').click();
	});*/
//imagebox code begin
	var prev = 'назад';
	var next = 'вперед';
	var closeimage = 'закрити';
	$('body').append('<div id="imagebox"><div id="imageborder"></div></div><div id="shadow"></div>');
	$('#imageborder').append('<img src="" alt="" /><div id="video"></div><div id="imagepanel"></div>');
	$('#imagepanel').append('<div style="float:left;"><a id="imageprev" href="javascript:void(0)">'+prev+'</a></div><div style="float:right;"><a id="imagenext" href="javascript:void(0)">'+next+'</a></div><div style="width:100px; margin:0 auto; text-align:center;"><a id="imageclose" href="javascript:void(0)">'+closeimage+'</a></div>');
	$("#shadow").css({opacity: "0.7"});
	$("#imageborder img").css({opacity: "0"});
	var last = $("a.imagebox").size()-1;
	$("a.imagebox,a.showbig").click(function(){
		var current = $(this);
		var index = $("a.imagebox").index(current);
		//alert(index);
		if(index == 0){
			$("#imageprev").hide();
		}
		if(index == last){
			$("#imagenext").hide();
		}
		if($(this).hasClass('one')){
			$("#imageprev").hide();
			$("#imagenext").hide();
		}
		var path = $(current).attr("href");
		$("a#imagenext").click(function(){
			$("#imageborder img").fadeTo(100,0);
			var next = $("a.imagebox").eq(index+1).attr("href");
			$("#imageborder img").attr({src: next});
			$("#imageprev").show();
			index = index+1;
			if(index == last){
				$("#imagenext").hide();
			}
			return false;
		});
		$("a#imageprev").click(function(){
			$("#imageborder img").fadeTo(100,0);
			var prev = $("a.imagebox").eq(index-1).attr("href");
			$("#imageborder img").attr({src: prev});
			$("#imagenext").show();
			index = index-1;
			if(index == 0){
				$("#imageprev").hide();
			}
			return false;
		});
		$("#shadow").fadeIn(800);
		$("#imagebox").fadeIn(1000);
		$("#imageborder").css({top: $(document).scrollTop() +300+'px'});
		$("#imageborder img").attr({src: path}).bind("load", function(){
			var img = $(this);
			setTimeout(function(){
				var img_w = img.width();
				var img_h = img.height();
				$("#imageborder").animate({height:img_h+24+"px", marginTop: -1*img_h/2+"px"}, 300, function(){
					$("#imageborder").animate({width:img_w+"px", marginLeft: -1*img_w/2+"px"}, 300, function(){
						$("#imageborder img").fadeTo(100,1);
						$("#imagepanel").slideDown(200);
					});
				});
			},1000);
		});
		return false;
	});
	$("#imageclose,#shadow").click(function(){
		$("#imageborder img").fadeTo(0,0).attr({src: ""});
		$("#imagepanel").slideUp(0);
		$("#imageborder").animate({height:"16px", marginTop: "-8px"}, 200, function(){
			$("#imageborder").animate({width:"16px", marginLeft: "-8px"}, 200);
		});
		$("#shadow").fadeOut(200);
		$("#imagebox").fadeOut(200);
		$("#imagenext").show(0);
		$("#imageprev").show(0);
	});
	//imegabox code end
});
