
var StatusIndicator = {
	statusIndicatorState: "minimized",
	statusIndicatorHeight: 0,
	init: function(){
		// Creeer statusindicator wrapper
		// Hierin worden referenties naar this met StatusIndicator aangegeven
		$.get("http://www.lifedesign.nl/status-indicator/statusIndicator.php", function(data){
			/* Error in IE 6
			 * $("body").html('<div id="si-container">'+$("body").html()+'</div>'+data);
			 */
			document.body.innerHTML = '<div id="si-container">'+$("body").html()+'</div>'+data;
			
			if(element = document.getElementById("status-indicator")){
				// Preload complete event
				ipLoad = function (){
					StatusIndicator.statusIndicatorHeight = $("#status-indicator").height();
					
					$("#status-indicator").css('height', '0px');
					$("#status-indicator").css('display', 'block');
					
					$("#status-indicator .btn").css('cursor', 'pointer');
					$("#status-indicator .btn").click(function(){
						StatusIndicator.animateStatusIndicator();
					});
					
					var cookieState = $.cookie('statusIndicator');
					cookieState = cookieState == "maximized" ? "minimized" : (cookieState == "minimized" ? "maximized" : cookieState);
					
					StatusIndicator.statusIndicatorState = cookieState != null ? cookieState : StatusIndicator.statusIndicatorState;
					
					StatusIndicator.animateStatusIndicator();
				}
				
				var iElm = element.getElementsByTagName('IMG');
				var imgA = new Array();
				
				for(i in iElm){
					if(typeof(iElm[i]) == "object"){
						imgA[imgA.length] = iElm[i].src;
					}
				}
				
				var ip = new ImagePreloader(imgA, ipLoad);
			}
		});
	},
	animateStatusIndicator: function(){
		switch(this.statusIndicatorState){
			case "minimized":
				$("#status-indicator").animate({ 
					height: this.statusIndicatorHeight,
					right: 100
					}, 1500, 'easeinout' );
				this.statusIndicatorState = "maximized";
				$.cookie('statusIndicator', 'maximized');
				$("#status-indicator .btn").attr('src','http://statusindicator.lifedesign.nl/status-indicator/minimaliseren.jpg');
				break;
			case "maximized":
			default:
				$("#status-indicator").animate({ 
					height: 20,
					right: 20
					}, 1500, 'easeinout' );
				this.statusIndicatorState = "minimized";
				$.cookie('statusIndicator', 'minimized');
				$("#status-indicator .btn").attr('src','http://statusindicator.lifedesign.nl/status-indicator/maximaliseren.jpg');
				break;
		}
	}
};

// Image preloader class
function ImagePreloader(images, callback){
	// store the call-back
	this.callback = callback;
	// initialize internal state.
	this.nLoaded = 0;
	this.nProcessed = 0;
	this.aImages = new Array;
	// record the number of images.
	this.nImages = images.length;
	
	// for each image, call preload()
	for(var i=0; i < images.length; i++)
		this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image){
	// create new Image object and add to array
	var oImage = new Image;
	this.aImages.push(oImage);
	// set up event handlers for the Image object
	oImage.onload = ImagePreloader.prototype.onload;
	oImage.onerror = ImagePreloader.prototype.onerror;
	oImage.onabort = ImagePreloader.prototype.onabort;
	// assign pointer back to this.
	oImage.oImagePreloader = this;
	oImage.bLoaded = false;
	// assign the .src property of the Image object
	oImage.src = image;
}

ImagePreloader.prototype.onComplete = function(){
	this.nProcessed++;
	if(this.nProcessed == this.nImages){
		this.callback();
	}
}

ImagePreloader.prototype.onload = function(){
	this.bLoaded = true;
	this.oImagePreloader.nLoaded++;
	this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function(){
	this.bError = true;
	this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function(){
	this.bAbort = true;
	this.oImagePreloader.onComplete();
}

// Initialiseer statusindicator wanneer DOM is geladen
$(document).ready(function(){
	jQuery.easing = {
		easeinout: function(x, t, b, c, d) {
			if (t < d/2) return 2*c*t*t/(d*d) + b;
			var ts = t - d/2;
			return -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b;
		},
		linear: function(x, t, b, c, d) {
			return c*t/d + b; //linear
		}
	};
	StatusIndicator.init();
});
