
//--- admin control layers
function showAdminControl(controlid) {
	hideAllAdminControls(controlid);
	$('#'+controlid).show("fast");
}

function hideAdminControl(controlid) {
	$('#'+controlid).hide("fast");
}

function hideAllAdminControls(except) {
	$('div.admincontrol').each(function () {
		if (this.id != except) {
			$(this).hide("fast");
		}
	});
	$('div._admincontrol').each(function () {
		if (this.id != except) {
			$(this).hide("fast");
		}
	});
}

$(document).ready(function(){
	//--- click anywhere closes the admin controls
	$(document).click(function () { 
		hideAllAdminControls();
		hidePortalLayers();
	} );
});


//--- colorbox related stuff
$(document).ready(function(){
	$("a.picturepreview").colorbox({
		current: "Bild {current} von {total}",
		previous: "voriges",
		next: "nächstes",
		close: "schließen"
	});
});


//--- switch buttons overlays
$(document).ready(function(){
	$("#headerswitches > a > img").each(function () { 
		$(this).mouseover( function() {
			$(this).animate({opacity: 0.5}, 'fast');
		} );
		$(this).mouseout( function() {
			$(this).animate({opacity: 1}, 'fast');
		} );
	});
});


//--- projects clickable
$(document).ready(function(){
	$("ul.projects > li > div.inner > div.innerpadded").each(function () { 
		$(this).click(function () { 
			$(this).find('div > a').each(function () { 
				document.location = $(this).attr('href'); 
			});
		} );
	});
	$("ul.projects_archive > li > div.inner > div.innerpadded").each(function () { 
		$(this).click(function () { 
			$(this).find('div > a').each(function () { 
				document.location = $(this).attr('href'); 
			});
		} );
	});
});


//--- forms related preparation
$(document).ready(function(){
	var searchfield = $("#searchinput");
	if (searchfield) {
		searchfield.focus(function () { 
			if (this.value == 'Suchbegriff') {
				this.value = '';
			}
		} );
		searchfield.blur(function () { 
			if (this.value == '') {
				this.value = 'Suchbegriff';
			}
		} );
	}
	var logindiv = $("#boxlogindiv");
	if (logindiv) {
		var logindivoverlay = $('#logindivoverlay');
		var loginform = $('#boxlogindiv > form');
		if (loginform) {
			loginform.submit(function () {
				logindivoverlay.width(logindivoverlay.parent().width());
				logindivoverlay.height(logindivoverlay.parent().height());
				$('#logindivoverlay > image').css('margin', ''+parseInt((logindivoverlay.height()-31) / 2)+'px 0px 0px '+parseInt((logindivoverlay.width()-31) / 2)+'px');
				logindivoverlay.fadeIn(function() { 
					$.post("./?action=login", loginform.serializeArray(), function(data) {
						if (data.success) {
							window.location.reload();
						} else {
							logindiv.find("span.general_error").each(function () { $(this).html(data.errors); });
							loginform.find("input").each(function () { this.disabled = false; });
							loginform.find("input").each(function () { 
								if (this.name == 'fdsa') { this.select(); this.focus(); } 
								if (this.name == 'asdf') { this.value = ''; } 
							});
						}
						logindivoverlay.width(logindivoverlay.parent().width());
						logindivoverlay.height(logindivoverlay.parent().height());
						$('#logindivoverlay > image').css('margin', ''+parseInt((logindivoverlay.height()-31) / 2)+'px 0px 0px '+parseInt((logindivoverlay.width()-31) / 2)+'px');
						logindivoverlay.fadeOut();
					}, 'json');

					loginform.find("input").each(function () { this.disabled = true; });
				} );

				return false;
			} );
		}
	}
	
});


//--- sortable lists triggers
$(document).ready(function(){
	$("ul.slideshow[id*='cmssl']").sortable({
		update: function(event, ui) { 
			$.get("./?action=serializeupdate&" + ui.item.parent().sortable('serialize'), function(data) {
				if (data) {
					alert(data);
				}
			});
		}
	});
	$("ul.projects[id*='movable']").sortable({
		update: function(event, ui) { 
			$.get("./?action=serializeupdate&" + ui.item.parent().sortable('serialize'), function(data) {
				if (data) {
					alert(data);
				}
			});
		}
	});
	$("ul.projects_archive[id*='movable']").sortable({
		update: function(event, ui) { 
			$.get("./?action=serializeupdate&" + ui.item.parent().sortable('serialize'), function(data) {
				if (data) {
					alert(data);
				}
			});
		}
	});
});

//--- basecms forms loading please wait fixup
function formSubmit(f) {
	if (f._submitbutton) {
		if (f._submitbutton.length) {
			for (var i=0; i<f._submitbutton.length; i++) {
				f._submitbutton[i].value=' - - - - - - ';
				f._submitbutton[i].disabled=true;
			}
		} else {
			f._submitbutton.value=' - - - - - - ';
			f._submitbutton.disabled=true;
		}
	}
	if (f._cancelbutton) { 
		f._cancelbutton.value=' - - - - - - '; 
		f._cancelbutton.disabled=true;
	}
}

//--- standard popup stuff.
function popup(url, name, w, h) { 
	window.open(url, name, 'width='+w+',height='+h+',menubar=no,scrollbars=no,toolbars=no');
}

//--- portal javascript
function portalAppearLayer(type) {
	hidePortalLayers(type);
	$('#portal_overlay_' + type).slideDown("fast");
}

function hidePortalLayers(except) {
	$('div.portaloverlay').each(function () {
		if (this.id != 'portal_overlay_' + except) {
			$(this).slideUp("fast");
		}
	});
}

function mcvrt(f) {
	var i = f.innerHTML;
	if (!i)	{
		return;
	}

	i = i.replace(/ at /i, '@');
	i = i.replace(/ dot /i, '.');

	f.innerHTML = i;
}