﻿// JScript File   

	var openEdit = null;
	var collapseEditTimeout = null;

	function showEdit(id, url, div)
	{
		stopAutoCollapseEdit();
		openEdit = document.getElementById('OnlineTextEdit');
		
		document.getElementById('languageId').value	= id;
		document.getElementById('contentId').value	= url;
		document.getElementById('contentDiv').value	= div.id;
		
		openEdit.style.top			= findEditPos(div, 'height') + 3 + 'px';
		openEdit.style.left			= (findEditPos(div, 'width') + (div.clientWidth -17)) +'px';
		openEdit.style.visibility	= "visible";
		
	}
	
	function hideEdit()
	{
		autoCollapseEdit();
	}
	
	function autoCollapseEdit()
	{	
		if(collapseEditTimeout == null)
			collapseEditTimeout = setTimeout("hideEditDiv()", 500);
	}
		
	function stopAutoCollapseEdit()
	{
		if (collapseEditTimeout)
			window.clearTimeout(collapseEditTimeout);
			
		collapseEditTimeout = null;
	}
	
	function hideEditDiv()
	{
		if(openEdit != null)
		{
			openEdit.style.visibility = "hidden";
			openEdit = null;
		}
			
		collapseEditTimeout = null;
	}
	
	function findEditPos(obj, getType)
	{
		var curleft = curtop = 0;
		
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		
		if(getType == 'width')
			return curleft;
		else
			return curtop;
	}

	function OpenEditWindow(subPath)
	{
		var url;
	
		if(window.location.pathname.indexOf("content.aspx") != -1)
			url = './System/Admin/Editors/TextEditor.aspx?languageId=' + document.getElementById('languageId').value + '&contentId=' + 
					document.getElementById('contentId').value;
		else
			url = './' + subPath + 'System/Admin/Editors/TextEditor.aspx?languageId=' + document.getElementById('languageId').value + '&contentId=' + 
					document.getElementById('contentId').value;
		
		var windowWidth = document.body.offsetWidth;	
				
		var documentHeight =  document.body.offsetHeight;		
		var windowHeight = document.documentElement.clientHeight;
		var scrollpos = document.documentElement.scrollTop;
		
		if(windowHeight > 475 && windowWidth > 682)
		{
			var top	 = scrollpos + 15;
			var left = (windowWidth-682)/2;
		
			var blocker			  = document.getElementById('blocker');
			blocker.style.display = '';
			blocker.style.width	  = windowWidth + "px";
			blocker.style.height  = documentHeight + "px";
		
			var popupeditor			  = document.getElementById('popupeditor');
			popupeditor.src			  = url;
			popupeditor.style.left	  = left + "px";
			popupeditor.style.top	  = top + "px";
			popupeditor.style.display = '';
			
	//		document.getElementById(document.getElementById('contentDiv').value).style.display = 'none';
			
			if(navigator.userAgent.indexOf("MSIE") > 0)
			{
				popupeditor.style.height = "580px";
			}
		}
		else
		{
			if(navigator.userAgent.indexOf("MSIE") > 0)
				window.open(url + "&open=popup", 'texteditor', 'height=520, width=925, top=50');	
			else
				window.open(url + "&open=popup", 'texteditor', 'height=513, width=925, top=50');	
		}
	}
	
	function CloseEditWindow(content)
	{
		var popupeditor = document.getElementById('popupeditor');
		
		document.getElementById(document.getElementById('contentDiv').value).style.display = '';
	
		document.getElementById('blocker').style.display = 'none';
		
		popupeditor.style.display = 'none';
		popupeditor.src = 'about:blank';
		
		var editName = document.getElementById('contentDiv').value;
		var editor = document.getElementById(editName);
		
		if(content)
			editor.innerHTML = content;
	}
	
	var saveText = "";
	
	function LoadText(object)
	{
		saveText = object.innerHTML;
	}
	
	function SaveText(object, uDiv) 
	{ 
		if(saveText != object.innerHTML)
		{
			alert(object.innerHTML);
			alert(saveText);
			
			if(confirm('Innehållet har ändrats, vill du spara?'))
			{
				GetAjaxUpdate('/System/Admin/AjaxResponse/UpdateContentTexts.aspx', '&contId=' + object.id + '|||' + uDiv + '&content=' + object.innerHTML.replace(/</g, '**||**').replace(/>/g, '*|**|*').replace(/&/g, '*|*|*|*'));
			//	document.getElementById(uDiv).getElementsByTagName('input')[0].value =  object.id + '|||' + object.innerHTML.replace(/</g, '**||**').replace(/>/g, '*|**|*');
			}
			else
				object.innerHTML = saveText;
		}
	}
