//--- request parameters
var params = new Array();
var params_asarray = window.location.href.match(/([\?&][^=]+=[^&#]*)/gi);
if (params_asarray) {
	for (var i=0; i<params_asarray.length; i++) {
		var insidehits = params_asarray[i].match(/[\?&]([^=]+)=([^&#]*)/i);
		if (insidehits) {
			params[insidehits[1]] = insidehits[2];
		}
	}
}

//--- popup but not in a popup?
if (window.location.href == parent.location.href && params['popup']) {
	var newurl= window.location.href;
	newurl = newurl.replace(/\?popup=1/i, '?');
	newurl = newurl.replace(/&popup=1/, '');
	newurl = newurl.replace(/\?$/, '');
	window.location = newurl;
}

//--- 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"
	});
	$("a.productdetails").colorbox({
		innerWidth: '500px',
		innerHeight: '650px',
		iframe: true
	});

	//--- 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.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);
				}
			});
		}
	});
	$("ul.products[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 slideshow_move(plsorminus) {
	var wemove = $('#slides');
	if (wemove.width() <= 290) {
		return;
	}

	var howmuch = 200;
	var remainderright = wemove.width() + wemove.position().left - 290;
	if (plsorminus == -1 && howmuch > remainderright) {
		howmuch = remainderright;
	}
	if (plsorminus == 1 && howmuch > - wemove.position().left) {
		howmuch = - wemove.position().left;
	}
	if (howmuch == 0) {
		return;
	}

	var movestring = '' + (plsorminus == -1 ? '-' : '+') + '=' + howmuch;
	wemove.animate({
		left: movestring,
	}, 500, function() {});
}
function slideshw_showslide(picid) {
	$('#slides > a > img').each(function(index) {
		$(this).css({opacity: 1});
	});
	$('#picture_'+picid).css({opacity: 0.25});
	$('#previewimage').attr('src', './?showmediimage=' + picid);
}

function etoggle(whoid) {
	$('#'+whoid).toggle();
}
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;
}

