// Copyright (c) 2006 Tibor Claassen
/**
 * JavaScript class library
 *
 * @author	Tibor Claassen
 * @author	tibor@imagerator.com
 * @date	2006-12-06
 */

/* Prepare namespace simulation */
if (!de) var de = new Object();
if (!de.mediaunit) de.mediaunit = new Object();


/**
 * Creates classes, manages inheritance.
 */
de.mediaunit.Class = {
	
	create: function(constructor, prototype, staticFields) {
		
		return de.mediaunit.Class.extend(Function, constructor, prototype, staticFields);
		
	},
	
	extend: function(superclass, constructor, prototype, staticFields) {
		
		if (superclass instanceof Function) {
			
			var c = (constructor instanceof Function) ? constructor : function() { superclass.apply(this, arguments) };
			c.prototype = new superclass();
			c.prototype.constructor = c;
			for (var p in prototype) c.prototype[p] = prototype[p];
			for (p in staticFields) c[p] = staticFields[p];
			return c;
			
		} else throw new Error("Cannot inherit from " + superclass + ". Invalid argument.");
	}
};

/**
 * Preload images.
 */
de.mediaunit.ImageLoader = de.mediaunit.Class.create(
	null,
	{
		add: function(url) {
			var image = new Image();
			image.src = url;
		}
	}
);

/**
 * Toggles the visibility of child nodes with the class attribute "expanded".
 */
de.mediaunit.Squeezer = de.mediaunit.Class.create(
	
	function(id) {
		this.expanded = true;
		this.id = id;
		this.nodes = [];
		this.link = null;
		this.root = document.getElementById(id);
		this.scrollOffset = {x: 0, y: 0};
		
		if (this.root == null) return;
		this.searchNodes(this.root);
		var links = this.root.getElementsByTagName("a");
		var i = links.length;
		while (i--) {
			if (/\btoggle\b/.exec(links[i].className)) {
				this.link = links[i];
				break;
			}
		}
		this.preloadImages();
		if (this.link == null) throw new Error("No link found with class \"toggle\" in element with id \"" + id + "\".");
		var self = this;
		this.link.onfocus = function() {this.blur()};
		this.link.onclick = function() {self.toggle(self); self.resetScrolling(); return false;};
		this.toggle(this);
	}, {

		toggle: function(self) {
			self.toggleVisibility();
			self.refreshStatus();
		},
		
		searchNodes: function(parentNode) {
			for (var n = parentNode.firstChild; n != null; n = n.nextSibling) {
				if (/\bexpanded\b/.exec(n.className)) this.nodes.push(n);
				else if (n.hasChildNodes) this.searchNodes(n);
			}
		},
		
		toggleVisibility: function() {
			this.expanded = !this.expanded;
			var newValue = (this.expanded) ? "block" : "none";
			for (var i in this.nodes ) this.nodes[i].style.display = newValue;
		},
		
		refreshStatus: function() {
			var prefix = (this.expanded) ? "minus" : "plus";
			this.link.onmouseout = function() { this.style.backgroundImage = "url(img/" + prefix + ".gif)";};
			this.link.onmouseover = function() { this.style.backgroundImage = "url(img/" + prefix + "_over.gif)";};
			this.link.onmouseout();
		},
		
		preloadImages: function() {
			var plus = new Image();
			plus.src = "img/plus.gif";
			var plusOver = new Image();
			plusOver.src = "img/plus_over.gif";
			var minus = new Image();
			minus.src = "img/minus.gif";
			var minusOver = new Image();
			minusOver.src = "img/minus_over.gif";
		},
		
		resetScrolling: function() {
			var scrollOffset = de.mediaunit.Squeezer.getScrollOffset();
			scrollTo(scrollOffset.x, scrollOffset.y);		}
	}, {
		
		getScrollOffset: function() {
			
			var offset = {x: 0, y: 0};	
				
			if (typeof(window.pageYOffset) == 'number') {
				offset.y = window.pageYOffset;
				offset.x = window.pageXOffset;
			}
			else if (document.body && (document.body.scrollTop || document.body.scrollLeft)) {
				offset.y = document.body.scrollTop;
				offset.x = document.body.scrollLeft;
			}
			else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
				offset.y = document.documentElement.scrollTop;
				offset.x = document.documentElement.scrollLeft;
			}
			
			return offset;
		}
	}
);


de.mediaunit.JobSqueezer = de.mediaunit.Class.extend( de.mediaunit.Squeezer,
	
	function(id) {
		de.mediaunit.Squeezer.call(this, id);
	}, {
		
		refreshStatus: function() {
			var prefix = (this.expanded) ? "minus" : "plus";
			var folder = this.id;
			if (this.expanded) {
				this.link.onmouseout = this.link.onmouseover = null;
				this.link.style.backgroundImage = "url(img/minus_over.gif)";
				this.link.firstChild.style.backgroundImage = "url(jobs/" + this.id + "/headline_over.gif)";
			} else {
				this.link.onmouseout = function() { 
					this.style.backgroundImage = "url(img/plus.gif)";
					this.firstChild.style.backgroundImage = "url(jobs/" + folder + "/headline.gif)";
				};
				this.link.onmouseover = function() {
					this.style.backgroundImage = "url(img/plus_over.gif)";
					this.firstChild.style.backgroundImage = "url(jobs/" + folder + "/headline_over.gif)";
				};
				this.link.onmouseout();
			}
		}
	}
);
