// &#238;&#242;&#234;&#240;&#251;&#226;&#224;&#229;&#242; &#236;&#229;&#237;&#254; &#238;&#225;&#250;&#229;&#234;&#242;&#224; &#226; &#242;&#224;&#225;&#235;&#232;&#246;&#229;
function show_table_menu(elem)
{
	var el = elem.parentNode;
	$(".search-result").find("ul.menu").hide();
	$(el).find("ul").show();
	
};

//&#239;&#240;&#255;&#247;&#229;&#242; &#244;&#238;&#240;&#236;&#243; set-form
function price_edit_hide_link_click(elem)
{
	$(".set-form").hide();
	//$(elem).hide();
	$(document).unbind("click");
}

// &#212;&#243;&#237;&#234;&#246;&#232;&#255; &#228;&#235;&#255; &#239;&#238;&#232;&#241;&#234;&#224; &#226;&#226;&#229;&#228;&#229;&#237;&#238;&#227;&#238; &#241;&#235;&#238;&#226;&#224; &#226; &#242;&#224;&#225;&#235;&#232;&#246;&#229; &#239;&#238;&#232;&#241;&#234;&#224;
function FindWord()
{
	//&#197;&#241;&#235;&#232; &#229;&#241;&#242;&#252; &#242;&#224;&#225;&#235;&#232;&#246;&#224; &#239;&#238;&#232;&#241;&#234;&#224; &#242;&#238; &#232;&#249;&#229;&#236;
	if ($("table.search-result").length)
	{
		var search_value =  $("table.search-result").find("input[name='search_value']").val();
		var search_param = $("table.search-result").find("input[name='search_param']").val();

		var re =  RegExp("("+search_value+")", "ig");

		if (search_value != "")
		{	
			switch (search_param)
			{
				case "1":
					$("table.search-result  ul.object-input").find("li.object-title  a").each(function()
					{
						str = $(this).html();
						str = str.replace(re, "<strong>$1</strong>");
						$(this).html(str);
					});
				break;
				case "2":
					$("table.search-result  ul.object-input").find("li.object-mark  a").each(function()
					{
						str = $(this).html();
						str = str.replace(re, "<strong>$1</strong>");
						$(this).html(str);
					});
				break;
				case "3":
					$("table.search-result  ul.object-input").find("li.object-model  a").each(function()
					{
						str = $(this).html();
						str = str.replace(re, "<strong>$1</strong>");
						$(this).html(str);
					});
				break;
				case "5":
					$("table.search-result  ul.object-input").find("li.object-cod a").each(function()
					{
						str = $(this).html();
						str = str.replace(re, "<strong>$1</strong>");
						$(this).html(str);
					});
				break;
				case "7":
					$("table.search-result  ul.object-input").find("li.collection a").each(function()
					{
						str = $(this).html();
						str = str.replace(re, "<strong>$1</strong>");
						$(this).html(str);
					});
				break;
				case "6":
					$("table.search-result  ul.object-input").find("li a").each(function()
					{
						str = $(this).html();
						str = str.replace(re, "<strong>$1</strong>");
						$(this).html(str);
					});
				break;
			}
		}
	}
}

// &#238;&#242;&#234;&#240;&#251;&#226;&#224;&#229;&#242; &#244;&#238;&#240;&#236;&#243; &#228;&#235;&#255; &#240;&#229;&#228;&#224;&#234;&#242;&#232;&#240;&#238;&#226;&#224;&#237;&#232;&#255; &#246;&#229;&#237;&#251; &#226; Table
function set_price_for_table_link_click(elem)
{	
	var top = $(elem).offset().top;
	var left = $(elem).offset().left;
	var index = $(elem).attr("value");
	var tr = elem.parentNode.parentNode.parentNode.parentNode;
	active_reload_element = $(tr).find("a.reload");
	
	var id;
	for (id in ajax_data[13]['reload']);

	$("div[@id*='" + id + "']").removeAttr("id");
	$(elem).parent().parent().attr("id", "ajax_label_" + id + "_0");
	//$(elem).parent().parent().attr("id", id + "_0");
	$(".set-form:visible").hide();

	if (typeof price[index] != 'undefined')
	{	
		var text = price[index].text;

		text = text.replace(/<br>/gi, "\n");
		text = text.replace(/&quot;/gi, "\"");
		
		$("#price").find("input[@name='object_price_price']").val(price[index].price);
		$("#price").find("textarea").val(text);
		$("#price").find("option[@value='"+price[index].currency+"']").attr("selected","selected");
		$("#price").find("input[@name='object_price_id']").val(index);
		$("#price").find("input[@name='object']").val(price[index].object);
		$("#price").find("input[@name='mode']").val("update_price");
		$("#price").find("input[@name='object_price_date_finish']").val(price[index].date_finish.substr(0,10));

	}
	else
	{
		var date = new Date();
		date.setMonth(date.getMonth() + 2);
	
		var day = date.getDate();
		var month = date.getMonth();
		var d = new Date(date.getFullYear()+"/"+(date.getMonth()+1)+"/"+0);
		var lastDay = d.getDate();
	
		if (day > lastDay)
			day = lastDay;
		if (day <= 0)
			day = 1;
		if (day < 10)
			day = "0" + day;
		if (month <= 0)
			month = 1;
		if (month < 10)
			month = "0" + month;
		
		$("#price").find("input[@name='mode']").val("add_price");
		$("#price").find("input[@name='object_price_price']").val("");
		$("#price").find("textarea").val("");
		$("#price").find("option[@value='1']").attr("selected","selected");
		$("#price").find("input[@name='object']").val($(elem).attr("alt"));
		var str_date = date.getFullYear() + "-" + month + "-" + day;
		$("#price").find("input[@name='object_price_date_finish']").val(str_date);
		
	}

	$("#price").css("top",top).css("left",left).show();
	bind_hover_for_set_form();	
}


// &#238;&#242;&#234;&#240;&#251;&#226;&#224;&#229;&#242; &#244;&#238;&#240;&#236;&#243; &#228;&#235;&#255; &#240;&#229;&#228;&#224;&#234;&#242;&#232;&#240;&#238;&#226;&#224;&#237;&#232;&#255; &#231;&#224;&#239;&#240;&#241;&#224; &#226; Table
function set_request_for_table_link_click(elem)
{	
	var top = $(elem).offset().top;
	var left = $(elem).offset().left;
	var index = $(elem).attr("value");

	var id;
	for (id in ajax_data[33]['reload']);
	
	$("div[@id*='" + id + "']").removeAttr("id");
	$(elem).parent().parent().attr("id", "ajax_label_" + id + "_0");
	$(".set-form:visible").hide();
	
	if (typeof request[index] != 'undefined')
	{	
		var text = request[index].text;
		text = text.replace(/<br>/gi, "\n");
		text = text.replace(/&quot;/gi, "\"");

		$("#request").find("input[@name='object_request_value']").val(request[index].value);
		$("#request").find("textarea").val(text);
		$("#request > ul > li > span").text($(elem).parent().find("input.quantity:hidden").val());
		$("#request").find("input[@name='object_request']").val(index);
		$("#request").find("input[@name='object']").val(request[index].object);
		$("#request").find("input[@name='mode']").val("change_object_request");
		$("#request").find("input[@name='object_request_date_finish']").val(request[index].date_finish.substr(0,10));

	}
	else
	{
		var date = new Date();
		date.setMonth(date.getMonth() + 2);
	
		var day = date.getDate();
		var month = date.getMonth();
		var d = new Date(date.getFullYear()+"/"+(date.getMonth()+1)+"/"+0);
		var lastDay = d.getDate();
	
		if (day > lastDay)
			day = lastDay;
		if (day <= 0)
			day = 1;
		if (day < 10)
			day = "0" + day;
		if (month <= 0)
			month = 1;
		if (month < 10)
			month = "0" + month;

		$("#request").find("input[@name='mode']").val("add_object_request");
		$("#request").find("input[@name='object_request_value']").val("");
		$("#request").find("textarea").val("");
		$("#request").find("input[@name='object']").val($(elem).attr("alt"));
		$("#request > ul > li > span").text($(elem).parent().find("input:hidden").val());
		var str_date = date.getFullYear() + "-" + month + "-" + day;
		$("#request").find("input[@name='object_request_date_finish']").val(str_date);	
	}

	$("#request").css("top",top).css("left",left).show();
	bind_hover_for_set_form();	
}

// &#238;&#242;&#234;&#240;&#251;&#226;&#224;&#229;&#242; &#244;&#238;&#240;&#236;&#243; &#228;&#235;&#255; &#243;&#228;&#224;&#235;&#229;&#237;&#232;&#255; &#246;&#229;&#237;&#251; &#226; Table
function delete_price_for_table_link_click(elem)
{
	var top = $(elem).offset().top;
	var left = $(elem).offset().left;
	var obj = $(elem).parent().find("a.edit").attr("alt");
	var tr = elem.parentNode.parentNode.parentNode.parentNode;
	active_reload_element = $(tr).find("a.reload");
	
	var id;
	
	for (id in ajax_data[56]['reload']);

	$("div[@id*='" + id + "']").removeAttr("id");
	$(elem).parent().parent().attr("id", "ajax_label_" + id + "_0");
	//$(elem).parent().parent().attr("id", id + "_0");
	$(".set-form:visible").hide();
	
	$("#delete_price").find("input[@name='object_price_id']").val($(elem).attr("value"));
	$("#delete_price").find("input[@name='ajax_object']").val(obj);
	
	$("#delete_price").css("top",top).css("left",left).show();
	bind_hover_for_set_form();
}

// &#238;&#242;&#234;&#240;&#251;&#226;&#224;&#229;&#242; &#244;&#238;&#240;&#236;&#243; &#228;&#235;&#255; &#243;&#228;&#224;&#235;&#229;&#237;&#232;&#255; &#231;&#224;&#239;&#240;&#238;&#241;&#224; &#226; Table
function delete_request_for_table_link_click(elem)
{
	var top = $(elem).offset().top;
	var left = $(elem).offset().left;
	var obj = $(elem).parent().find("a.edit").attr("alt");
	var id;
	for (id in ajax_data[57]['reload']);

	$("div[@id*='" + id + "']").removeAttr("id");
	$(elem).parent().parent().attr("id", "ajax_label_" + id + "_0");
	//$(elem).parent().parent().attr("id", id + "_0");
	$(".set-form:visible").hide();
	
	$("#delete_request").find("input[@name='object_request']").val($(elem).attr("value"));
	$("#delete_request").find("input[@name='ajax_object']").val(obj);
	
	$("#delete_request").css("top",top).css("left",left).show();
	bind_hover_for_set_form();
}

function show_message(elem)
{

	var ind = $(elem).parent().find("input.value:hidden").val();
	var name = $(elem).parent().find("input.value:hidden").attr('name');
	var top = $(elem).offset().top;
	var left = $(elem).offset().left;

	if (name == 'object_price_id')
		var text = price[ind].text;
	if (name == 'object_request')
		var text = request[ind].text;

	if (text != "")
	{
		$("div.advert > span").html( text );
		$("div.advert").css("top", top + 20).css("left", left).show();
	}
}

function hide_message(elem)
{
	$("div.advert").hide();
}


function show_pictures(elem)
{
	var src_image = $(elem).attr("value");
	var label = $(elem).attr("name");
	if (src_image)
	{
		if (label)
		{
			var index = label.substr(label.lastIndexOf("_") + 1, label.length);
			var last_label = $("a[name*='image_file_']:last").attr("name");
			var last_index = last_label.substr(last_label.lastIndexOf("_") + 1, last_label.length);
		};
		
		var top = $(document).scrollTop();
		var image = new Image();
		
		var marg_top = 100;
		var marg_left = Math.round(screen.width / 2) - 16;
		
		if (label)
		{
			$("#image-big.overbody > div.menu > a.next").attr("value", index*1 + 1).show();
			$("#image-big.overbody > div.menu > a.prev").attr("value", index*1 - 1).show();
		}
		
		if (label)
		{
			if (index == '0')
				$("#image-big.overbody > div.menu > a.prev").removeAttr("value").hide();
			if (index == last_index) 
				$("#image-big.overbody > div.menu > a.next").removeAttr("value").hide();
		}
		else
		{
			$("#image-big.overbody > div.menu > a.prev").removeAttr("value").hide();
			$("#image-big.overbody > div.menu > a.next").removeAttr("value").hide();
		}
		
		$("#image-big.overbody > img").removeAttr("style");
		$("#image-big.overbody > img").attr("src", "loading.gif").css("margin-left", marg_left).css("margin-top", marg_top).attr('width', '32').attr('height', '32');
		$("#image-big.overbody > img").show();
		$("#image-big.overbody").css("top", top);
		$("#image-big.overbody").animate({ opacity: "show" }, "fast");
		
		image.onload =  (function()
		{
			var width =  image.width;
			var height = image.height;
			
			if (width > $(window).width())
			{
				height = Math.round( ($(window).width()*1 - 100) * (height / width));
				width = Math.round($(window).width()*1 - 100);
			}

			if  (height > $(window).height()*1 - 150)
			{
				width = Math.round( ($(window).height()*1 - 150) * (width / height));
				height = Math.round($(window).height()*1 - 150);
			}
			
			marg_top = Math.round($(window).height() / 2) - Math.round(height / 2);	
			marg_left = Math.round($(window).width() / 2) - Math.round(width / 2);
			
			$("#image-big.overbody > img").removeAttr("width");
			$("#image-big.overbody > img").removeAttr("height");
			$("#image-big.overbody > img").css("margin-left", marg_left).css("margin-top", marg_top).animate({ width: width }, "fast").animate({ height: height }, "fast");
			$("#image-big.overbody > img").attr("src", image.src);
		});
		
		image.src = src_image;
				

		$("#image-big.overbody > a.overbody-close").unbind("click");
		$("#image-big.overbody > a.overbody-close").click(function()
		{
			$("#image-big.overbody").hide();
			$("#image-big.overbody > img").attr("src", "loading.gif").attr('width', '32').attr('height', '32');
		});
		
		$(window).unbind("scroll");
		$(window).scroll(function()
		{
			$("#image-big.overbody > a.overbody-close").click();
		});
	}
	return false;
}


function show_next_pictures(elem)
{
	var index = $(elem).attr("value");
	var el = $("a[name*='image_file_"+ index + "']");
	$("#image-big.overbody > img").hide();
	$("#image-big.overbody > img").removeAttr("style");
	$("#image-big.overbody > img").attr("src", "loading.gif").attr('width', '32').attr('height', '32');
	show_pictures(el);
}

function show_prev_pictures(elem)
{
	var index = $(elem).attr("value");
	var el = $("a[name*='image_file_"+ index + "']");
	$("#image-big.overbody > img").hide();
	$("#image-big.overbody > img").removeAttr("style");
	$("#image-big.overbody > img").attr("src", "loading.gif").attr('width', '32').attr('height', '32');
	show_pictures(el);
}


$(document).ready(function(){

	$("a.selected").removeClass();
	$("ul.submenu li a.selected").removeClass();
	$("a#"+$("#mode").val()+"").toggleClass("selected");

	$("ul.alfabetic > li:first > a").addClass('selected');

	update_table_class();	
	
	$(".alfabetic > li > a").click(function()
	{
			$(".alfabetic > li > a.selected").removeClass();
			$(this).addClass('selected');
	});
	
	$(".search-result").find("ul.menu").hover(
		function(){},
		function()
		{
			$(document).click(function(event)
			{	
				var id = $(event.target).attr("id");
				if (!id) 
				{
					$(event.target).attr("id","clicked");
					id = "clicked";
				};
				if ($("#"+ id).attr("class") != "menu" && $("#"+ id).attr("class") != "favorite-add" && $("#"+ id).attr("class") != "favorite-del")
				{
	 				$(".search-result").find("ul.menu").hide();
	 				$(document).unbind("click");
				};
				if (id == "clicked")
					$(event.target).removeAttr("id");;
			});
		}
	);

	FindWord();
});
