/**
 *  Created by: Dave Sniper  
 * Copyright 2009 - KD WEB 
 * Free to use or contribute this code .. at your own risk 
 * FOR MATS AND NAPKINS PAGE
 * sc_  means selected_currency
 */
 
function generateProductElements(){
	$('.recommended>li').each(
		function (iterator, element){
			var title = $(element).find('h3').html();
			
			// initialising sub-total
			var sub_total = 0;
			var sub_quantity = 0;			
			// traversing the doc and calculate total price				
			$('ul.items',element).each(function(iterator, ul){			
				sub_total +=				
						//quantity (can be specified to select[name=quantity] option:selected)
						$('input[name=qty[]]',ul).val()						
						//times						
						*						
						//price   (can be specified to input[name=price[]])
						$('input[name=price[]]',ul).val()
				;				
				sub_quantity += 1 * ($('input[name=qty[]]',ul).val());			
			});
			
			// Get currency price for current currency
			sc_sub_total = priceSymbolFormating(currencyConvertor(sub_total));
			
			$('<ul />')
			.attr('rel',iterator)			
			.html(	'<li class="sum-title">'+title+'</li>' + 
					'<li class="sum-total"><span class="priceTag" title="'+sub_total+'">'+sc_sub_total+'</span></li>'+
					'<li class="sum-quantity">qty '+sub_quantity+'</li>')
			.css({'display': (sub_quantity == 0)? 'none':'block'})
			.appendTo('#selectedProList');			
		}
	);
}


/**	Function is to calculate total price and show in the summary box
*
*
*
*/
function calTotalPrice(){	
	//intial total price value
	var total = 0;
	
	// traversing doc and calculate total price
	$('.items li.subProdQuantity').each(
		function (iterator, element){
			var price = $(this).parent().find('input[name=price[]]').val();
			var quantity = $(element).find('input[name=qty[]]').val();
			total += quantity * price;
		}		
	);	
			
	$('.recommended>li').each(
		function (iterator, element){						
			// initialising sub-total
			var sub_total = 0;
			var sub_quantity = 0;			
			// traversing the doc and calculate total price				
			$('ul.items',element).each(function(iterator, ul){			
				sub_total +=				
						//quantity (can be specified to input[name=quantity] option:selected)
						$('input[name=qty[]]',ul).val()						
						//times						
						*						
						//price   (can be specified to input[name=price[]])
						$('input[name=price[]]',ul).val()
				;				
				sub_quantity += parseInt( $('[name=qty[]]',ul).val(), 10);			
			});			
			
			// Convert price in pounds into selected currency ! These converting functions are in multiple_currency.js file
			sc_sub_total = priceSymbolFormating(currencyConvertor(sub_total));
						
			if (sub_quantity !=0){				
				$('#selectedProList ul[rel='+iterator+'] li:nth-child(2)').html('<span class="priceTag" title="'+sub_total+'">'+sc_sub_total+'</span>');
				$('#selectedProList ul[rel='+iterator+'] li:last').text('qty '+sub_quantity);
				$('#selectedProList ul[rel='+iterator+']').addClass('show');				
			}
			else
			{
				$('#selectedProList ul[rel='+iterator+']').removeClass('show').hide();
			}									
		}
	);
	
	$('#selectedProList ul.show').fadeIn();
	$('#selectedProList ul:not(.show)').fadeOut();
		
	
	//parce total price to total div	
	sc_total = priceSymbolFormating(currencyConvertor(total));
	$('#totalCost').html('<span class="priceTag" title="'+total+'">'+sc_total+'</span>');			
}



function validateSubmitMatNapkins(){
	
	$('#summarybox li.error').html("").hide();
	
	// Validate the table sizes
	errorMsg = "";
	
	
	// initial total quantity
	var total = 0;
	
	// traversing doc and calculate total price
	$('.items li.subProdQuantity').each(
		function (iterator, element){
			var price = $(this).parent().find('input[name=price[]]').val();
			var quantity = $(element).find('input[name=qty[]]').val();
			total += quantity * price;
		}		
	);	
			
	if (total > 0){ 
		$('#summarybox li.error').html("").hide();
		return true;
	}
	else
	{
		errorMsg = "You need to enter product's quantity";
		$('#summarybox li.error').html(errorMsg).show();
		return false;
	}
	
}






$(document).ready(function(){	
	
	//innitial caculating the total cost
	calTotalPrice();
	
	//generate the list
	generateProductElements();
	
	//do the calculation base on prices
	
	
	// $('li.subProdQuantity>input').change(calTotalPrice);   -- Not sure why IE does not work well with change event
	
	 $('li.subProdQuantity>input[type="text"]').bind("change keyup",calTotalPrice);		
	/* ************************************  IMAGE GALLERY ************************************ */
	
	$('#imgGallery').hide();
	
	//reset all thumbnail images's href to "#nogo" .. to handle javascript being turned off
	$('a.thumbItem').each( function(){
		$(this).attr('href','#nogo');
	});
	
	//handle click event when user click on thumbnail image in the Gallery
	$(".thumbnailList a.thumbItem").click(function() {
		// get orginal image src on event
		var imgSrc = $(this).parent().find('input').val();		
	
		// only load / replace a new image
		if ($('#LargeImgContainer img.largeImage').attr('src') != imgSrc){
			$('#LargeImgContainer img.largeImage')
				.fadeOut(
					300
					, 
					function()
					{
						// remove current large image
						$(this).remove();
						//display loading image
						$('#LargeImgContainer img.loadingImage').show();						
						
						$('#LargeImgContainer')
							.append(
								$('<img>')
									.attr('src',imgSrc)
									.addClass('largeImage')
									.hide()
									.load(
										function()
										{
											// hide loading image
											$('#LargeImgContainer img.loadingImage').hide();
											
											// show latest large image
											$(this).fadeIn();
										}
									)
							)
						;
					}
				)
			;
		}		
		return false;
	});
	
	

	
	/* ************************************  COLOUR PALETTE ************************************ */
	
	
	//reset all thumbnail images's href to "#nogo" .. to handle javascript being turned off
	$('a.thumbItem').each( function(){
		$(this).attr('href','#nogo');
	});
	
	//handle click event when user click on thumbnail image in the Gallery
	$(".colour-samples a.thumbItem").click(function() {
		// get orginal image src on event
		var imgSrc = $(this).parent().find('input').val();
		var caption = $(this).parent().find('label').text();	
	
		// only load / replace a new image
		
		
		if ($('#BigImgContainer img.BigImage').attr('src') != imgSrc && window.inAction != true){
			window.inAction = true;
			$('#BigImgContainer img.BigImage')
				.fadeOut(
					300
					, 
					function()
					{
						// remove current large image
						$(this).remove();
						//display loading image
						$('#BigImgContainer img.loadingImage').show();						
						
						$('#shown-caption').text(caption);
						
						$('#BigImgContainer')
							.append(
								$('<img>')
									.attr('src',imgSrc)
									.addClass('BigImage')
									.hide()
									.load(
										function()
										{
											// hide loading image
											$('#BigImgContainer img.loadingImage').hide();
											
											// show latest large image
											$(this).fadeIn();
											window.inAction = false;
										}
									)
							)
						;
					}
				)
			;
		}		
		return false;
	});
	
	
	
	

	
	// SWITCH INCH TO CM OR VISOR VERSOR
	
	// Switch measurement figures and display
	$('#sizeSwitcher input[name="sizeSwitcher"]').click(
		function(){
			
			switch ($('#sizeSwitcher input[name="sizeSwitcher"]:checked').val()){
				case 'inch':		
					// show all inch values
					$('.size_cm').hide();
					$('.size_inches').show();
				break;
				case 'cm':
					// show cm values					
					$('.size_inches').hide();
					$('.size_cm').show();
				break;			
			}		
						
		}
	);	
	
	
	/* ************************************MAGIC UTILITY   ************************************** */
	
	// TO REMOVE ALL IMAGES WITH EMPTY SRC ATTRIBUTE
	$('img').each( function(){
		if ( $(this).attr('src') == "" ){
			$(this).hide();
		}
	});
	
});
