var interval = "";
var leuchtpultPos = 0;
var draggables = new Array();
var imagesContainer = new Array();
var	imagesContainerLength = 0;
var leftArrowEnabled = false;
var rightArrowEnabled = true;
var bMouseOverImage = false;
var hoverContainerId = "";
var dragPics = 0;
var checkIfOnHover = false;
var leftObserver = false;
var rightObserver = false;
var dropZone = 0;
var leuchtpultCoordY = 0;
var leuchtpultCoordX = 0;
var scrollOffX = 0;
var scrollOffY = 0;
var leuchtpultOnResize = false;
var url = "";
var oldElementId = "";
var accordionBlindUp = false;
var accordionBlindDown = false;
var infoDialogTimeout = 0;
var popupImageId = "";
var imageUid = "";
var infoMessage = '';
var leuchtpultOnShow = false;
var ifNoLeuchtpult = false;
var parkedImage = '';
var actLeuchtpult = '';
var onPopup = false;

/*Set "true" to enable drag&drop*/
var draggablesEnabled = false;

/*Switch trough fixed & absolute positioned Lightbox*/
var fixedLeuchtpult = true;


function draggablePictures(){

	checkSlidePossibilityOnload();
	rightArrowEnabled = false;
	imagesContainer = $A($('leuchtpult-pics-only').getElementsByClassName('media-pics'));
	imagesContainerLength = imagesContainer.length;
	mouseOverOnload();
	dragPics = $A($('main-column').getElementsByClassName('draggables'));
	dragPicsLength = dragPics.length;
	if (draggablesEnabled){
		for (i=0; i < dragPicsLength; i++){
			enableDragPics(dragPics[i]);	
		}
	}else{
		for (i=0; i < dragPicsLength; i++){
			checkIfSelectedYet(dragPics[i]);
		}
	}
	droppableZones(dragPics,dragPicsLength);
	
}

function droppableZones(dragPics,dragPicsLength){
	dropZone = Droppables.add('media-nav', {
		accept: 'draggables',
		onDrop: insertImage }
	);
			
}

function hideLeuchtpultImages() {
	for(i = leuchtpultPos; i < imagesContainerLength; i++) {
		imagesContainer[i].hide();
	}
}

function showLeuchtpultImages() {
	for(i = 1; i < 7; i++) {
		imagesContainer[imagesContainerLength-i].show();
	}
}

function resizeImage(dropImage){
	dropImage.height -= 3;
	if(dropImage.height<=65){
		clearInterval(interval);
		dropImage.height = 63;
	}
}

function slideToLeft(){

	//Falls man sich vor der Linksverschiebung am Schluss der Navigation befindet:
	//->Rechter Pfeil einblenden und das Onclick zuweisen
	if (imagesContainerLength==(leuchtpultPos+6)){
		enableRightArrow();
	}

	// Falls L�nge der Navigation gr�sser ist wie angezeigt und wir uns nicht am Anfang der Navigation befinden:
	//->Vorheriges Bild einblenden, letztes Bild ausblenden und Position des ersten Bildes der Nav neu setzen
	if (imagesContainerLength>6){
		if(leuchtpultPos>0)	{
			imagesContainer[leuchtpultPos-1].show();
			imagesContainer[leuchtpultPos+5].hide();
			leuchtpultPos--;
		}
	}
	//Checkt ob eine Links- oder Rechtverschiebung nach der vorherigen Verschiebung noch m�glich ist
	checkSlidePossibility();
}

function slideToRight(){

	//Falls man sich vor der Rechtsverschiebung am Anfang der Navigation befindet:
	//->Linker Pfeil einblenden
	enableLeftArrow();

	// Falls L�nge der Navigation gr�sser ist als angezeigt und wir uns nicht am Schluss der Navigation befinden:
	//->Erstes Bild ausblenden, n�chstes Bild einblenden und Position des ersten Bildes der Nav neu setzen
	if (imagesContainerLength>(leuchtpultPos+6)){
		imagesContainer[leuchtpultPos].hide();
		imagesContainer[leuchtpultPos+6].show();
		leuchtpultPos++;
	}
	//Checkt ob eine Links- oder Rechtverschiebung nach der vorherigen Verschiebung noch m�glich ist
	checkSlidePossibility();
}

function checkSlidePossibility(){
	if (imagesContainerLength==(leuchtpultPos+6)){
		disableRightArrow();
	}
	if (leuchtpultPos == 0){
		disableLeftArrow();
	}
}

function checkSlidePossibilityOnload(){
	disableRightArrow();
	if (imagesContainerLength>6){
		if (!leftObserver){
			$('navLeftArrow').observe('click', slideToLeft);
			leftObserver = true;
			leftArrowEnabled = true;
		}
		leuchtpultPos = imagesContainerLength - 6;
	} else {
		new Effect.Opacity('navLeftArrow', {duration:0.5, from:1.0, to:0.3});
		leftArrowEnabled = false;
	}

}

function checkIfSelectedYet(element){
	/*dropImages = $('leuchtpult-pics-only').getElementsByClassName('dropImage');
	dropImagesLength = dropImages.length;
	for (j=0; j<dropImagesLength; j++){
		if(dropImages[j].id.replace(/droppic/,"")==element.id.replace(/dragpic/,"")){
			i=element.id.replace(/dragpic/,"")
			new Effect.Opacity(element, {duration:0.5, from:1.0, to:0.3});
			draggables[element.id].destroy();
			Element.writeAttribute($('insertImageToLeuchpult'+i), {onclick: ''});
		}
	}*/
	
	
	try {
		id = element.id.replace(/dragpic/,"droppic");
		iItL = element.id.replace(/dragpic/,"insertImageToLeuchpult");
		$(id).id;
		//i=element.id.replace(/dragpic/,"")
		if($('main-template') == null){
			if(!leuchtpultOnShow){
				new Effect.Opacity(element, {duration:0.5, from:1.0, to:0.3});
			}
			if(draggablesEnabled){
				draggables[element.id].destroy();
			}
		}
		
	} catch (e) {
		new Effect.Opacity(element, {duration:0, from:1, to:1});
		
	}
}

function popup(link){
	height = $('popup-container').height
	var win = new Window(
	{className: "dialog",
	width:487,
	height: 513,
	zIndex:20000,
	resizable: false,
	closable: true,
	destroyOnClose: true,
	onClose: resetPopup,
	title: "",
	showEffect: Effect.Appear,
	hideEffect: Effect.Fade,
	draggable: false
	});
	
	win.setURL(url);
	//win.setAjaxContent(url);
	win.setStatusBar("Status bar info");
	//win.updateHeight(height);
	win.showCenter();
	
}

function resetPopup(){
	onPopup =  false;	
}

function enableLeftArrow(){
	if (!leftArrowEnabled){
		new Effect.Opacity('navLeftArrow', {duration:0.5, from:0.3, to:1.0});
		if (!leftObserver){	
			$('navLeftArrow').observe('click', slideToLeft);
			leftObserver = true
			leftArrowEnabled = true;
		}
		
	}
}

function disableLeftArrow(){
	if (leftArrowEnabled){
		new Effect.Opacity('navLeftArrow', {duration:0.5, from:1.0, to:0.3});
		if (leftObserver){
			Event.stopObserving('navLeftArrow', 'click', slideToLeft);
			leftObserver = false;
			leftArrowEnabled = false;
		}
	}
}

function enableRightArrow(){
	if (!rightArrowEnabled){
		new Effect.Opacity('navRightArrow', {duration:0.5, from:0.3, to:1.0});
		if (!rightObserver)	{
			$('navRightArrow').observe('click', slideToRight);
			rightObserver = true;
			rightArrowEnabled = true;
		}
	}
}

function disableRightArrow(){
	if (rightArrowEnabled){
		new Effect.Opacity('navRightArrow', {duration:0.5, from:1.0, to:0.3});
		if (rightObserver){
			Event.stopObserving('navRightArrow', 'click', slideToRight);
			rightObserver = false;
			rightArrowEnabled = false;
		}
	}
}

function mouseOverOnload(){
	//�bergibt den Images auf dem Leuchtpult mouseover und mouseout Eigenschaften
	for (i=0 ; i < imagesContainerLength ; i++){
		// Erteilt den LeuchtpultContainer, falls sich kein "leeres Bild" darin befindet eine Id
		
		tempImage = $A($('pic-slider').getElementsByClassName('dropImage'))[i];
		
		if (tempImage.src.match(/[\/|\\]([^\\\/]+)$/)[1] != "media-pic-empty.gif"){
			imagesContainer[i].id = "imagesContainer"+i;
		}
		// Event beobachten (Wenn Hoover auf Image)
		Event.observe(imagesContainer[i], 'mouseover', effectOnMouseOver);
		
	}
	// Event beobachten (Wenn Hoover auf pic-slider-content)
	Event.observe('media-nav', 'mouseover', effectOnMouseOut);
}

function effectOnMouseOver(event){
	hoverImage = event.element();
	temp = hoverImage.ancestors();
	hoverContainerId = temp[0].id;
	//Setzt boolian auf true (Hoover auf Bild)
	bMouseOverImage = true;
	//Falls es sich nicht um ein leeres Bild handelt:
	if(hoverImage.src.match(/[\/|\\]([^\\\/]+)$/)[1] != "media-pic-empty.gif"){
		//Speichert X/Y-Koordinaten des Images im Bezug zur Seite
		coordinateOfImageContainer = hoverImage.cumulativeOffset();
		//Setzt das versteckte Trashbin links unten auf dem Image
		
		if ( navigator.userAgent.toLowerCase().indexOf( "msie 6" ) != -1){
			$('trashbin').setStyle ({
				top: coordinateOfImageContainer["top"]+47+"px",
				left: coordinateOfImageContainer["left"]+"px"
			});
			
		}else{
			$('trashbin').setStyle ({
				top: (coordinateOfImageContainer["top"]-scrollOffY+47)+"px",
				left: (coordinateOfImageContainer["left"]-scrollOffX)+"px"
			});
		}
			//Deckt den Trashbin auf
			$('trashbin').show();
	//Falls es sich um ein leeres Bild handelt:
	} else {
		//Versteckt Trashbin
		$('trashbin').hide();
		bMouseOverImage = false;
	}
}

function effectOnMouseOut(){
	//Wenn kein Hover auf Bild
	if(bMouseOverImage===false){
		//Versteckt Trashbin
		$('trashbin').hide();
	}else{
		//Setzt boolian auf false (Kein Hoover auf Bild)
		bMouseOverImage = false;
	}
}

function trashBinOnClick(){
	
	deleteSelectedImages();
}

function deleteSelectedImages(){
	deletedImage = $A($(hoverContainerId).getElementsByClassName('dropImage'))[0];
	deletedImageId = deletedImage.id;
	if (imagesContainerLength <= 6){
		x = imagesContainer[imagesContainerLength-1].cloneNode(true);
		x.id = "";
		$(hoverContainerId).remove();
		effectOnMouseOut();
		Element.insert($("last-media-pic"),{before: x});
		imagesContainer = $A($('leuchtpult-pics-only').getElementsByClassName('media-pics'));
		imagesContainer.splice(imagesContainerLength,0,x);
		Event.observe(x, 'mouseover', effectOnMouseOver);
		checkSlidePossibility()
		
	}else{
	
		if (leuchtpultPos == imagesContainerLength-6){
			$(hoverContainerId).remove();
			imagesContainer = $A($('leuchtpult-pics-only').getElementsByClassName('media-pics'));
			imagesContainerLength--;
			effectOnMouseOut();
			leuchtpultPos--;		
			hideLeuchtpultImages();
			showLeuchtpultImages();
			checkSlidePossibility()
		}else{
			$(hoverContainerId).remove();
			imagesContainer = $A($('leuchtpult-pics-only').getElementsByClassName('media-pics'));
			imagesContainerLength--;
			effectOnMouseOut();
			hideLeuchtpultImages();
			for(i = leuchtpultPos; i < leuchtpultPos+6; i++) {
				imagesContainer[i].show();
			}
			checkSlidePossibility()
		}
	}
	reDragDeletedImages(deletedImageId);
	imageId = deletedImageId.replace(/droppic/,"");
	updateLeuchtpultRemove(imageId)
}

function reDragDeletedImages(deletedImageId){
	try{
		imageToReDrag = deletedImageId.replace(/droppic/,"dragpic");
		containerToReDrag = $(imageToReDrag).parentNode;
		enableReDragImage(containerToReDrag, imageToReDrag);
	}catch (e){
		
	}
}

function enableReDragImage(containerToReDrag, imageToReDrag){
	if(draggablesEnabled){
		draggables[imageToReDrag.replace(/droppic/,"dragpic")] = new Draggable(imageToReDrag,{ghosting:true, revert:true,
			endeffect: picDropped
		});
	}
	new Effect.Opacity(imageToReDrag, {duration:0.5, from:0.3, to:1.0});
	dragPics = $A($('main-column').getElementsByClassName('draggables'));
	imageToReDragUid = imageToReDrag.replace(/dragpic/,"");
}

function enableDragPics(element){

	try {
		if(draggablesEnabled){	
			draggables[element.id].destroy();
		}
	} catch (e) {
		
	}
	if($('main-template')== null){
		if(draggablesEnabled){	
			draggables[element.id] = new Draggable(element,{ghosting:true, revert:true,
				endeffect: picDropped
			});
		}
	/*}else{
		draggables[element.id] = element;
	*/
	}
	checkIfSelectedYet(element);
	
}

function picDropped (element){
	dropId	= element.id;
	dropId = dropId.replace(/dragpic/,"droppic");
	dragUid = dropId.replace(/droppic/,"");
	try{
		$(dropId).id;
		if($('main-template')== null){
			if(draggablesEnabled){
				draggables[element.id].destroy();
			}
			new Effect.Opacity(element, {duration:0.5, from:1.0, to:0.3});
		}
	} catch (e) {
		
	}
	
}

function clearPopupOnclick(iconToClear){

	//Element.writeAttribute(iconToClear, {onclick: "parent.openInfoDialog('<h1>Bild schon vorhanden</h1><p>Das Bild ist bereits in Ihrem Leuchtpult vorhanden.</p>\')"});
}

function dropImageOnIconClick(element){
	if (element){
		elementId = element.id.replace(/dragpic/,"");
		dropImages = $A($('leuchtpult-pics-only').getElementsByClassName('dropImage'));
		dropImagesLength = dropImages.length;
		alreadyInserted = false;
		for(i=0; i < dropImagesLength ;i++){
			dropImagesId = dropImages[i].id.replace(/droppic/,"");
			if(elementId == dropImagesId){
				alreadyInserted = true
				i = dropImagesLength
			}
		}
		if(!alreadyInserted){
			insertImage(element);
			picDropped (element);
			if($('main-template')!= null && ($('leuchtpultSelect').value != "")){
				openInfoDialog("<h1>Bild hinzugef&uuml;gt</h1><p class=\"infoMessage\">Das Bild wurde dem aktiven Leuchtpult hinzugef&uuml;gt.</p>");
			}
		}else{
			openInfoDialog('<h1>Bild schon vorhanden</h1><p>Das Bild ist bereits in Ihrem Leuchtpult vorhanden.</p>')
		}
	}
}

function insertImage(element){
	if($('leuchtpultSelect').value == ""){
		newLeuchtpultPopup();
		parkedImage = element;
		ifNoLeuchtpult = true;
	}else{
		dropImages = $A($('leuchtpult-pics-only').getElementsByClassName('dropImage'));
		imagesContainer = $A($('leuchtpult-pics-only').getElementsByClassName('media-pics'));
		dropImagesLength = dropImages.length;
		for (i=0; i < dropImagesLength; i++){
			dropImage = dropImages[i];
			if (imagesContainer[i].id!="last-media-pic"){
				imagesContainer[i].id = "imagesContainer"+i;
			}
			if (dropImage.src.match(/[\/|\\]([^\\\/]+)$/)[1] == "media-pic-empty.gif") {
	
				if (imagesContainer[i].id=="last-media-pic"){
	
					x = imagesContainer[i].cloneNode(true);
					x.id = "";
	
					//Am Ende der Bildnavigation: versteckt alle Bilder
					hideLeuchtpultImages();
	
					// Neues Element erzeugen
					dropImage = $A(x.getElementsByClassName('dropImage'))[0];
					Element.insert($("last-media-pic"),{before: x});
					imagesContainer.splice(imagesContainerLength-1,0,x);
					imagesContainer[imagesContainerLength-1].id = "imagesContainer"+(imagesContainerLength-1);
					//Erteilt dem neuen Element den Rollover-Effekt und dessen Funktion
					Event.observe(x, 'mouseover', effectOnMouseOver);
					//if (tempImage.src.match(/[\/|\\]([^\\\/]+)$/)[1] != "media-pic-empty.gif"){
					//}
					imagesContainerLength++;
					
					// Bilder wieder einblenden
					showLeuchtpultImages();
					leuchtpultPos = imagesContainerLength-6;
					
					if (imagesContainerLength>6){
						enableLeftArrow();
					}
	
					disableRightArrow();
				}
	
				i = dropImagesLength;
	
			}
		}
		

		dropImage.src = element.src;
		dropImage.height = element.height;
		dropImage.id = element.id.replace(/dragpic/,"droppic");
		interval = setInterval("resizeImage(dropImage)", 20);
		imageId = dropImage.id.replace(/droppic/,"");
		
		updateLeuchtpultInsert(imageId);
	}	
}

/**********************************************/
/******** Mediathek Draggables Effects ********/
/**********************************************/

function checkViewportOffset(imageToEnlargeTemp, bigSizedImageSrcTemp, bigSizedImageHeight, bigSizedImageWidth, contImageToEnlargeTemp){
	checkIfOnHover = true
	imageToEnlarge = imageToEnlargeTemp;
	bigSizedImageSrc = bigSizedImageSrcTemp;
	contImageToEnlarge = contImageToEnlargeTemp;
	
	
	leftScrollOffset = document.viewport.getScrollOffsets().left;
	topScrollOffset = document.viewport.getScrollOffsets().top;
		
	viewportCoordinatesX = document.viewport.getDimensions().width+leftScrollOffset;
	/*125 = H�he des Leuchtpultes*/
	viewportCoordinatesY = (document.viewport.getDimensions().height)-115+topScrollOffset;
	
	leftCoordinatesOfElement = $(imageToEnlarge).cumulativeOffset()["left"];

	topCoordinatesOfElement =  $(imageToEnlarge).cumulativeOffset()["top"];
	
	
	elementHeight = parseFloat(bigSizedImageHeight);
	elementWidth = parseFloat(bigSizedImageWidth);
	
	
	if (elementHeight+topCoordinatesOfElement > viewportCoordinatesY){
		topCoordinatesOfElement = viewportCoordinatesY-elementHeight;
	}
	
	if (elementWidth+leftCoordinatesOfElement+450 > viewportCoordinatesX){
		leftCoordinatesOfElement = leftCoordinatesOfElement - (elementWidth+60);
	}else{
		leftCoordinatesOfElement = leftCoordinatesOfElement+160;
	}

	timeoutAktive = setTimeout("createBigSizedImage(topCoordinatesOfElement, leftCoordinatesOfElement, imageToEnlarge, bigSizedImageSrc)", 1000);
		
}

function createBigSizedImage(topCoordinatesOfElement, leftCoordinatesOfElement, imageToEnlarge, bigSizedImageSrc){
	margin = elementHeight/2-16; 
	$('enlargedImage').setStyle({
		marginTop: margin+"px"
	});
	
	$('enlargedImage').src = 'fileadmin/templates/images/ajax-loader.gif';	
	
	$('enlargedImage-box').setStyle ({
		paddingTop: 22+"px",
		paddingBottom: 22+"px",
		height: elementHeight+"px",
		width: elementWidth+"px",
		top: topCoordinatesOfElement+"px",
		left: leftCoordinatesOfElement+"px"
	});
	
	
	
	$('enlargedImage').src = bigSizedImageSrc;
	$('enlargedImage').setStyle({
		marginTop: 0
	});
	//Deckt den Container auf
	if (checkIfOnHover == true){
		Effect.Appear('enlargedImage-box',{duration: 0.5});
	}
	
}

function destroyBigSizedImage(){
	checkIfOnHover = false;
	//$('enlargedImage').src = '';
	clearTimeout(timeoutAktive);
	$('enlargedImage-box').hide();
	
}

/*********************************************/
/******** Mediathek Content Functions ********/
/*********************************************/

function mediathekContentLoad(link){
	temp1 = Element.ancestors(link);
	if (temp1[0].id == 'act-header'){
		return false;	
	}
	temp2 = temp1[0].childElements()
	temp2 = temp2[0].nextSiblings();
	checkSubheaders = temp2.length;
	if(accordionBlindUp == false && accordionBlindDown == false ){
		if(checkSubheaders == 0){
			new Ajax.Updater('main-column', link.href, {onLoading: function(){Element.hide('main-column'); Element.show('loading-text')}, onComplete: onCompleteActionsMediathek});
		}
		
		prevActiveElement = $('act');
		if (prevActiveElement != null){
			prevActiveElement.id = '';	
		}
		if(checkSubheaders != 0 || temp1[0].className != "headers"){
			activeElement = temp1[0];
			activeElement.id = 'act';
		}
	}
}

function onCompleteActionsMediathek(){
	try{
		Element.hide('loading-text');
	}catch(e){
		
	}
	
	Effect.Appear('main-column',{duration: 0.5});
	draggablePictures();
	leuchtpultOnShow = false;
}

function popupContentLoad(link, uid){
	if(onPopup == false){
		onPopup = true
		imageUid = uid
		url = link+$('leuchtpultSelect').value;
		new Ajax.Updater('popup-container', url, {onLoading: Element.hide('popup-container'), onComplete: popup});
	}
}

function leuchtpultOptions(value){
	switch (value){
		case 'new':	
			newLeuchtpultPopup();
			break;
		case 'delete':
			if($('leuchtpultSelect').value != ""){
				deleteLeuchtpult();
			}
			break;
		case 'view':
			if($('leuchtpultSelect').value != ""){
				showLeuchtpult();
			}
			break;
		case 'zip':
			if($('leuchtpultSelect').value != ""){
				zipAndDownloadLeuchtpult();
			}
			break;
		break;
		case 'edit':
			if($('leuchtpultSelect').value != ""){
				editLeuchtpultPopup();
			}
			break;
		default:
			break;
	}

	$('optionsSelect').selectedIndex = 0;
	
}

function zipAndDownloadLeuchtpult(){
	url = $('tx_x4eleuchtpult_pi1_link').value+'&tx_x4eleuchtpult_pi1[action]=zip&tx_x4eleuchtpult_pi1[uid]='+$('leuchtpultSelect').value;
	dlIFrm = $('downloadIFrm');
	dlIFrm.src = url;
	return;
}

function showLeuchtpult(){
	leuchtpultOnShow = true;
	actLeuchtpult = $('leuchtpultSelect').value;
	link = 'index.php?id=113&no_cache=1&tx_mmdamfilelist_pi1[cat]='+actLeuchtpult;
	new Ajax.Updater('main-column', link, {onLoading: Element.hide('main-column'), onComplete: onCompleteActionsMediathek});
}

function newLeuchtpultPopup(){
	
	
	Dialog.confirm({url: "fileadmin/templates/new_leuchtpult.html", options: {method: 'get'}},
	{
	className: "dialog",
	width: 350,
	height: 90,
	zIndex:20000,
	okLabel: "senden",
	cancelLabel: "abbrechen",
	ok:function(win) {debug("validate confirm panel"); return true;},
	onOk: submitNewEntry,
	closable: false,
	resizable: false,
	overlayOpacity: 0.6,
	title: "Neues Leuchtpult erstellen",
	draggable: false,
	wiredDrag: false}
	);
	
	
/*
	win.getContent().innerHTML= $("popup-container-new-leuchtpult").innerHTML;
	win.setStatusBar("Status bar info");
	win.showCenter();*/
	
}
function focusOnInput(){
	$('tx_x4eleuchtpult_pi1_name').focus();
}

function submitNewEntry(){
	if ($('tx_x4eleuchtpult_pi1_name').value == ''){
		popupWindow = $$('dialog');
		popupWindow.id = 'popup-window';
		new Effect.Shake(Windows.focusedWindow.getId());
	}else{
		if ( navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1) {
			$('tx_x4eleuchtpult_pi1_name').value = encode_utf8($('tx_x4eleuchtpult_pi1_name').value);
		}
		link = $('tx_x4eleuchtpult_pi1_link').value+'&tx_x4eleuchtpult_pi1[value]='+$('tx_x4eleuchtpult_pi1_name').value+'&tx_x4eleuchtpult_pi1[action]=new';
		new Ajax.Updater('selection-selects', link, {onComplete: displayLeuchtpult});
		
	}return true;
}

function deleteLeuchtpult(){
	actLeuchtpult = $('leuchtpultSelect').options[$('leuchtpultSelect').options.selectedIndex].text;
	$('confirm-delete-text').innerHTML = actLeuchtpult;
	Dialog.confirm($('popup-container-delete-alert').innerHTML,
	{className: "dialog",
	width: 350,
	height: 90,
	zIndex:20000,
	okLabel: "Ja",
	cancelLabel: "Nein",
	ok:function(win) {debug("validate confirm panel"); return true;},
	onOk: deleteAction,
	closable: false,
	resizable: false,
	overlayOpacity: 0.6,
	title: "Leuchtpult löschen",
	draggable: false,
	wiredDrag: false}
	);	
}

function deleteAction(){
	link = $('tx_x4eleuchtpult_pi1_link').value+'&tx_x4eleuchtpult_pi1[action]=delete&tx_x4eleuchtpult_pi1[uid]='+$('leuchtpultSelect').value;
	new Ajax.Updater('selection-selects', link, {onComplete: displayLeuchtpult});	
	return true;
}

function displayLeuchtpult(){
	link = $('tx_x4eleuchtpult_pi1_link').value+'&tx_x4eleuchtpult_pi1[action]=show&tx_mmdamfilelist_pi1[cat]='+$('leuchtpultSelect').value;
	/*document.write(link);*/
	if($('leuchtpultSelect').value != ""){
		new Ajax.Updater('leuchtpult-pics-only', link, {onComplete: initLeuchtpult});	
	}else{
		$('navLeftArrow').hide();
		$('pic-slider').hide();
	}
	
	return true;
}

function initLeuchtpult(){
	imagesContainer = $A($('leuchtpult-pics-only').getElementsByClassName('media-pics'));
	imagesContainerLength = imagesContainer.length;
	
	new Effect.Opacity('navRightArrow', {duration:0.5, from:0.3, to:1.0});
	rightArrowEnabled = true;
	
	new Effect.Opacity('navLeftArrow', {duration:0.5, from:0.3, to:1.0});
	leftArrowEnable = true;
	
	if (imagesContainerLength == 0){
		x = $('last-media-pic-temp').cloneNode(true);
		Element.insert($('leuchtpult-pics-only'),{bottom: x});
		x.id = '';
		imagesContainer = $A($('leuchtpult-pics-only').getElementsByClassName('media-pics'));
		imagesContainerLength = imagesContainer.length;
	}
	
	if (imagesContainerLength<6){
		lastPic = imagesContainer[imagesContainerLength-1];
		for (i=1; imagesContainerLength+i<7; i++ ){
			x = $('last-media-pic-temp').cloneNode(true);
			x.id = "";
			Element.insert(lastPic,{after: x});
			lastPic = x;
		}
		x.id = 'last-media-pic';
	}else{
		lastPic = imagesContainer[imagesContainerLength-1];
		x = $('last-media-pic-temp').cloneNode(true);
		x.id = "";
		Element.insert(lastPic,{after: x});
		lastPic = x;
		x.id = 'last-media-pic';
	}
		
	imagesContainer = $A($('leuchtpult-pics-only').getElementsByClassName('media-pics'));
	imagesContainerLength = imagesContainer.length;
	for(i = 1; i < 7; i++) {
		Effect.Appear(imagesContainer[imagesContainerLength-i]);
	
	}
	leuchtpultPos = imagesContainerLength-6;
	
	if (rightObserver){
		Event.stopObserving('navRightArrow', 'click', slideToRight);
		rightObserver = false;
	}
	
	if (leftObserver){
		Event.stopObserving('navLeftArrow', 'click', slideToLeft);
		leftObserver = false;
	}
	draggablePictures();
	$('navLeftArrow').show();
	$('pic-slider').show();
	
	if(ifNoLeuchtpult == true){
		dropImageOnIconClick(parkedImage);
		
		
		
		ifNoLeuchtpult = false;
		
		
	}
	/*checkSlidePossibilityOnload();
	rightArrowEnabled = false;
	imagesContainer = $('leuchtpult-pics-only').getElementsByClassName('media-pics');
	imagesContainerLength = imagesContainer.length;
	mouseOverOnload();*/
}

function updateLeuchtpultInsert(imageId){
	link = $('tx_x4eleuchtpult_pi1_link').value+'&tx_x4eleuchtpult_pi1[action]=insert_image&tx_x4eleuchtpult_pi1[uid]='+$('leuchtpultSelect').value+'&tx_x4eleuchtpult_pi1[uid_local]='+imageId;
	new Ajax.Request(link);	
	return true;
}

function updateLeuchtpultRemove(imageId){
	link = $('tx_x4eleuchtpult_pi1_link').value+'&tx_x4eleuchtpult_pi1[action]=remove_image&tx_x4eleuchtpult_pi1[uid]='+$('leuchtpultSelect').value+'&tx_x4eleuchtpult_pi1[uid_local]='+imageId;
	new Ajax.Request(link);	
	return true;
}

function editLeuchtpultPopup(){
	
	Dialog.confirm({url: "fileadmin/templates/new_leuchtpult.html", options: {method: 'get'}},
	{className: "dialog",
	width: 350,
	height: 90,
	zIndex:20000,
	okLabel: "senden",
	cancelLabel: "abbrechen",
	ok:function(win) {debug("validate confirm panel"); return true;},
	onOk: editLeuchtpult, 
	closable: false,
	resizable: false,
	overlayOpacity: 0.6,
	title: "Name des Leuchtpultes Ändern",
	draggable: false,
	wiredDrag: false}
	);
	
	
/*
	win.getContent().innerHTML= $("popup-container-new-leuchtpult").innerHTML;
	win.setStatusBar("Status bar info");
	win.showCenter();*/
	insertName =setInterval("showLeuchtpultName()", 125);
}

function showLeuchtpultName(){
	if(typeof $('tx_x4eleuchtpult_pi1_name').value!="undefined"){
		$('tx_x4eleuchtpult_pi1_name').value = $('leuchtpultSelect').options[$('leuchtpultSelect').options.selectedIndex].text;
		clearInterval(insertName);
	}
}

function editLeuchtpult(){

	if ($('tx_x4eleuchtpult_pi1_name').value == ''){
		popupWindow = $$('dialog');
		popupWindow.id = 'popup-window';
		new Effect.Shake(Windows.focusedWindow.getId());
	}else{
		link = $('tx_x4eleuchtpult_pi1_link').value+'&tx_x4eleuchtpult_pi1[value]='+$('tx_x4eleuchtpult_pi1_name').value+'&tx_x4eleuchtpult_pi1[action]=edit&'+'tx_x4eleuchtpult_pi1[uid]='+$('leuchtpultSelect').value;
		new Ajax.Updater('selection-selects', link);	
		return true;
	}
}

function scrollLeuchtpult(){
	if(!fixedLeuchtpult){
		leuchtpultCoordY = (document.viewport.getDimensions().height);
		leuchtpultCoordX = $('topimage').cumulativeOffset()["left"];
		if (leuchtpultOnResize == true) {
			$('footer-wrap-media').hide();
		}
		scrollAction();
		
		if (leuchtpultOnResize == false){
			if($('main-template')== null){
				Effect.Appear('footer-wrap-media',{duration: 0.5});
			}
			leuchtpultOnResize = true;
			document.observe('scroll' , enableScrollFunctions);
			
			if ( navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1 ) {
				
				window.onscroll = enableScrollFunctions;
			}
			
		window.onresize = scrollLeuchtpult;
		
		} else {
			if($('main-template')== null){
				$('footer-wrap-media').show();
			}
		}
	}else{
		
		if ( navigator.userAgent.toLowerCase().indexOf( "msie 6" ) != -1 ) {
			window.onresize = ie6ResizeLeuchtpult;
			leftIe6 = $('topimage').cumulativeOffset()["left"]
			$('footer-wrap-media').setStyle ({	
				marginLeft: leftIe6+"px"
			});
		}
		if($('main-template')== null){
			$('footer-wrap-media').show();
		}
	}
}

function enableScrollFunctions(){
	scrollAction();
}

function scrollAction(){
	scrollOffX = document.viewport.getScrollOffsets().left;
	scrollOffY = document.viewport.getScrollOffsets().top;
	
	$('footer-wrap-media').setStyle ({
		left: (leuchtpultCoordX+scrollOffX)+"px",
		top: ((leuchtpultCoordY+scrollOffY)-130)+"px"
			
	});
	$('trashbin').hide();


}

function modelleContentLoad(link){
	temp1 = Element.ancestors(link);
	temp2 = temp1[0].childElements()
	temp2 = temp2[0].nextSiblings();
	checkSubheaders = temp2.length;
	if(accordionBlindUp == false && accordionBlindDown == false ){
		if(checkSubheaders == 0){
			new Ajax.Updater('main-column', link.href, {onLoading: Element.hide('main-column'), onComplete: onCompleteActionsModelle});
		}
		
		prevActiveElement = $('act');
		if (prevActiveElement != null){
			prevActiveElement.id = '';	
		}
		if(checkSubheaders != 0 || temp1[0].className != "headers"){
			activeElement = temp1[0];
			activeElement.id = 'act';
		}
	}
}

function onCompleteActionsModelle(){

	Effect.Appear('main-column',{duration: 0.5});

}

function doSearch(formO){
	//alert(formO.action);
	//return true;
	
}

function loadAccordions(){
	accordionHeaders = $A($('pw-nav').getElementsByClassName('headers')); 
	accordionHeadersLength = accordionHeaders.length;
	for (i=0; i < accordionHeadersLength; i++){
		accordionHeaders[i].id = "accordion"+i;
		temp = accordionHeaders[i].childElements();
		//console.dir(temp);
		//onclick = temp;
		//onclick = temp[0];
		/* causees error in ff9.0.1 */
		Event.observe(temp[0], 'click', accordionEffects);
	}		
	blindDownFirstHeader();
}

function blindDownFirstHeader(){
	temp = $A($('pw-nav').getElementsByClassName('headers'));
	oldElementId = temp[0].id;
	temp[0].id = 'act-header';
	subheaders = $('act-header').childElements();
	if(subheaders.length > 1){
		subheaders[1].id = 'act-subheader';
		accordionBlindDown = true
		accordionIntervalBD = setInterval("clearAccordionInterval()", 1200)
		Effect.BlindDown('act-subheader', {duration:1});
	}else{
		temp[0].id = oldElementId;
	}
}

function accordionEffects(event){
	//alert (accordionOnEffect);
	if(accordionBlindUp == false && accordionBlindDown == false){	
		elements = event.element().ancestors();
		element = elements[0];
		if (element.id != "act-header"){
			accordionSubHeader = $A($(element.id).getElementsByClassName('subheaders'));
			if($('act-subheader')!= null) {
				accordionBlindUp = true
				accordionIntervalBU = setInterval("clearAccordionInterval()" , 1200);
				Effect.BlindUp('act-subheader', {duration:1});
				$('act-subheader').id = '';
			}
			if ($('act-header')!= null){
				$('act-header').id = oldElementId;
				oldElementId = element.id;
				element.id = 'act-header';
			}else{
				oldElementId = element.id;
				element.id = 'act-header';
			}
			if(typeof accordionSubHeader[0] != "undefined") {
				accordionSubHeader[0].id = 'act-subheader';
				accordionBlindDown= true
				accordionIntervalBD = setInterval("clearAccordionInterval()", 1200)
				Effect.BlindDown('act-subheader', {duration:1});
			}
		}
	}
}

/*
if ($('act-header')!= null){
				oldATag = $('act-header').getElementsByClassName('a-header');
				Element.writeAttribute($(oldATag[0]), {onclick: 'mediathekContentLoad(this);blurLink(this);return false;'});
				$('act-header').id = oldElementId;
				oldElementId = element.id;
				element.id = 'act-header';
				aTag = $('act-header').getElementsByClassName('a-header');
				Element.writeAttribute($(aTag[0]), {onclick: 'return false;'});
			}else{
				oldElementId = element.id;
				element.id = 'act-header';
				aTag = $('act-header').getElementsByClassName('a-header');
				Element.writeAttribute($(aTag[0]), {onclick: 'return false;'}); 
			}
*/

function clearAccordionInterval(){
	if (accordionBlindUp == true){
		clearInterval(accordionIntervalBU);
		accordionBlindUp = false
	}
	
	if(accordionBlindDown == true){
		clearInterval(accordionIntervalBD);
		accordionBlindDown = false
	}
}

function addToFavorites(addToFavorites_link){
	var href = addToFavorites_link.href;
	new Ajax.Request(href, {onComplete: openInfoDialog("<h1>Zu Ihren Favoriten hinzugef&uuml;gt</h1><p>Die Pressemitteilung ist in Ihren Favoriten zu finden</p>",true)});
	//new Ajax.Request(href, {onComplete: alert('ok')});
}

function openInfoDialog(content,ignoreLeuchtpult) { 
	if (!ignoreLeuchtpult) {
		if ($('leuchtpultSelect')!= null){
			actLeuchtpult = $('leuchtpultSelect').options[$('leuchtpultSelect').options.selectedIndex].text;
		}else{
			alert('test3');
			actLeuchtpult = parent.$('leuchtpultSelect').options[parent.$('leuchtpultSelect').options.selectedIndex].text;
		}
	}
	
	infoMessage = content;	
	infoDialogTimeout=3;
	Dialog.info(infoMessage,
		{width:450,
		 height:100,
		 showProgress: true}
	);
	setTimeout(infoTimeout, 1000)
	
}

function infoTimeout() {
	infoDialogTimeout--;
	if (infoDialogTimeout >0) {
		Dialog.setInfoMessage(infoMessage);
		setTimeout(infoTimeout, 1000);
	}else{
		Dialog.closeInfo();
	}
}

function ie6ResizeLeuchtpult(){
	leftResize = $('topimage').cumulativeOffset()["left"];
	$('footer-wrap-media').setStyle({
		marginLeft: leftResize+"px"
	});
}

function encode_utf8( s )
{
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s )
{
  return decodeURIComponent( escape( s ) );
}
