/* Application.js */
/* This is basic js for functioning shop. */
/* For custom js, use javascript.js instead */

$(function() {
	
	$('#web-payment').click(function() {
		$('#epaymentform').submit();
		return false;
	});
	
	$('.product-image').fancybox({
		'padding': 0,
		'transitionIn'	: 'elastic',
		'transitionOut': 'elastic',
		'overlayColor'	: '#000',
		'overlayOpacity': '0.5',
		'titlePosition': 'inside',
		'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Kuva ' + (currentIndex + 1) + ' / ' + (currentArray.length)+ (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});

	$('.fancy').fancybox({
		autoScale: false,
		autoDimensions: false,
	   scrolling: false
	});

	$('.image-launcher').click(function() { $('.product-image:first').click(); return false; });

	$('#print').click(function() { window.print(); return false; });
		
	$('#togglesearch').click(function() {
		$('#searchform1').hide();
		$('#searchform2').slideDown();
		return false;
	});

	$('.toggle-recent-order').toggle(function() {
		var $this = $(this);
		$this.text('Piilota');
		$this.parents('tr').next().show();
		return false;
	}, function() {
		var $this = $(this);
		$this.text('Näytä');
		$this.parents('tr').next().show().hide();
		return false;	
	});

	$('#top-navigation li:not(".current"), .button-wrapper-highlight2').hover(function() { $(this).toggleClass('hover'); });

	$('.openTab').click(function() {
		var $this = $(this);
		$this.parents('ul').find('a').each(function() {
			var $this = $(this);
			$($this.attr('href')).addClass('hidden');
			$($this.attr('href')).hide();
			$this.removeClass('current');
		});
		$this.addClass('current');
		$($this.attr('href')).removeClass('hidden');
		$($this.attr('href')).show();
		return false;
	});

	$('#feedbackSubmit').click(function() {
		var $form = $(this).closest('form');
		var maili = $form.find('input[name="email"]').val();
		var nimi = $form.find('input[name="name"]').val();
		var viesti = $form.find('textarea').val();
		if(maili.length < 1 || nimi.length < 1 || viesti.length < 1) { $('.errorFeedback').show(); return false; }
		return true;
   });

	if(window.ie6) {
		$('.formtable td:first-child').css('width','30%');
		$('#product-list td:first-child').css('width', 'auto');
	}

	/**
	 * Ostoskorin yhteenveto-sivulla kokonaishinnan lasku toimituskuluista
	 * ja maksukuluista. Huom: hinnat saadaan syötteessä sentteinä,
	 *	koska JavaScriptin float pyöristelyt ovat mitä ovat. 
	 */
	var pay_price = 0,
	deli_price = 0,
	pay_vat = 0,
	deli_vat = 0,
	order_price =  $('#total_price_hidden').html(),
	order_vat = $('#total_vat_hidden').html();
	if (order_price != undefined) {
		order_price = order_price.replace(',','.');
		order_price = +order_price;
	}
	if (order_vat != undefined) {
		order_vat = order_vat.replace(',','.');
		order_vat = +order_vat;
	}
	
	function updateOrderPrice() {
		var total_price = pay_price + deli_price + order_price;
		if (isNaN(total_price)) {
			return false;
		}
		total_price = total_price + '';
		var part1,
		part2,
		output;
		part1 = total_price.substring(0, total_price.length-2);
		part2 = total_price.substring(total_price.length-2, total_price.length);
		if (part1.length == 0)
			part1 = "0";
		output = part1 + "," + part2;
		$('#total_price').html(output);
	}
	
	function updateOrderVAT() {
		var total_vat = pay_vat + deli_vat + order_vat;
		if (isNaN(total_vat)) {
			return false;
		}
		total_vat = total_vat + '';
		var part1,
		part2,
		output;
		part1 = total_vat.substring(0, total_vat.length-2);
		part2 = total_vat.substring(total_vat.length-2, total_vat.length);
		if (part1.length == 0)
			part1 = "0";
		output = part1 + "," + part2;
		$('#total_vat').html(output);
	}
	
	$('#deliveryMethod').bind('change', function() {
		
		deli_price = $(this).find('option:selected').data('price');
		deli_price = +deli_price;
		updateOrderPrice();
		deli_vat = $(this).find('option:selected').data('vat');
		deli_vat = +deli_vat;
		updateOrderVAT();
	});
	
	$('#paymentType').bind('change', function() {
		pay_price = $(this).find('option:selected').data('price');

		pay_price = +pay_price;
		updateOrderPrice();
		pay_vat = $(this).find('option:selected').data('vat');
		pay_vat = +pay_vat;
		updateOrderVAT();
	});
	
	$('#deliveryMethod').trigger('change');
	$('#paymentType').trigger('change');

});
