$(document).ready(function() {
	img = $("#rightbar a[href='http://www.christus-treff-marburg.de/Projekte-Quo_Vadis.html'] img");
	img.before('<div id="quovadis-banner"></div>');
	img.remove();
	$("#quovadis-banner").flash({
        src: '/_qouvadis/banner.swf',
        width: 150,
        height: 380
    });
	
	$("#qouvadis").click(function(){
		$("#qouvadiswrap").show();
		return false;
	});
	$("#close a").click(function(){$("#qvadiswrap").hide(); return false;});
	
	$('#dialog').jqm({modal: true, overlay: 20});
	
    $("#articleform").submit(function () { 
		var values = $("#articleform").serialize();
		getCart(values);
		return false;
    });
    
    $("#sendorder").click(function(){
    	var values = $("#orderform").serialize();
		$.post(
			"_qouvadis/xhr.php", 
			values,
			function(data){
				if(data.error){
					$("#error").html(data.error);	
					$('#dialog').jqmShow();
					return false;
				}
				document.forms[1].reset();
				
				$("#cartitems tbody tr").each(function(){ $(this).remove(); });
				addEmptyRow();
				$("#slider a[href='#send']").click();
			}, 
			'json'
		);
    });

 	if($("#emptycart_msg").html() != null){
 		$("#next_button").attr("disabled", true);
 		$("#next_button").addClass("disabled");
 	}
 	
 	$("#next_button").click(function(){
 		if($("#cartitems tbody tr[id^='item_']").length == 0)
 			return false;

 		$("#slider a[href='#order']").click();
 	});
 	
    bindRemoveButtons();
    bindHoverEffect();
});

function getCart(values){
	$.post(
		"_qouvadis/xhr.php", 
		values,
		function(data){
			computeResult(data);
		}, 
		'json'
	);
}

function bindRemoveButtons(){
	$(".remove").click(function () { 
		id = $(this).attr('rel');
		getCart('action=remove&id=' + id);
    });
}

function bindHoverEffect(){
    $("#cartitems tbody tr").hover(function(){
	  $(this).addClass("hover");
	},function(){
	  $(this).removeClass("hover");
	});
}

function computeResult(data){
	if(data.error){
		$("#error").html(data.error);
		$('#dialog').jqmShow();
		
		return false;
	}

	if(data.add){
		if($("#emptycart_msg").html() != null){
			$("#emptycart_msg").remove();
			$("#next_button").attr("disabled", false);
			$("#next_button").removeClass("disabled");
		}
		index = data.index;
		var row  = '<tr id="'+index+'" class="">';
			row += '<td>'+data.add.title+'</td>';
			row += '<td>'+data.add.author+'</td>';
			row += '<td>'+data.add.publisher+'</td>';
			row += '<td>'+data.add.isbn+'</td>';
			row += '<td><img src="_qouvadis/images/delete.png" border="0" alt="l&ouml;schen" class="remove" rel="'+index+'" /></td>';
			row += '</tr>';
		$("#cartitems tbody").append(row);
		
		$("#"+index).css({backgroundColor:'#ff9'}).animate({backgroundColor: '#fff'}, "slow"); 
		
		bindRemoveButtons();
		bindHoverEffect();
		
		document.forms[0].reset();
		
	} else {
		$("#"+data.remove).removeClass("hover");
		$('#'+data.remove).fadeOut("slow", function(){$('#'+data.remove).remove(); addEmptyRow();});
	}
}

function addEmptyRow(){
	if($("#cartitems tbody tr[id^='item_']").length == 0 && $("#emptycart_msg").html() == null){
			var row  = '<tr id="emptycart_msg">';
				row += '<td colspan="5">Bitte f&uuml;llen Sie das Formular aus und klicken auf den (+)Button.</td>';
				row += '</tr>';
			$("#cartitems tbody").append(row);
			
			$("#"+index).css({backgroundColor:'#ff9'}).animate({backgroundColor: '#fff'}, "slow"); 
			
			$("#next_button").attr("disabled", true);
			$("#next_button").addClass("disabled");
		}
}

