/*
 * @author: Florin Muresan
 * @email: florin@album2.com
 * @package: JS
 * @date: 04.11.2009
 * @description: main javascript functions
 * @additionalInfo:
 * @changeLog:{
*	[Attila]:[29.01.2010] - modify the way the paging happens when a page is choosen 
 *	[florin]:[04.11.2009] - basic implementation
 *	[florin]:[04.11.2009] - added paging event handling
 *	[adrian]:[31.01.2011] - added MainSearch, available for logged in users on every page
 *		functions PopulateUsersList, PopulateAlbumsList, searchWatchWriting, searchForUsersAndAlbums
 * }
 */

function PopulateUsersList(objectList)
{
	$('#resultUsers').html('');
	//add title
	$('#resultUsers').append('<li><b>Users</b></li>');

	if (objectList.length !== 0)
	{
		$.each(objectList,
			function(index, item)
			{
				var cloneObj = $('.model_li_user li').clone();
				cloneObj.find('.main_search_allcansee').attr('name',item.itemId);
				cloneObj.find('.main_search_profile').attr('href','/profile/'+item.itemId);
				cloneObj.find('.imageBox img').attr('src',item.itemIcon);
				cloneObj.find('.main_search_userName').html(item.itemName);
				cloneObj.appendTo(('#resultUsers'));
			});
		//event for click on "All can see" link
		$(".main_search_allcansee").live('click',function()
		{
			$.post(
				'/getAllCanSeeAccessId',
				{
					objectId: $(this).attr("name")
				},
				function (returnData)
				{
					if (returnData['result'] == 0)
					{
						alert(_("User does not have an 'All can see' page"));
					}
					else if (returnData['result'] == 1)
					{
						redirectTo("/page/"+returnData['ppag']);
					}
				},
				'json');
		})
	}
	else
	{
		$('#resultUsers').append('<li>No users</li>');
	}
}

function PopulateAlbumsList(albumsList)
{
	$('#resultAlbums').html('');
	//add title
	$('#resultAlbums').append('<li><b>Albums</b></li>');

	if (albumsList.length !== 0)
	{
		$.each(albumsList,
			function(index, item)
			{
				var cloneObj = $('.model_li_album li').clone();
				cloneObj.find('.main_search_albumname').attr('href',"/thumbsview/"+item.albumid);
				cloneObj.find('.main_search_albumname').html(item.albumname);
				cloneObj.find('.imageBox img').attr('src',item.URL);
				cloneObj.appendTo(('#resultAlbums'));
			});
	}
	else
	{
		$('#resultAlbums').append('<li>No albums</li>');
	}
}

// starts searching only when user stopped typing for xx milliseconds
	function searchWatchWriting()
	{
		$('#inputSearchUsersAndAlbums')
			.animate({opacity: 1.0},
			300,
			function()
			{
				var now = new Date().getTime();
				if (now - $(this).data("searchLatestKeypress") < 300)
				{
					searchWatchWriting();
					return false;
				}
				else
				{
					if ($(this).data('search_isWritten'))
					{
						$(this).data('search_isWritten',false);
						searchForUsersAndAlbums($('#inputSearchUsersAndAlbums').val());
					}
				}
			});
	}

	function searchForUsersAndAlbums(str)
	{
		$.post(
			'/search-users-and-albums',
			{searchString: str},
			function (returnData)
			{
				if (returnData['result'] == 1)
				{
					PopulateUsersList(returnData['objectList']);
					PopulateAlbumsList(returnData['albumsList']);
				}
				else;
					//TooMany();;
			},
			'json');
	}


$(document).ready( function(){
	$("#loginForm").submit( function(){
		var username = $("#user").val();
		var pass = $("#password").val();

		if(username == "" || pass == "")
		{
			alert(_("Username and password fields cannot be empty")+"!");
			if(username == "")
				$("#user").focus();
			else
				$("#password").focus();
			return false;
		}
	})
	$.each($("UL.main-menu").find("A"),
		function ()
		{
			$(this).wTooltip({
				content: $(this).attr("tooltip"),
				className: "toolTip-main-menu"
			});
		})

	// paging handling
	$('.paging-pages').change(function(){
		document.location = 'http://' + location.host + location.pathname + '?page=' + this.value;
	})

	// change language
	$('#selLang').change(function(){
		$('#frmLang').submit();
	})

//MAIN SEARCH FOR USERS AND ALBUMS
	
	$('#inputSearchUsersAndAlbums')
		.keyup(function()
		{
			if($(this).val()!='')
			{
				//search in db for users and albums
				$(this).data("search_isWritten", true);
				var keypress = new Date().getTime();
				$(this).data("searchLatestKeypress", keypress);
				searchWatchWriting();
	//			searchForUsersAndAlbums($(this).val());
				$('.dropdown:eq(0)', '#searchUsersAndAlbums').show();
			}
			else
			{
				$("#resultUsers").html('');
				$("#resultAlbums").html('');
			}
		})
		.click(function()	
		{
			$('.sharing-bullet').css({'z-index' : '0'});

			if ($(this).val()==_("Search for users and albums"))
			{
				$(this).css('color','black')
				$(this).val('');
			}
			else if ($(this).val()!='')
			{
				//on click display the search results
				if (($("#resultUsers").html() == "") && ($("#resultAlbums").html() == ""))
				{
					//user refreshed the page, so he has the search string, but no results
					$(this).keyup();
				}
				else
				{
					//show hidden results
					if($('#searchUsersAndAlbums .dropdown').is(':animated'))
					{
						$('#searchUsersAndAlbums .dropdown').stop(true, false);
					}
					$('#searchUsersAndAlbums .dropdown').show();
				}
			}
		})
		.blur(function()
		{
			if ($(this).val()=='')
			{
				$(this).css('color','gray')
				$(this).val(_("Search for users and albums"));
			}
		})
		.bind('mouseenter',function()
		{
			$('.sharing-bullet').css({'z-index' : '0'});

			//display the search results
			if (($("#resultUsers").html() != "") || ($("#resultAlbums").html() != ""))
			{
				//show hidden results
				if($('#searchUsersAndAlbums .dropdown').is(':animated'))
				{
						$('#searchUsersAndAlbums .dropdown').stop(true, false).fadeTo("fast", 1.0);
				}
				else $('#searchUsersAndAlbums .dropdown').fadeTo("fast", 1.0);
			}
			else if ($(this).val()!=_("Search for users and albums") && $(this).val()!="")
			{
				//user refreshed the page, so he has the search string, but no results
				$(this).keyup();
			}
		})
		.bind('mouseleave',function(){
			$('.sharing-bullet').css({'z-index' : '2'});
			$('.dropdown:eq(0)', '#searchUsersAndAlbums').fadeOut(4000);
		})
		
	//prevent form from submitting
	$('#searchUsersAndAlbums')
		.submit(function(){
			return false;
		});
	//animation on results
	$('#searchUsersAndAlbums .dropdown')
		.bind('mouseenter',function(){
			$('.sharing-bullet').css({'z-index' : '0'});
			if($(this).is(':animated'))
			{
				$(this).stop(true, false).fadeTo("fast", 1.0);
			}
		})
		.bind('mouseleave',function(){
			$('.sharing-bullet').css({'z-index' : '2'});
			$(this).fadeOut(4000);
		})
		
//END - MAIN SEARCH

	//star with the unread number of news
	$('.news-alert')
		.wTooltip({
			content: _('Number of unread news'),
			className: "toolTip-main-menu"
		})
		.click(function(){
			redirectTo('/news');
		})
		.css('margin-left',$('.newsTab .item-right a').width()+5)
})

