
/**
  * @brief a simple class to wrap the reloadResults() function so that it can be used with the Misc::getNextPrev function
**/
function LinkWrapper(){}

LinkWrapper.prototype.getPage = function(page_num)
{
	reloadResults(page_num);
}

var currentPanel = -1;
var linkWrapper = new LinkWrapper();
var code = "";
var numResults = 0;
var tagId = -1;
var galleryPicHeight = 1;


function setConstants(tag_id, pic_height)
{
	tagId = tag_id;
	galleryPicHeight = pic_height;
}

function showLinkBox(id)
{
	if (id != currentPanel)
	{
		if (currentPanel != -1)
			hideLinkBox(currentPanel);

		currentPanel = id;
		$('#link_box' + id).SlideInDown(500);
	}
	else
	{
		currentPanel = -1;
		$('#link_box' + id).SlideOutDown(500);
	}
		
	return false;
}

function hideLinkBox(id)
{
	currentPanel = -1;
	$('#link_box' + id).SlideOutDown(500);
	return false;
}

function reportBrokenLink(galleryID, reportCode)
{
	jQuery.ajax({
		type: "GET",
		url: "ajax/add_link_report.php",
		data: "link_id=" + galleryID + "&report_code=" + reportCode + "&dummy=" + new Date().getTime(),
		success: function(){
			alert("The link will be reviewed by the site Administrator. Thank you for your collaboration.");
				
			hideLinkBox(currentPanel);
		}
	});
}

function reloadResults(page_num, test)
{
	if (page_num == null)
		page_num = 1;
		
	var gallerySearch = "ajax/gallery_search.php";
	
	if (test != null)
		gallerySearch = "ajax/gallery_search_test.php";
		
	currentPanel = -1;
		
	document.getElementById("other_galleries").innerHTML = "<img src = \"images/loadingAnimation.gif\" alt = \"\" />";
		
	var featureds = document.getElementById("featured_galleries");
	if (page_num == 1)
	{
		featureds.style.display = "block";
	}
	else
	{
		featureds.style.display = "none";
	}
	
	jQuery.ajax({
		type: "POST",
		url: gallerySearch,
		data: "gallery_type=" + document.getElementById("gallery_type").value + "&page_num=" +
			page_num + "&tag_id=" + tagId,
		success: function(response){
			var firstHash = response.indexOf('#');
			var secondHash = response.indexOf('#', firstHash + 1);
			var thirdHash = response.indexOf('#', secondHash + 1);
			
			numResults = response.substring(0, firstHash);
			var numPerPage = response.substring(firstHash + 1, secondHash);
			var numOnThisPage = parseInt(response.substring(secondHash + 1, thirdHash));
			
			if (page_num == 1)
			{
				if (numOnThisPage > 30)
					numOnThisPage = 30;
			}
			
			var newheight = Math.ceil(numOnThisPage / 5) * (galleryPicHeight + 4) + //size of image panels
				30;	//size of the nextPrev bar
				
			if (numOnThisPage == 0)
				newheight = 20;
				
			code = response.substr(thirdHash + 1);
			
			//document.getElementById("other_galleries").innerHTML = "";
			
			$('#other_galleries').animate(
				{
					height: newheight
				},
				'slow',
				0,
				function(){
					document.getElementById("other_galleries").innerHTML = code;
					
					if (numResults != -1)
						document.getElementById("gallery_title").innerHTML = "Galleries { " + numResults + " }";
					else
					{
						document.getElementById("gallery_title").innerHTML = "Galleries { " +
							document.getElementById("number_of_rows").innerHTML + " }";
					}
				}
			);
			
		}
	});
}

function sendClick(tag_id, gallery_id)
{
	jQuery.ajax({
		type: "GET",
		url: "ajax/on_click.php",
		data: "tag_id=" + tag_id + "&gallery_id=" + gallery_id,
		success: function(response){}
	});
}

