/*
 * $Id: product_info.js 212 2010-05-25 19:21:01Z 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.
 * 
 * Der Text für Designer wird mit der jQuery Plugin jTruncate an der richtigen Stelle abgeschnitten
 * 
 * Farbauswahlbilder werden zusätzlich direkt unterhalb der Auswahlbox angezeigt. Die Auswahlbox muss
 * die CSS-Klasse "color-selection-dropdown". Das Farbauswahlbild wird dann in den Nachfolger dieser 
 * Auswahlbox geladen. Der Nachfolger ist ein "img.color-selection-image" Knoten.
 * 
 * Für jedes Merkmal kann es eine bestimmte Lieferzeit geben. Diese muss oberhalb des Merkmalwertes angezeigt
 * werden. ausserdem muss die Lieferzeit am Artikelpreis geändert 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');
	var variantenId = $j("input:[name='variantsId']").attr('value');
	$j("#product_configuration select").unbind('change', getDeliveryTime);
	//alert("Option : " + optionId + ", Option value :" + optionValueId + ", Css-Class : " + cssClass);
	$j.get(url,{
				"action": 'variantChanges',
				"option_id" : optionId,
				"option_value_id" : optionValueId,
				"productsId": productsId,
				"variantsId": variantenId},
				function(responseText){
					$j("." + cssClass).html(responseText);
					getDeliveryTime();
					$j("#product_configuration select").bind('change', getDeliveryTime);
					//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 (Info-Telefon, Callback-Service) an den unteren Rand
	Designer und Hersteller Text kürzen, Change Trigger auf alle Merkmale mit Farbauswahl
	Change Trigger auf alle select-Nodes innerhalb div id="product_configuration"
*/
$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>"
			
	});
	
	$j('.color-selection-dropdown').live('change', setColorSelectionImage);
	
	$j("#product_configuration select").bind('change', getDeliveryTime);
});

var setColorSelectionImage = function(event){
	var name = $j(event.target).attr('name');
	var optionId = name.substring(3, name.length-1);
	
	var value = $j(event.target).attr('value');
	var productsId = $j('#products_id').attr('value');
	//alert('colorSelectionImage, Name: ' + name+', Value: ' + value +' ProductsId: ' + productsId + ' OptionId: ' + optionId);
	$j.get(
			'ajax_driven.php',
			{action: 'colorSelectionImage', productsId: productsId, optionValue: optionId, optionsValuesId: value},
			function(data){
				//alert(data);
				$j('.color-selection-image_' +optionId).attr('src', data);
			}
		);
}


var getDeliveryTime = function(event){
	var productsId = $j('#products_id').attr('value');
	var variantenId = $j("input:[name='variantsId']").attr('value');
	var queryString = $j('#cart_quantity select').fieldSerialize();
	//alert("Change of node " + name +" Querystring: " + queryString);
	$j.getJSON(
			'ajax_driven.php',
			{action: 'deliveryTimeForAttributes', productsId: productsId, optionValues: queryString, variantsId: variantenId},
			function(data){
				$j(data['attrib']).replaceAll("#product_configuration .attrib-delivery-time");
				$j(data['product']).replaceAll("#deliveryTimeTag");
				//$j("#product_configuration .attrib-delivery-time").innerHtml()
			}
		);
}

