$(init);

var currentURL = window.location.protocol + '//' + window.location.host + window.location.pathname;

if (!window.console) {
	window.console = {
		log: function() {}
	};
}

function init() {
	if ($.browser.msie){
		try {
			document.execCommand("BackgroundImageCache", false, true);
		}
		catch (err) {}
	}
	
	$('body').removeClass('nonJS').addClass('js');
	
	PopupMessage().set({lightboxButton: 'assets/images/gallery/close.gif', width: 678});
	
	$.ewyseGallery.set({
		imgPath: 'assets/images/gallery/',
		maxHeight: 467,
		minHeight: 467,
		minWidth: 697,
		maxWidth: 697,
		autoMaxDimension: true,
		fitImage: true,
		cacheAjaxRequests: true,
		flashparams: {height: 330},
		downloadLink: true
	});
    
	if ($.browser.msie) {
		var mainHeight = $('#main').height();
		$('#main .main-left, #main .main-right').css('height', mainHeight + 2);
	}
	$('.delete_button').click(function(){
		if(confirm('Weet u zeker dat u deze afbeelding wilt verwijderen?')) {
			return true;
		}
		return false;
	});
	
	processContent(document);
}

function processContent(context) {
	context = $(context || this);
	
	$('div.assets:not(.jq_gallery, .local_home, .local_other, .general_home)', context).addClass('jq_gallery').each(function() {
		var id = $(this).attr('id').split('_'), args = {output: 'json', retrieve: 'collectionImages', doc_id: ($(this).metadata().col ? $(this).metadata().col : $(this).metadata().doc), order: 'created'};
		$('a.collection_item_url', this).ewyseGallery({rewriteFunction: resizeImage, mode: 'json', url: $.makeURL(rootURL, args)});
	});
	
	$('a.lightbox').lightbox({output: 'json', retrieve: 'content'}, {cache: true, onShow: processContent});
}

function addEwyseGallery() {
	$('a.collection_item_url:not(.processed)', this).addClass('processed').ewyseGallery();
}

function resizeImage(url, width, height) {
	var parts = url.split('?');
	var args = $.parseGetParameters(url, true);
	args['width'] = width;
	args['height'] = height;
	return $.makeURL(parts[0], args);
}
