jQuery.fn.place = function (absolute) {
	return this.each(function () {
		var t = $(this);

		t.css({
			position:	absolute ? 'absolute' : 'fixed',
			top:		'104px',
			left:		'50%',
			zIndex:		'99'
		}).css({
			marginLeft:	'-' + (t.outerWidth() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginLeft:	parseInt(t.css('marginLeft'), 10) + $(window).scrollLeft()
			});
		}
	});
};

$(document).ready(function() {
	if($('#shop_form').hasClass('s1')) {

		
	/*
		$("a.choose_material").click(function(evt){
			evt.preventDefault();
			$('#material').val($(this).attr("href").split("#")[1]);
		});
	*/
	
		$("a.choose_knob").click(function(evt){
			evt.preventDefault();
			$('#knob').val($(this).attr("href").split("#")[1]);
			if($('#material').val() != 0 && $('#knob').val() != 0) {
				if($('#material').val() == 4 && $('#color').val() == '') {
					alert('Du måste välja en färg');
				}
				else {
					$('#shop_form').submit();			
				}
			}
			else {
				alert('Du måste välja ett material och grepp innan du kan gå vidare.')
			}
		});

		$.scrollTo(0);
		$('.colorpicker').css('width', '100px');

		$('#material').val($('.materialthumb.first').attr('id').replace('materialthumb_',''));
	
		$('.materialthumb').click(function(event) {
			select_material($(this));
		});
		
		function select_material(elm) {

			var id = $((elm)).attr('id').replace('materialthumb_','');
			$('#material').val(id);

			var currentMaterialThumb = $(elm);
			var currentMaterialBlock = '.' + $(elm).attr('id').replace('materialthumb_','material_');
			
			$('.materialthumb').not(elm).removeClass('active');
			$.scrollTo( $('.materialthumb_wrapper'),500, {offset:-112});
			if($('.materialblock:visible').length > 0) {
				$('.materialblock:visible').not(currentMaterialBlock).fadeOut(250, function() {
					$(currentMaterialThumb).addClass('active');
					$(currentMaterialBlock+':hidden').slideDown(500);
				});			
			}
			else {
				$(currentMaterialThumb).addClass('active');
				$(currentMaterialBlock+':hidden').slideDown(500);
			}
		}

/* Inspirationsbilderna på greppen */

		$('#knobInspirationContainer').appendTo('body');

		$('.knobInspirationGallery').each(function(index) {
    		var thisGalleryImg = '.' + $(this).attr('id').replace('knobInspirationGallery', 'knob_');
			$(thisGalleryImg).children('.knob_img').addClass('knobHasGallery');

    		var thisGallery = '#' + $(this).attr('id');
    		var thisGalleryWrapper = '#' + $(this).attr('id').replace('knobInspirationGallery', 'knobInspirationGalleryWrapper');
			$(thisGallery).jCarouselLite({
				btnNext: thisGalleryWrapper + ' .next',
				btnPrev: thisGalleryWrapper + ' .prev',
				visible: 1
			});
  		});

		$('.knobInspirationContainer, .knobInspirationGalleryWrapper').hide();

		$('.knobHasGallery .knobZoomIcon').fadeTo(0, 0.4);

		$('.knobHasGallery').hover(function() {
			$(this).children('.knobZoomIcon').fadeTo(100, 0.8);
		}, function() {
			$(this).children('.knobZoomIcon').fadeTo(100, 0.4);
		});

		$('.knobHasGallery').click(function() {
			$('.knobInspirationBg, .knobInspirationWrapper').css('visibility', 'visible');

			var currentGallery = '#' + $(this).parent().attr('class').replace('knobblock knob_', 'knobInspirationGalleryWrapper');

			$('#knobInspirationContainer').fadeIn(200, function() {
				$('#knobInspirationWrapper').place('absolute');
				$(currentGallery).show();
			});

			$.scrollTo(0, 400);
		});

		$('.knobInspirationClose span, #knobInspirationBg').click(function() {
			$('#knobInspirationContainer').fadeOut(200, function() {
				$('.knobInspirationGalleryWrapper').hide();
			});
		});

		$('.prev img, .next img').fadeTo(0, 0.4);

		$('.prev, .next').hover(function() {
			$(this).children('img').fadeTo(200, 0.8);
		}, function() {
			$(this).children('img').fadeTo(200, 0.4);
		});
	}
});
