var
	gCurrentItem = 0;

$(document).ready(function () {
	var aSel = $("#home .links");
	if (aSel.length > 0) {
		// home page
		setUp("aanmelden");
		
	} else {
		// inside page
		$( "#ItemForm" ).dialog({ autoOpen: false, modal: true, width: 566 }); // , height: 299
		$( "#ListForm" ).dialog({ autoOpen: false, modal: true, width: 390, minHeight: 82 });
		$( "#AccountForm" ).dialog({ autoOpen: false, modal: true, width: 520, title: "Mijn gegevens" }); //, height: 236
		$( "#DeleteListForm" ).dialog({ autoOpen: false, modal: true, width: 390, title: "Verwijderen?" });
		$( "#DeleteItemForm" ).dialog({ autoOpen: false, modal: true, width: 390, title: "Verwijderen?", minHeight: 76 });
		$( "#DonateForm" ).dialog({ autoOpen: false, modal: true, width: 550, title: "Schenken" });
		$( "#ShareForm" ).dialog({ autoOpen: false, modal: true, width: 550, title: "Lijstje emailen" });
	
		  $.extend(jQuery.validator.messages, {
				required: "Dit veld is verplicht.",
				maxlength: jQuery.format("U kunt niet meer dan {0} karakters invoeren."),
				minlength: jQuery.format("U dient minimaal {0} karakters in te voeren."),
				rangelength: jQuery.format("U dient minimaal {0} en maximaal {1} karakters in te voeren."),
				email: "Een geldig e-mailadres is verplicht.",
				url: "Een geldig webadres is verplicht.",
				date: "Een geldige datum is verplicht.",
				number: "Een geldig getal is verplicht.",
				digits: "Gebruik alleen cijfers.",
				equalTo: "Herhaal de invoer nogmaals.",
				range: jQuery.format("U dient een waarde tussen {0} en {1} in te voeren."),
				max: jQuery.format("U dient een waarde kleiner dan of gelijk aan {0} in te voeren."),
				min: jQuery.format("U dient een waarde groter dan of gelijk aan {0} in te voeren."),
				creditcard: "Een geldig creditcardnummer is verplicht."
			});
	
		  $.datepicker.setDefaults({
			showOn: "both",
			buttonImage: gContext + "/images/icon_calendar.png",
			buttonImageOnly: true,
			dateFormat: "dd-mm-yy"});
		
		$("input[placeholder]").placeholder();
		$(".notification").delay(6000).fadeOut(1000);
		$(".dateBE").datepicker();
		
		// Account form - Gegevens
		$("#AccountForm").hide();
		$("#editAccount").click( showEditAccount );
		$("#AccountForm #dontAccount").click( dontAccount );
		
		// Add/Edit item form
		$("#dontAddItem").click( dontItem );
		$("#addItem").click( showAddItem );
		$(".article a.wijzig").click( showEditItem );
		$("#ItemForm").hide();
		
		// Add/Edit list form
		$("#dontAddList").click( dontList );
		$("#ListForm").hide();
		
		// Donate
		$("#dontDonate").click( dontDonate );
		$("#DonateForm").hide();
		
		// Share
		$("#dontShare").click( dontShare );
		$("#ShareForm").hide();
		
		// Delete List
		$("#doDeleteList").click( doDeleteList );
		$("#dontDeleteList").click( dontDeleteList );
		$("#DeleteListForm").hide();
		
		// Delete Item
		$("#doDeleteItem").click( doDeleteItem );
		$("#dontDeleteItem").click( dontDeleteItem);
		$("#DeleteItemForm").hide();
	}
	
	// try to prevent spammers
	$(".actor").val("valid");	
});


function setUp(what) {
	$(".box").hide();
	$("#"+what).show();
	
	var A = $("#A");
	var B = $("#B");
	
	A.unbind("click");
	B.unbind("click");

	if (what == "aanmelden") {
		A.text("Paswoord vergeten");
		A.click( function () { setUp("vergeten"); });
		B.text("Registreer");
		B.click( function () { setUp("registreer"); });
		
	} else if (what == "vergeten") {
		A.text("Aanmelden");
		A.click( function () { setUp("aanmelden"); });
		B.text("Registreer");
		B.click( function () { setUp("registreer"); });
		
	} else if (what == "registreer") {
		A.text("Aanmelden");
		A.click( function () { setUp("aanmelden"); });
		B.text("Paswoord vergeten");
		B.click( function () { setUp("vergeten"); });
	}
}


function showAllButtons() {
	$("#editList").show();
	$("#addItem").show();
	$("#addList").show();
	$("#forgetList").show();
}

function dontAccount() {
	var theForm = $("#AccountForm"); 
	theForm.dialog("close");
	theForm.unbind("submit");
}

function showEditAccount() {
	var theForm = $("#AccountForm"); 
	theForm.validate();
	theForm.dialog("open");
}



function dontItem() {
	var theForm = $("#ItemForm"); 
	theForm.dialog("close");
	theForm.unbind("submit");
	$("#ItemForm input[type=text]").val("");
}
function showAddItem() {
	$("#ItemForm #doSaveItem").hide();
	$("#ItemForm #doAddItem").show();
	
	$("#ItemForm id").val("");
	$("#ItemForm #priority").val( "50" );
	
	var theForm = $("#ItemForm"); 
	theForm.validate();
	theForm.dialog("option", "title", "Nieuw Item");
	theForm.dialog("open");
}
function showEditItem() {
	$("#ItemForm #doSaveItem").show();
	$("#ItemForm #doAddItem").hide();
	
	var div = $(this).parent().parent();
	
	/* fill fields */
	$("#ItemForm #id").val( div.attr("id") );
	$("#ItemForm #name").val( div.find("h2").text() );
	$("#ItemForm #price").val( div.find(".prijs").text() );
	$("#ItemForm #description").val( div.find("p").text() );
	$("#ItemForm #priority").val( div.find("span.priority").text() );
	$("#ItemForm #status"+ div.find("span.status").text()).attr("checked", true);
	var link = div.find(".link");
	if (link.length > 0)
		$("#ItemForm #url").val( link.attr("href").replace("http://", "") );
	else
		$("#ItemForm #url").val( "" );
	
	var theForm = $("#ItemForm"); 
	theForm.validate();
	theForm.dialog("option", "title", "Wijzig Item");
	theForm.dialog("open");
	
	return false;
}



function dontList() {
	var theForm = $("#ListForm"); 
	theForm.dialog("close");
	theForm.unbind("submit");
	$("#ListForm input[type=text]").val("");
}
function doEditList(theId, theName, isPublic) {
	// fill values
	$("#ListForm #id").val(theId);
	$("#ListForm #name").val(theName);
	
	// make a difference for edit/new list
	var theForm = $("#ListForm"); 
	if (theId == '') {
		$("#ListForm #public").attr("checked", "checked" );
		$("#ListForm #doAddList").show();
		$("#ListForm #doSaveList").hide();
		theForm.dialog("option", "title", "Nieuw Lijstje");
	} else {
		$("#ListForm #doAddList").hide();
		$("#ListForm #doSaveList").show();
		theForm.dialog("option", "title", "Wijzig Lijstje");
		if (isPublic == 'Y')
			$("#ListForm #public").attr("checked", "checked" );
		else
			$("#ListForm #public").removeAttr("checked" );
	}
	
	// replace double quotes by single quotes
	theForm.submit( function () {
		$("#ListForm #name").val( $("#ListForm #name").val().replace(/\"/g, "'") );
		return true;
	});
	
	theForm.validate();
	theForm.dialog("open");
	return false;
}


function doDeleteList(theId, theNrItems) {
	$("#DeleteListForm #id").val(theId);
	if (theNrItems > 0)
		$("#DeleteListForm #warning").html("Let op, er zijn nog " + theNrItems + " items in dit lijstje.");
	else
		$("#DeleteListForm #warning").html("Het lijstje is leeg.");

	$("#DeleteListForm").dialog("open");
	return false;
}
function dontDeleteList() {
	$("#DeleteListForm").dialog("close");
}

function doDeleteItem(theId) {
	$("#DeleteItemForm #id").val(theId);
	$("#DeleteItemForm").dialog("open");
	return false;
}
function dontDeleteItem() {
	$("#DeleteItemForm").dialog("close");
}

function doDonate(theId) {
	$("#DonateForm #id").val(theId);
	$("#DonateForm").dialog("open");
	return false;
}
function dontDonate() {
	$("#DonateForm").dialog("close");
}

function doShare(theId) {
	$("#ShareForm #id").val(theId);
	$("#ShareForm").dialog("open");
	return false;
}
function dontShare() {
	$("#ShareForm").dialog("close");
}

