var currCutId;
var currArtId;
var realSum;

$(document).ready(function() {
	realSum = $('.sum').html();
	calcVersand();

	
	if ($("#showcase").length > 0) {
		$('#female').attr('checked', true);
		
		$('li.male').hide();
		$('li.children').hide();
		calcArticles("female", collection);
		$(".changelisten").click(function() {
			var gender = "female";
			if ($(this).hasClass('male')) {
				gender = "male";
			}
			if ($(this).hasClass('children')) {
				gender = "children";
			}
			calcArticles(gender, collection);
		});
		
		$("ul.cuts > li").click(function() {
			showArticles($(this).attr('id').substring(3));
		});
		
		$("#sizeinfo").click(function() {
			var gender = "female";
			if ($('li.selectedCut').hasClass('male')) {
				gender = "male";
			} else if ($('li.selectedCut').hasClass('child')) {
				gender = "children";
			}
			$.post("/shopbroker.php", { action: "getSizetable", gender: gender }, function(data) {
				var result = eval('(' + data + ')');
				$("#sizetable").html(result.sizetable);
			});
		})
		
	}
	
	$('.versand').click(function() {
		calcVersand();
	});

	$("#size").click(function() {
		selectedSize = $("#size").val();
		if(selectedSize == "xs" || selectedSize == "xl" || selectedSize == "xxl") {
			$("#sizewarning").html("* Die Bestellung dieser Grösse ist mit einer längeren Lieferzeit verbunden");
			$("#sizewarning").show();
		} else {
			$("#sizewarning").html("");
			$("#sizewarning").hide();
		}
	});
});

function calcArticles(gender, collection) {
	if (collection == 1) {
		defaultCutIdFemale = 9;
		defaultCutIdMale = 6;
	} else if (collection == 2) {
		defaultCutIdFemale = 12;
		defaultCutIdMale = 16;
	} else {
		defaultCutIdFemale = 12;
		defaultCutIdMale = 16;
	}
	if (gender == "male") {
		$('li.female').slideUp();
		$('li.child').slideUp();
		$('.typepullover').slideDown();
		$('li.male').slideDown();
		showArticles(defaultCutIdMale);
	} else if(gender == "children") {
		$('li.female').slideUp();
		$('li.male').slideUp();
		$('.typepullover').slideUp();
		$('li.child').slideDown();
		showArticles(20);
	} else {
		$('li.male').slideUp();
		$('li.child').slideUp();
		$('.typepullover').slideDown();
		$('li.female').slideDown();
		showArticles(defaultCutIdFemale);
	}
}

function showArticles(cutId) {
	if (cutId == currCutId) {
		return;
	}
	$("#sizetable").html('');
	$('.cut').removeClass('selectedCut');
	$('#cut' + cutId).addClass('selectedCut');
	currCutId = cutId;
	$("#articlethumbs").html('');
	$("#showcase").html('');
	$.post("/shopbroker.php", { action: "getArticles", id: cutId, collectionId: collection}, function(data) {
            var result = eval('(' + data + ')');
            if (result.success) {
		
		for(var i=0; i<result.articles.length; i++) {
			var onclick = ' onclick="chooseArticle(' +result.articles[i].id+ ');" ';
			var articlestring = '';
			if (result.articles[i].discount == 10) {
				articlestring += '<div ' +onclick+ ' id="discount10" class="discountimage"></div>';
			} else if (result.articles[i].discount == 20) {
				articlestring += '<div ' +onclick+ ' id="discount20" class="discountimage"></div>';
			}
			articlestring += '<img ' + onclick + ' class="articlethumb selectable" id="thumb' +result.articles[i].id+ '" src="/images/articles/' + result.articles[i].cutId + '/thumb' + result.articles[i].id + '.jpg" />';
			$("#articlethumbs").append(
				articlestring
			);
		}
		chooseArticle(result.articles[0].id);
		
		for(var i=0; i<result.articles.length; i++) {
			preloadImage(result.articles[i].id);
		}
            } else {
                showMessage(result.message, false);
            }
        });
}

function preloadImage(id) {
	preloading = new Image(); 
	preloading.src = '/images/articles/' + currCutId + '/' + id + '.jpg';
}

function chooseArticle(id) {
	preloadImage(id);
	currArtId = id;
	$('#addToCart').hide();
	
	$('#ordermessage').html('');
	$('#fulldescription').html('');
	$('#price').html('');
	$('#size').html('');
	$('#showcase li').css('width', 'auto');
	$('#showcase li').css('height', 'auto');
	$('#showcase').css('width', 'auto');
	$('#showcase').css('height', 'auto');
	$('.showcaseButton').remove();
	
	$('.articlethumb').removeClass('selectedArticle');
	$('#thumb' + id).addClass('selectedArticle');
	$('#showcase').html('<ul><li><img class="showcasepic" alt="" src="/images/articles/' +currCutId+ '/' + id + '.jpg" id="showcase' +id+ '" /></li></ul>'
	);
	
	$.post("/shopbroker.php", { action: "getArticle", articleId: id }, function(data) {
		var article = eval('(' + data + ')');
		$('#fulldescription').html(article.cutname + ': ' + article.articleDescription);
		
		if (article.discount > 0) {
			$('#price').html('<span class="oldprice">' + article.oldprice + '.-</span><span class="discountprice">' + article.price + '.-</span>');
		} else {
			$('#price').html(article.price + '.-');
		}
		
		if (article.xs == 1) {
			$('#size').append('<option value="xs">xs*</option>');
		}
		if (article.s == 1) {
			$('#size').append('<option value="s">s</option>');
		}
		if (article.m == 1) {
			$('#size').append('<option value="m" selected="selected">m</option>');
		}
		if (article.l == 1) {
			$('#size').append('<option value="l">l</option>');
		}
		if (article.xl == 1) {
			$('#size').append('<option value="xl">xl*</option>');
		}
		if (article.xxl == 1) {
			$('#size').append('<option value="xxl">xxl*</option>');
		}
		if (article.cut104 == 1) {
			$('#size').append('<option value="104">104</option>');
		}
		if (article.cut116 == 1) {
			$('#size').append('<option value="116">116</option>');
		}
		if (article.cut128 == 1) {
			$('#size').append('<option value="128">128</option>');
		}
		if (article.cut140 == 1) {
			$('#size').append('<option value="140">140</option>');
		}
		if (article.cut152 == 1) {
			$('#size').append('<option value="152">152</option>');
		}
		
		if (article.soldout == 0) {
			$('.soldout').html('');
			$('#addToCart').show();
		} else {
			$('#addToCart').hide();
			$('.soldout').html('Ausverkauft!');
		}
		var imagecount = parseInt(article.imagecount);
		imagecount--;
		var preloading;
		for (var imgnr=1; imgnr<=imagecount;imgnr++) {
					
			$('#showcase ul').append('<li><img class="showcasepic" alt="" src="/images/articles/' +currCutId+ '/' + id + '_' + imgnr + '.jpg" /></li>');
			preloading = new Image(); 
			preloading.src = '/images/articles/' + currCutId + '/' + id + '_' + imgnr + '.jpg';
			if (imgnr == imagecount) {
				chooseArticleCheckSize();
			}
		}
		
	});
	$('#addToCart').show();
}
	function chooseArticleCheckSize() {
		width = $('.showcasepic').width();
		height = $('.showcasepic').height();
		
		if (width > 100 || height > 100) {
			chooseArticleNextStep();
		} else {
			setTimeout("chooseArticleCheckSize()", 200);
		}
	}

	function chooseArticleNextStep() {
		width = $('.showcasepic').width();
		height = $('.showcasepic').height();
		
			$('#showcase li').css('width', width);
			$('#showcase li').css('height', height);
			$('#showcase').css('width', width);
			$('#showcase').css('height', height);
			
			$("#showcase").easySlider();
			$('#nextBtn').css('left', width + 4);
	}

function addToCart() {	
	$.post("/shopbroker.php", { action: "addToCart", articleId: currArtId, cutId: currCutId, size: $("#size option:selected").attr('value') }, function(data) {
		var answer = eval('(' + data + ')');
		if (answer.success) {
			var a = new Date();
			a = new Date(a.getTime() +1000*60*60*24*2);
			document.cookie = 'cart' +answer.secret+ '=1; path=/; expires='+a.toGMTString()+';'; 
			$('#ordermessage').html(answer.message);
			showMessage(answer.message, true);
		} else {
			showMessage(answer.message, false);
			$('#ordermessage').html(answer.message);
		}
	});
}

function calcVersand() {
	if ($('.versand:checked').attr('value') == 'post') {
		$('.sum').html(parseInt(realSum)+7);
		$('#reqaddress').show();
	} else {
		$('.sum').html(parseInt(realSum));
		$('#reqaddress').hide();
	}
}

function checkOrder() {
	name = $('.cartname').attr('value');
	email = $('.cartemail').attr('value');
	cartaddress = $('.cartaddress').attr('value');
	post = $("#post").attr("checked");
	
	if (name.length < 2) {
		showMessage("Bitte Name angeben", false);
		return false;
	}
	var reg = /^.+\@.+\..+$/;	
	if(reg.test(email) == false) {
		showMessage('Du hast eine ungültige E-Mail Adresse angegeben', false);
		return false;
	}
	if (post && (cartaddress.length < 5)) {
		showMessage('Bitte Adresse angegeben', false);
		return false;
	}
	return true;
}
