function urlencode( str ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Philip Peterson
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +      input by: AJ
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Brett Zamir
	// %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
	// *     example 1: urlencode('Kevin van Zonneveld!');
	// *     returns 1: 'Kevin+van+Zonneveld%21'
	// *     example 2: urlencode('http://kevin.vanzonneveld.net/');
	// *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
	// *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
	// *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
							 
	var histogram = {}, tmp_arr = [];
	var ret = str.toString();

	var replacer = function(search, replace, str) {
		var tmp_arr = [];
		tmp_arr = str.split(search);
		return tmp_arr.join(replace);
	};

	// The histogram is identical to the one in urldecode.
	histogram["'"]   = '%27';
	histogram['(']   = '%28';
	histogram[')']   = '%29';
	histogram['*']   = '%2A';
	histogram['~']   = '%7E';
	histogram['!']   = '%21';
	histogram['%20'] = '+';

	// Begin with encodeURIComponent, which most resembles PHP's encoding functions
	ret = encodeURIComponent(ret);

	for (var search in histogram) {
		var replace = histogram[search];
		ret = replacer(search, replace, ret) // Custom replace. No regexing
	}

	// Uppercase for full PHP compatibility
	return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
		return "%"+m2.toUpperCase();
	});

	return ret;
}

$(document).ready(function() {
	// fix png for IE
	$('img[@src$=.png], div').not('#spalsh .box1').ifixpng();

	// prettyPhoto - zoom images
	if ( $("a[rel^='prettyPhoto']").length ) {
		//$("a[rel^='prettyPhoto']").prettyPhoto();
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_square'});
	}

	// Safari CSS
	if ( $.browser.safari ) {
		$("head").append('<link href="css/safari.css" media="screen" rel="stylesheet" type="text/css" />');
	}

	// remove any borders on last LI element
	$("ul, #footer ul").not('ul.highlights, .accordion ul').each(function(){
		 $(this).children("li:last").css({borderRight:"0",borderBottom:"0",background:"none",paddingRight:"0",marginRight:"0"});
	});
	$("ul li:last").each(function(){
		 $(this).css({borderRight:"0",borderBottom:"0",background:"none",paddingRight:"0",marginRight:"0"});
	});

	// links in splash page
	$('#splashplinth').each(function(){
		$(this).hover(
			function(){
				$(this).css('cursor','pointer');
			},
			function(){
				$(this).css('cursor','auto');
			}
		);
		$(this).click(function(){
			window.location.href = 'home.html';
		});
	});

	// change font size
	$('#smaller, #larger').click(function(){
		alterFontSize($(this).attr('id'));
		return false;
	});

	// h/v alignament of title inside plinth
	if ( $('.rotimage img').length ) {
		$('.rotimage img').each(function(){
			var _w = (280-$(this).attr('width'))/2;
			var _h = (190-$(this).attr('height'))/2;
			$(this).css({left:_w,top:_h});
		});
	}

	// cycle plinths
	if ($('ul.plinths').length) {
		$('ul.plinths').cycle({
			fx:     'fade',
			speed:  1200,
			timeout: 0,
			next:   '#nextplinth, ul.plinthpager li#middle a',
			prev:   '#backplinth',
            after:   onAfter
		});

		// redirect to exhibitions when clicking on inside plith boxes!
		$('.plinth_container_advert').click(function(){
			var div_url = $('.plinth_url', $(this).parent()).get(0);
			var url = jQuery.trim($(div_url).html());
			if(url != '')
				window.location.href = url;
		});
	}

	// hover other things than A element
	$('.rotimage, .exdesc, ul.plinths li').hover(
		function(){
			$(this).css('cursor','pointer');
		},
		function(){
			$(this).css('cursor','auto');
		}
	);

	function onAfter() {
		var splitTitle = this.title;
		splitTitle = splitTitle.split('|');

		$('#plinthtitle').html(splitTitle[1]);
		$('#period').html(splitTitle[2]);
		$('#outof').html(splitTitle[0]);
	}

	// cycle mastheads
	if ($('ul.simpleGal').length) {
		$('ul.simpleGal').cycle({
			fx:     'fade',
			speed:  1200,
			timeout: 0,
			next:   '#nextgal, ul.simpleGal, ul.plinthpager li#middle a',
			prev:   '#backgal',
            after:   onAfterGal
		});

		$('.simpleGal li').hover(
			function(){
				$(this).css('cursor','pointer');
			},
			function(){
				$(this).css('cursor','auto');
			}
		);
	}

	function onAfterGal() {
		$('#outof').html(this.value);
	}

	// right additional info over the images
	if ( $('ul.gal3 li div.additional').length ) {
		$('ul.gal3 li div.additional').hide();
		$('ul.gal3 li a').hover(
			function(){
//				if ( $.browser.msie ) {
//					$(this).children('div.additional').show();
//				} else {
					$(this).children('div.additional').fadeIn();
//				}
			},
			function(){
//				if ( $.browser.msie ) {
//					$(this).children('div.additional').hide();
//				} else {
					$(this).children('div.additional').fadeOut();
//				}
			}
		);
	}

	// accordion
	$('div.accordion> div').not('.shoputil').hide();
	$('div.accordion> h3').click(function() {
		var $nextDiv = $(this).next('div');
		var $visibleSiblings = $nextDiv.siblings('div:visible').not('.shoputil');

		if ($visibleSiblings.length ) {
			$(this).siblings('h3').removeClass('clicked');
			$visibleSiblings.slideUp('fast', function() {
				$nextDiv.slideToggle('fast');
			});
		} else {
			$nextDiv.slideToggle('fast');
		}

		$(this).toggleClass('clicked');
	});	

	// inner accordion
	$('.accordion ul.plusminus li ul').hide();
	$('.accordion ul.plusminus > li > a').click(function() {
		$(this).toggleClass('clicked');
		$(this).next().toggle();
		return false;
	});

	// exhibitions fix
	if ( $('ul.exhibitions li div').length ) {
		$('ul.exhibitions li:odd').css({marginRight:'0',background:'none'});
	}

	// shop categories fix
	if ( $('ul.shopcats li div.additional').length ) {
		$('ul.shopcats li:even').css({paddingRight:'14px'});
		$('ul.shopcats li:odd').css({marginRight:'0',background:'none'});
		$('ul.shopcats li:last a').css({background:'none'});
		$('ul.shopcats li:last').prev().children('a').css({background:'none'});

		// position of images inside LI tag
		$('ul.shopcats li img').each(function(){
			var _w = (270-$(this).attr('width'))/2;
			var _h = (234-$(this).attr('height'))/2;
			$(this).css({left:_w,top:_h});
		});
	}

	// publications fix
	if ( $('ul.publications li').length ) {
		var liIndex = $('ul.publications li').length;
		for ( var i = 0; i <= liIndex; i++ ) {
			if ( i%3 == 0 ) {
				$('ul.publications li:nth-child('+i+')').css({marginRight:'0'});
			}
		}
	}

	// cycle product images
	if ($('#listimages').length) {
		$('#listimages').cycle({
			fx:     'fade',
			speed:  1000,
			timeout: 0,
			next:   '#nextgal, ul.plinthpager li#middle a',
			prev:   '#backgal',
            after:   onAfterProdImg
		});
	}

	/*$('#listimages a').hover(
		function(){ alert($(this).attr('href')); },
		function(){}
	);*/

	function onAfterProdImg() {
		$('li.viewlarge a').attr('href',$(this).attr('href'));
		$('#outof').html(this.rel);
	}

	// zoom product images
	if ( $('#listimages a').length ) {
		var options = {
			zoomWidth: 300,
			zoomHeight: 313,
			xOffset: 21,
			yOffset: 0,
			position: "right",
			zoomType:'reverse',
			title:false
		};
		$('#listimages a').jqzoom(options);
	}

	// share URL
	if ( $('a.a2a_dd').length ) {
		$('a.a2a_dd').attr('href', 'http://www.addtoany.com/share_save?linkname=&amp;linkurl='+window.location.href);
	}
	
	// handle deliver billing
	$('#deliver_billing').change(function(){
		if ( $(this).attr('checked') ){
			populateFields();
		} else {
			clearFields();
		}
	});
	$('#address, #postcode, #city, #country, #firstnames, #surname').keyup(function(){
		if ( $('#deliver_billing:checked').length ){
			populateFields();
		} else {
			clearFields();
		}
	});
	
	$('#tooltip').hide();
	$('#label_donate').hover(
		function(){
			$('#tooltip').show();
		},
		function(){
			$('#tooltip').hide();
		}
	);
	
});

populateFields = function(){
	$('#delivery_firstnames').val($('#firstnames').val());
	$('#delivery_firstnames').attr('readonly','readonly');
	$('#delivery_surname').val($('#surname').val());
	$('#delivery_surname').attr('readonly','readonly');
	$('#delivery_address').val($('#address').val());
	$('#delivery_address').attr('readonly','readonly');
	$('#delivery_postcode').val($('#postcode').val());
  $('#delivery_postcode').attr('readonly','readonly');
	$('#delivery_city').val($('#city').val());
	$('#delivery_city').attr('readonly','readonly');
	$('#delivery_country').val($('#country').val());
	$('#delivery_country').attr('readonly','readonly');
}

clearFields = function(){
	$('#delivery_firstnames').val('');
	$('#delivery_firstnames').attr('readonly','');
	$('#delivery_surname').val('');
	$('#delivery_surname').attr('readonly','');
	$('#delivery_address').val('');
	$('#delivery_address').attr('readonly','');
	$('#delivery_postcode').val('');
  $('#delivery_postcode').attr('readonly','');
	$('#delivery_city').val('');
	$('#delivery_city').attr('readonly','');
	$('#delivery_country').val('');
	$('#delivery_country').attr('readonly','');
}

// change font size
alterFontSize = function(direction){
	var getCurrentSize = parseFloat($('body').css('font-size'));
	(( direction == 'larger' ) ? getCurrentSize++ : getCurrentSize--)+"px";
	$('body').css('font-size',getCurrentSize);
	return false;
}

// Define indexOf for IE
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}