
function showprev(theform) {
	$.ajax({
		data	: 'act=getrow&row='+(Number(theform.currentrow.value)-1)+'&ob='+theform.ob.value,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				$('#librarymaintbody').html(thedata.dat['editform']);
				$('#currentrow').val(thedata.dat['currentrow']);
				if (Number(thedata.dat['currentrow']) <= 1) {
					$('#prevbutton').attr('disabled','disabled');
				}
				else {
					$('#prevbutton').attr('disabled',false);
				}
				if (Number(thedata.dat['currentrow']) >= Number(thedata.dat['nrofrecords'])) {
					$('#nextbutton').attr('disabled','disabled');
				}
				else {
					$('#nextbutton').attr('disabled',false);
				}
			}
		}
	});
	
	//location.href = "test2.php?row="+(Number(theform.currentrow.value)-1)+"&ob="+theform.ob.value;
}
function shownext(theform) {
	$.ajax({
		data	: 'act=getrow&row='+(Number(theform.currentrow.value)+1)+'&ob='+theform.ob.value,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				//alert(thedata.dat['editform']);
				$('#librarymaintbody').html(thedata.dat['editform']);
				$('#currentrow').val(thedata.dat['currentrow']);
				if (Number(thedata.dat['currentrow']) <= 1) {
					$('#prevbutton').attr('disabled','disabled');
				}
				else {
					$('#prevbutton').attr('disabled',false);
				}
				if (Number(thedata.dat['currentrow']) >= Number(thedata.dat['nrofrecords'])) {
					$('#nextbutton').attr('disabled','disabled');
				}
				else {
					$('#nextbutton').attr('disabled',false);
				}
			}
		}
	});
//	location.href = "test2.php?row="+(Number(theform.currentrow.value)+1)+"&ob="+theform.ob.value;
}
$(document).ready(function(){
	// Get the source directory of this script
	initMenu();
	$('#shoppingcartfilled').live('click', function() { basketshowcontents(); });
//	$('#shoppingcartfilled').live('click', function() { showshoppingcart(); });
	$.ajaxSetup( {
		type	: 'POST',
		url	: '/_inc/deappelinc.php',
		dataType: 'json',
		error	: function(t,u,v) {
			alert(t+u+v);
		}
	});
	$('a').each(function() {
		if ($(this).attr('href').substr(0,1) != '/' && $(this).attr('href').substr(0,21) != 'http://www.deappel.nl' && $(this).attr('href') != 'javascript:void(0);') {
			$(this).attr('target','_blank');
		}
	});
	try {
		$("#acsartistname").autocomplete({
			source: "/_inc/deappelinc.php",
			data: 'act=getartistsautocomplete&term=',
			minLength: 1,
			select: function( event, ui ) {
				$('#sartistname').val(ui.item.id);
			}
		});
	} catch(e) {}
	$('#searchtext').keyup(function (ev) {
		if (ev.keyCode == '13') {
                	dosearch($('#searchform')[0]);
		}
		return false;
        });
});
//http://www.deappel.nl
window.onload = function() {
	if($.fn.adGallery) {
		// use autostart, unless there is only one item!
		if ($('ul.ad-thumb-list li').length > 1) {
			$('.ad-gallery').adGallery({display_next_and_prev: false, description_wrapper: $('#descriptions'), slideshow: { autostart: true, stop_on_scroll: true}});
			$('ul.ad-thumb-list').css('width','9000');
		}
		else {
			$('.ad-gallery').adGallery({display_back_and_forward: false, display_next_and_prev: false, description_wrapper: $('#descriptions'), slideshow: { enable: false}});
		}
	}
}
function initMenu(){
/* 
determine the height of the submenus:
must be improved by introducing each...
*/
	var maxheight = 0;
	var totwidth  = 0;
	for (var i = 1; i <= 7; i++) {
		var theheight = $('#submenu'+i).height();
		if (theheight > maxheight) {
			maxheight = theheight;
		}
		totwidth += $('#submenu'+i).width();
	}
	for (var i = 1; i <= 7; i++) {
		$('#submenu'+i).css('height', (6+maxheight)+'px');
	}
	$('#ulbottom').css('top', (6+maxheight) + 'px');
	$('#ulbottom').css('width', (4+totwidth) + 'px');

	$('div#menu_holder > ul#menulist').hoverIntent(
		function(){
			menuOver($('div#menu_holder'), 80+maxheight);
		},
		function() {
			menuOut($('div#menu_holder'));
		}
	);
}
function menuOver(thisItem, menuheight){
	if (thisItem.is('.up')) {
		thisItem.attr('class', 'down');
	}
	else if (thisItem.is('.none')) {
		thisItem.attr('class', 'down');
		thisItem.animate({ height: menuheight }, 'slow', function() {
			if (thisItem.is('.up')) {
				thisItem.attr('class', 'out');
				menuOut(thisItem);
			}
			else {
				thisItem.attr('class', 'out');
			}
		});
	}
}
function menuOut(thisItem){
	iIv = null;
	if (thisItem.is('.down')) {
		thisItem.attr('class', 'up');
	}
	else if (thisItem.is('.out')) {
		thisItem.attr('class', 'up');
		thisItem.animate({ height: 61}, 'slow', function() {
			var thisItem = $(this);
			if(thisItem.is('.down')){
				thisItem.attr('class', 'none');
				menuOver(thisItem);
			}else{
				thisItem.attr('class', 'none');
			}
		});
	}
}
function showloginmenu() {
	$('#loginmenu').css('display', 'block');
	$('#loginusername').focus();
}
function hideloginmenu() {
	$('#loginmenu').css('display', 'none');
}
function hideshoppingcart() {
	$('#basketcontents').css('display', 'none');
}
function publicationsshow(theselect) {
	var thehref = "/publications/l/0/"+theselect.value+"/";
	var qa = '';
	if (theselect.form.elements['f[pubshowcat]'].value != '') {
		qa += 's1='+theselect.form.elements['f[pubshowcat]'].value;
	}
	if (qa != '') qa = '?' + qa;
	location.href = thehref + qa;
}
function publicationsshowwhere(theselect) {
//	alert( "/publications/l/0/"+theselect.form.elements['f[pubsort]'].value+"/?publicationtype="+theselect.value)
	if (theselect.value == '') {
		location.href = "/publications/l/0/";
	}
	else {
		location.href = "/publications/l/0/"+theselect.form.elements['f[pubsort]'].value+"/?s1="+theselect.value;
	}
}
function publicationsshowsearch(theinput) {
	location.href = "/publications/l/0/"+theinput.form.elements['f[pubsort]'].value+"/?sf=" + theinput.value;
}
function newsitemsshowsearch(theinput) {
	var s1 = theinput.form.elements['f[newsshowyear]'].value;
	location.href = "/news/l/0/"+theinput.form.elements['f[newssort]'].value+"/?sf=" + theinput.value + "&s1=" + s1;
}
function newsitemsshowwhere(theselect) {
	var sf = '';
	if (theselect.form.elements['f[newssearch]'].value != '') {
		sf += "sf="+theselect.form.elements['f[newssearch]'].value;
	}
	if (theselect.value == '') {
		if (sf != '') {
			location.href = "/news/l/0/?"+sf;
		}
		else {
			location.href = "/news/l/0/";
		}
	}
	else {
		if (sf != '') {
			location.href = "/news/l/0/?s1="+theselect.value+"&"+sf;
		}
		else {
			location.href = "/news/l/0/?s1="+theselect.value;
		}	
	}
}
function libraryshowsearch(theinput) {
	location.href = "/library/l/0/"+theinput.form.elements['f[librsort]'].value+"/?sf=" + encodeURIComponent(theinput.value);
}
function exhibitionsshowwhere(theselect) {
	if (theselect.value == '') {
		location.href = "/exhibitions/l/";
	}
	else {
		location.href = "/exhibitions/l/0/?s1="+theselect.value;
	}
}
function exhibitionsshow(theselect) {
	location.href = "/exhibitions/e/"+theselect.value;
}
function infoshow(theselect) {
	location.href = "/info/"+theselect.value;
}
function newsitemshow(theselect) {
	location.href = "/news/e/"+theselect.value;
}
function librshow(theselect) {
	var newlocation = "/library/l/0/";
	newlocation += theselect.form.elements['f[librsort]'].value + "/";
	var sfvalue = theselect.form.elements['f[librsearch]'].value;
	if (sfvalue != '') {
		newlocation += "?sf=" + encodeURIComponent(sfvalue);
	}
	location.href = newlocation;
}
function librjumptopage(theselect, itemsperpage) {
	var newlocation = "/library/l/"+(itemsperpage * Number(theselect.value-1)) + "/";
	newlocation += theselect.form.elements['f[librsort]'].value + "/";
	var sfvalue = theselect.form.elements['f[librsearch]'].value;
	if (sfvalue != '') {
		newlocation += "?sf=" + encodeURIComponent(sfvalue);
	}
	location.href = newlocation;
}
function pubjumptopage(theselect, itemsperpage) {
	var newlocation = "/publications/l/"+(itemsperpage * Number(theselect.value-1)) + "/";
	newlocation += theselect.form.elements['f[pubsort]'].value + "/";
	var sfvalue = theselect.form.elements['f[pubsearch]'].value;
	if (sfvalue != '') {
		newlocation += "?sf=" + encodeURIComponent(sfvalue);
	}
	location.href = newlocation;
}
function newsjumptopage(theselect, itemsperpage) {
	var newlocation = "/news/l/"+(itemsperpage * Number(theselect.value-1)) + "/";
	newlocation += theselect.form.elements['f[newssort]'].value + "/";
	var sfvalue = theselect.form.elements['f[newssearch]'].value;
	if (sfvalue != '') {
		newlocation += "?sf=" + encodeURIComponent(sfvalue);
	}
	location.href = newlocation;
}
/* shop scripts */
function addtobasket(itemsrc, itemid, lang) {
//alert(itemsrc + " and " + itemid + " and " + lang);
	$.ajax({
		data	: 'act=addtobasket&itemsrc=' + itemsrc + '&itemid=' + itemid,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				if (thedata.msg) {
					alert(thedata.msg);
				}
				else {
					if ($('#basketcontents').css('display') == 'block') {
						$('#basketcontents').html(thedata.dat);
					}
					var nrofitems = thedata.items;
					$('#shoppingcartempty').css('display','none');
					$('#shoppingcartfilled').css('display','inline');
					basketshowcontents();
					if (lang == 'nl') {
						alert("Het item is toegevoegd aan het winkelmandje.\n\nWilt u afrekenen? Klik op 'Afrekenen' bij het winkelmandje.");
					}
					else {
						alert("The item has been added to your shopping cart.\n\nTo start the payment procedure, click on 'pay now' within the shopping cart window.");
					}
				}
			}
		}
	});

}
function removefrombasket(theid, lang) {
	if (lang == 'nl') {
		if (!confirm('Weet u zeker dat u dit item uit uw winkelmandje wilt verwijderen?')) {
			return false;
		}
	}
	else {
		if (!confirm('Are you sure you want to remove this item from your shopping cart?')) {
			return false;
		}
	}
	$.ajax({
		data	: 'act=removefrombasket&id=' + theid,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				if ($('#basketcontents').css('display') == 'block') {
					$('#basketcontents').html(thedata.dat);
				}
				var alerttext = '';
				if (lang == 'nl') {
					alerttext += "Het item is verwijderd uit het winkelmandje.\n";
				}
				else {
					alerttext += "The item has been removed from your shopping cart.\n";
				}
				var nrofitems = thedata.items;
				if (nrofitems == 0) {
					$('#shoppingcartempty').css('display','inline');
					$('#shoppingcartfilled').css('display','none');
					$('#basketcontents').css('display','none');
					if (lang == 'nl') {
						alerttext += "Het winkelmandje is nu leeg.";
					}
					else {
						alerttext += "Your shopping cart is now empty.";
					}
				}
				alert(alerttext);
				location.href = location.href;
			}
		}
	});
}
function basketshowcontents() {
//	if ($('#baskettotalnrofitems').html() == '0') return;
	$.ajax({
		data	: 'act=basketshowcontents',
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				$('#basketcontents').css('display','block');
				$('#basketcontents').html(thedata.dat);
			}
		}
	});
}
function baskethidecontents() {
	$('#basketcontents').css('display','none');
}
function dologin(theform) {
	var str = $("#"+theform.id).serialize();
	$.ajax({
		data	: 'act=dologin&'+str,
		success	: function(thedata) {
			if (thedata.err)	alert(thedata.err);
			else {
				alert(thedata.dat['t']);
				if (thedata.dat['url'] != '') {
					location.href = thedata.dat['url'];
				}
			}
		}
	});
}
function dologout() {
	$.ajax({
		data	: 'act=dologout',
		success	: function(thedata) {
			if (thedata.err)	alert(thedata.err);
			else {
				alert(thedata.dat);
				location.href = location.href;
			}
		}
	});
}
var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&;=]+)=?([^&;]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();
function kassa1(lang) {
	// first check if everything is filled out correctly
	var theform = $("#checkoutform1")[0];
	$('#checkoutform1 input').each(function() {
		$(this).val(jQuery.trim($(this).val()));
	});
	var errmsg = '';
	if (theform.elements['f[firstname]'].value == '') {
		if (lang == 'nl')
			errmsg += "Er is nog geen voornaam ingevuld...\n";
		else
			errmsg += "Please fill in your first name...\n";
	}
	if (theform.elements['f[lastname]'].value == '') {
		if (lang == 'nl')
			errmsg += "Er is nog geen achternaam ingevuld...\n";
		else
			errmsg += "Please fill in your last name...\n";
	}
	if (theform.elements['f[street]'].value == '') {
		if (lang == 'nl')
			errmsg += "Er is nog geen adres ingevuld...\n";
		else
			errmsg += "Please fill in your address...\n";
	}
	if (theform.elements['f[postalcode]'].value == '') {
		if (lang == 'nl')
			errmsg += "Er is nog geen postcode ingevuld...\n";
		else
			errmsg += "Please fill in your postal code...\n";
	}
	if (theform.elements['f[city]'].value == '') {
		if (lang == 'nl')
			errmsg += "Er is nog geen plaats ingevuld...\n";
		else
			errmsg += "Please fill in your city...\n";
	}
	if (theform.elements['f[country]'].value == '') {
		if (lang == 'nl')
			errmsg += "Er is nog geen land geselecteerd...\n";
		else
			errmsg += "Please select your country...\n";
	}
	if (theform.elements['f[email]'].value == '') {
		if (lang == 'nl')
			errmsg += "Er is nog geen e-mail-adres ingevuld...\n";
		else
			errmsg += "Please fill in your e-mail address...\n";
	}
	else {
		var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
		if (! re.test(theform.elements['f[email]'].value)) {
			if (lang == 'nl')
				errmsg += "Het opgegeven e-mail-adres lijkt niet geldig...\n";
			else
				errmsg += "The entered e-mail address appears to be invalid...\n";
		}
	}
	if (! theform.elements['av'].checked) {
		if (lang == 'nl')
			errmsg += "Op bestellingen zijn onze algemene voorwaarden van kracht...\n";
		else
			errmsg += "Please accept our general terms and conditions for this order...\n";
	}
	if (errmsg != '') {
		alert(errmsg);
		return;
	}
	var str = $("#checkoutform1").serialize();
	$.ajax({
		data	: 'act=checkout1&'+str,
		success	: function(thedata) {
			if (thedata.err)	alert(thedata.err);
			else {
				//alert(thedata.dat);
				location.href = "kassa.php?v=2";
			}
		}
	});
}
function kassa2() {
	$('#mspform').submit();
}
function restartsip() {
	$.ajax({
		data	: 'act=restartsip',
		success	: function(thedata) {
			if (thedata.err)	alert(thedata.err);
			else {
				//alert(thedata.dat);
				location.href = "/exhibitions/s/";
			}
		}
	});
}

function autocomplete(e) {
	// if (navigator.vendor == 'KDE') { // konqueror does not scroll down automatically?
	// see key 40
	var key= e.keyCode;
	var theinput = e.target;
	var theselect = document.getElementById('jcms_dynsel');
	var val = theinput.value;
	if (key == 27) {	// escape
		theselect.style.display = 'none';
		return;
	}
	else if (key == 38) {	// arrow up
		if (theselect.options[theselect.selectedIndex - 1])
			theselect.selectedIndex = theselect.selectedIndex - 1;
		return false;
	}
	else if (key == 40) {	// arrow down
		if (navigator.vendor == 'KDE') {
			theselect.focus();
		}
		else {
			if (theselect.options[theselect.selectedIndex + 1])
				theselect.selectedIndex = theselect.selectedIndex + 1;
			if (theselect.style.display != 'none')	// we allow it on an empty box too!
				return false;
		}
	}
	else if (key == 13) {	// enter
		theinput.value = theselect.value;
		theselect.style.display = 'none';

		return false;
	}
	else {
		// reset the selectedIndex if key is not 38 or 40 or 13
		theselect.selectedIndex = 0;
	}
	if (theinput.value == '' && (!(e.type == 'keyup' && (key == 38 || key == 40)))) {
		// if the inputvalue is empty and we're not arrowup/arrowdown-ing
		theselect.style.display = 'none';
		return;		// at least one letter, we're not going to show them all by default
	}
	// opera seems to need it here, but prevent flickering in other browsers
	if (window.opera)	theselect.style.display = 'inline';
	$.ajax({
		data	: 'act=' + e.data.act + '&word=' + theinput.value,
		success	: function(thedata) {
			if (thedata.err)	feedback(thedata.err);
			else {
	 			// remove earlier displayed options from the selectbox
				while (theselect.childNodes.length > 0) {
					theselect.removeChild(theselect.childNodes[0]);
				}
				// (re)fill the selectbox with actual data
				for (var i = 0; i < thedata.dat.length; i++) {
					var opt = document.createElement("option");
					// IE 5.5 does not understand opt.text
					opt.value = opt.innerHTML = thedata.dat[i]['val'];
					if (i == 0) opt.selected = 'selected';
					theselect.appendChild(opt);
				}
				// position the selectbox near the input
				var pos = getpos(theinput);
				var xpos = pos[0] + 8;				// move a little right
				var ypos = pos[1] + theinput.offsetHeight - 4;	// move a little up
				theselect.style.left  = xpos + 'px';
				theselect.style.top   = ypos + 'px';
				theselect.style.width = theinput.offsetWidth + 'px';
				// add some event logic (select option by mouse click)
				theselect.onclick = function() {
					theinput.value = theselect.value;
					theselect.style.display = 'none';
				}
				theselect.onkeyup = function(e) {
					// we need this for konqueror...
					if (e.keyCode == 13) {
						theinput.value = theselect.value;
						theselect.style.display = 'none';
					}
				}
				theselect.style.display = 'inline';
				return false;
			}
		}
	});
}
function getpos(el) {
	var posX = el.offsetLeft;
	var posY  = el.offsetTop;
	while(el = el.offsetParent) {
		posX += el.offsetLeft;
		posY += el.offsetTop;
	}
	return [posX,posY];
}

function show_props2(obj, obj_name) {
	var result = '';
	for (var i in obj) {
		if (i)
			result += obj_name + '.' + i + ' = ' + obj[i] + '<br />\n';
	}
	return result
}

