﻿// ----------------------------------------------------------------------------------------------
// userselection.js - iMedia.nl
// Copyright iMedia 2011
// ----------------------------------------------------------------------------------------------


function toggleBasket(){
	
	intCounter = parseInt($("#SELECTION_InSelection").text());
	
	if(intCounter > 0 )  {
		if ($('#menu-selection:visible').length == 0) {
			$("#menu-selection").show();
			}
		}
		
		else {
			if ($('#menu-selection:visible').length > 0) {
				$("#menu-selection").hide();
			}
		}
	}

function pictureSelect(strURL, strTag, intType) {
	
	// intType refers to: deselect = 0 / select = 1

	$.ajax(
	{
		method: "get",
		url: strURL + "&rnd=" + Math.random(),
		dataType: "xml",
		success: function(data)
		{
			var intInSelection = $(data).find('inselection').text();
			$("#SELECTION_InSelection").html(intInSelection);
			
			
			// SAME MISCTEXTS FROM TEMPLATES ARE USED TO RETURN TO LABEL
			var strLabel = $(data).find('label').text();

			if (intType == 1)
			{
				// revert select-link to deselect-link
				if (strURL.indexOf("=deselect") == -1) // trying to avoid dededeselect-effect (note: the = is needed because of the unfortunate name of ProcessAjaxSelect.pp
					strURL = strURL.replace("=select", "=deselect");
				
				$("#thumbselector" + strTag).html("<a class=\"selected\"  href=\"#\" onclick=\"pictureSelect('" + strURL + "', '" + strTag + "', 0); return false;\">" + strLabel + "</a>");
			}
			else
			{
				// revert deselect-link to select-link
				strURL = strURL.replace("=deselect", "=select");

				$("#thumbselector" + strTag).html("<a href=\"#\" onclick=\"pictureSelect('" + strURL + "', '" + strTag + "', 1); return false;\">" + strLabel + "</a>");

				// check visibility: c# ONLY renders this DIV-id in pageselection, preventing thumbs from disappearing elsewhere
				if ($("#thumb" + strTag).is(":visible"))
				{
				  $("#thumb" + strTag).hide("slow");
				}
			}
			
			toggleBasket();
			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			/*alert(errorThrown);*/
		}
	});
}
