/**
 * PAGEBUTLER JavaScript
 * (c) Copyright 2004-2007 Media Trooper GmbH
 * www.mediatrooper.de
 */
 
function submitAdminForm( sa )
{
	var f = document.getElementById('adminform');
	if( sa )
	{
		f.submitaction.value = sa;
	}
	if( f.getAttribute("ajax") )
	{
		var tas = f.getElementsByTagName("iframe");
		if( tas.length>0 )
		{
			tinyMCE.triggerSave();
		}
		for (i = 0; i < tas.length; i++)
		{ 
			if( tas[i].className=="mceEditorIframe" )
			{
				tinyMCE.execCommand("mceRemoveControl", false, tas[i].id); 
			}
		}
		submitAjaxForm('adminform','ajaxAdminForm');
	}
	else
	{
		f.submit();
	}
}

function pbRedirect( url, pause )
{
	window.setTimeout("document.location='"+url+"';", pause*1000);
}

function getSelectValue( domid )
{
	var s = document.getElementById(domid);
	var i = s.selectedIndex;
	return s.options[i].value;
}

function clearInput(i,s)
{
	if(i.value==s)i.value='';
}
function unclearInput(i,s)
{
	if(i.value=='')i.value=s;
}

/**
 * CONTENT EDITOR
 */

var currentPageNavigationId = 0;
function checkTypSelection( typeDomid, contentDomid )
{
	var v = getSelectValue( typeDomid );
	if( v=='content' )
	{
		document.getElementById( contentDomid ).style.display = "block";
	}
	else
	{
		document.getElementById( contentDomid ).style.display = "none";
	}
}

function copyTitleToName(fm, targetId)
{
	var t = document.getElementById(targetId);
	var v = fm.value.toLowerCase();
	v = v.replace(/-/g, "_");
	v = v.replace(/\W/g, "");
	v = v.replace(/_/g, "-");
	if( t )
	{
		t.value = v;
	}
	return true;
}

function setCurrentPageNavigationId(i)
{
	currentPageNavigationId = i;
}

function updateCurrentPageNavigationSelect()
{
	if(currentPageNavigationId)
	{
		document.getElementById('navilink'+currentPageNavigationId).style.fontWeight = "bold";
	}
}

function changePageNavigationId( newnav )
{
	
}

function updateNaviTreeSelect(preselecid)
{
	var wi = document.getElementById('selectwebsite').selectedIndex;
	var wv = document.getElementById('selectwebsite').options[wi].value;
	var li = document.getElementById('selectlanguage').selectedIndex;
	var lv = document.getElementById('selectlanguage').options[li].value;
	ajaxRemoteFunction('navigationidselect', '/admin/pages/naviaddtreeselect/'+wv+'/'+lv+'/'+preselecid+'');
}

// ---------------------------------------
//  CEL = Content Editor List
// ---------------------------------------
function CEL() {
	
	var CEL_contentTypeId = 0;
	var CEL_offset = 0;
	var CEL_offsetSteps = 25;
	var CEL_searchword = "";
	var CEL_itemcount = 0;
	var CEL_orderby = "";
	var CEL_orderdesc = false;
	var CEL_requestident = 0;
	var CEL_lastsearchtime = 0;
	var CEL_searchevent = false;
	
	this.setTypeId = function ( contenttypeid )
	{
		CEL_contentTypeId = contenttypeid;
	}
	this.setItemcount = function (i)
	{
		CEL_itemcount = Number(i);
	}
	this.setOffsetSteps = function (i)
	{
		CEL_offsetSteps = Number(i);
	}
	this.reloadSearchword = function ()
	{
		var s = document.getElementById('searchtext'+CEL_contentTypeId).value;
		CEL_searchword = s;
	}
	this.changeItemsPerPage = function(i)
	{
		this.setOffsetSteps(i);
		this.requestList();
	}
	this.handleSearch = function (force)
	{
		var s = document.getElementById('searchtext'+CEL_contentTypeId).value;
		var jetzt = new Date();
	  	var Zeit = jetzt.getTime() - 500;
	  	if(force || (s.length>1 && CEL_lastsearchtime<Zeit && s!=CEL_searchword))
		{
			CEL_lastsearchtime = jetzt.getTime();
			CEL_searchword = s;
			CEL_offset = 0;
			this.requestList();
			if(CEL_searchevent)
				window.clearInterval(CEL_searchevent);
			CEL_searchevent = window.setInterval("myCEL"+CEL_contentTypeId+".handleSearch()", 500);
		}
	}
	this.clearSearch = function ()
	{
		if(CEL_searchevent)
			window.clearInterval(CEL_searchevent);
		document.getElementById('searchtext'+CEL_contentTypeId).blur();
		document.getElementById('searchtext'+CEL_contentTypeId).value = "Suchbegriff";
		CEL_searchword = "";
		CEL_lastsearchtime = 0;
		this.requestList();
	}
	this.requestList = function ()
	{
		var orderdesc = 0;
		if( CEL_orderdesc )
			orderdesc = 1;
		CEL_requestident++;
		document.getElementById('waitimage'+CEL_contentTypeId).style.display='block';
		var url = "/admin/contents/list_view/"+CEL_contentTypeId+"/"+CEL_requestident+"/"+CEL_offset+"/"+CEL_offsetSteps+"/"+escape(CEL_searchword)+"/"+CEL_orderby+"/"+orderdesc;
		ajaxRemoteFunction("ajaxContentList"+CEL_contentTypeId, url);
	}
	this.next = function ()
	{
		CEL_offset += CEL_offsetSteps;
		this.requestList();
	}
	this.back = function ()
	{
		CEL_offset -= CEL_offsetSteps;
		if( CEL_offset<0 )
		{
			CEL_offset=0;
		}
		this.requestList();
	}
	this.redrawToolbar = function (requestident)
	{
		if( CEL_offset<=0 )
		{
			document.getElementById('backBtnOn'+CEL_contentTypeId).style.display = "none";
			document.getElementById('backBtnOff'+CEL_contentTypeId).style.display = "block";
		}
		else
		{
			document.getElementById('backBtnOn'+CEL_contentTypeId).style.display = "block";
			document.getElementById('backBtnOff'+CEL_contentTypeId).style.display = "none";
		}
		if( (CEL_offset+CEL_offsetSteps)>=CEL_itemcount )
		{
			document.getElementById('nextBtnOn'+CEL_contentTypeId).style.display = "none";
			document.getElementById('nextBtnOff'+CEL_contentTypeId).style.display = "block";
		}
		else
		{
			document.getElementById('nextBtnOn'+CEL_contentTypeId).style.display = "block";
			document.getElementById('nextBtnOff'+CEL_contentTypeId).style.display = "none";
		}
		var rangeTo = (CEL_offset+CEL_offsetSteps);
		if(rangeTo>CEL_itemcount)
		{
			rangeTo=CEL_itemcount;
		}
		document.getElementById('rangeText'+CEL_contentTypeId).innerHTML = (CEL_offset+1)+"-"+rangeTo+" / "+CEL_itemcount+"";
		// wait-image
		if(requestident==CEL_requestident)
		{
			document.getElementById('waitimage'+CEL_contentTypeId).style.display='none';
		}
	}
}

/**
 * ASSETS-LIST ContentHandler
 */

var ALcount = 0;
function ALassetAd(id,name,assetId)
{
	ALcount++;
	var newAssetSelectid = id+'-'+ALcount;
	var emptyAsset = 	'<div class="alitem" id="alitem_'+newAssetSelectid+'"><div id="ajaxcontainer_'+newAssetSelectid+'"></div>'+
						'<input type="hidden" name="'+name+'[]" value="'+assetId+'" id="'+newAssetSelectid+'" /></div>';
	var container = document.getElementById('al_container_'+id);
	var h = container.innerHTML;
	container.innerHTML = h + emptyAsset;
	ajaxRemoteFunction('ajaxcontainer_'+newAssetSelectid+'','/admin/assets/viewsmall/'+assetId+'/'+newAssetSelectid+'');
	ALsortingStart(id);
}

var currentChoosedAssetDivId = "";
function chooseAsset(id)
{
	var parentId = id.substring(0,id.lastIndexOf("-"));
	if( currentChoosedAssetDivId!="" )
	{
		var currentParentId = currentChoosedAssetDivId.substring(0,currentChoosedAssetDivId.lastIndexOf("-"));
		if(currentParentId!=parentId)
		{
			document.getElementById('al_options_'+currentParentId+'_on').style.display = 'none';
			document.getElementById('al_options_'+currentParentId+'_off').style.display = 'block';
		}
		document.getElementById('alitem_'+currentChoosedAssetDivId).style.borderColor = '#ffffff';
	}
	if( currentChoosedAssetDivId==id )
	{
		currentChoosedAssetDivId = "";
		document.getElementById('al_options_'+parentId+'_on').style.display = 'none';
		document.getElementById('al_options_'+parentId+'_off').style.display = 'block';
	}
	else
	{
		var alitem = document.getElementById('alitem_'+id);
		alitem.style.borderColor = '#f4a022';
		currentChoosedAssetDivId = id;
		document.getElementById('al_options_'+parentId+'_on').style.display = 'block';
		document.getElementById('al_options_'+parentId+'_off').style.display = 'none';
	}
	//document.getElementById('al_options_'+parentId).blur();
}
function ALsortingStart(id)
{
	Sortable.create('al_container_'+id, {tag:'div',only:'alitem',overlap:'horizontal',constraint:false});
}
function deleteChoosedAsset()
{
	var id = currentChoosedAssetDivId;
	if( id!='' )
	{
		var parentId = id.substring(0,id.lastIndexOf("-"));
		var alitem = document.getElementById('alitem_'+id);
		alitem.innerHTML='';
		alitem.style.display = 'none';
		currentChoosedAssetDivId = "";
		ALsortingStart(parentId);
		document.getElementById('al_options_'+parentId+'_on').style.display = 'none';
		document.getElementById('al_options_'+parentId+'_off').style.display = 'block';
	}
}

/* NOT IN USE: */
function deleteDroppedAsset(element)
{
	var id = element.id.substring(element.id.indexOf("_")+1,100);
	chooseAsset(id);
	deleteChoosedAsset();
}
function ALregisterTrashElement(id)
{
	Droppables.add('actionbar_'+id, {
	   accept: 'alitem',
	   onDrop: function(element) 
	     { deleteDroppedAsset(element); }});
}


/**
 * ASSETS MANAGER
 */

var idForAssetSelection = "";
var editorForAssetSelection = "";
var currentSelectedAssetId = 0;
var currentAssetFolderId = 0;
var viewtypeForAssetSelection = "";
function openAssetsManager()
{
	new Effect.BlindDown('assetsManager', {duration: 0.5});
}
function hideAssetsManager()
{
	document.getElementById('assetsFolder').style.display = 'none';
	document.getElementById('assetsList').style.display = 'none';
	document.getElementById('assetListActionBar').style.display = 'none';
	new Effect.BlindUp('assetsManager', {duration: 0.5});
}
function startSelectAsset(assetid, divid, viewtype)
{
	idForAssetSelection = divid;
	if( viewtype )
		viewtypeForAssetSelection = viewtype;
	else
		viewtypeForAssetSelection = "view";
	ajaxRemoteFunction( "AjaxHolderForAssetsManager", "/admin/assets/manager/"+assetid );
}
function clickAsset(assetid)
{
	if( currentSelectedAssetId>0 )
	{
		document.getElementById('assetitemthumb_'+currentSelectedAssetId).style.borderColor = '#222';
	}
	if( currentSelectedAssetId==assetid )
	{
		currentSelectedAssetId = 0;
		document.getElementById('assetOptions').style.display = 'none';
		document.getElementById('assetListUploaderFrame').style.display = 'block';
	}
	else
	{
		ajaxRemoteFunction( 'assetOptions', '/admin/assets/options/'+assetid );
		document.getElementById('assetOptions').style.display='block';
		document.getElementById('assetListUploaderFrame').style.display = 'none';
		document.getElementById('assetitemthumb_'+assetid).style.borderColor = '#f4a022';
		currentSelectedAssetId = assetid;
	}
	doubleClickAssetId(assetid);
	//document.getElementById('assetitemthumb_'+assetid).blur();
}
function preselectAsset(assetid)
{
	clickAsset(assetid);
	var posTop = document.getElementById('assetitemthumb_'+assetid).offsetTop - document.getElementById('assetsListScrollFrame').offsetTop;
	var itemHeight = document.getElementById('assetitemthumb_'+assetid).offsetHeight;
	var assetsListScrollFrame = document.getElementById('assetsListScrollFrame');
	if( posTop > (assetsListScrollFrame.offsetHeight-itemHeight) )
	{
		assetsListScrollFrame.scrollTop = posTop-4;
	}
}
function reloadAssetList()
{
	ajaxRemoteFunction( 'assetsList', '/admin/assets/index/'+currentAssetFolderId );
}
function deleteAsset(assetid)
{
	var assetItem = document.getElementById('assetitem_'+assetid);
	assetItem.style.display = 'none';
	resetAssetListOptions();
}
function resetAssetListOptions()
{
	currentSelectedAssetId = 0;
	document.getElementById('assetOptions').style.display = 'none';
	document.getElementById('assetListUploaderFrame').style.display = 'block';
}
var assetsmanagerDoubleClickId = 0;
var assetsmanagerDoubleTime = 0;
function doubleClickAssetId(assetid)
{
	var jetzt = new Date();
	var zeit = jetzt.getTime();
	if( assetsmanagerDoubleClickId==assetid )
	{
		if(assetsmanagerDoubleTime+400 > zeit)
		{
			selectAsset(assetid);
		}
	}
	assetsmanagerDoubleTime = zeit;
	assetsmanagerDoubleClickId = assetid;
}
function selectAsset(assetid)
{
	if( viewtypeForAssetSelection=="mce" )
	{
		// in html-editor einfügen
		var inst = tinyMCE.getInstanceById(idForAssetSelection);
		var elm = inst.getFocusElement();
		var src = '/admin/assets/mce/'+assetid+'';
		if (elm != null && elm.nodeName == "IMG") 
		{
			setAttrib(elm, 'src', src);
			setAttrib(elm, 'assetid', assetid);
			setAttrib(elm, 'alt');
			setAttrib(elm, 'title');
			setAttrib(elm, 'width');
			setAttrib(elm, 'height');
			setAttrib(elm, 'id');
			setAttrib(elm, 'class', '');
	
			// Repaint if dimensions changed
			inst.repaint();
	
			// Refresh in old MSIE
			if (tinyMCE.isMSIE5)
				elm.outerHTML = elm.outerHTML;
		} 
		else 
		{
			var html = "<a ";
			
			html += makeAttrib('href', '#linkToAsset:'+assetid);
			html += makeAttrib('assetid', assetid);
			
			html += "><img";
	
			html += makeAttrib('src', src);
			html += makeAttrib('assetid', assetid);
			html += makeAttrib('alt');
			html += makeAttrib('title');
			html += makeAttrib('width');
			html += makeAttrib('height');
			html += makeAttrib('id');
			html += makeAttrib('class', '');
			html += " /> Asset: "+assetid+"</a>";
	
			tinyMCE.execInstanceCommand(inst.editorId, "mceInsertContent", false, html);
		}
	}
	else
	{
		document.getElementById(idForAssetSelection).value = assetid;
		ajaxRemoteFunction( 'ajaxcontainer_'+idForAssetSelection, '/admin/assets/'+viewtypeForAssetSelection+'/'+assetid+'/'+idForAssetSelection );
	}
	hideAssetsManager();
	//
	// reset manager
	idForAssetSelection = "";
	currentSelectedAssetId = 0;
}
function resetAssetInput(divid)
{
	document.getElementById(divid).value = 0;
	ajaxRemoteFunction( 'ajaxcontainer_'+divid, '/admin/assets/view/0/'+divid );
}
function setFolderIdForUpload(folderid)
{
	currentAssetFolderId = folderid;
	var frame = getFrame('assetListUploaderFrame');
	var inp = frame.getElementById('assetUploadFolderId');
	if(inp)
		inp.value = folderid;
	else
		window.setTimeout("setFolderIdForUpload('"+folderid+"')",100);
}
function getFrame( frameIdName )
{
	d = document;
	f = d.frames ? d.frames[frameIdName] : d.getElementById(frameIdName);
	p = f.document || f.contentWindow.document;
	return p;
}

/**
 * AJAX
 */

function submitAjaxForm(formId,updatedivid)
{ 
	var actionurl = document.getElementById(formId).action;
	new Ajax.Updater(updatedivid, actionurl, {asynchronous:true, evalScripts:true, parameters:Form.serialize(formId), requestHeaders:['X-Update', updatedivid]}) 
}
function ajaxRemoteFunction(domid, url)
{
	new Ajax.Updater(domid,url, {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', domid]});
}
function ajaxRemoteFunctionTimed(domid, url, tt)
{
	window.setTimeout("ajaxRemoteFunction('"+domid+"','"+url+"')", tt);
}
function keyPressHandler(e, domid, url) 
{
	return keyPressHandlerFunc(e, "ajaxRemoteFunction('"+domid+"', '"+url+"')");
}
function keyPressHandlerFunc(e, func) 
{
	var kC  = (window.event) ?    // MSIE or Firefox?
		event.keyCode : e.keyCode;
	var Esc = (window.event) ?   
		27 : e.DOM_VK_ESCAPE // MSIE : Firefox
	//alert(kC);
	if(kC==Esc)
	{
		window.setTimeout(func, 10);
	}
	return true;
}


/**
 * ALL MIGHTY HIDER 
 */
 
var HidedElements = new Array();
var HideDuration = 0.4;

function switchHideElement( elemid )
{
	if( HidedElements[elemid]!=undefined )
	{
		new Effect.BlindDown(elemid, {duration: HideDuration});
		HidedElements[elemid] = undefined;
		document.getElementById('imgbtn_'+elemid).src = '/pb/img/masp_titleopened.gif';
	}
	else
	{
		new Effect.BlindUp(elemid, {duration: HideDuration});
		HidedElements[elemid] = true;
		document.getElementById('imgbtn_'+elemid).src = '/pb/img/masp_titleclosed.gif';
	}
}
function resetHideElement( elemid )
{
	HidedElements[elemid] = undefined;
}
function doHideElement( elemid )
{
	if( HidedElements[elemid]==undefined )
	{
		new Effect.BlindUp(elemid, {duration: HideDuration});
		HidedElements[elemid] = true;
		document.getElementById('imgbtn_'+elemid).src = '/pb/img/masp_titleclosed.gif';
	}
}
function undoHideElement( elemid )
{
	if( HidedElements[elemid]!=undefined )
	{
		new Effect.BlindDown(elemid, {duration: HideDuration});
		HidedElements[elemid] = undefined;
		document.getElementById('imgbtn_'+elemid).src = '/pb/img/masp_titleopened.gif';
	}
}



/**
 * Sonstige Helferlein
 */

function setAttrib(elm, attrib, value) 
{
	if (typeof(value) == "undefined" || value == null)
		value = "";
		
	if (value != "") 
	{
		elm.setAttribute(attrib, value);

		if (attrib == "style")
			attrib = "style.cssText";

		if (attrib == "width") 
		{
			attrib = "style.width";
			value = value + "px";
			value = value.replace(/%px/g, 'px');
		}

		if (attrib == "height") 
		{
			attrib = "style.height";
			value = value + "px";
			value = value.replace(/%px/g, 'px');
		}

		if (attrib == "class")
			attrib = "className";

		eval('elm.' + attrib + "=value;");
	} else
		elm.removeAttribute(attrib);
}

function makeAttrib(attrib, value) 
{
	if (typeof(value) == "undefined" || value == null)
		value = "";
		
	if (value == "")
		return "";

	// XML encode it
	value = value.replace(/&/g, '&amp;');
	value = value.replace(/\"/g, '&quot;');
	value = value.replace(/</g, '&lt;');
	value = value.replace(/>/g, '&gt;');

	return ' ' + attrib + '="' + value + '"';
}

function reloadLightbox()
{
	myLightbox.updateImageList();
}




