
(function($) {
	$.fn.shadowbox = function() {
		$(this).click(function(event) {
			event.preventDefault();
		    var s = Shadowbox;

		    s.open({
		        player:		'iframe',
		        content:	$(this).attr('href'),
		        width:		700,
		        height:		500
		    });
		});
	};

	$.fn.changeOrderBy = function (pValue) {
		$(this).click(function (){
			if ($('#paramOrderBy').val() == pValue) {
				if ($('#paramSort').val() == 'ASC')
					$('#paramSort').val('DESC');
				else
					$('#paramSort').val('ASC');
			} else {
				$('#paramOrderBy').val(pValue);
	
				if (pValue == 'date')
					$('#paramSort').val('DESC');
				else
					$('#paramSort').val('ASC');
			}
	
			$('#params').submit();
		});
	};

	$.fn.changeCategory = function () {
		$(this).change(function () {
			$('#paramCatID').val($(this).val());
			$('#paramPage').val(1);

			$('#params').submit();
		});
	};

	$.fn.paginator = function () {
		$(this).click(function (pEvent) {
			pEvent.preventDefault();
			$('#paramPage').val($(this).html());
			$('#params').submit();
		});
	};
	
	$.fn.initProjectlist = function () {
		// Initialize the shadowbox
		$('ul.projectList li a').shadowbox();
		
		// Some stuff for the project-list
		$('#orderDate').changeOrderBy('date');
		$('#orderProject').changeOrderBy('project');
		$('#orderKunde').changeOrderBy('kunde');

		$('#categories select').changeCategory();

		$('#pages a').paginator();
		
		// Overwrite the submit-event
		$('form#params').submit(function(pEvent) {
			pEvent.preventDefault();
			
			url = $(this).attr('action');
			
			params = {	noframe:	'true',
						catID:		$('#paramCatID').attr('value'),
						orderBy:	$('#paramOrderBy').attr('value'),
						sort:		$('#paramSort').attr('value'),
						page:		$('#paramPage').attr('value')
			};
			
			$.get(url, params, function(pReturn) {
				$('#iframe').html(pReturn);
				$('#iframe').append('<div class="gototop"><a href="#top">Top</a></div>');
				
				$().initProjectlist();
			});
		})
	};
})(jQuery);


jQuery(document).ready(function ($) {
	// Replace the iframe
//	$('iframe').parent().attr('id', 'iframe');

//	var url = $('iframe').attr('src');
//	params = { noframe: 'true' };

//	$.post(url, params, function(pReturn) {
//		$('#iframe').html(pReturn);
//		$('#iframe').append('<div class="gototop"><a href="#top">Top</a></div>');

		// Overwrite the form-submit
//		$().initProjectlist();
//	});
});
