/**
 * Displaying of the basket layer after a product "lands" in the basket
 *
 * @param string sImageBase -> base path for the images
 * @param string sBasketLink
 * @param string sTitle -> article title
 * @param int iQuantity
 * @param string sVariant
 * @param string sImage
 * @param float fPrice
 * @param strong sMessageText
 * @return bool
 */
function basketlayer(sImageBase,sBasketLink,sTitle,iQuantity,sVariant,sImage,fPrice,sMessageText)
{
	Element.insert(
		$('wrapper'),
		{
			after: '<div class="message warenkorb-layer"><a href="#" onclick="removeLayers(\'message\', \'layer\');return false;"><img src="' + sImageBase +
					'message_close.gif" width="13" height="13" border="0" alt="close" /></a>' +
					'<p>'+ sMessageText +'</p><br class="clear" />' +
					'<a href="#" onclick="removeLayers(\'message\', \'layer\');return false;"><img src="'+ sImage +'" class="produkt" width="65" height="85" border="0" alt="sImage" /></a>' +
					'<div>' +
					'<p><strong>'+ sTitle +'<br />'+ sVariant +'</strong></p>' +
					'<p>Anzahl: '+ iQuantity +'</p>' +
					'<h2>'+ fPrice +' &euro;</h2>' +
					'</div><a href="'+ sBasketLink +'"><img src="'+ sImageBase +'btn_warenkorb-layer.gif" class="warenkorb" width="161" height="30" border="0" alt="btn_warenkorb-layer.gif" /></a><a href="#" onclick="removeLayers(\'message\', \'layer\');return false;"><img src="' + sImageBase + 'btn_weitershoppen-layer.gif" class="weitershoppen" width="161" height="30" border="0" alt="btn_weitershoppen-layer.gif" /></a></div>' +
					'<div id="layer">&nbsp;</div>'
		}
	);

	element2MaxSize('layer', 'footer');

	return false;
}//end - function

/**
 * Preloader for the basket layer
 *
 * @param string sImageBase
 * @param Image oLayerImage
 * @return bool
 */
function basketpreloader(sImageBase, oLayerImage)
{
	Element.insert(
		$('wrapper').up(0),
		{
			bottom: '<div id="message preload" class="message preload" onclick="removeLayers(\'message\', \'layer\');return false;">'+
					'<a href="#" onclick="removeLayers(\'message\', \'layer\');return false;"><img src="'+ sImageBase +'message_close.gif" width="13" height="13" border="0" alt="close" /></a>'+
					'<img src="'+ oLayerImage.src + '" class="preload" width="190" height="295" border="0" alt="Preload" />'+
					'</div><div id="layer" onclick="removeLayers(\'message\', \'layer\');return false;">&nbsp;</div>'

		}
	);

	element2MaxSize('layer', 'footer');

	return true;
}//end - function

/**
 * This function refreshes the given img with the given url
 *
 * @param object oChangeImg
 * @param string sUrl
 */
function changeDetailPicture(oChangeImg, sUrl)
{
	oChangeImg.src = sUrl;

	return false;
}//end - function

var bSaferpaySemaphore = false;

/**
 * This method checks if the value of the sPaymentRadioName - Element is used for saferpay.
 * If the saferpay payment is wished, this function retrieves the scd registration url
 * using an ajax request
 * @param Array aSaferpayFields -> The allowed fields for the saferpay request
 * @param mixed mCCNumField -> field for the cc number
 * @param mixed mForm -> payment form
 * @param object oButton -> submit button for deactivation while ajax requesting
 * @param string sBaseUrl -> base url for the ajax request
 * @param string sHashKey -> hash key for the cc reference
 * @param string sPaymentRadioName
 * @return bool
 */
function checkForSaferpayPayment(aSaferpayFields, mCCNumField, mForm, oButton, sBaseUrl, sHashKey, sPaymentRadioName)
{
	if (bSaferpaySemaphore)
	{
		return false;
	}

	var oForm = $(mForm);

	if ($F(oForm.down('input:checked[name="' + sPaymentRadioName + '"]')) != 'oxidcaisaferpay')
	{
		return true;
	} // if

	bSaferpaySemaphore = true;

	var oBool = new Ajax.Request(
		sBaseUrl + '&fnc=ajaxscdlink&sData=' + hex_hmac_sha1(sHashKey, $F($(mCCNumField))),
		{
			method: 'get',
			onCreate: function()
			{
				// location.href    = sBaseUrl + '&fnc=ajaxscdlink&sData=' + hex_hmac_sha1(sHashKey, $F($(mCCNumField)));
				oButton.disabled = true;
			}, // function
			onFailure: function(oTrans)
			{
				alert(oTrans.responseText);
				oButton.disabled = false;
			}, // function
			onSuccess: function(oTrans)
			{
				var sUrl = oTrans.responseText.replace (/^\s+/, '').replace (/\s+$/, '');

				if (sUrl.startsWith('https://www.saferpay.com/'))
				{
					var aTagNames = new Array('input', 'select');
					var sFormId   = oForm.identify();

					aTagNames.each(function(sTagName)
					{
						var aInputs = $$('#' + sFormId + ' ' + sTagName);

						aInputs.each(function(oFormField)
						{
							if ((aSaferpayFields.indexOf(oFormField.name) === -1) && (oButton !== oFormField))
							{
								oFormField.disabled = true;
							} // if
						}); // .each
					}); // .each

					oForm.action = sUrl;
					oForm.submit();
				} // if
			} // function
		} // parameters
	);

	return false;
}//end - function

/**
 * This function checks if the selected index of the size select is the first one
 * to create a layer to tell the user, that he should select a size.
 *
 * @param object oSizeSelect
 * @param string sImageBase -> Base path to template images
 * @param string sWarningString -> Message for the user
 * @return bool
 */
function checkSize(oSizeSelect, sImageBase, sWarningString)
{
	removeLayers('message');

	//checking if the select was changed to another index
	if (oSizeSelect.selectedIndex != 0)
	{
		//basketlayer(sImageBase);
		return true;
	}

	/*
	 * the creating of single dom objects was not working in ie, so the dom is
	 * manipulated with pure html
	 */

	Element.insert(
		oSizeSelect.up('form'),
		{
			after: '<div class="message" onclick="$(this).remove();"><a href="#" onclick="$(this).up(\'div\').remove();;return false;"><img src="' + sImageBase +
		'message_close.gif" width="13" height="13" border="0" alt="close" /></a><p>' +
		sWarningString + '</p><!--[if lte IE 6.5]><iframe></iframe><![endif]--></div>'
		}
	);

	var oBox	= $$('.message')[0];

	try {
		oBox.down('iframe').setStyle({
			'height' : oBox.getHeight() + 'px',
			'width' : oBox.getWidth() + 'px'
		});
	} catch (e) {}

	return false;
}//end - function

/**
 * This function uses the given fotoer element to resize the changable element to the
 * maximum size
 *
 * @param mixed mChangeElement -> Element to change
 * @param mixed mFooterElement -> Elemente for the max size
 * @return void
 */
function element2MaxSize(mChangeElement, mFooterElement)
{
	var oFooter = $(mFooterElement);
	$(mChangeElement).setStyle({'height' : oFooter.viewportOffset().top + oFooter.getHeight() + 'px'});
}//end - function

/**
 * Removing of layer elements.
 *
 * @param string sCssClass -> css class for the normal layer
 * @param string sAdditionalId -> give this id if there is an additional layer
 * @return void
 */
function removeLayers(sCssClass, sAdditionalId)
{
	//basic vars for removing messages
	var aMessages	= $$('.' + sCssClass);
	var iCount		= aMessages.length;

	for (i = 0; i < iCount; i++)
	{
		aMessages[i].remove();
	}

	var oLayer = $(sAdditionalId);

	if (oLayer != null)
	{
		oLayer.remove();
	}
}//end - function

/**
 * This function opens a new window to display the more details page for an article
 *
 * @param object oLink -> pressed link
 */
function showMoreDetails(oLink)
{
	oLink = $(oLink);

	if (oLink.href)//checking if there is a reference attr
	{
		open(oLink.href, 'send_link', 'status=yes,scrollbars=no,menubar=no,width=450,height=500');
	}//end - if

	return false;
}//end - function

    var sCopyTitle   = null;
    var oCurrToolTip = null;
    var oToolTipPos  = null;
    //
    function showTooltip(obj, blShow)
    {   if (blShow)
        {   sCopyTitle = obj.title;
            obj.title  = '';
            oCurrToolTip = document.createElement('span');
            oCurrToolTip.innerHTML  = sCopyTitle;
            oCurrToolTip.className  = 'tooltip';

            oToolTipPos = document.createElement('span');
            oToolTipPos.className  = 'tooltippos';
            oToolTipPos.appendChild(oCurrToolTip);

            obj.parentNode.insertBefore(oToolTipPos, obj);
        }
        else
        {   obj.title  = sCopyTitle;
            sCopytitle = '';
            if (oToolTipPos != null)
            {   obj.parentNode.removeChild(oToolTipPos);
                oToolTipPos = null;
            }
        }
    }
