/**
 * @author Developing
 */

$(document).ready(function()
{
	$("input.fadedText").click( function() 
	{ 
		if ( $(this).val() == "поиск в каталоге...") 
		{
			$(this).val("");
			$(this).removeClass("fadedText");
			$(this).addClass("normalText");
		}
	} );
	
	var orderButton = $("a[href$='/#']");
	orderButton.attr("href", "javascript:void(0)");
	
	var orderWindow = $("#orderWindow");
	var orderTextarea = $("textarea", orderWindow);	
	$("#minimize").hover( function()
	{
		$(this).addClass("onOver");
	}, function()
	{
		$(this).removeClass("onOver");
	});
	$("#backgroundWindow", orderWindow).css("opacity", "0.8");
	
	$("#minimize").click( hideOrder );
	orderButton.click( displayOrder );

	
})

function changeCurrency(id)
{
	$("#changeCurrency").val(id);
	$("#currencyForm").submit();
}


function displayOrder()
{
	var orderWindow = $("#orderWindow");
	var orderTextarea = $("textarea", orderWindow);
	orderWindow.css("top", getTopScreenPos()+5+"px");
	orderWindow.show();

	$(".resultsTable tbody tr").each( function()
	{
		$(this).click( function()
		{
			rowClick($(this), orderTextarea);
		});
		$(this).hover( function()
		{
			$(this).addClass("onOver");
			
		}, function()
		{
			$(this).removeClass("onOver");
		} );
	} );
	
	orderTextarea.focus(function()
	{
		$("#backgroundWindow", orderWindow).css("opacity", "1.0");
	} );
	
	
	orderTextarea.blur(function()
	{
		$("#backgroundWindow", orderWindow).css("opacity", "0.8");
	} );
	
	$(window).scroll( function()
	{
		orderWindow.css("top", getTopScreenPos()+5+"px");
	});
}

function hideOrder()
{
	var orderWindow = $("#orderWindow");
	orderWindow.hide();
	var rows = $(".resultsTable tbody tr");
	rows.unbind();
	rows.removeClass("onOver");
}

function getTopScreenPos() 
{
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function rowClick(row, textContainer)
{
	var cells = $("td", row);
	var text1 = $(cells[2]).text() + " " + $(cells[4]).text() + " " + $(cells[6]).text() + " " + $(cells[8]).text() + " " + parseFloat($(cells[10]).text()).toFixed(2) + $("#priceSufix").text() + " " + $(cells[12]).text() + " \r";
	var text = textContainer.val() + text1;
	textContainer.val( text );
}
