/**
 * Multiple currency convertor
 *
 * Copyright (c) 	2009
*  Author:  		Dave Sniper
 * Studio: 		KD WEB  - kdweb.co.uk
 * Location: 	LONDON
 */
 
 
/** This function is to convert current price to the rate.
 */
 function currencyConvertor(price){
	//default rate is pound = 1
	var rate = 1;
	var selected_currency = $.cookie('selected_currency');
	
	price = parseFloat(price).toFixed(2);
	
	switch (selected_currency){
		case "euro":
			rate = $.cookie('euro_rate');
		break;
			
		case "dollar":
			rate = $.cookie('dollar_rate');
		break;
			
		case "yen":
			rate = $.cookie('yen_rate');
		break;
	}
	return (price * rate).toFixed(2);
 }
 
 
/**function is to add a correct Currency Symbol to the price 
*    @param: price
*    @return: it depends on the selected currency and we need to add its symbol next to the price (infront or behind)
 */

 function priceSymbolFormating(price){
 
	// Initialising vairables
	var symbol = "&pound;";
	var selected_currency = $.cookie('selected_currency');
	
	// Declare currency symbol
	switch (selected_currency){
		case "euro":
			symbol = "&euro;";
		break;
			
		case "dollar":
			symbol = "&#36;";
		break;
			
		case "yen":
			symbol = "&yen;";
		break;
	}
 
	// Add currency symbol next to the given price
	/*
	switch (selected_currency){
		case "euro":
			return  price+symbol;
		break;
		default:
			return  symbol+price;
		break;
	}
	*/
	// Return the result
	return symbol+price;
 }

 
 
 /* jQuery action ready */
 $(document).ready(function(){
 	
	
	/****************** INITIAL SCRIPT TO SHOW THE CORRECT PRICE AND CURRENCY******************************/
		
	$('span.priceTag').each(function(){
		var priceInPound = $(this).attr('title');			
		//alert(priceInPound);
		$(this).html(priceSymbolFormating(currencyConvertor(priceInPound)));
	});
	
		
	// Magic will happen when user click on Currency Symbol 
	$(".currency-button").click(function(){
	
		//Set active class for selected currency symbol	
		$(".currency-button").removeClass('active');
		$(this).addClass('active');
		
		var selected_currency = $(this).attr('id');		
		// Update selected currency cookies
		if ( $.cookie('selected_currency') != selected_currency){
			$.cookie('selected_currency', selected_currency, { path: '/' });  // Path is currently set as "/" ! very important
		
			// CONVERT PRICE FOR BED LINEN
			$('span.priceTag').each(function(){
				var priceInPound = $(this).attr('title');			
				//alert(priceInPound);
				$(this).html(priceSymbolFormating(currencyConvertor(priceInPound)));
			});
		}
		return false;
	});	
	
 });