/*
 * $Id: product_info.js 34 2008-07-04 20:06:43Z manfred $
 * $Author: administrator $
 * $Log: product_info.js,v $
 * Revision 1.11  2007/09/06 04:52:15  administrator
 * removeEvent() und initEvents() neu angelegt
 *
 * Revision 1.10  2007/08/04 17:38:27  administrator
 * requestShippingInfo() parametrisiert mit windows parametern
 *
 * Revision 1.9  2007/06/25 13:13:01  administrator
 * Funktionen um die Diashow zu starten und anzulegen.
 *
 * Revision 1.8  2007/04/10 13:37:14  administrator
 * Funktion "showLongDescription" und "hideLongDescription" erweitert um Parameter "element"
 *
 * Revision 1.7  2006/09/29 17:51:22  administrator
 * Neue WÃ¤hrung : Schweizer Franken
 *
 * Revision 1.6  2006/07/01 15:15:15  administrator
 * Die Produktbeschreibung kÃ¼rzen und bei Anforderung in einem separaten DIV anzeigen.
 *
 * Revision 1.5  2006/06/07 03:45:54  administrator
 * Neue WÃ¤hrung: Britisches Pfund
 *
 * Revision 1.4  2006/05/16 15:08:38  administrator
 * Merkliste
 *
 * Revision 1.3  2006/05/07 04:14:19  administrator
 * Merkliste NEU!
 * Produkt hinzufÃ¼gen
 *
 * Revision 1.2  2006/03/30 08:17:41  administrator
 * WÃ¤hrungen wechseln
 *
 * Revision 1.1  2006/03/19 16:50:02  administrator
 * Versandinformationen fÃ¼r jeden Artikel aus der Produktinfo aus anzeigen
 *
 *
 * Task: 
 * all information in productInfo page, which normally goes over post
 * to shopping-cart, has now to go over GET to shipping_cost_product_info.
 * Therefore I have to read all <select> Elements and their selected options
 * to get the information.
 * After that I build an URL with this values and send it to shipping_cost_product_info.
 *
 * Wenn eine zusÃ¤tzliche WÃ¤hrung angezeigt werden soll, muss das Array arrCurrency erweitert werden
 * und in der Methode convertPriceCurrency() ein weiterer CASE - Fall hinzugefÃ¼gt werden.
 * 
 * Good luck!
*/

$j = jQuery.noConflict();

var arrCurrency = new Array("EUR", "USD", "GBP", "CHF");
var toggle4LongDescription = 0;

function parseSelectFields(fromForm){
	var field, option, mapping = new Array();
	var queryString ="";
	for (var i = 0; i < fromForm.elements.length; i++){
		field = fromForm.elements[i];
		if (field.nodeName == 'SELECT'){
			option = field.options[field.selectedIndex];
			if (queryString.length == 0){
				queryString = field.name + "=" + option.value;
			}else{
				queryString += "&" + field.name + "=" + option.value;
			}
		}
	}
/*	alert("QueryString: " + queryString);*/
	return queryString;
} 


function requestShippingInfo(url, windowPara){
	var form = document.forms["cart_quantity"];
	var queryString = parseSelectFields(form);
	window.open(url + "&" + queryString, "_blank", windowPara);
	
}


/**
*** Währung wechseln : starten des Requests 
*/
var load = function(page){
	var moreArguments = new Array;
	if (arguments.length > 1){
		for (var i = 1; i < arguments.length; i++){
			moreArguments.push(arguments[i]);
		}	
	}
	/* we need some more parameter for our request */
	var toCurr = $j("#toCurr").attr('value');
	var fromCurr = $j("#fromCurr").attr('value');
	var amount = $j("#amount").attr('value');
	var discount = $j("#banktransfer_discount").attr('value');
	//page += "?toCurr=" + toCurr + "&fromCurr=" + fromCurr + "&amount=" + amount + "&discount=" + discount;
	//moreArguments.push(toCurr);
	//moreArguments.push(fromCurr);
	//moreArguments.push(amount);
	$j.getJSON(page, {"toCurr": toCurr,
					  "fromCurr": fromCurr,
					  "amount": amount,
					  "discount": discount
					 },function(json){
					 		//var data = eval("(" + json +")");
					 		$j('#' +moreArguments[0]).html(json[1]);
					 		switch (toCurr){		//new actual currency, the next in the global array arrCurrency
								case "EUR":
									$j("#toCurr").attr('value', arrCurrency[1]);
									break;
								case "USD":
									$j("#toCurr").attr('value', arrCurrency[2]);
									break;
								case "GBP":
									$j("#toCurr").attr('value', arrCurrency[3]);
									break;
								case "CHF":
									$j("#toCurr").attr('value', arrCurrency[0]);
									break;
								default:
									//normally it shouldn't go there
									$j("#toCurr", 0).attr('value', args[2]);  // for another conversion back
							}
							$j("#fromCurr").attr('value', toCurr);	//we change the conversion direction
							$j("#amount").attr('value', json[1]);
							$j("#banktransfer_discount_amount").html(json[0]);
					 }
				);
}

/**
 * Function "add an item to the watchlist"
 * Ajax Request
 * @param 	url		url to the resource where the item is added
 * @return  message to user or go to login page, if customer is not logged in
*/
var addToWatchList = function(url, formName){
	var params = $j("form:[name='" +formName +"']").formSerialize(true);
	$j.get(	url,
			params,
			function(response){
				if ( response.indexOf("Success:") > -1){
					var str = new String(response.slice(response.indexOf("Success:")+ 8));
					alert(str);
				}else{ //goto Login page
					if (response.indexOf("http") > -1){  //falls https eingegeben ist
						document.location.href= response.slice(response.indexOf("http"));
					}else{
						alert(response);
					}
				}	
			});

}


var changeVariant = function(url, optionId, node, cssClass){
	var optionValueId = node.options[node.options.selectedIndex].value;
	var productsId = $j("input:[name='products_id']").attr('value');
	//alert("Option : " + optionId + ", Option value :" + optionValueId + ", Css-Class : " + cssClass);
	$j.get(url,{
				"option_id" : optionId,
				"option_value_id" : optionValueId,
				"products_id": productsId},
				function(responseText){
					$j("." + cssClass).html(responseText);
					//alert(responseText);
				});
}

/**
 * Iterating over form fields, extracting and encoding the name an value
 * 
 */
function getRequestBody(oForm){
	var aParams = new Array();
	aParams.push("action=add_product_to_watchlist");
	for (var i=0; i < oForm.elements.length; i++){
		var sParam = encodeURIComponent(oForm.elements[i].name);
		sParam += "=";
		sParam += encodeURIComponent(oForm.elements[i].value);
		aParams.push(sParam);
	}	
	return aParams.join("&");
}





/**
**  Start ZoomoViewer
*/
var zoomoViewerStart = function(url, windowPara){
	window.open(url, "_blank", windowPara);
}

/**
	Positionieren des containers at-bottom-left an den unteren Rand
*/
$j(function(){
	if($j("div.energy-saving-lamp").size() > 0){
		//energy saving lamp
		$j("div.price-section").css('padding-top', '20px');
	}

	// $j('.at-bottom-left p').valign({wrap:true});
	var heightLeft = $j(".product_info_left").height();
	var heightRight = $j(".product_info_right").height()
	if(heightLeft >= heightRight){
		$j('.at-bottom-left').css({
					"margin-top": "60px",
					"margin-bottom": "20px"
			})
	}
	if(heightLeft < heightRight){
		var marginTop = heightRight - heightLeft;
		if(marginTop < 60) marginTop = 60;
		$j('.at-bottom-left').css({
					"margin-top": marginTop,
					"margin-bottom": "20px"
			})
	}
	//alert('Hoehe links : ' + $j(".product_info_left").height() + ' Höhe rechts: ' + $j(".product_info_right").height());
	var position = $j("div.header-bottom").position();
	var tmp;
	if(position.top < 250) {	//positionieren der Links zum Warenkorb und zur Merkliste
		var heightParent = $j("#product_info_header").height();
		tmp = heightParent -position.top - $j("div.header-bottom").height() +20;
		$j("div.header-bottom").css({"padding-top": tmp +"px"});
	}
	
	
	
	/* Designer Text */
	var moreLink = $j('#design_ellipsis').html();
	
	$j('div.designer-text ').jTruncate({
			length: 380,
			minTrail: 0,
			ellipsisText: moreLink,
			moreText: "",
			lessText: "",
			openHideTag: "<div",
			closeHideTag: "</div>",
			closeTextTag: "</p>"
			
	});
	
	moreLink = $j('#manufacturer_ellipsis').html();
	$j('div.manufacturer-text ').jTruncate({
			length: 680,
			minTrail: 0,
			ellipsisText: moreLink,
			moreText: "",
			lessText: "",
			openHideTag: "<div",
			closeHideTag: "</div>",
			closeTextTag: "</p>"
			
	});
	
});