if(!ajaxRequestCollection) var ajaxRequestCollection = new Array();

/* set variables to detect IE versions... */
Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

function addHover(obj, strSuffix)	{
	if(null==strSuffix) strSuffix = '_hover';

	//obj.src=obj.src.replace(new RegExp("("+strSuffix+")*.(\w+)$"), strSuffix+'.$2');
	var rExp = new RegExp('('+strSuffix+')*.(\\w+)$');
	obj.src = obj.src.replace(rExp, strSuffix+'.$2');
}

function remHover(obj, strSuffix)	{
	if(null==strSuffix) strSuffix = '_hover';

	var rExp = new RegExp('('+strSuffix+')*.(\\w+)$');
	obj.src=obj.src.replace(rExp, '.$2');
}

function Tip(){return;}
function UnTip(){return;}


var tx_mevshop_blurFunctionInitiated = false;
Event.observe(document, 'dom:loaded', function() {
	if(!tx_mevshop_blurFunctionInitiated) {
		$$('input.blurText').each(function(elem, key) {
			elem.value = Element.readAttribute(elem, 'title');

				// ...beim Begehen des Elements...
			Event.observe(elem, 'focus', function(){
				if(elem.value==Element.readAttribute(elem, 'title')) {
						// ...wenn momentan der Standardtext verwendet wird, dann Feld leeren...
					elem.value = '';
						// ...und eine Info-Klasse hinzufügen
					Element.addClassName(elem, 'blurText-typing');
				}
			});

			Event.observe(elem, 'blur', function(){
				if(!elem.value) {
					Element.removeClassName(elem, 'blurText-typing');
					elem.value = Element.readAttribute(elem, 'title');
				}
			});
		});

		tx_mevshop_blurFunctionInitiated = true;
	}
});


/**
 * Erstellt bzw. entfernt, falls vorhanden, einen Klassenamen von einem Element
 * @param el	Das entsprechende Element, wird übl. mit this übergeben
 * @param cN	Klassenname
 * @return void(0);
 */
function toggle_class(el, cN)	{
	//prüfen ob Klassenname vorhanden:
	var theRegex = new RegExp('\\b'+cN+'\\b','g');
	//console.log(theRegex.test(el.className));
	if(theRegex.test(el.className))	{
		//Klassenname wurde gefunden, also entfernen:
		el.className = el.className.replace(theRegex,'');
	} else {
		//nicht gefunden, hinzufügen:
		el.className+=' '+cN;
	}
}


/**
 * This function is called by an AJAX request when required. used to substitute the dynamic prices.
 * @return void
 */
function substArticlePrices() {
	$$('table.articlelist td.itemprice').each(function(elem, index) {

		//add the event to the textbox to refresh the prices:
		var adj = Element.adjacent(elem, 'td input.amount')[0];
		Event.observe(adj, 'keyup', function(event) {
			refreshElemPrice(elem);
		});

		//add the AJAX request to the price to retreive it:
		refreshElemPrice(elem);
	});

}

/**
 * Refresh the element price of a single element.
 *
 * @param elem	Element identifier, should be the <td> containing the price.
 * @return void
 */
function refreshElemPrice(elem)	{
	var itemId = parseInt(Element.adjacent(elem, 'td.itemno span.itemId')[0].firstChild.data);

	var theAmount = parseInt(Element.adjacent(elem, 'td input.amount')[0].value);
	if(!theAmount || 0==theAmount || isNaN(theAmount)) {
			//theAmount ist keien gültige Zahl, also abbrechen und 'nichts' annehmen
		Element.adjacent(elem, 'td input.amount')[0].value = '';
		Element.update(elem, '&laquo; ' + tx_mevshop_lng.label_fillInQuantitiy);
		return;
	}

	//if(console)console.log('would call index.php?eID=tx_mevshop_dynItemPrice&itemId='+itemId+'&amount='+theAmount);
	//Aufruf: http://schulung.mevaco.de/index.php?eID=tx_mevshop_dynItemPrice&itemId=15396&amount=1

	ajaxRequestCollection.push(new Ajax.Request('index.php',	{
		method:'get',
		parameters: {
			eID: 'tx_mevshop_dynItemPrice',
			'itemId': itemId,
			'amount': theAmount
		},
		onCreate: function()	{
			Element.update(elem, '<img src="typo3conf/ext/mevshop/res/img/loadingicon.gif" width="16" height="16" alt="Ladeicon" title="Ihr persönlicher Preis wird geladen..." />');
		},
		onSuccess: function(ajxResp)	{
			if(ajxResp.responseXML && ajxResp.responseXML.getElementsByTagName('errormsg') && ajxResp.responseXML.getElementsByTagName('errormsg')[0])	{
				if(ajxResp.responseXML.getElementsByTagName('errormsg')[0].getAttribute('maxAmount')) {
					Modalbox.show(
						'<span>'+tx_mevshop_lng.errmsg_maxArticleAmount(ajxResp.responseXML.getElementsByTagName('errormsg')[0].getAttribute('maxAmount'))+'</span>'
						,{
							title: tx_mevshop_lng.errmsg_maxArticleAmount_title
						});
				}
				Element.update(elem, '..');
			} else if(ajxResp.responseXML && ajxResp.responseXML.getElementsByTagName('itemid') && itemId == parseInt(ajxResp.responseXML.getElementsByTagName('itemid')[0].firstChild.data))	{
				Element.update(elem, ajxResp.responseXML.getElementsByTagName('baseprice')[0].firstChild.data + ' '
							+ ajxResp.responseXML.getElementsByTagName('t3curr')[0].firstChild.data);
			} else {
				Element.update(elem, 'XX');
			}
		},
		onFailure: function(){ Element.update(elem,'XX'); }
	})); //END new ajax.request
}


