/*!
	Copyright (c) 2009 Samoon
	samoon.js
 */

// start of jquery //////////////////////////////////
$(document).ready(function(){

//$.validator.setDefaults({
//	submitHandler: function() { alert("submitted!"); }
//});

	$("#tipform").validate({ 
		rules: {
			tip_title: "required",
			tip_text: "required",
			tip_email: {
				required: true,
				email: true 
			}
		},
		messages: {
			tip_title: "Je hebt nog geen onderwerp ingevuld.",
			tip_text: "Je hebt nog geen tip ingevuld.",
			tip_email: {
				email: "Vul een geldig e-mailadres in.",
				required: "Je hebt nog geen e-mailadres ingevuld."
			}
		} 
	});

	$("#bever").ready(function()
	{
		getBeverFeed();
	});

});
// end of jquery ////////////////////////////////////

function getRandom()
{
	return Math.random() * 99999;
}

// generic toggle, without additional specifics. It does allow an Ajax call. More specific toggles are below.
function toggleDiv(strID, strURL, strData)
{
	// show or hide panel
	if ($("#" + strID).is(":visible"))
	{
		$("#" + strID).slideUp("slow");
	}
	else
	{
		if (strURL != "")
		{
			$.ajax(
			{
				method: "post",
				url: strURL,
				data: strData + "&r=" + getRandom(),
				success: function(html)
				{
					$("#" + strID).html(html);
				}
			});
		}
		$("#" + strID).slideDown("slow");
	}
}

// toggle disclaimer
function toggleDisclaimer()
{
	// show or hide panel
	if ($("#disclaimer").is(":visible"))
	{
		$("#disclaimer").slideUp("slow");
	}
	else
	{
		if ($("#contact").is(":visible"))
			$("#contact").slideUp("slow");

		if ($("#feedback").is(":visible"))
			$("#feedback").hide();

		$.ajax(
		{
			method: "post",
			url: "/data.asp",
			data: "option=disclaimer&r=" + getRandom(),
			success: function(html)
			{
				$("#disclaimer").html(html);
			}
		});

		$("#disclaimer").slideDown("slow");
	}
}

function toggleContact()
{
	// show or hide panel
	if ($("#contact").is(":visible"))
	{
		$("#contact").slideUp("slow");
	}
	else
	{
		if ($("#disclaimer").is(":visible"))
			$("#disclaimer").slideUp("slow");

		// get the form html
		$.ajax(
		{
			method: "post",
			url: "/data.asp",
			data: "option=contactform&r=" + getRandom(),
			success: function(html)
			{
				$("#contactform").html(html);
			}
			
		});

		$("#contact").slideDown("slow");
	}
}

function reloadContact()
{
	// re-show contactform, use only after feedback, when entered values must be preserved
	$("#contactform").slideDown("slow");
	$("#feedback").hide();
}

function submitContact()
{
	var strMessage;
	var strName;
	var strEmail;
		
	strMessage = $("#contact_message").val();
	strName = $("#contact_name").val();
	strEmail = $("#contact_email").val();

	$.ajax(
		{
			method: "post",
			url: "/dotask.asp",
			data: "task=contactsamoon&contact_message=" + strMessage + "&contact_name=" + strName + "&contact_email=" + strEmail + "&r=" + getRandom(),
			success: function(html)
			{
				$("#contactform").slideUp("fast");
				$("#feedback").slideDown("slow");
				$("#feedback").html(html);
			}
		});
}

function submitTip()
{
	var strTitle;
	var strText;
	var strName;
	var strEmail;
	var strCountry;
	var strReiswijze;
	var strAccommodatie;

	strTitle = $("#tip_title").val();
	strText = $("#tip_text").val();
	strName = $("#tip_name").val();
	strEmail = $("#tip_email").val();
	strCountry = $("#landcode").val();
	strReiswijze = $("#rwcode").val();
	strAccommodatie = $("#acccode").val();

	$.ajax(
		{
			method: "post",
			url: "/dotask.asp",
			data: "task=addtip&title=" + strTitle + "&text=" + strText + "&name=" + strName + "&email=" + strEmail + "&landcode=" + strCountry + "&rwcode=" + strReiswijze + "&acccode=" + strAccommodatie + "&r=" + getRandom(),
			success: function(html)
			{
				$("#tipform").slideUp("normal");
				$("#tipfeedback").slideDown("normal");
				$("#tipfeedback").html(html);
			}
		});
}

function excludeItem(ListID, ItemCode)
{
	if (confirm("Weet je zeker dat je dit item wilt verwijderen?"))
	{
		// post back to process exclude
		$.ajax(
		{
			method: "post",
			url: "/dotask.asp",
			data: "task=excludeitem&listid=" + ListID + "&itemcode=" + ItemCode + "&r=" + getRandom(),
			success: function(html)
			{
				$("#" + ItemCode).slideUp("slow");
			}
		});
	}
}

function unlinkItem(intPersoonID, intItemID)
{
	if (confirm("Weet je zeker dat je dit definitief uit de extra spullen wilt verwijderen?"))
	{
		// post back to process exclude
		$.ajax(
		{
			method: "post",
			url: "/dotask.asp",
			data: "task=ontkoppelitempersoon&persoonid=" + intPersoonID + "&itemid=" + intItemID + "&r=" + getRandom(),
			success: function(html)
			{
				$("#item_" + intItemID).slideUp("slow");
			}
		});
	}
}

function addItem()
{
	var strItem;
	var intPersoonID;

	strItem = $("#ownitems_item").val();
	intPersoonID = $("#ownitems_persoonid").val();

	$.ajax(
		{
			method: "post",
			url: "/dotask.asp",
			data: "task=additems&item=" + strItem + "&persoonid=" + intPersoonID + "&r=" + getRandom(),
			success: function(html)
			{
				$("#ownitems_added").show();
				$("#ownitems_added").html(html);
				$("#ownitems_item").val("");
			}
		});
}

function deleteLijst(intLijstID)
{
	if (confirm("Weet je zeker dat je deze lijst wilt verwijderen? Je kan dit niet ongedaan maken."))
	{
		// post back to process exclude
		$.ajax(
		{
			method: "post",
			url: "/dotask.asp",
			data: "task=listdelete&lijstid=" + intLijstID + "&r=" + getRandom(),
			success: function(html)
			{
				$("#lijst" + intLijstID).slideUp("slow");
			}
		});
	}
}

function getBeverFeed()
{
	// post back to process exclude
	$.ajax(
	{
		method: "get",
		dataType: "xml",
		url: "/proxy.asp",
		data: "source=bever&r=" + getRandom(),
		success: function(xml)
		{
			generateBeverItems(xml);
		},
		error: function(XMLHttpRequest, e)
		{
			//alert(XMLHttpRequest.responseText);
		}
	});
}

function generateBeverItems(xml)
{
	var strResult = "";
	var intCounter = 0;

	strResult += "<h1>Nieuw bij Bever Zwerfsport <a href='http://www.bever.nl/artikelen/nieuw' target='_blank'></a></h1>";
	$(xml).find('item').each(function()
	{
		if (intCounter < 4)
		{
			var strTitle = $(this).find("title").text();
			var strLink = $(this).find("link").text();
			var strDescription = $(this).find("description").text();

			// get the image from the description
			var intEnd = strDescription.indexOf("alt=");
			strDescription = strDescription.substring(0, (intEnd + 9))
			strDescription = strDescription.replace("img src=", "img width='100' src=");

			strResult += "<div class=\"beveritem\"><h2>" + strTitle + "</h2><a href=" + strLink + " target=_blank>" + strDescription + "</a></div>";
		}
		intCounter++;
	});
	strResult += "<div class='clearfloat'></div>";
//	strResult += "<br><a href='http://www.bever.nl/artikelen/nieuw' target='_blank'>meer...</a>"; 

	if (strResult != "")
	{
		$("#bever").html(strResult);
		$("#bever").slideDown();
	}

}
