
var nextNewsItemInterval;
var hideHead;

$(document).ready
(
	function()
	{
		if( siteUrl + "/" == requestedUrl )
		{
			
			swfobject.embedSWF( swfDir + "intro.swf", "introSWFContent", 550, 400, "9.0.0", swfDir + "expressInstall.swf");
			
			setTimeout('hideIntro()', introTime);
		}
		else
		{
			showTagCloud();
			showMenu();
		}
		
		// Fix firefox bug with floating objects and height
		if (jQuery.browser.mozilla && $('#editButtonPanel') )
		{ 
			$('#editButtonPanel').height(0);
		}
		
		if( jQuery.browser.mozilla && $('#newsItems') )
		{
			$('#newsItems').height(0);
		}
		
		if($('#upload'))
		{
			uploadImage();
		}
		
		if( typeof(numberOfNewsItems) != "undefined" )
		{
			nextNewsItemInterval = setInterval('nextNewsItem()', 12000);
			$('#contentText').addClass("pointer");
			$('#contentText').click(function () { 
				stopNextNewsItemInterval();
				expandContentText();
				}
			)
		}
	}
)

function nextNewsItem()
{
	nextScrollPage = (currentScrollPage + 1);
	
	if( nextScrollPage > numberOfNewsItems )
	{
		nextScrollPage = 1;
	}
	
	window.location = siteUrl + "/" + scrollPageType + "/" + nextScrollPage;
}

function stopNextNewsItemInterval()
{
	clearInterval( nextNewsItemInterval );
}

function expandContentText()
{
	$('#contentText').removeClass("contentTextMaxHeight");
	$('#contentText').removeClass("pointer");
}

function hideIntro()
{
	$('#introSWFContent').hide();
	showTagCloud();
	showMenu();
}

function showMenu()
{
	swfobject.embedSWF( swfDir + "menubalk3.swf", "navigationSwfContent", 830, 100, "9.0.0", swfDir + "expressInstall.swf");
}

function showTagCloud()
{
	swfobject.embedSWF( swfDir + "woorden4.swf", "tagCloudSWFContent", 350, 200, "9.0.0", swfDir + "expressInstall.swf");
}

function loadEditor(type)
{
	if( typeof(numberOfNewsItems) != "undefined" )
	{
		stopNextNewsItemInterval();
	}
	
	var text = $('#contentText').html();
	text = replaceTags(text);
	titletxt = "";
	
	if( $('#contentTitle').attr('class') == "hidden" )
	{
		hideHead = true;
		$('#contentTitle').removeClass("hidden");
	}
	
	if( $('#contentTitle h1').html() != null )
	{
		titletxt = $('#contentTitle h1').html();
	}
	
	$('#contentTitle').html("Paginatitel: <input style='width: 100px;' type='text' id='contentTitleEdit' value='" + titletxt + "'/>");
	
	$('#editButtonLink').attr("disabled", "true");
	$('#editControls').show();
	
	
	$('#contentText').wrap('<textarea id="contentEditor">'+text+'</textarea>');
	//$('#contentText').hide();
}

function unloadEditor(content)
{
	$('#editButtonLink').removeAttr("disabled");
	$('#contentEditor').wrap('<div id="contentText">'+content+'</div>');
	
	if( $('#contentTitleEdit').val() != "")
	{
		$('#contentTitle').html( "<h1>" + $('#contentTitleEdit').val() + "</h1>");
	}
	else
	{
		$('#contentTitle').html("");
	}
	
	if( hideHead == true )
	{
		$('#contentTitle').addClass("hidden");
	}
	
	$('#editControls').hide();
}

function replaceTags(text)
{
	text = replaceAll(text, '\n','');
	
	if (jQuery.browser.msie)
	{ 
		text = replaceAll(text, '</P>',"\r\n");
		text = replaceAll(text, '</p>',"\r\n");
	}
	else
	{
		text = replaceAll(text, '</P>',"\r\n");
		text = replaceAll(text, '</p>',"\r\n\r\n");
	}
	
	text = replaceAll(text, '<P>','');
	text = replaceAll(text, '<p>','');
	
	text = replaceAll(text, '<BR>', "\n");
	text = replaceAll(text, '<br>', "\n");
	text = replaceAll(text, '<br />', "\n");
	
	text = text.replace(/<a\s+href.+?>(.+?)<\/a>/g, "%$1%");

	
	// remove last enter
	text = text.replace(/<a.+?href[\s\S]+?>([\s\S]+?)<\/a>/ig, "%$1%");
	
	return text;
}

function savePage( )
{
	
	var text = taggify($('#contentEditor').val());
	
	var title = $('#contentTitleEdit').val();
	$('#contentEditor').attr('disabled', 'true');

	$.post( rootDir + "/Ajax/saveContent", { action: "cmsSave", data: text, contentType: contentType, contentId: contentId, contentTitle: title, newsId: newsId },
		function(data){
			
			//alert(data);
			
			if (/^\s*error.*/im.test(data))
			{
				alert("Er is iets mis gegaan bij het opslaan van de gegevens. Neem aub contact op met de beheerder. (CODE: 001) " + data);
				unloadEditor( 'error' );
			}
			else
			{
				unloadEditor( taggify( text ) );
			}
			
		}
	);
	
}

function taggify( text )
{
	
	text = "<p>" + text + "</p>";
	text = replaceAll(text, '\r\n\r\n', '</p><p>');
	text = replaceAll(text, '\n', '<br />');
	
	return text;
}
function replaceAll(Source,stringToFind,stringToReplace)
{

  var temp = Source;

    var index = temp.indexOf(stringToFind);

        while(index != -1){

            temp = temp.replace(stringToFind,stringToReplace);

            index = temp.indexOf(stringToFind);

        }

        return temp;

}

function uploadImage(  )
{
	$('#upload').uploadify({
	'auto'      : true,
	'uploader': swfDir + '/uploadify.swf',
	'script': rootDir + '/uploadify.php',
	'folder': userFiles,
	'buttonText': "Afb. wijzigen",
	'onComplete': function(event, queueID, fileObj, response, data) {
		//alert(fileObj.filePath);
			
			$.post( rootDir + "/Ajax/saveImage", { action: "imageSave", data: fileObj.name, contentType: contentType, contentId: contentId, newsId: newsId },
				function(data){
					
					//alert(data);
					
					if (/^\s*error/im.test(data))
					{
						alert("Er is iets mis gegaan bij het opslaan van de gegevens. Neem aub contact op met de beheerder. (CODE: 002)");
					}
					else
					{
						$('#contentImageImage').attr("src", fileObj.filePath);
					}
					
				}
			);
		}
	});
}

function removeLink( id )
{
	$.post( rootDir + "/Ajax/removeLink", { submitRemoveLink: "cmsRemove", linkid: id },
		function(data){
			
			if (/^\s*error/im.test(data))
			{
				alert("Er is iets mis gegaan bij het opslaan van de gegevens. Neem aub contact op met de beheerder. (CODE: 003)");
			}
			else
			{
				$('#link_' + id).height(0);
				$('#link_' + id).remove();
			}
			
		}
	);
} 

function tagClick( word )
{
	word = replaceAll(word, ' ', '_');
	window.location = rootDir + "/tag_" + word
}

function newItem( type )
{
	$.post( rootDir + "/Ajax/addItem", { contentType: type, action: 'addItem' },
		function(data){
			
			if (/^\s*error/im.test(data))
			{
				alert("Er is iets mis gegaan bij het opslaan van de gegevens. Neem aub contact op met de beheerder. (CODE: 004)");
			}
			else
			{
				text = replaceAll(data, ' ', '');
				window.location = rootDir + "/" + type + "/id_" + text;
			}
			
		}
	);
}

function removeItem( id, type )
{
	$.post( rootDir + "/Ajax/removeItem", { newsId: id, action: 'removeItem' },
		function(data){
			if (/^\s*error/im.test(data))
			{
				alert("Er is iets mis gegaan bij het opslaan van de gegevens. Neem aub contact op met de beheerder. (CODE: 005)");
			}
			else
			{
				window.location = rootDir + "/" + type;
			}
			
		}
	);
}
