(function($) {

$.fn.scale = function(options) {

	var defaults = {
		maxWidth : 100,
		maxHeight: 60
	}

	options = $.extend(defaults, options);

	return this.each(function() {

		var maxWidth = options.maxWidth; // Max width for the image
		var maxHeight = options.maxHeight;    // Max height for the image
		var ratio = 0;  // Used for aspect ratio
		var width = $(this).width();    // Current image width
		var height = $(this).height();  // Current image height

		// Check if the current width is larger than the max
		if(width > maxWidth){
			ratio = maxWidth / width;   // get ratio for scaling image
			$(this).css("width", maxWidth); // Set new width
			$(this).css("height", height * ratio);  // Scale height based on ratio
			height = height * ratio;    // Reset height to match scaled image
			width = width * ratio;    // Reset width to match scaled image
		}

		// Check if current height is larger than max
		if(height > maxHeight){
			ratio = maxHeight / height; // get ratio for scaling image
			$(this).css("height", maxHeight);   // Set new height
			$(this).css("width", width * ratio);    // Scale width based on ratio
			width = width * ratio;    // Reset width to match scaled image
		}


	})

};

})(jQuery);



(function($){


$.fn.products = function(options) {

	/**
	 * Plugin Default Options
	 */
	var defaults = {
		serviceURL : '?service=product_information'
	}

	options = $.extend(defaults, options);

	var getURL = function(parent) {
		return $(parent).attr('serviceURL');
	}
	
	var getSKUs = function(parent) {

		var sku =""


		$(parent).find('ul.products > li').each(function(i, item) {
			sku+=$(this).attr('sku');
			if(i != ($(parent).find('ul.products > li').length-1)) {
				sku+= ',';
			}
		})
		return sku;
	}


	return this.each(function() {
		
		var SKUs	= getSKUs(this),
			URL		= getURL(this),
			self	= this,
			i		= 0,
			template = $(this).find('.product-detail').clone(),
			product;

		$(this).find('.product-container').empty();

		$.getJSON(URL+options.serviceURL+'&product_sku='+SKUs+'&jsoncallback=?', function(data) {
			
			$(data).each(function(i, item) {

				
				product = $(template).clone();


				product
					.find('.product-image > img')
					.attr('src', item.images.image1)
	


				if(item.manufacturer_image == "") {
					product
						.find('.product-manufacture')
						.hide();
				}
				product
					.find('.product-manufacture')
					.attr('src', item.manufacturer_image);
			
				product
					.find('h4')
					.html(item.title);

				
				product
					.find('.product-sku')
					.html('Artikelnr.:' +item.product_sku);
			
				product
					.find('.product-description')
					.html(item.shortdescr);

				product
					.find('.product-available')
					.html(item.available_flag_text);

				product
					.find('.product-price-discount')
					.html(item.price + '*');

				if(item.price_max == "") {
					product
						.find('.product-price')
						.hide();
				}
				product
					.find('.product-price')
					.html(item.price_max + '*')

				product
					.find('.product-available')
					.addClass('av'+item.available_flag);

				product
					.find('.product-view').parent()
					.attr('href',item.link);

			
				product
					.find('.product-basket').parent()
					.attr('href',item.basket_link);

				product
					.find('.product-mwst > a')
					.attr('href',item.shipping_link)
					.html(item.mwst)

				var list = $('<li>'),
					image = $('<img src="'+item.images.thumbnail1+'" border="0">');

				list
					.append(
						image
					)
					.hover(function() {
						$(this).addClass('hover');
					}, function() {
						$(this).removeClass('hover');
					})
					.click(function() {

						$(self).find('.product-menu > li').removeClass('active');
						$(this).addClass('active');
						$(self).find('.product-detail:visible').fadeOut(200, function() {
							$(self).find('.product-detail:nth-child('+ (i+1) +')').fadeIn(200);
							$('.product-image > img').scale({
								maxWidth: 250,
								maxHeight: 260
							});
						});
						
						
					})


				$(self).find('.product-menu').append(list);
				$(self).find('.product-menu > li:first').addClass('active');
				$(self).find('.product-container').append(product);
			});

			$(self).find('.product-detail:first')
				.show();

			$(self).find('.product-menu > li > img').scale();
			

		});

	
		
	})


};
})(jQuery);

$j(document).ready(function() {
	$j('.product-switch').products();
})
