discount = 1;

function calculateTotalPrice() {
	var total = 0;
	var count = 0;
	for(i=0;i<product_price.length;i++)
	{
		total += parseInt(product_price[i]*$("#order-n-" + i).val());
		count += parseInt($("#order-n-" + i).val());
	}
	
	if(count > 99)
	{
		discount = 0.89;
	}
	else if(count > 24)
	{
		discount = 0.92;
	}
	else
	{
		discount = 1;
	}
	
	return total;
}

function switchEdition(edition) {
	$(".edition-image").each(function() {
    	$(this).attr("src",$(this).attr("src").substring(0, $(this).attr("src").length-5) + edition + ".jpg")
  	});
}

function openordermenu() {
	$("#dimmer").css("height",$(document).height())
	
	$("#ordermenu").css("left",($(window).width()/2 - $("#ordermenu").width()/2))
	$("#ordermenu").css("display","block")
	$("#ordermenu").css("top",$(window).scrollTop()+$(window).height()*0.05)
	
	$('#dimmer').fadeTo(500, 0.7);
	
	if (Modernizr.csstransitions) {
		if(Modernizr.csstransforms) {
			$("#ordermenu").css(Modernizr.prefixed("transform"),"scale(1.0)")
		}
		$("#ordermenu").css("opacity","1")
	}
	else
	{
		$("#ordermenu").fadeIn(500);
	}
}

function closeordermenu() {
	$('#dimmer').fadeTo(400, 0);
	
	if(Modernizr.csstransforms) {
		$("#ordermenu").css(Modernizr.prefixed("transform"),"scale(0.4)")
		$("#ordermenu").css("opacity","0")
			
		setTimeout('$("#ordermenu").css("display","none")',500)
		setTimeout('$("#dimmer").css("height",0)',400)
	}
	else
	{
		$("#ordermenu").css("display","none");
		$("#dimmer").css("height",0);
	}
}

function resize() {
	$("#top").css("height",Math.max($(window).height(),$("#top-lower").offset().top+160));

	$("#dimmer").css("width",$(document).width())
}

$(window).load(function () {
	resize()

	if (typeof collection != 'undefined')
	{
		if(collection == "challenges")
		{
			setTimeout("$(window).scrollTop($('#challenges').offset().top);",1)
		}
		else if(collection == "buffe")
		{
			setTimeout("$(window).scrollTop($('#buffe').offset().top);",1)
		}
		else if(collection == "accent")
		{
			setTimeout("$(window).scrollTop($('#accent').offset().top);",1)
		}
	}
	
	
	if (Modernizr.csstransitions)
	{
		$("#wrapper").css("opacity","1");
	}
	else
	{
		$("#wrapper").fadeIn(2500);
	}
	
	
	$("#loading-spinner").css("display","none")
});


$(function() {
	$("#loading-spinner").css("display","block")
	$("#loading-spinner").css("left",$(window).width()/2 - $("#loading-spinner").width()/2)
	$("#loading-spinner").css("top",$(window).height()*0.3)

	$(window).resize(function() {
		resize()
	});
    
    collectionsactive = false;
    $('html, body').mousemove(function(e){
		if(collectionsactive)
		{
			if(e.pageX > $("#collections-link").offset().left && e.pageX < $("#collections-link").offset().left+$("#collections-link").width() && e.pageY > $("#collections-link").offset().top && e.pageY < $("#collections-dropdown").offset().top + $("#collections-dropdown").height()+35)
			{
				// WTF?!
			}
			else 
			{
				collectionsactive = false;
				$("#collections-dropdown").stop(true,true).fadeTo("fast",0)
			}
		}
		else if(e.pageX > $("#collections-link").offset().left && e.pageX < $("#collections-link").offset().left+$("#collections-link").width() && e.pageY > $("#collections-link").offset().top && e.pageY < $("#collections-link").offset().top + $("#collections-link").height())
		{
    		$("#collections-dropdown").css("left",$("#collections-link").offset().left + 10)
    		$("#collections-dropdown").css("top",$("#collections-link").offset().top + $("#collections-link").outerHeight(true) + 12)
    	
    		collectionsactive = true;
    		$("#collections-dropdown").stop(true,true).fadeTo("fast",1)
		}
    });
    
    $("#contact-send").click(function () {
    	var xmlhttp = new XMLHttpRequest();
    	
    	xmlhttp.onreadystatechange = function() {
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == "success") {
					$("#contact-finished").html("<h6>Tack, vi hör av oss snarast!</h6>")
				}
				else {
					$("#contact-message").html("Du måste skriva ett giltigt namn och telefonnummer/email!" + xmlhttp.responseText);
				}
    		}
  		}
  		
  		xmlhttp.open("GET","contact.php?who=" + $("#contact-input-who").val() + "&how=" + $("#contact-input-how").val(),true);
		xmlhttp.send();
    });
    
    $("#contact-input-who").focus(function () {
    	if($("#contact-input-who").val()=="Namn")
    	{
    		$("#contact-input-who").val("")
    		$("#contact-input-who").removeClass("text-dimmed")
    	}
    });
    
    $("#contact-input-who").blur(function () {
    	if($("#contact-input-who").val()=="")
    	{
    		$("#contact-input-who").val("Namn")
    		$("#contact-input-who").addClass("text-dimmed")
    	}
    });
    
    $("#contact-input-how").focus(function () {
    	if($("#contact-input-how").val()=="E-mail eller telefonnummer")
    	{
    		$("#contact-input-how").val("")
    		$("#contact-input-how").removeClass("text-dimmed")
    	}
    });
    
    $("#contact-input-how").blur(function () {
    	if($("#contact-input-how").val()=="")
    	{
    		$("#contact-input-how").val("E-mail eller telefonnummer")
    		$("#contact-input-how").addClass("text-dimmed")
    	}
    });
    
    $("#dimmer").click(function () {
    	closeordermenu()
    });
    
    $("#elsewhere-check").change(function (){
    	if($("#elsewhere-check").attr('checked'))
    	{
    		$('#elsewhere').css('height','auto');
    	}
    	else
    	{
    		$('#elsewhere').css('height',0);
    	}
    });
    
    $("#order-send").click(function () {
    	var checkbox = "false"
    	if($("#elsewhere-check").attr('checked'))
    	{
    		checkbox = "true"
    	}
    	
    	products = "&product-n=" + product_price.length + "&totalprice=" + parseInt(calculateTotalPrice()*discount);

		for(i=0;i<product_price.length;i++)
		{
			products += "&product-" + i + "=" + $("#order-n-" + i).val();
		}

		$.ajax({
			type: "POST",
   			url: "order.php",
   			data: "business-name=" + $("#order-business-name").val() +
   			"&name=" + $("#order-name").val() +
   			"&adress=" + $("#order-adress").val() +
   			"&zip-code=" + $("#order-zip-code").val() +
   			"&city=" + $("#order-city").val() +
   			"&phone-number=" + $("#order-phone-number").val() +
   			"&org-number=" + $("#order-org-number").val() +
   			"&elsewhere=" + checkbox +
   			"&elsewhere-business-name=" + $("#order-elsewhere-business-name").val() +
   			"&elsewhere-name=" + $("#order-elsewhere-name").val() +
   			"&elsewhere-adress=" + $("#order-elsewhere-adress").val() +
   			"&elsewhere-zip-code=" + $("#order-elsewhere-zip-code").val() +
   			"&elsewhere-city=" + $("#order-elsewhere-city").val() +
   			products,
			success: function(response){
				$("#order-send-message").html(response)
			}
		});
    });
    
    $(".order-number").change(function (){
    	var value = $(this).val()
    	if(value == "")
    	{
    		value = 0;
    		$(this).val(0)
    	}
    	var id = $(this).attr('id')
    	id = id.substring(id.lastIndexOf('-')+1)
		$("#order-n-total-" + id).html(product_price[id]*$(this).val())
		
		var price = calculateTotalPrice();
    	$("#order-part").html(parseInt(price))
    	$("#order-total-discount-rate").html(parseInt(100-discount*100))
    	$("#order-total").html(parseInt((price*discount)))
    });
});
