// animating index images
function animate_i_imgs(photo, num_imgs, period, fade_out)
{
	var i = photo;
	var nav_elm = $("#images .navs dt").eq(i);
	var new_img = $("#images .list dt").eq(i);
	var old_img = $("#images .list .a");
	if (nav_elm.length == 1 && new_img.length == 1)
	{
		$("#images .list dt").removeClass("next_a");
		new_img.addClass("next_a");
		old_img.stop();
		old_img.fadeOut(fade_out, function()
			{
				new_img.removeClass("next_a").addClass("a");
				old_img.css({opacity: 1}).show().removeClass("a");
				i = $("#images .list dt").index($("#images .list .a"));
				$("#images .navs dt").removeClass("a");
				nav_elm = $("#images .navs dt").eq(i);
				nav_elm.addClass("a");
			}
		);
		++photo;
		if (photo > num_imgs - 1)
		{
			photo = 0;
		}
	}
	return photo;
}
// setting block height equal to given one (ie6< check)
function correct_blck_height(blck, h_model, flg_iechk)
{
	if ((flg_iechk == true && $.browser.msie && $.browser.version <= 6) || flg_iechk == false)
	{
		blck.height(h_model);
	}
	return;
}
// setting elm bottom to bottom - only for ie6
function correct_bottom_pos(elm, parent, bottom)
{
	if ($.browser.msie && $.browser.version <= 6)
	{
		var h = parent.height();
		var bb = (!isNaN(parseInt(parent.css("paddingBottom")))) ? parseInt(parent.css("paddingBottom")) : 0;
		var bt = (!isNaN(parseInt(parent.css("paddingTop")))) ? parseInt(parent.css("paddingTop")) : 0;
		elm.css({top: h + bb + bt + bottom});
	}
	return;
}
// setting footer to bottom - only for ie6
function correct_footer_pos()
{
	if ($.browser.msie && $.browser.version <= 6)
	{
		var doc_h = $(body).height();
		var f_h = $("#footer").height();
		$("#footer").css({top: doc_h - f_h});
	}
	return;
}
// switching photo in gallery
function switch_photo(indx, total_gal)
{
	$("#gallery .list .a").removeClass("a");
	$("#gallery .list .item").eq(indx).addClass("a");
	loadPhoto(getPhotoId($("#gallery .list .a a")[0]));
	show_photo_navigation(indx, total_gal);
	return;
}
function show_photo_navigation(indx, total_gal){
	if (indx == total_gal - 1 && !$("#gallery .nav_next").is(":hidden"))
	{
		$("#gallery .nav_next").hide();
	}
	else if (indx != total_gal - 1 && $("#gallery .nav_next").is(":hidden"))
	{
		$("#gallery .nav_next").show();
	}
	if (indx == 0 && !$("#gallery .nav_prev").is(":hidden"))
	{
		$("#gallery .nav_prev").hide();
	}
	else if (indx != 0 && $("#gallery .nav_prev").is(":hidden"))
	{
		$("#gallery .nav_prev").show();
	}
}
function getPhotoId(a){
	var re = /photo_(\d+)/i;
	var matches = a.id.match(re);
	return matches[1];
}
function loadPhoto(id) {
	oImg = $('div.image img')[0];
	//oImg.style.width = '622px';
	//oImg.style.height = '356px';
	//oImg.src = '/img/main/tochka.gif';

	$.getJSON("/index.php?section=photo_ajax&photo_html="+id, null,
			replacePhoto);
}

function replacePhoto(data) {
	oPhoto = data[0];
	var oNewImg = new Image();
	oNewImg.onload = function() {
		var oImg = document.getElementById('main_image');
		oImg.style.width = '';
		oImg.style.height = '';
		oImg.src = "/img/gallery/photos/" + oPhoto.file_name;
		
		var oLink = document.getElementById('huge_image');
		oLink.href = "/img/gallery/photos/" + oPhoto.huge_file_name;
		
	}

	oNewImg.src = "/img/gallery/photos/" + oPhoto.file_name;
}
//setting vertical align
function set_vertical_align(elm)
{
	var elm_h = elm.height();
	elm.css({marginTop: -elm_h / 2, top: "50%"});
	return;
}
$(document).ready(function(){
	$(".iepng").ifixpng();
	// #images block .nav showup
	$("#images .navs").show();
	// setting params for #images block images change
	var period = 2000;
	var fade_out = 1500;
	var num_imgs = $("#images .list dt").length;
	// animate images in #images block
	$(window).load(function()
		{
			if ($("#images .navs dt").length > 1 && num_imgs > 1)
			{
				var i = 1;
				$(document).everyTime(period+fade_out, "images", function()
					{
						i = animate_i_imgs(i, num_imgs, period, fade_out);
					}
				);
			}
		}
	);
	// navigate #images
	$("#images .navs dt a").bind("click", function(e)
		{
			e.preventDefault();
			var indx = $("#images .navs dt").index($(this).parent("dt"));
			if ($("#images .list dt").eq(indx).length == 1)
			{
				i = animate_i_imgs(indx, num_imgs, period, fade_out);
				$(document).stopTime("images");
				$(document).everyTime(period+fade_out, "images", function()
					{
						i = animate_i_imgs(i, num_imgs, period, fade_out);
					}
				);
			}
		}
	);
	// gallery init
	
	var total_gal = $("#gallery .list .item").length;
	if (total_gal > 0)
	{
		$("#gallery .nav_next").show();
	}
	/*
	$("#gallery .item .thumb a").bind("click", function(e)
		{
			e.preventDefault();
			var parent = $(this).parents(".item");
			var indx = $("#gallery .item").index(parent);
			if (!parent.hasClass("a"))
			{
				switch_photo(indx, total_gal);
			}
		}
	);*/
	
	$("#gallery .nav_next a").bind("click", function(e)
		{
			e.preventDefault();
			var parent = $(this).parents(".item");
			var indx = $("#gallery .list .item").index($("#gallery .list .a"));
			if (indx + 1 <= total_gal)
			{
				switch_photo(indx + 1, total_gal);
			}
		}
	);
	$("#gallery .nav_prev a").bind("click", function(e)
		{
			e.preventDefault();
			var parent = $(this).parents(".item");
			var indx = $("#gallery .list .item").index($("#gallery .list .a"));
			if (indx - 1 >= 0)
			{
				switch_photo(indx - 1, total_gal);
			}
		}
	);
	
	// feedback submit change
	$("#feedback .submit input").hover( function() {
		var elm = $(this);
		var bg = elm.css("backgroundImage");
		var bg_new = bg.replace(/[\",\']?(.*?)\.(\w{2,4}[\",\']?)\)$/, '$1_a.$2)');
		var bg_ie = elm.css("filter");
		var bg_new_ie = bg_ie.replace(/(src='.*)\.(.*'\))/, '$1_a.$2');
		if (!$.support.cssFloat && $.browser.msie && $.browser.version == 6) {
			elm.css( {
				filter :bg_new_ie
			});
		} else {
			elm.css( {
				backgroundImage :bg_new
			});
		}
	}, function() {
		var elm = $(this);
		var bg = elm.css("backgroundImage");
		var bg_new = bg.replace(/[\",\']?(.*?)_a\.(\w{2,4}[\",\']?)\)$/, '$1.$2)');
		var bg_ie = elm.css("filter");
		var bg_new_ie = bg_ie.replace(/(src='.*?)_a\.(.*'\))/, '$1.$2');
		if (!$.support.cssFloat && $.browser.msie && $.browser.version == 6) {
			elm.css( {
				filter :bg_new_ie
			});
		} else {
			elm.css( {
				backgroundImage :bg_new
			});
		}
	});
	
	// setting vertically
	$("#gallery .list img").each(function()
		{
			set_vertical_align($(this));
		}
	);
});
