// product_details.js

function updatePriceCalc(basePrice, optionPrices, totalCartSize) {

	// Get bns
	var bnsp = parseFloat(0.00);
	var bns = parseFloat(0.00);
	var cartSize = (basePrice + optionPrices + totalCartSize);

	for(var i in g_bSteps) {
		if(cartSize>=i) bnsp = parseFloat(g_bSteps[i]);
	}

	if (bnsp > 0.00) bns = parseFloat((basePrice + optionPrices) * (bnsp/100.00)).toFixed(2);
	
	// Get total price
	var totalPrice = (basePrice + optionPrices - bns).toFixed(2);

	// Print price calc
	var priceCalcHtml = '<span style="color:#333;">' + String(totalPrice).replace('.', ',') + ' &euro;</span>';
	if (totalPrice!=basePrice) priceCalcHtml += ' = ' + String(basePrice).replace('.', ',');
	
	if (!isNaN(optionPrices) && optionPrices>0) priceCalcHtml += ' + ' + String(optionPrices).replace('.', ',');
	if (!isNaN(bns) && bns>0.00) priceCalcHtml += ' <span class="bonus bonusCalcShow">- ' + String(bns).replace('.', ',') + ' ('+String(bnsp.toFixed(1)).replace('.', ',')+' % bonus)</span>';
	$('div.priceCalculation').html(priceCalcHtml);
}

function calculatePrices() {
	// Get base price and total cart size
	var basePrice = parseFloat($('input.spr').val());
	var totalCartSize = parseFloat($('input.tcs').val());
	
	var optionPrices = parseFloat(0.00);
	
	// Loop selected option prices and add to basePrice
	$('input.optionRadio:checked').each(function(index) {
		optionPrices += parseFloat($(this).parent().parent().find('input.optionExtraPriceHidden').val());
	});
	
	optionPrices = parseFloat(optionPrices.toFixed(2));
	updatePriceCalc(basePrice, optionPrices, totalCartSize);
}

function operateBox(e) {
	if($(this).attr('id')!=null && e.target.tagName!='A') {
		var pDescClass = '.'+$(this).attr('id');
		if($(pDescClass).is(':hidden'))
			$(pDescClass).show(200);
		else
			$(pDescClass).hide(200);
		return false;
	}
}

function openYtVideo(e) {
	var ytTitle = $(this).attr('title');
	var ytUrl = 'http://www.youtube.com/v/'+$(this).attr('rel')+'?enablejsapi=1&amp;version=3&amp;border=0'; //showinfo=0
	//swfobject.removeSWF("ytapiplayer");
	var ytparams = { allowScriptAccess: "always" };
	swfobject.embedSWF(ytUrl, "ytapiplayer", "618", "463", "8", ytparams, null);
	$("p.videoTitle").text(ytTitle);
	return false;
}

$(document).ready(function() {			

	// Add product description boxes hide/show functionality
	$('.boxBodyHeader').click(operateBox);
	
	// Add cart qty button functionality
	$('a.plusQty').click(addProductQty);
	$('a.minusQty').click(removeProductQty);

	$("input.optionRadio").click(function() {
		// Show selected row, disable others
		$(this).parent().parent().parent().find('tr').each(function(index) { $(this).removeClass('selectedRow'); });
		$(this).parent().parent().addClass('selectedRow');
		calculatePrices(); 
	});	
	
	// Add colorbox
	$("a.zoomImage").colorbox({'photo':true, 'width':'800px', 'opacity':0.3, 'previous':'&lt;Edellinen', 'next':'&nbsp;&gt;Seuraava', 'close':'[x] Sulje', 'current':'(kuva {current}/{total})'});
	
	$('div.zoomButton, div.freeDelivPImageOverlay').click(function() { $("a.zoomImage").click();});
	
	// Add show bonus calculation with colorbox
	$("a.showBonusCalculation, span.bonusCalcShow").colorbox({opacity:0.3, inline:true, close: 'sulje', href:"#productBonusCalculation"});

	// Add option label details display (if exists)
	$("tr.optionRow").hover(function(e) {
		var optionDetskut = $(this).find('div.optionDetails');
		if (optionDetskut!=null) optionDetskut.show(200);
	}, function(e) { 
		var optionDetskut = $(this).find('div.optionDetails');
		if (optionDetskut!=null) optionDetskut.hide(200);
	});

	// Show delivery service tooltip
	$(".deliveryServiceTimed").tooltip({tipClass: "deliveryServiceTimedTooltip", position: "bottom center", relative: true, offset: [10, 120]});

	// Tooltip for delivery info
//	$(".deliveryInfoTipLink").tooltip({tipClass: "deliveryInfoTip", position: "bottom center", relative: true, offset: [10, 0]});
	$(".deliveryInfoTipLink").colorbox({opacity:0.3, inline:true, close: 'Sulje', href:"#deliveryInfoTipBox"});//tooltip({tipClass: "deliveryInfoTip", position: "bottom center", relative: true, offset: [10, 0]});

	// Add smooth scrolling to product description anchors
	$(".scroll").click(function(event){		
		event.preventDefault();
		if ($(this.hash)!=null)	$('html,body').animate({scrollTop:$(this.hash).offset().top}, 800);
	});
	
	// Yt video open
	$('a.openYtVideo').click(openYtVideo);

});
