var bigImageWidth = $('.detailImage').css('width');
var bigImageCount = $('.detailImage').length;
bigImageWidth = parseInt(bigImageWidth.substring(0, bigImageWidth.length - 2));

var slide_images_timout;

function slide_images(backwards) {
	
	clearTimeout(slide_images_timout);
	
//	backwards = backwards || false;
	
	var margin, width, temp, current;

	temp = $('#image_detailed_container_inner').css('margin-left');
	margin = Math.abs(parseInt(temp.substring(0, temp.length - 2)));
	width = bigImageCount * bigImageWidth;

	if (backwards) {
		current = margin - bigImageWidth;
	} else {
		current = margin + bigImageWidth;
	}
	
	if (current < width) {
		$('#image_detailed_container_inner').stop().animate({
			marginLeft: '-' + current + 'px'
		}, 250, function() {
			slide_images_timout = setTimeout(slide_images, 5000);
		});
	} else {
		$('#image_detailed_container_inner').stop().animate({
			marginLeft: '0px'
		}, 500, function() {
			slide_images_timout = setTimeout(slide_images, 5000);
		});
	}
}


/*
 function preview(img, selection) {
 var scaleX = 100 / (selection.width || 1);
 var scaleY = 100 / (selection.height || 1);
 
 $('#lupe > img').css({
 width: Math.round(scaleX * 400) + 'px',
 height: Math.round(scaleY * 300) + 'px',
 marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
 marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
 });
 
 }
 */

function show_klarna_popup() {
	$left_margin = ($('#wrapper').width() - 500) / 2;

	$('#klarna_payment_info_popup').css({
		'display': 'block',
		'left': $left_margin + 'px'
	});
}

function hide_klarna_popup() {
	$('#klarna_payment_info_popup').css({
		'display': 'none'
	});
}


$(document).ready(function() {
	// image lupe
	/*var ias = $('#big_image').imgAreaSelect({
	 aspectRatio: '1:1',
	 onSelectChange: preview
	 });
	 */

	$('#more_klarna_info').click(function(e) {
		e.preventDefault();

		show_klarna_popup();
	});

	$('#klarna_payment_info_popup a.close').click(function(e) {
		e.preventDefault();

		hide_klarna_popup();
	});

	var currentPosition = 0;
	var scrollContainerChildren = $('#thumbScrollContainer a').length;
	var scrollContainerPages = Math.ceil(scrollContainerChildren / 3);

	// Facebook sharer
	$('#facebook_share_item').click(function() {
		var FBShareWin = window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(window.location.href), "FBShareWin", "width=600,height=400,status=yes,scrollbars=no,resizable=no");
		FBShareWin.focus();
	});

	// Thumbs slider	
	$('#thumbSlideRight').click(function(e) {
		e.preventDefault();

		++currentPosition;

		if (currentPosition > scrollContainerPages - 1) {
			currentPosition = scrollContainerPages - 1;
		}

		$('#thumbScrollContainer').stop().animate({
			'left': -(currentPosition * 138 * 3) + 'px'
		});

		var currentPicStart = currentPosition * 3 + 1;
		var currentPicEnd = currentPosition * 3 + 3;

		if (currentPicEnd > scrollContainerChildren) {
			currentPicEnd = scrollContainerChildren;
		}

		$('#thumbPageInfo').text('Bilder ' + currentPicStart + ' bis ' + currentPicEnd + ' von ' + scrollContainerChildren);
	});

	$('#thumbSlideLeft').click(function(e) {
		e.preventDefault();

		--currentPosition;

		if (currentPosition < 0) {
			currentPosition = 0;
		}

		$('#thumbScrollContainer').stop().animate({
			'left': -(currentPosition * 138 * 3) + 'px'
		});

		var currentPicStart = currentPosition * 3 + 1;
		var currentPicEnd = currentPosition * 3 + 3;

		$('#thumbPageInfo').text('Bilder ' + currentPicStart + ' bis ' + currentPicEnd + ' von ' + scrollContainerChildren);
	});

	$('.thumbimage').unbind('mouseenter').bind('mouseenter', function() {
		$(this).stop().animate({
			'opacity': '0.75'
		}, 200);
	});

	$('.thumbimage').unbind('mouseleave').bind('mouseleave', function() {
		$(this).stop().animate({
			'opacity': '1'
		});
	});

	// Content tabs
	$('#item_menu a').click(function(e) {
		e.preventDefault();

		$('#description_tabs .active,#item_menu .active').removeClass('active');

		$('#' + $(this).attr('rel')).addClass('active');
		$(this).addClass('active');
	});
	// Open tab on hash
	var url = window.location.href;
	var idx = url.indexOf("#")
	var hash = idx != -1 ? url.substring(idx + 1) : "";

	if (hash === 'additionalitems') {
		$('#item_tab_trigger_2').trigger('click');
	} else if (hash === 'spareparts') {
		$('#item_tab_trigger_4').trigger('click');
	} else if (hash === 'realfriends') {
		$('#item_tab_trigger_3').trigger('click');
	}


	// left menu tabs
	$('#item_left_menu a').click(function(e) {
		e.preventDefault();

		$('#left_description_tabs .active,#item_left_menu .active').removeClass('active');

		$('#' + $(this).attr('rel')).addClass('active');
		$(this).addClass('active');
	});

	$('#image_detailed_container_inner').css('width', bigImageCount * bigImageWidth + 'px');

	// Generate barcode
	if ($('#itemBarcode').length > 0) {
		$('#itemBarcode').barcode($('#itemBarcode').text(), 'ean13');
	}

	// Image slider buttons
	slide_images_timout = setTimeout(slide_images, 5000);
	$('#image_detailed_backward').bind('click', function(e) {
		e.preventDefault();
		
		slide_images(true);
	});
	$('#image_detailed_forward').bind('click', function(e) {
		e.preventDefault();
		
		slide_images(false);
	});



	// show shipping calculation
	$('#select_shipping_calc').change(function() {
		var country_id = $(this).val();
		alert(country_id);

	});

	$('#back_button').click(function() {
		window.history.back();
	});

	$('#graduated_price_title').click(function() {
		$('#graduated_price_content').slideToggle();

		if ($('.graduated_price_title_arrow').hasClass('graduated_price_title_arrow_open')) {
			$('.graduated_price_title_arrow').removeClass('graduated_price_title_arrow_open');
			$('.graduated_price_title_wrapper').removeClass('graduated_price_title_wrapper_open');
		} else {
			$('.graduated_price_title_arrow').addClass('graduated_price_title_arrow_open');
			$('.graduated_price_title_wrapper').addClass('graduated_price_title_wrapper_open');
		}
	});


	//$('#qrcode').qrcode({width: 100,height: 100,text: window.location.href});


	$('.graduatedCart').click(function() {
		var quantity = $(this).attr('rel');
		var sample_price = $(this).attr('sample_price');
		var price = $(this).attr('price');
		var bargain_price = $(this).attr('bargain_price');
		$('.amountbox').val(quantity);
		$('.amountbox').css({'background-color': '#ff8c26', 'font-weight': 'bold'});

		if (bargain_price != '0,00') {
			$('.price').empty().html(bargain_price + ' &euro;');
			$('.old_price').empty().html(price + ' &euro;');
		}
		else {
			$('.price').empty().html(price + ' &euro;');
			$('.old_price').empty().html();
		}
		$('.sample_price').empty().html(sample_price);

		$('#graduated_price_content').slideToggle();
	});



	// Slider
	var ItemWidth = 200;
	var ItemMargin = 25;
	var HolderW = 900;

	var ScrollMaxLeft = -(((ItemWidth + ItemMargin) * ItemsMax) - HolderW);

	$('.image_slider_content_inner_wrapper').css({
		'width': ((ItemWidth + ItemMargin) * ItemsMax) + 'px'
	});

	var ScrollPosition = 0;

	$('.slider_image_slider_trigger_right').click(function() {
		++ScrollPosition;
		++ScrollPosition;
		++ScrollPosition;
		++ScrollPosition;

		var scrollTo = -((ItemWidth + ItemMargin) * ScrollPosition);
		if (scrollTo < ScrollMaxLeft) {
			scrollTo = ScrollMaxLeft - ItemMargin;

			--ScrollPosition;
			--ScrollPosition;
			--ScrollPosition;
			--ScrollPosition;
		}

		$('.image_slider_content_inner_wrapper').animate({
			'left': scrollTo + 'px'
		});
	});

	$('.slider_image_slider_trigger_left').click(function() {
		--ScrollPosition;
		--ScrollPosition;
		--ScrollPosition;
		--ScrollPosition;

		var scrollTo = -((ItemWidth + ItemMargin) * ScrollPosition);
		if (scrollTo > 0) {
			scrollTo = 0;

			++ScrollPosition;
			++ScrollPosition;
			++ScrollPosition;
			++ScrollPosition;
		}

		$('.image_slider_content_inner_wrapper').animate({
			'left': scrollTo + 'px'
		});
	});




	$('#item_cart_button').click(function(e) {
		e.preventDefault();
		if (parseInt($('#input_min_quantity').val()) <= 1 || parseInt($('.amountbox').val()) >= parseInt($('#input_min_quantity').val())) {
			if (parseInt($('#input_buy_quantity').val()) <= 1 || (parseInt($('.amountbox').val()) % parseInt($('#input_buy_quantity').val())) == 0) {
				$('#error_message_two').css({'display': 'none'});
				$('#error_message_one').css({'display': 'none'});
				warenkorbflitzer();

				//$('#item_buy_form').submit();
			}
			else {
				$('#error_message_one').css({'display': 'none'});
				$('#error_message_two').css({'display': 'inline'});
			}
		}
		else {
			$('#error_message_two').css({'display': 'none'});
			$('#error_message_one').css({'display': 'inline'});
		}
	});

	$('.close_message_box').click(function(e) {
		e.preventDefault();
		$('#error_message_two').css({'display': 'none'});
		$('#error_message_one').css({'display': 'none'});
	});

	function warenkorbflitzer() {
		var first_article_image, image_id, image_url, image_position;

		first_article_image = $('.detailImage:first');
		image_id = Math.floor(Math.random() * (999 - 100 + 1) + 100);
		image_url = first_article_image.css('background-image').replace('url("', '').replace('")', '');
		image_position = first_article_image.offset();

		$('<img>').attr({
			'src': image_url,
			'id': image_id
		}).css({
			'position': 'absolute',
			'width': '330px',
			'height': '330px',
			'left': ($(window).width() - $('#wrapper').outerWidth()) / 2 + 70 + 'px',
			'top': image_position.top + 70 + 'px',
			'opacity': '0.8',
			'zIndex': '999999'
		}).appendTo('body');

		$('#' + image_id).animate({
			'width': (first_article_image.outerWidth() / 10) + 'px',
			'height': (first_article_image.outerHeight() / 10) + 'px',
			'top': (20 + $(window).scrollTop()) + 'px',
			'left': (($(window).width() - $('#wrapper').outerWidth()) / 2 + 780) + 'px'
		}, 1750, function() {
			$(this).empty().remove();
			$('#item_buy_form').submit();
		});
	}


	/*$('.variant_selectbox_1').change(function(e) {
	 alert(var2[$(this).val()].id);
	 
	 });
	 
	 $('#variant_selection_box').change(function(e) {
	 window.location.href = '/index.php?item_id=' + $(this).val();
	 });*/


	$('.item_variant_box').click(function(e) {
		e.preventDefault();

		$('#variant_selection_box').slideToggle();
		if ($('.item_variant_box').hasClass('item_variant_box_active')) {
			$('.item_variant_box').removeClass('item_variant_box_active');
		}
		else {
			$('.item_variant_box').addClass('item_variant_box_active');
		}

	});

	$('.item_assign_box').click(function(e) {
		e.preventDefault();

		$('.item_selection_box').slideToggle();
		if ($('.item_selection_box').hasClass('item_selection_box_active')) {
			$('.item_selection_box').removeClass('item_selection_box_active');
		}
		else {
			$('.item_selection_box').addClass('item_selection_box_active');
		}

	});

	$('.hover-star').rating({
		focus: function(value, link) {
			var tip = $('#hover-test');
			tip[0].data = tip[0].data || tip.html();
			tip.html(link.title || 'value: ' + value);
		},
		blur: function(value, link) {
			var tip = $('#hover-test');
			$('#hover-test').html(tip[0].data || '');
		}
	});


        $('.print_button').click(function(e) {
            e.preventDefault();
            fenster = window.open(window.location + "?action=print", "Popupfenster", "width=800,height=800,resizable=yes");
            fenster.focus();
            fenster.print();
            return false;
        });



});