$(init);

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

function init() {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	}
	catch (err) {}
	
	$('body').removeClass('nonJS').addClass('js');
	
	PopupMessage().set({lightboxButton: 'assets/images/buttons/close.png', width: 678});
	
	Cufon.replace('h1, h2, h3, div.documents span.collection_item_title:not(div.project_criteria span.collection_item_title, div.case_left span.collection_item_title),div.home_teasers span.collection_item_title, h2.collection_title, #mainMenu li a, a.collection_readMore, span.collection_item_client_name');
	
	$('.cases_teaser .collection_navigation:first:not(.processed)').each(function() {
		// Pretty stupid piece of code which could be cleaned up and abstracted of course, but it needs a prettier solution anyways
		$(this).addClass('processed');
		//.find('.prevItem, .nextItem').remove();
		var buttons = {right: $('a.nextItem', this), left: $('a.prevItem', this)};
		// Now, ajaxify our project pager
		$('a.page', this).ajaxNav({
			target: $(this).parent().find('ul.collection_items li'),
			hash: false,
			preload: false,
			dimensional: false,
			fade: false,
			callback: function(){
				Cufon.replace('.cases_teaser span.collection_item_title');
			},
			createWorkspace: false,
			createViewport: true,
			fixViewport: false,
			classActive: 'activePage',
			ajaxParams: {output: 'json', retrieve: 'pane', pane: 'topCollectionPane'},
			buttons: buttons,
			find: 'ul li'
		});
	});
	
	$('.case_slider .collection_navigation:first:not(.processed)').each(function() {
		// Pretty stupid piece of code which could be cleaned up and abstracted of course, but it needs a prettier solution anyways
		$(this).addClass('processed').find('.prevItem, .nextItem').remove();
		// Now, ajaxify our project pager
		$('a.page', this).ajaxNav({
			target: $(this).parent().find('ul.collection_items'),
			hash: false,
			preload: false,
			fade: false,
			callback: function(){
				$('div.assets:not(.home_teasers)').each(function() {
					var id = $(this).attr('id').split('_'), args = {output: 'json', retrieve: 'collectionImages', doc_id: $(this).metadata().col};
					$('a.collection_item_url', this).ewyseGallery({rewriteFunction: resizeImage, mode: 'json', url: $.makeURL(rootURL, args)});
				});
			},
			createWorkspace: true,
			createViewport: true,
			fixViewport: true,
			classActive: 'activePage',
			ajaxParams: {output: 'json', retrieve: 'pane', pane: 'topCollectionPane'},
			buttons: {right: 'assets/images/arrow.png', left: 'assets/images/arrow_prev.png'},
			find: 'ul'
		});
	});
	
	$('.project_criteria a, a.show_all').click(function(){
		$('.project_criteria a, a.show_all').removeClass('active');
		$(this).addClass('active');
		var url = this.href, params = $.extend(parseGetParameters(url), {output: 'json', retrieve: 'pane', pane: 'centreCollectionPane'});
		var base = url.split('?').shift();
		$.getJSON(encodeURI(base), params, function(json) {
			$('.projects div.collection_innerWrapper').slideUp(function() {
				$('ul.collection_items', this)
					.html($('.projects ul.collection_items li', json.content))
					.parent()
					.slideDown(function(){
						Cufon.replace('.projects span.collection_item_title, .projects span.collection_item_client_name');
					});
			});
		});
		return false;
	});
	
	$.ewyseGallery.set({
		imgPath: 'assets/images/gallery/',
		maxHeight: 467,
		minHeight: 467,
		minWidth: 697,
		maxWidth: 697,
		autoMaxDimension: true,
		fitImage: true,
		cacheAjaxRequests: false,
		flashparams: {height: 330}
	});
	
	$.ifixpng('assets/pixel.gif');
	$('.read_more, .news .collection_item_date, .jobs .collection_item_url').ifixpng();	
	
	processContent(document);
}

function processContent(context) {
	context = $(context || this);
	
	$('div.assets:not(.jq_gallery, .home_teasers)', context).addClass('jq_gallery').each(function() {
		var id = $(this).attr('id').split('_'), args = {output: 'json', retrieve: 'collectionImages', doc_id: $(this).metadata().col};
		$('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 resizeImage(url, width, height) {
	var parts = url.split('?');
	var args = $.parseGetParameters(url, true);
	args['width'] = width;
	args['height'] = height;
	return $.makeURL(parts[0], args);
}

//Returns an array with either the current get-arguments or the get-arguments of the given url.
function parseGetParameters(url, noEnv) {
	var array = {}, index, args, arg, i, key;
	if ((url || (!noEnv && (url = window.location.search))) && (index = url.indexOf('?')) !== -1) {
		args = url.substring(index + 1).split(/&amp;|&/);
		for (i = 0; i < args.length; i++) {
			arg = args[i].split('=');
			// Remove the first entry; it's our key in this pair
			key = arg.shift();
			// If the value also contains unencoded '=' signs, we save the value by rejoining the remainings
			arg = arg.join('=');
			array[key] = (typeof(arg) != 'undefined') ? decodeURIComponent(arg) : '';
		}
	}
	return array;
};

