var prevSearchQuery = '';

$(document).ready(function () 
{
	initSearchSuggestionsListing ();
});

$(window).scroll(function()
{
  $('#messageBox').animate({top:$(window).scrollTop()+10+"px" },{queue: false, duration: 350});
});

function showMessage (text, type)
{
	$("#messageBox").remove();
	var top = $(window).scrollTop()+"px"
	$("body").append('<div id="messageBox" style="top: '+($(window).scrollTop()+10)+'px" class="' + type + '">' + text + '</div>');
	
	setTimeout(function() 
	{ 
		$('#messageBox').animate({opacity: 0}, 1000, null, function() 
		{ 
			$("#messageBox").remove();
		}); 
	}, 5000);
}

function showScreenShot (id)
{
	$("#appScreenShots").find("img").hide();
	$("#appScreenShots #ScreenShot"+id).show();
}

function liquidDecBox (descBoxID, btn)
{
	if ($("#"+descBoxID).css('height') != 'auto') 
	{
		$("#"+descBoxID).css('height', 'auto');
		$(btn).text("- View less");
	}
	else
	{
		$("#"+descBoxID).css('height', '45px');
		$(btn).text("+ View more");
	}
	return false;
}

/* COMMENTS */

function showComments (itemID, type, page)
{
	var loaderHTML = '<div align="center"><img src="/images/loader.gif" alt="Loading..." /></div>';
	
	if (page == 0) $("#userComments").append(loaderHTML);
	else $("#commentsLoader").html(loaderHTML);
	
	//$("#commentsLoader").html(loaderHTML);
	
	$.post("/ajax", { act: "showComments", itemID: itemID, type: type, page: page },  
		function (res) 
		{
			$("#userComments").html(res);
		}
	);
	return false;
}

function submitComment (itemID, type)
{
	var params = 'act=submitComment&'+$('[name=form]').serialize();
	
	$.ajax({ type: "POST", url: "/ajax", data: params, success: 
		function(res)
		{
			if (res == 1) 
			{
				showMessage ("Спасибо за комментарий!", "ok");
				showComments(itemID, type);
			}
			else showMessage (res, "err");
		}
	});
	
	return false;
}

var commentCurrentRating = 0;

function ratingAnimation (num) 
{
	 $("#ratingStars a").find("img").attr("src", "/images/star_empty.gif");
	 if (num == 0 && commentCurrentRating != 0) num = commentCurrentRating;
	 $("#ratingStars a:lt("+num+")").find("img").attr("src", "/images/star_full.gif");
}

function changeRating(int) 
{
     $('#ratingBlockInactive div:lt(' + int + ')').attr('class', 'full');
     $('#ratingBlockInactive div:gt(' + (int - 1) + ')').attr('class', '');
     $('#numVoicesInactive').text("("+(parseInt($('#numVoices').html().substr(1))+1)+")");
     $('#ratingBlock').remove();
     $('#ratingBlockInactive').toggle();
}
function setRating(tbl, elementId, mark) 
{
     //alert(tbl+"   "+elementId+"   "+mark);
     $.ajax({
          type:           "POST", 
          url:           "/ajax/", 
          data:           "act=setRating&tbl="+tbl+"&id="+elementId+"&mark="+mark, 
          success:      function(res) {
                              var rat = parseInt(res);
                              if(rat) {
                                   //alert(rat);
                                   changeRating(rat);
                              } else {
                                   $("#mesArea").attr('class', 'mesArea err');
                                   $("#mesText").html(res);
                                   scrollTo(0,0);
                              }
                         }
     });
     return false;
}



/* AUTO SUGGESTIONS */

function getSearchSuggestions (q)
{
	if (!q || q == " ") 
	{
		$("#searchAutoSuggestBox").hide();
		return false;
	}
	
	if (q == prevSearchQuery) return false;
	prevSearchQuery = q;

	$.post("/ajax", { act: "showSearchSuggestions", q: q },  
		function (res) 
		{
			if (res) showSearchSuggestionsBox(res);
			else hideSearchSuggestionsBox();
		}
	);
	return false;
}

function showSearchSuggestionsBox (res)
{
	$("#searchAutoSuggestBox").html(res);
	
	$("#searchAutoSuggestBox div a").mouseover(function () 
	{
		$("#searchAutoSuggestBox div a").removeClass("selected");
		$(this).addClass("selected");
	});
	
	$("#searchAutoSuggestBox div a").click(function () 
	{
		window.location = $(this).attr("href");
	});
	
	
	$("#searchAutoSuggestBox").show(100);

	return false;
}

function hideSearchSuggestionsBox ()
{
	setTimeout(function()
	{
		$("#searchAutoSuggestBox").hide(100);					
	},100);
	return false;
}

function initSearchSuggestionsListing ()
{
	$("#searchInput").keyup(function (e) 
	{
		var selectedIndex = -1;
		var suggestionsCount = $("#searchAutoSuggestBox div a").length;
		
		jQuery.each($("#searchAutoSuggestBox div a"), function(i, val) 
		{
			if ($(this).hasClass("selected")) selectedIndex = i;
		});
		
		$("#searchAutoSuggestBox div a").removeClass("selected");
		
		//Down
		 if (e.which == 40)
		 {
			if (suggestionsCount > selectedIndex + 1) selectedIndex++;
			$("#searchAutoSuggestBox div a").eq(selectedIndex).addClass("selected");
			$("#searchInput").val($("#searchAutoSuggestBox div a").eq(selectedIndex).attr("title"));
			return;
		 }
		 
		 //Up
		 if (e.which == 38)
		 {
			if (selectedIndex > 0) selectedIndex--;
			$("#searchAutoSuggestBox div a").eq(selectedIndex).addClass("selected");
			$("#searchInput").val($("#searchAutoSuggestBox div a").eq(selectedIndex).attr("title"));
			return;
		 }

		getSearchSuggestions($("#searchInput").val());
	});
}

