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

	init_colorbox();

	//--- 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');
	});

	//--- 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;
		}
	});
	*/
});

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

	$("a.youtube_wide").colorbox({
		close: "schließen",
		innerWidth: '640px',
		innerHeight: '370px',
		iframe: true
	});
	$("a.youtube_normal").colorbox({
		close: "schließen",
		innerWidth: '640px',
		innerHeight: '480px',
		iframe: true
	});

	

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

//--- 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 visibilitychange(elementname) {
	var field = $('#visibility_' + elementname)[0];
	var link = $('#visibilitylink_' + elementname)[0];
	var state = parseInt(link.className.substr(link.className.length-1)) + 1;
	if (state > 3) { state = 1; }

	field.value = state;
	link.className = 'general_visibilityselector_forms visibility_' + state;
	switch (state) {
		case 1: $(link).html('everyone'); break;
		case 2: $(link).html('users only'); break;
		case 3: $(link).html('noone'); break;	
	}
}


function profile_displayuserimages(profilename) {
	var c = $('#profile_displayuserimages');

	var mynewdiv = $('<div></div>'); 
	mynewdiv.attr('style', 'position: absolute; top: 0px; left: 0px; background-color: white; border: 1px solid #CCCCCC; display: none;');
	mynewdiv.html('<img src="/images/layout/loading.gif">');
	mynewdiv.appendTo(c);

	mynewdiv.width(mynewdiv.parent().width());
	mynewdiv.height(mynewdiv.parent().height());
	mynewdiv.find('image').css('margin', ''+parseInt((mynewdiv.height()-31) / 2)+'px 0px 0px '+parseInt((mynewdiv.width()-31) / 2)+'px');

	mynewdiv.fadeIn(function() { 
		c.load('./?ajax=displayuserimages&profile=' + profilename, null, function(responseText, textStatus, XMLHttpRequest) {
			init_colorbox();
			c.parent().height(c.height());
		});
	} );

	return;
}


function profile_displayuservideos(profilename) {
	var c = $('#profile_displayuservideos');

	var mynewdiv = $('<div></div>'); 
	mynewdiv.attr('style', 'position: absolute; top: 0px; left: 0px; background-color: white; border: 1px solid #CCCCCC; display: none;');
	mynewdiv.html('<img src="/images/layout/loading.gif">');
	mynewdiv.appendTo(c);

	mynewdiv.width(mynewdiv.parent().width());
	mynewdiv.height(mynewdiv.parent().height());
	mynewdiv.find('image').css('margin', ''+parseInt((mynewdiv.height()-31) / 2)+'px 0px 0px '+parseInt((mynewdiv.width()-31) / 2)+'px');

	mynewdiv.fadeIn(function() { 
		c.load('./?ajax=displayuservideos&profile=' + profilename, null, function(responseText, textStatus, XMLHttpRequest) {
			init_colorbox();
			c.parent().height(c.height());
		});
	} );

	return;
}


function profile_displayevents(profilename) {
	var c = $('#profile_displayevents');

	var mynewdiv = $('<div></div>'); 
	mynewdiv.attr('style', 'position: absolute; top: 0px; left: 0px; background-color: white; border: 1px solid #CCCCCC; display: none;');
	mynewdiv.html('<img src="/images/layout/loading.gif">');
	mynewdiv.appendTo(c);

	mynewdiv.width(mynewdiv.parent().width());
	mynewdiv.height(mynewdiv.parent().height());
	mynewdiv.find('image').css('margin', ''+parseInt((mynewdiv.height()-31) / 2)+'px 0px 0px '+parseInt((mynewdiv.width()-31) / 2)+'px');

	mynewdiv.fadeIn(function() { 
		c.load('./?ajax=displayevents&profile=' + profilename, null, function(responseText, textStatus, XMLHttpRequest) {
			init_colorbox();
			c.parent().height(c.height());
		});
	} );

	return;
}




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

