//--- 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: '690px',
		innerHeight: '500px',
		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');
	});

	$("#startpageboxescontainer > ul > li > a > img").mouseover(function () { $(this).animate({opacity: 0.5}, 'fast'); });
	$("#startpageboxescontainer > ul > li > a > img").mouseout(function () { $(this).animate({opacity: 1}, 'fast'); });

	$("#startpagebox1").mouseover(function () { $(this).animate({opacity: 0.5}, 'fast'); });
	$("#startpagebox1").mouseout(function () { $(this).animate({opacity: 1}, 'fast'); });
	

	$("#shop_navi > ul > li").mouseover(function () {
		if (!$(this).attr('originalBackgroundColor')) {
			$(this).attr('originalBackgroundColor', $(this).css('background-color'));
		}
		$(this).css('background-color', '#D9DADB');
		$(this).find('a.navi').css('color', '#000000');
		return false;
	});
	$("#shop_navi > ul > li").mouseout(function () {
		$(this).css('background-color', $(this).attr('originalBackgroundColor'));
		$(this).find('a.navi').css('color', '#FFFFFF');
		return false;
	});


	//--- 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);
				}
			});
		}
	});
	$("#shop_navi > ul[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() {
	if ($('#shop_navi').length) {
		height = $('#shop_navi').height();
		if ($(window).height() - 158 > height) {
			height = $(window).height() - 158;
		}
		if ($('#shop_content').length && $('#shop_content').height() + 109 > height) {
			height = $('#shop_content').height() + 109;
		}
		$('#shop_navi').height(height);
	}
}

function productPropShowMore() {
	$("div[id*='properties_']:hidden:first").show().find("input[name*='_name']").focus();
}

//--- shop magic
function addtocart_button(formid) {
	addtocart_formsubmit($('#' + formid));
}

function addtocart_formsubmit(myform) {
	var url = './?popup=1';
	var atleastonefull = false;
	$(myform).find('input').each(function () {
		url += '&' + this.name + '=' + escape(this.value);
		if (this.name.match(/^\d+$/) && this.value != '') { atleastonefull = true; }
	});
	if (atleastonefull)	{
		$(myform)[0].reset(); 
		opencart(url);
	}
	return false;
}

function opencart(url) {
	if (!url) {
		url = './?jumpto=cart&popup=1';
	}
	$.fn.colorbox({ 
		innerWidth: '690px',
		innerHeight: '400px',
		iframe: true,
		open: true,
		href: url
	});
	return false;
}

function cartupdated() {
	var cartdiv = $('#navbar > div > div.cart > div');
	if ((!cartdiv || !cartdiv.length) && window.parent) {
		cartdiv = $(window.parent.document).find('#navbar > div > div.cart > div');
	}
	if (cartdiv && cartdiv.length) {
		cartdiv.load('./?ajax=navbar_cart');
	}
}

function paypalloading() {
	var maincontainer = $('#shop_maincontainer');
	maincontainer.append('<div id="myoverlayid" style="background-color: #FFFFFF; width: ' + maincontainer.width() + 'px; height: ' + maincontainer.height() + 'px; position: absolute; left: 0px; top: 0px; text-align: center; display: none;"><img src="/images/layout/loading.gif" style="margin-top: ' + (parseInt(maincontainer.height() / 2) - 50) + 'px"><br><br><p class="page_title">Paypal is loading...</p></div>');
	$("#myoverlayid").css({opacity: 0});
	$("#myoverlayid").css({zIndex: 20000});
	$("#myoverlayid").show();
	$("#myoverlayid").animate({opacity: 0.85});
}

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

