var loginVisible = false;
var meta = '';
var meta2 = '';



document.observe('dom:loaded', function () { 
	
	resizeBackground();
	
	Event.observe(window, "resize", resizeBackground);
	
	$$('.lightbox_close').each(function(element) {
		Event.observe(element, 'click', lightboxclose);
	});
	
	$$('.file_upload_form_cancel').each(function(element) {
		Event.observe(element, 'click', function () {
			Effect.Fade('file_upload_form_layer', { duration:1.0 });
		});
	});
	$$('.files_upload_link').each(function(element) {
		Event.observe(element, 'click', uploadFile);
	});
	$$('.file_list_row').each(function(element) {
		Event.observe(element, 'click', fileClick);
	});
	$$('.collapse_directory_link').each(function(element) {
		Event.observe(element, 'click', collapseDirectory);
	});
	$$('.expand_directory_link').each(function(element) {
		Event.observe(element, 'click', expandDirectory);
	});
	$$('.open_directory_link').each(function(element) {
		Event.observe(element, 'click', openDirectory);
	});
	$$('.files_new_dir_link').each(function(element) {
		Event.observe(element, 'click', newDirectory);
	});
	$$('.files_rename_link').each(function(element) {
		Event.observe(element, 'click', renameItem);
	});
	$$('.files_delete_link').each(function(element) {
		Event.observe(element, 'click', deleteItem);
	});
	$$('.files_download_link').each(function(element) {
		Event.observe(element, 'click', downloadItem);
	});
	
	$$('.news_delete_link').each(function(element) {
		Event.observe(element, 'click', confirmDelteNews);
	});
	
	
	$$('.intern_login_show').each(function(element) {
		Event.observe(element, 'click', showLoginForm);
	});
	
	// add click event handler 
	$$('.news_item_header').each(function(element) {
		var cns = element.classNames();
		var cna = cns.toArray();
		var eu = element.down('a');
		eu.addClassName(cna[1]);
		Event.observe(eu, 'click', function(event) {
			var cns = $(this).classNames();
			var cna = cns.toArray();
			
			// set urls (making it linkable)
			window.location.hash = '#news' + cna[0].replace(/news_content_/g, '');
			
			openNews(cna[0]);
			
			Event.stop(event);
			
		});
	});
	
	
	h = window.location.hash;
	if (h) {
		// open selected news
		id = h.replace(/#news/g, 'news_content_');
		
		item = $(id);
		if (!item) {
			if (window.location.href.indexOf('/pinnwand-archiv/') == -1) {
				window.location.href = 'http://you-th.de/pinnwand/pinnwand-archiv/' + h;
			}
		}
		
		openNews(id);
	}
	else {
		// open first news on startup
		var ar = $$('.news-latest-container > .news-latest-item:first-child .news-latest-content-wrapper').toArray();
		if (ar[0]) {
			openNews(ar[0].id);
		}
	}
});

function resizeBackground() {
	var width = document.viewport.getWidth();
	var height = document.viewport.getHeight();
	$('bg_image').setStyle({maxWidth : width + 'px'});
	$('bg_image').setStyle({minWidth : width + 'px'});
	$('bg_image').setStyle({minHeight : height + 'px'});
}


/* #BEGIN - Lightbox */
function lightboximage(imgUrl) {
	$('lightbox_image').src = imgUrl;
	
	Effect.Fade('secondBox', { duration:0.5, afterFinish: function() {
			Effect.Appear('lightbox', { duration:0.5 });
		}
		
	});
	
}
function lightboxclose(event) {
	Effect.Fade('lightbox', { duration:0.5, afterFinish: function() {
			Effect.Appear('secondBox', { duration:0.5 });
		}
		
	});
	
	Event.stop(event);
}
/* #END - Lightbox */


/* #BEGIN - FE-Files */
function uploadFile(event) {
	Effect.Appear('file_upload_form_layer', { duration:1.0 });
	
	Event.stop(event);
}

var lastSelectedListItem = null;
function fileClick(event) {
	if (lastSelectedListItem)
		$(lastSelectedListItem).removeClassName('file_list_row_active');
	
	$(this).addClassName('file_list_row_active');
	lastSelectedListItem = this;
	
	eval($(this).onclick);
	$('file_form_path').value = meta;
}

function downloadItem(event) {
	if (!meta2)
		eval($(this).onclick);
		
	$('file_form_action').value = 'download';
	$('file_form_path').value = meta2;
	$('file_form').submit();
		
	
	Event.stop(event);
}

function deleteItem(event) {
	if (meta2) {
		if (confirm('Möchten Sie "' + meta2 +'" wirklich unwiederruflich löschen?')) {
			
			$('file_form_action').value = 'delete';
			$('file_form_path').value = meta2;
			$('file_form').submit();
		}
	}
	
	Event.stop(event);
}

function renameItem(event) {
	if (meta2) {
		var r = prompt('Geben Sie einen neuen Namen für "' + meta2 +'" an:', meta2);
		
		if (r && r.trim() != '') {
			$('file_form_action').value = 'rename';
			$('file_form_path').value = meta2;
			$('file_form_rename').value = r;
			$('file_form').submit();
		}
	}
	
	Event.stop(event);
}

function newDirectory(event) {
	
	var r = prompt('Geben Sie einen Namen für den Ordner an:', 'Neuer Ordner');
	
	if (r && r.trim() != '') {
		$('file_form_action').value = 'newdir';
		$('file_form_path').value = r;
		$('file_form').submit();
	}
	
	Event.stop(event);
}

function openDirectory(event) {
	eval($(this).onclick);
	
	$('file_form_action').value = 'open';
	$('file_form_path').value = meta;
	$('file_form').submit();

	
	Event.stop(event);
}

function collapseDirectory(event) {
	eval($(this).onclick);
	
	$('file_form_action').value = 'collapse';
	$('file_form_path').value = meta;
	$('file_form').submit();

	
	Event.stop(event);
}

function expandDirectory(event) {
	eval($(this).onclick);
	
	$('file_form_action').value = 'expand';
	$('file_form_path').value = meta;
	$('file_form').submit();

	
	Event.stop(event);
}

/* #END - FE-Files */


function confirmDelteNews(event) {
	var con = confirm('Willst du diese News wirklich löschen?');
	
	if (!con)
		Event.stop(event);
}


function openNews(id) {
	if (!$(id).hasClassName('news-latest-open')) {		
		// close all open items
		$$('.news-latest-open').each(closeOpen);
		
		// open item
		$(id).setStyle({display : 'none'});
		$(id).removeClassName('news-latest-content-wrapper');
		Effect.SlideDown(id);
		
		// mark as open
		$(id).addClassName('news-latest-open');
	}
	else {
		closeOpen(id);
	}
}


function closeOpen(id) {
	$(id).removeClassName('news-latest-open');
	Effect.SlideUp(id);
	
}


function showLoginForm(event) {
	if (!loginVisible) {
		$('headerLogin').setStyle({display : 'none'});
		$('headerLogin').removeClassName('hidden');
		Effect.Appear('headerLogin', {duration:1.0, afterFinish: function() { $('user').focus(); } });
		loginVisible = true;
	}
	else {
		Effect.Fade('headerLogin', {duration:1.0});
		loginVisible = false;
	}
	
	Event.stop(event);
}
