/* ********************************************************
	Pictures System geklaut bei ricardo.ch
* *******************************************************/

function getObj(targetDocument, name)
{
  // retrieve an object with its ID
  if (targetDocument.getElementById)
  	return targetDocument.getElementById(name);
  else if (targetDocument.all)
	return targetDocument.all[name];
  else if (targetDocument.layers)
   	return targetDocument.layers[name];
}

function getSelectedValue(listbox)
{
	// return value of a selected line in a listbox
	if (listbox.options.selectedIndex != -1)
		return listbox.options[listbox.options.selectedIndex].value;
	
	return '';
}

function getSelectedName(listbox)
{
	// return value of a selected line in a listbox
	if (listbox.options.selectedIndex != -1)
		return listbox.options[listbox.options.selectedIndex].text;
	
	return '';
}

function isDivVisible(targetDocument, divname)
{
	// true if visible, false if not.
	if (targetDocument.getElementById)
		return (targetDocument.getElementById(divname).style.display != "none")
	else if (targetDocument.all)
		return (targetDocument.all[name].style.display != "none")
	else if (targetDocument.layers)
		return (targetDocument.layers[divname].display != "none");
}


function setDivStatus(targetDocument, divname, isVisible)
{
	// will be shown if isVisible is true
	if (targetDocument.getElementById)
		targetDocument.getElementById(divname).style.display  = (isVisible) ? "block" : "none";
	else if (targetDocument.all)
		targetDocument.all[name].style.display  = (isVisible) ? "block" : "none";
	else if (targetDocument.layers)
		targetDocument.layers[divname].display  = (isVisible) ? "block" : "none";
}

function uncheckOthers(checkboxUsed, namePattern)
{

	var chkIndex = 0;
	var objCheck = getObj(document, namePattern + chkIndex);
	
	while (objCheck)
	{
		objCheck.checked = false;
		objCheck = getObj(document, namePattern + ++chkIndex);
	}

	if (checkboxUsed)
		checkboxUsed.checked = true;
}

function showModalPopup(url, name, width, height, isCentered)
{		
	var left = (screen.availWidth - width)/2;
	var top = (screen.availHeight - height)/2;
	var ID = name;
	
	if (!isCentered)
	{
		left=10;
		top=10
	}
	
	if (window.showModalDialog)
	{
		var dialogArguments = new Object();
		var _R = window.showModalDialog(url, dialogArguments, "dialogWidth="+width+"px;dialogHeight="+height+"px;scroll=no;status=no;");
		if ("undefined" != typeof(_R))
		{
			SetName(_R.strName);	
		}
	}		
	else	//NS			
	{  	
 		winHandle = window.open(url, ID, "modal,toolbar=false,location=false,directories=false,status=false,menubar=false,scrollbars=no,resizable=no,left="+left+",top="+top+",width="+width+",height="+height);
		winHandle.focus();
	}
	return false;
}

function hideThumbnailAfterdeletion(thTargetDocument, pictureIndex)
{
	// hidding picture insertion div
	setDivStatus(thTargetDocument, "DivpicInput", false);
	
	//setting up new pic link to img object
	getObj(thTargetDocument, "picThumb" + pictureIndex).src = blankPicPath;

	// hiding picture display div
	setDivStatus(thTargetDocument, "DivPicShow" + pictureIndex, false);
	setDivStatus(thTargetDocument, "DivPicAdd" + pictureIndex, true);

	var lastLink = getObj(thTargetDocument, "LinkPicdel" + (pictureIndex-1));
	var currentLink = getObj(thTargetDocument, "LinkPicdel" + pictureIndex);
	var nextLink = getObj(thTargetDocument, "LinkPicadd" + (pictureIndex+1));
	
	if (lastLink)
		lastLink.className = "n_PictureToChoose";

	if (currentLink)
		currentLink.className = "n_PictureToChooseOff";

	if (nextLink)
		nextLink.className = "n_PictureToChooseOff";
		
	// we update the picture count
	getObj(thTargetDocument, "picturescount").value = pictureIndex-1;
	SetCookie ("AddObjPicturesCount", pictureIndex-1);
}

function restorePicturesInterface(NbPicturesMax)
{
	var iPicturesCount = GetCookie ("AddObjPicturesCount");
	var iUniqueID = GetCookie ("AddObjUniqueID");

	// on clean
	for (i=NbPicturesMax; i > 0; i--)
	{
		hideThumbnailAfterdeletion(document, i);
	}

	if (iUniqueID && iPicturesCount && iPicturesCount > 0 && iPicturesCount <= NbPicturesMax)
		for (i=1; i <= iPicturesCount; i++)
			showThumbnailAfterInsertion(window.parent.document, i, "bild/tmp/temporary_" + iUniqueID + "_" + i +"_p.jpg");
}

function restorePageAppearance()
{
	var iUniqueID = GetCookie ("AddObjUniqueID");	
	var UniID = getObj(window.parent.document, "UID").value;

	if (iUniqueID != UniID) {
		SetCookie ("AddObjUniqueID", UniID);
		SetCookie ("AddObjPicturesCount", 0);
	}

	var Piccount = GetCookie ("AddObjPicturesCount");	
	var Pics = getObj(window.parent.document, "picturescount").value;

	if(Piccount == 0) {
		SetCookie ("AddObjPicturesCount", Pics);
	}

	// pictures
	restorePicturesInterface(10);

}


function openPictureInputBox(targetdocument, pictureIndex, action, randomNumber)
{
	var clickedLink = getObj(targetdocument, "LinkPic" + action + pictureIndex);
	var newPicInput = getObj(document, "hasnewpictures");

	if (newPicInput)
		newPicInput.value = "1";

	// is link active ?
	if (clickedLink.className != "n_PictureToChooseOff")
	{
		var isVisible = isDivVisible(targetdocument, 'DivpicInput');

		var objPicIframe = getObj(targetdocument, 'picturesiframe');
		var newLocation = "/addimageFrame.php?index=" + pictureIndex + "&actiontype=" + action + "&rnd=" + randomNumber;
		var oldLen = objPicIframe.src.length;
		var newLen = newLocation.length;
		var oldLocation = objPicIframe.src.substring(oldLen-newLen, oldLen);
		var isSameLocation = (oldLocation == newLocation);

		//if (!isSameLocation)
		objPicIframe.src = newLocation;

		// we show the div
		setDivStatus(targetdocument, 'DivpicInput', !(isSameLocation && isVisible));
	}
	else
		if (action == "add")
			alert(lng_error_bad_picadd);
		else
			alert(lng_error_bad_picrem);

	// remove selection
	clickedLink.blur();
}

function showThumbnailAfterInsertion(thTargetDocument, pictureIndex, picturePath)
{
	// hidding picture insertion div
	setDivStatus(thTargetDocument, "DivpicInput", false);
	
	//setting up new pic link to img object
	var picture = getObj(thTargetDocument, "picThumb" + pictureIndex);
	var tmpSrc = picturePath + "?" + Math.random();
	var setcookie = true;
	
	picture.src = tmpSrc;

	
	// showing picture display div
	setDivStatus(thTargetDocument, "DivPicAdd" + pictureIndex, false);
	setDivStatus(thTargetDocument, "DivPicShow" + pictureIndex, true);

	var lastLink = getObj(thTargetDocument, "LinkPicdel" + (pictureIndex-1));
	var currentLink = getObj(thTargetDocument, "LinkPicdel" + pictureIndex);
	var nextLink = getObj(thTargetDocument, "LinkPicadd" + (pictureIndex+1));
	
	if (lastLink)
		lastLink.className = "n_PictureToChooseOff";

	if (currentLink)
	{
		if (nextLink && nextLink.className == "n_PictureToChoose")
		{
			setcookie = false;
			currentLink.className = "n_PictureToChooseOff"; // no remove if other pic over it
		}
		else
			currentLink.className = "n_PictureToChoose";
	}

	if (nextLink)
		nextLink.className = "n_PictureToChoose";
	
	if (setcookie)
	{
		// we update the picture count
		getObj(thTargetDocument, "picturescount").value = pictureIndex;
		SetCookie ("AddObjPicturesCount", pictureIndex);
	}
}


