/**
 *	Color functions
 *
 *	Developed for Greenflame
 *	October 2011
 *
 *	Version 1.0
 */
 
//-------------------------------------------------------------------

/**
 *	Select color
 *
 *	When a color is selected, this function changes some values
 *	in the form used when adding a product to the shopping cart.
 *
 *	It also changes the shown price and product info.
 */
$(document).ready(function() {
	$('.color').click(function() {
		// Get id's
		id = $(this).parent().attr('id');
		part_id = $(this).attr('id');
		
		// Change product form details
		$('.product_info_id_'+id).val(part_id);
		$('.part_number_'+id).val($('.part_number_'+part_id).val());
		$('.weight_'+id).val($('.weight_'+part_id).val());
		$('.price_'+id).val($('.price_'+part_id).val());
		$('.stock_'+id).val($('.stock_'+part_id).val());
		$('.img_'+id).val($('.img_original_'+part_id).val());
		$('.color_'+id).val($('.color_name_'+part_id).val());
		
		// Change product info number on links
		$('.show_more_link_'+id).attr('href','/produkt/'+$('.product_url_name_'+id).val()+'/'+id+'/'+part_id);
		
		// Change shown part number
		$('.part_number_container_'+id).html($('.part_number_'+part_id).val());
		
		// Change shown color name
		$('.color_name_container_'+id).html($('.color_name_'+part_id).val());
		
		// Change shown image
		$('.bg_image_'+id).css('background-image','url('+$('.img_thumb_'+part_id).val()+')');
		$('.bg_image_link_'+id).attr('href',$('.img_view_'+part_id).val());
		
		// Change shown price
		if($('.price_discount_'+part_id).val() != '') {
			$('.price_container_'+id).html(
				'<li class="overstreget">'+$('.price_original_'+part_id).val()+'<span>DKK</span></li>'+
				'<li class="tilbud">'+$('.price_discount_'+part_id).val()+'<span>DKK</span></li>'
			);
		} else {
			$('.price_container_'+id).html(
				'<li>'+$('.price_original_'+part_id).val()+'<span>DKK</span></li>'
			);
		};
		
		// Change shown stock info
		$.ajax({
			url: "/ajax/update_color_stock",
			async: true,
			cache: false,
			type: 'post',
			data: {
				product_info_id: part_id
			},
			statusCode: {
		    	404: function() {
					$.jGrowl('En ajax funktion kunne ikke få kontakt til den ønskede url', { header: '404 error' , life: 6000 });
		    	}
		  	},
			error:function (xhr, ajaxOptions, thrownError) {
				$.jGrowl(xhr.status);
				$.jGrowl(thrownError);
			},
			success: function(data) {
				$('.stock_container_'+id).html(data);
			}
		});
	});
});


/**
 *	Hover color
 *
 *	When hovering over a color, this function changes the shown
 *	price and product info.
 */
$(document).ready(function() {
	var id;
	$('.color').hover(
		function() {
			// Get id's
			id = $(this).parent().attr('id');
			part_id = $(this).attr('id');
			
			// Change shown image
			$('.bg_image_'+id).css('background-image','url('+$('.img_thumb_'+part_id).val()+')');
			
			// Change shown price
			if($('.price_discount_'+part_id).val() != '') {
				$('.price_container_'+id).html(
					'<li class="overstreget">'+$('.price_original_'+part_id).val()+'<span>DKK</span></li>'+
					'<li class="tilbud">'+$('.price_discount_'+part_id).val()+'<span>DKK</span></li>'
				);
			} else {
				$('.price_container_'+id).html(
					'<li>'+$('.price_original_'+part_id).val()+'<span>DKK</span></li>'
				);
			};
		},
		function() {
			// Get current
			current_part_id = $('.product_info_id_'+id).val();
			
			// Change shown image
			$('.bg_image_'+id).css('background-image','url('+$('.img_thumb_'+current_part_id).val()+')');
			
			// Change shown price
			if($('.price_discount_'+current_part_id).val() != '') {
				$('.price_container_'+id).html(
					'<li class="overstreget">'+$('.price_original_'+current_part_id).val()+'<span>DKK</span></li>'+
					'<li class="tilbud">'+$('.price_discount_'+current_part_id).val()+'<span>DKK</span></li>'
				);
			} else {
				$('.price_container_'+id).html(
					'<li>'+$('.price_original_'+current_part_id).val()+'<span>DKK</span></li>'
				);
			};
		}
	);
});
