function $id(id) {
	return document.getElementById(id);
}



function submitForm(formId) {
	try {
		if (editors != undefined) {
			for (var i = 0; i < editors.length; i++) {
				if (!editors[i].htmlEditMode)
					editors[i].syncTextarea();
			}
		}
	} catch (e) {
		//alert(e.message);
	}

	document.forms[formId].submit();
	return false;
}

function AddComment(moduleId, id)
{
	alert($id('new_comment_topic').value + '\n\n' + $id('new_comment_name').value + '\n\n' + $id('new_comment_content').value);
}

function SendTip()
{
	var qs = "try=sendTip&to=" + $("#tip_to").val() + "&from=" + $("#tip_from").val();
	AjaxLinkRequest(qs);
}

function SendSingleRequest()
{
	var qs = "try=getSingle&to=" + $("#single_to").val() + "&name=" + $("#single_name").val();
	AjaxLinkRequest(qs);
}

function AjaxRequest(queryString, blockingObj, hideObj)
{
	$.ajax({
		method: "get",
		url: "ajaxResponse.php",
		data: queryString,
		beforeSend: function() { $("#loading").show();}, //show loading just when link is clicked
		complete: function() { $("#loading").fadeOut();}, //stop showing loading when the process is complete
		success: function(html) 
			{ //so, if data is retrieved, store it in html
				var result = html.split('{#}', 4); // status{#}info{#}icon[{#}callbackAsString]
				//showFeedback(result[1], result[2], true);
				if (result[0] == "success")
				{
					if (blockingObj != null)
						$('#' + blockingObj).show(); // TODO... fade... men då ta ut opacity från css först pga bugg i ie
					if (hideObj != null)
						$('#' + hideObj).slideUp();//fadeOut();
					if (result.length == 4)
						eval(result[3]);
				}
				else if (result[0] == "failure")
				{
					if (result.length >= 2)
					{
						alert(result[1]);
					}
				}
			}
	}); //close $.ajax(
	return false;
}

function AjaxAddComment()
{
	$.post(
		'ajaxResponse.php', 
		$('#new_comment_form').serialize(), 
		function(html)
		{
			var result = html.split('{#}', 4); // status{#}info{#}icon[{#}callbackAsString]
			//showFeedback(result[1], result[2], true);
			if (result[0] == "success")
			{
				//$('#' + blockingObj).show(); // TODO... fade... men då ta ut opacity från css först pga bugg i ie
				//$('#new_comment_form_container').slideUp();
				
				if (result.length == 4)
					eval(result[3]);
				
				AjaxLinkRequest('siteId=' + $id('moduleId').value + "&id=" + $id('postId').value);
				$id('new_comment_content').value = '';
			}
			else if (result[0] == "failure")
			{
				if (result.length >= 2)
				{
					alert(result[1]);
				}
			}
		}
	);
}


var currContactMessageInfoHideTimeoutHandle = null;
function AjaxSubmitContactForm()
{
	if (currContactMessageInfoHideTimeoutHandle != null)
	{
		clearTimeout(currContactMessageInfoHideTimeoutHandle);
	}
	
	$.post(
		'ajaxResponse.php', 
		$('#new_message_form').serialize(), 
		function(html)
		{
			var result = html.split('{#}', 4); // status{#}info{#}icon[{#}callbackAsString]
			//showFeedback(result[1], result[2], true);
			if (result[0] == "success")
			{
				
				if (result.length == 4)
					eval(result[3]);
				
				//$id('new_message_content').value = '';
				$id('new_message_form').reset();
			}
//			else if (result[0] == "failure")
//			{
//				if (result.length >= 2)
//				{
//					alert(result[1]);
//				}
//			}
			
			var feedback = (result.length >= 2 ? result[1] : 'Something went wrong. Please try again');
			$('#new_message_info').html(feedback);
			$('#new_message_info').slideDown();
			currContactMessageInfoHideTimeoutHandle = setTimeout("$('#new_message_info').slideUp();", 5000);
		}
	);
}

/*
function AjaxLinkRequest(queryStringData)
{
	$.ajax({
		method: "get",
		url: "ajaxResponse.php",
		data: queryStringData,
		beforeSend: function() { $("#loading").show();}, //show loading just when link is clicked
		complete: function() { $("#loading").fadeOut();}, //stop showing loading when the process is complete
		success: function(html) 
			{ //so, if data is retrieved, store it in html
				//$("#dynamicContent").show("slow"); //animation
				$("#dynamicContent").html(html); //show the html inside .content div
				$('#dynamicContent a').click(dynamicLinkResponse);
			}
	}); //close $.ajax(
}
*/

function AjaxLinkRequest(queryStringData, anchor, containerId)
{
	if (containerId == undefined || containerId == null)
	{
		containerId = 'dynamicContent';
	}

	$.ajax({
		method: "get",
		url: "ajaxResponse.php",
		data: queryStringData,
		beforeSend: function() { $("#loading").show();}, //show loading just when link is clicked
		complete: function() { $("#loading").fadeOut();}, //stop showing loading when the process is complete
		success: function(html) 
			{ //so, if data is retrieved, store it in html
				//$("#dynamicContent").show("slow"); //animation
				$('#' + containerId).html(html);
				$('#' + containerId + ' a').click(dynamicLinkResponse);
				
				try 
				{
					if (anchor != undefined && anchor != null)
					{
						window.location.hash = anchor;
					}
				}
				catch (e) {}
			}
	}); //close $.ajax(
}

function dynamicLinkResponse()
{
	if ($(this).attr("href").substr(0, 7) == 'http://' || $(this).attr("target") == "_blank" || $(this).attr("onclick") != undefined)
	{
		return true;
	}
	else
	{
		//start function when any link is clicked
		//$("#dynamicContent").slideUp("slow");
		//var content_show = $(this).attr("title"); //retrieve title of link so we can compare with php file
		var queryString = $(this).attr("href").split('?', 2); //retrieve title of link so we can compare with php file
		AjaxLinkRequest(queryString[1]);
		return false; // if javascript is disabled... we want the link to behave as normal...
	}
}

/////////////////////////

var currGbContactMessageInfoHideTimeoutHandle = null;
function AjaxSubmitGuestbookMessage()
{
	if (currGbContactMessageInfoHideTimeoutHandle != null)
	{
		clearTimeout(currGbContactMessageInfoHideTimeoutHandle);
	}
	
	$.ajax({
		type: "POST",
		url: "ajaxResponse.php",
		data: $('#guestbookForm').serialize(),
		beforeSend: 
			function() 
			{ 
				$("#loading").show();
				//AjaxSwitchGuestbookSubpage(1); // switch to first page in order to add the new post first
			}, //show loading just when link is clicked
		complete: function() { $("#loading").fadeOut();}, //stop showing loading when the process is complete
		success:  
			function(html)
			{
				var result = html.split('{#}', 4); // status{#}info{#}icon[{#}callbackAsString]
				//showFeedback(result[1], result[2], true);
				if (result[0] == "success")
				{
					if (result.length >= 3)
					{
						var emptyGuestbookInfo = $('#emptyGuestbookInfo');
						var guestbookContainer = $('#guestbookContainer');
						var newPostClass = 'new_post_class';
						// Om gästboken är tom finns en div med id 'emptyGuestbookInfo'...
						if (emptyGuestbookInfo.size() == 0)
						{
							guestbookContainer.prepend('<div class="' + newPostClass + '" style="display:none;">' + result[2] + '</div>');
							$('.' + newPostClass + ':first').slideDown(1000);
						}
						else
						{
							// Vi slänger in post i gömd container-div
							guestbookContainer.prepend('<div class="' + newPostClass + '">' + result[2] + '</div>');
							emptyGuestbookInfo.slideUp(200, function() {
								guestbookContainer.slideDown(1000);
							});
						}
					}
					if (result.length == 4)
						eval(result[3]);
					
					$('#guestbookForm').get(0).reset();
				}
				
				var feedback = (result.length >= 2 ? result[1] : 'Something went wrong. Please try again. '+ html);
				$('#guestbookFormInfo').html(feedback);
				$('#guestbookFormInfo').slideDown(300);
				currGbContactMessageInfoHideTimeoutHandle = setTimeout("$('#guestbookFormInfo').slideUp();", 5000);
			}
	});
}

function IncPostCount()
{
	var countSpan = $('#gbNofPosts');
	if (countSpan.size() > 0)
	{
		var currCount = parseInt(countSpan.html());
		countSpan.html(currCount+1);
	}
}

function AjaxSwitchGuestbookSubpage(pageNo)
{
	AjaxLinkRequest('page_nr=' + pageNo, null, 'gbMessagesContainer');
}
function AjaxSwitchAndGoToGuestbookSubpage(pageNo)
{
	AjaxLinkRequest('page_nr=' + pageNo, 'gbMessagesTop', 'gbMessagesContainer');
}

