/**
 * class oBgControl
 * this class handles the background image functionality
 */
var oBgControl = {

	/**
	 * @var string sBgHolder
	 * DOM Element for the background image
	 */
	sBgHolder: 'backgroundimage'

	/**
	 * @var object oImage
	 */
	,oImage: null

	/**
	 * @var string sBgPosition
	 */
	 ,sBgPosition: null




	/**
	 * initialise background functionality
	 */
	,init: function() {
		/**
		 * initialise DOM elements
		 */
		// wrapper
		var oContainer = $('<div />')
			.attr('id', oBgControl.sBgHolder)
			.css({'position': 'fixed', 'left': '0px', 'top': '0px', 'overflow': 'hidden', 'z-index': '-9999'});

		// first image
		$('<img />')
			.addClass('bg_first')
			.css({'position': 'fixed', 'left': '0px', 'top': '0px', 'display': 'none'})
			.appendTo(oContainer);

		// second image
		$('<img />')
			.addClass('bg_second')
			.css({'position': 'fixed', 'left': '0px', 'top': '0px', 'display': 'none'})
			.appendTo(oContainer);

		// append the whole thing to the DOM
		$("body").prepend(oContainer);

		// adjust the background size when the window is resized
		$(window).resize(function() {
			oBgControl.stretch($('#backgroundimage .bg_first'));
		});
	}




	/**
	 * swap background image
	 */
	,swap: function(sImgPath, sBgPosition, fCallback) {
		oBgControl.sBgPosition = sBgPosition;

		if (typeof sImgPath != 'string' || sImgPath == null || sImgPath == '/') {
			if (typeof fCallback == 'function') {
				fCallback();
			}
		}
		
		// workaround if background image is undefined
		if (typeof sImgPath == 'undefined') {
			sImgPath = '/fileadmin/templates/media/spacer.png';
		}
		
		$('#backgroundimage .bg_second')
			.unbind()
			.bind('load', function() {
				$('#loader-overlay').remove();
				if($('#donation').css('display')=='none') {
					window.setTimeout(function() {
						$('#donation')
							.css('display', 'block')
							.animate({'right':'-246px'}, 300, 'swing');
					},1000);
				}
				var oSelf = $(this);
				oBgControl.stretch(
					oSelf
					,function() {
						oSelf.fadeIn(
							500
							,function() {
								$('.bg_first').attr('src', '')
									.removeClass('bg_first')
									.addClass('bg_second')
									.css('width', 'auto')
									.css('height', 'auto')
									.hide();
								oSelf
									.removeClass('bg_second')
									.addClass('bg_first');
								$('#footer').css('top','auto');

								if (typeof fCallback == 'function') {
									fCallback();
								}
							}
						);
					}
				);
			})
			.attr('src', sImgPath);
	}



	/**
	 * stretch an image to the window size
	 */
	,stretch: function(oImage, callback) {
		var iWinW = $(window).width();
		var iWinH = $(window).height();
		var iImgW = oImage.width();
		var iImgH = oImage.height();
		var fImgR = iImgH / iImgW;
		var iBgW = 0;
		var iBgH = 0;

		var iLeftMargin = 0;
		var iTopMargin = 0;

		iBgW = iWinW;
		iBgH = parseInt(iWinW * fImgR);

		if (iBgH < iWinH) {
			iBgH = iWinH;
			iBgW = parseInt(iWinH / fImgR);
			iLeftMargin = parseInt((iWinW - iBgW) / 2);
		} else {
			switch(oBgControl.sBgPosition) {
				case 'middle':
					iTopMargin = parseInt((iWinH - iBgH) / 2);
					break;

				case 'bottom':
					iTopMargin = iWinH - iBgH;
					break;
			}
		}

		oImage.css({
			'top':		iTopMargin + 'px'
			,'left':	iLeftMargin + 'px'
			,'width':	iBgW + 'px'
			,'height':	iBgH + 'px'
		});

		if (typeof callback == 'function') {
			callback();
		}

	}

}

