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

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

	$("#startpage_images > li").mouseover(function () { 
		$(this).animate({opacity: 0.5}, 'slow');
	});
	$("#startpage_images > li").mouseout(function () { 
		$(this).animate({opacity: 1}, 'slow');
	});

	


	//--- 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 buttons - make them of class forms
	$("a[href*=?form=]").each(function () {
		if (this.className != 'form') {
			$(this).addClass('form');
		}
	});


	//--- forms open via colorbox!
	$("a.form").click(function () {
		hideAllAdminControls();

		var url = $(this).attr('href');
		var width = '850px';
		var height = '80%';
		
		if (!url.match(/popup=1/)) {
			url += '&popup=1';
		}
		var match1 = /popupwidth=(\d+)/i.exec($(this).attr('href'));
		if (match1 && match1[1]) {
			width =  match1[1] + 'px';
		}
		var match2 = /popupheight=(\d+)/i.exec($(this).attr('href'));
		if (match2 && match2[1]) {
			height =  match2[1] + 'px';
		}
		
		if (window.location.href != parent.location.href) {
			if (!url.match(/backtocolorbox=/)) {
				url += '&backtocolorbox=' + escape(window.location.href);
			}
			window.location = url;
		} else {
			$.fn.colorbox({
				innerWidth: width,
				innerHeight: height,
				iframe: true,
				open: true,
				href: url
			});
		}
		return false;
	} );

	//--- set the leftframe height proper
	checkleftframesize();

	//--- on window resuze - check once again the heights
	$(window).resize(function () {
		checkleftframesize();
	});

	//--- cart update needed?
	if (params['updatecart']) {
		cartupdated();
	}

	//--- ordereing process - delivery address same?
	$('#invoiceaddresssame').click(function () {
		if (this.checked) {
			$('#invoiceaddress').hide();
			$('#invoiceaddress').find("input[name*='invoice_']").each(function () {
				this.value = '';
			});
		} else {
			$('#invoiceaddress').show();
		}
	});

	//--- all admin controls - to top
	$('._admincontrol').css({'zIndex': 20001});
	//$('#shop_navi > ul > li').css({'zIndex': 10});
});


function checkleftframesize() {
	var abovepart = 347;
	if ($('#content').length) {
		height = $('#content').height();
		if ($('#lefttext').length) {
			var leftheight = $('#lefttext').height() + 609;
			if (leftheight - abovepart > height) {
				height = leftheight - abovepart;
			}
		}
		if ($(window).height() - abovepart > height) {
			height = $(window).height() - abovepart;
		}
		$('#content').height(height);
	}
	var footer = $('#footer');
	if (!footer.visible) {
		footer.fadeIn();
	}
}


//--- 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, action) { 
	$(f).find("input[name*='_submitbutton']").attr('value', ' - - - - - - ').attr('disabled', true);
	$(f).find("input[name*='_cancelbutton']").attr('value', ' - - - - - - ').attr('disabled', true);

	if (action != 'cancel') {
		$(f).css({position: 'relative'});
		$(f).append('<div id="myoverlayid" style="background-color: #FFFFFF; width: ' + $(f).width() + 'px; height: ' + $(f).height() + 'px; position: absolute; left: 0px; top: 0px; text-align: center; display: none;"><img src="/images/layout/loading.gif"></div>');
		$("#myoverlayid").css({opacity: 0});
		$("#myoverlayid").show();
		$("#myoverlayid").animate({opacity: 0.75});
	}
}

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