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

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

	//--- overlay animation for the header
	function header_overlayme(boo) {
		if (boo.src.match(/_o.gif/)) {
			boo.src = boo.src.replace(/_o.gif$/, '.gif');
		} else {
			boo.src = boo.src.replace(/.gif$/, '_o.gif');
		}
	}
	$("img[id*='b_']").mouseover(function () { header_overlayme(this) } );
	$("img[id*='b_']").mouseout(function () { header_overlayme(this) } );

	//--- overlay animation over some buttons
	$("a.nextprevious").mouseover(function () { 
		$(this).find('img').animate({opacity: 0.5}, 'fast');
	});
	$("a.nextprevious").mouseout(function () { 
		$(this).find('img').animate({opacity: 1}, 'fast');
	});
	$("a.plus_new").mouseover(function () { 
		$(this).find('img').animate({opacity: 0.5}, 'fast');
	});
	$("a.plus_new").mouseout(function () { 
		$(this).find('img').animate({opacity: 1}, 'fast');
	});
	$("a.editlink").mouseover(function () { 
		$(this).find('img').animate({opacity: 0.5}, 'fast');
	});
	$("a.editlink").mouseout(function () { 
		$(this).find('img').animate({opacity: 1}, 'fast');
	});

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

	//--- calendars if we have them
	/*
	$("input.general_input[name*=_year]").datepicker({
		showOn: 'button',
		buttonImage: '/images/layout/calendar.png',
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		onSelect: function(dateText, inst) {
			alert(dateText);
			return false;
		}
	});
	*/
});

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

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

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






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; }
}

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

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

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

	f.innerHTML = i;
}
