/* ORDER */
jQuery(function(){
	
	$("#total").text("Du har inga varor i kundvagnen");
	
	var cookieArr = new Array();
	var sumtotal = 250;
	var row;

	$('#added_popup').hide();

	$('#direktkontakt').hide();
	
	$('.direktkontakt_action').click(function(){
	$('#direktkontakt').show();
	});
	
	$('#close_direktkontakt').click(function(){
	$('#direktkontakt').hide();
	});
	
	$('#confirm').hide();
	
	if( window.location.href.indexOf( '#wpcf7' ) != -1 ) {
    $('#order_complete').show();
	}else{
	$('#order_complete').hide();
	}
	
	$('#shipping').text(sumtotal+' kr');
	
	$('.capsulecontent .button').click(function(){
		$('#added_popup').show().animate({opacity: 1.0}, 1000).fadeOut(250);
		$val = $(this).parent().children().children('.input_quantity').val();
		$id = $(this).parent().children("form").attr('id');
		$.cookie($id, $val);
		cookieArr.push($id);
		getcookievalue();
	})
	
	$('#close_conf').click(function(){
	$('#added_popup').hide();
	});
	
	$('.cartitem_button').click(function(){
		$('#confirm').show();
		row = $(this).attr('id');
		row = row.replace('remove-', '')
		$('#post-'+row).fadeTo("fast", 0.25);
	});
	
	$('#yes').click(function(){
		$('#post-'+row).hide();
		$.cookie(row, 0);
		var newsumtotal = $('#sum').text() - $('#total-'+row).text();
		$('#sum').text(newsumtotal);
		$('#confirm').hide();
		$('#post-'+row+' .sort').removeClass('sort');
		location.reload();
	})
	
	$('#no').click(function(){
		$('#post-'+row).fadeTo("fast", 1);
		$('#confirm').hide();
	})
	
	$('#submitorder').click(function(){
		$('#order_complete').show();
	});
	
	$('#close').click(function(){
		$('#order_complete').hide();
		window.location.href = 'http://www.espressoblue.se/shop/';
		return false;

	})
	
	$('.input_quantity').change(function(){
		var oldsumtotal = 250;
		var id = $(this).attr('id');
		id = id.replace('quantity-', '')
		
		$('.total').each(function(index) {
			if ($(this).attr('id') != 'total-'+id){
				oldsumtotal = parseInt($(this).text()) + oldsumtotal;
			}
  		});
		
		var newamount = $(this).val();
		
		var price = $("#price-"+id).text();
		$("#total-"+id).text(price*newamount);
		var newsumtotal = oldsumtotal + (price*newamount);
		$('#sum').text(newsumtotal);
		
		$.cookie(id, newamount);
			
	});
	
	$('#submitorder').click(function(){
		var $i = 0;
		$('.sort').each(function(index) {
			if ($i == 0) {
			 	var artnr = $(this).text();
				$('#cart').text($('#cart').text() + 'Art.nr: ' + artnr + ' - ');
				$i++;
			} else if ($i == 1){
				var title = $(this).text();
				$('#cart').text($('#cart').text() + title + ' ');
				$i++;
			} else {
				var amount = $(this).val();
				$('#cart').text($('#cart').text() + amount + ' st\n');
				$i = 0;
			}
  		});
	})

	function getcookievalue() {
		var arrVal = cookieArr;
        var uniqueArr = [];
        for (var i = arrVal.length; i--; ) {
            var val = arrVal[i];
            if ($.inArray(val, uniqueArr) === -1) {
                uniqueArr.unshift(val);
            }
			
        }
		var total = 0;
		for (var i = uniqueArr.length; i--; ) {
			total = total + parseFloat($.cookie(uniqueArr[i]));
        }
		if (total == 1){
        	$("#total").text("Du har "+ total +" vara i kundvagnen");
		} else {
			$("#total").text("Du har "+ total +" varor i kundvagnen");
		}
		
		$('.buttonholder_right .button').click(function(){
			window.location.href = window.location.href+'?order&checkout=true&array='+uniqueArr;
			return false;
		})
	};
	
	$('.input_quantity').each(function(index) {
		var id = $(this).attr('name');		
   		$("#quantity-"+id).val($.cookie(id));
		var price = $("#price-"+id).text();
		var total = $("#total-"+id).text(price*$.cookie(id));
		sumtotal = sumtotal + (price*$.cookie(id));
		$('#sum').text(sumtotal);
		
  	});
  
})

/* FORMS */
function resetFields(whichform) { 
  for (var i=0; i<whichform.elements.length; i++) { 
    var element = whichform.elements[i]; 
    if (element.type == "submit") continue; 
    if (!element.defaultValue) continue; 
    element.onfocus = function() { 
      if (this.value == this.defaultValue) { 
      this.value = ""; this.style.color='#404040';
      } 
    } 
    element.onblur = function() { 
      if (this.value == "") { 
      this.value = this.defaultValue; this.style.color='#AAA'; 
      } 
    } 
  } 
} 

window.onload = prepareForms;
function prepareForms() { 
  for (var i=0; i<document.forms.length; i++) { 
    var thisform = document.forms[i]; 
    resetFields(thisform); 
  }  
}
