/******************************************************************************
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License 
More info: http://creativecommons.org/licenses/by-sa/2.5/

Author: Colin Meerveld
Last update: 01-06-06
******************************************************************************/

// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// Assign a javascript namespace
if (!ATHOC) var ATHOC = new Object();

/******************************************************************************
 ATHOC.SVG module v1.1
 
 This module check if SVG is supported and if not it looks if flash is supported
 if SVG is not supported but flash does. The SVG object converts to Flash
 
 1.1 IE meeds to embed svg with the embed element
 
******************************************************************************/
 
ATHOC.SVG = {
	checkSVG		:	function () {
		if(!ATHOC.SVG.hasPlugin('image/svg-xml', 'Adobe.SVGCtl')){	
			// Only convert to flash if flash is supported
			if(ATHOC.SVG.hasPlugin('application/x-shockwave-flash', 'ShockwaveFlash.ShockwaveFlash.6')){
				ATHOC.SVG.SVG2SWF();
			}
		} else {
			// Place the embed element for IE
			/*@cc_on @*/
			/*@if (@_jscript_version >= 5)	
				var svgObject = document.getElementsByTagName("object")
					for (var i=0; i < svgObject.length; i++){	
						if(svgObject[i].getAttribute("type") == "image/svg+xml"){
						 var classname = svgObject[i].getAttribute("className");
						  var objectInnerHTML = svgObject[i].innerHTML;
						 svgObject[i].innerHTML = '<embed src="root_en/svg/'+ classname +'.svg" type="image/svg-xml" class="'+ classname +'" wmode="transparent" />' +
						 '<noembed>' +
						 '' + objectInnerHTML + '</noembed>';
						}
					}
			@end @*/		
		}
	},	
	hasPlugin		:	function (mimeType, activeX) {
		var plugin = false;
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		// M.b.v. JScript wordt dit enkel uitgevoerd in IE.
			var testPlugin;
			try {
				testPlugin = new ActiveXObject(activeX);
			}
			catch (e) {
				testPlugin = false;
			}
			if (testPlugin){
				var plugin = true;
			}
		@end @*/
		if(ATHOC.SVG.hasMimeType(mimeType)) {
				plugin = true; 
		}	
		return plugin;
	},
	hasMimeType		:	function (mimeType) {	
		// TODO FF dont detect the flash plugin
	
		var mimeType = false;
		// Plugin support		
		if(navigator.mimeTypes && navigator.mimeTypes[mimeType]){
			var mimeType = true;	
		}
		
		// Native support for SVG
		if(typeof window.SVGElement != 'undefined'){
			var mimeType = true;	
		}
		
		return mimeType;
	},
	SVG2SWF		:	function () {
		var svgObject = document.getElementsByTagName("object");
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
			var svgObject = document.getElementsByTagName("b");
		@end @*/
		for (var i=0; i < svgObject.length; i++){	
			if(svgObject[i].getAttribute("type") == "image/svg+xml"){
				classname = svgObject[i].getAttribute("class");
				/*@cc_on @*/
				/*@if (@_jscript_version >= 5)				
					classname = svgObject[i].getAttribute("className");
				@end @*/
				var objectInnerHTML = svgObject[i].innerHTML;
				// Must be one string because += doesn't work. The object close before the inner elements are put inside
				svgObject[i].innerHTML = '<object data="root_en/flash/'+ classname +'.swf" type="application/x-shockwave-flash" class="'+ classname +'">' +
				'<param name="movie" value="root_en/flash/'+classname+'.swf" />' +
				'<param name="wmode" value="transparent" />' + objectInnerHTML + '</object>';
			}	
		}
	}
};

/******************************************************************************
 ATHOC.Display module v1.0
 
 This module handles the changes of the Display like resizing.

******************************************************************************/
 
ATHOC.Display = {
	resize			:	function (){
		var oldFontSize = "62.5";
		var bodyElm = document.getElementsByTagName("body")[0];
		// Detect the body width of the browser in px
		var clientWidth = document.body.clientWidth;

		// Calculate the new font-size based on the clientWidth
		var newFontSize = Math.round(oldFontSize / (800 / clientWidth)*10)/10;
		// Set the new font-size if the size is bigger then the old font-size
		if(newFontSize > oldFontSize){
			bodyElm.style.fontSize = newFontSize + "%";
			document.getElementById("secundarycontent").style.width = "33em";
		} else {
			newFontSize = Math.round(oldFontSize / (451 / clientWidth)*10)/10;
			bodyElm.style.fontSize = newFontSize + "%";
			//bodyElm.style.fontSize = oldFontSize + "%";
			//document.getElementById("primarycontent").style.width = "100%";
			document.getElementById("secundarycontent").style.width = "44.1em";
		}
	}
};

/******************************************************************************
ATHOC.Delicious module v1.0
 
Handler for the delicious bookmarks

******************************************************************************/
 
ATHOC.Delicious = {
	cloud			:	function (){
		var ta=0,tz=30;
		function s(a,b,i,x){
			if(a > b){
				var m=(a-b)/Math.log(x),v=a-Math.floor(Math.log(i)*m);
			} else {
				var m=(b-a)/Math.log(x),v=Math.floor(Math.log(i)*m+a);
			};
			return v;
		}
		var ca = [150,150,150], cz = [50,50,50], c = [];
		var deliciousDiv = document.getElementById("delicious");
		var li = deliciousDiv.getElementsByTagName("li");
		for(var t=0; t < li.length; t++){
			var title = li[t].getAttribute("title");
			var count = title.substr(title.indexOf(":")+1);	
 			for (var i=0;i<3;i++) {
				c[i] = s(ca[i],cz[i],count-ta,tz);
				var fs = s(11,35,count-ta,tz);
				li[t].style.fontSize = (fs/10) +'em';
				//li[t].childNodes[0].style.color = 'rgb('+c[0]+','+c[1]+','+c[2]+')';
			}
		}
	}
}





/******************************************************************************
 ATHOC.ImgStyle module v1.0
******************************************************************************/

ATHOC.ImgStyle = {
	element					: 	null,
	getComputedStyle		:	function(element) {
		this.element = element;

	},
	getPropertyValue		:	function(style) {
		var result;
		document.getElementById('footer').innerHTML = "test: " +  this.element;		
		switch (style) {
		   case 'width':
			 result = this.element.width;
		   case 'height':
			 result = this.element.height;
		}
		return result;	
	}
}

/******************************************************************************
GARDO.Request module v1.0

AHAH is a very simple technique for dynamically updating web pages using JavaScript. 
It involves using XMLHTTPRequest to retrieve (X)HTML fragments which are then 
inserted directly into the web page, whence they can be styled using CSS.
See for more information: http://microformats.org/wiki/rest/ahah
* ******************************************************************************/

ATHOC.Request = {
	ahah 			:	function(url,target) {
	   // native XMLHttpRequest object
	   document.getElementById(target).innerHTML = 'Bezig met laden.';
	   if (window.XMLHttpRequest) {
	       req = new XMLHttpRequest();
	       req.onreadystatechange = function() {GARDO.Request.ahahDone(target);};
	       req.open("GET", url, true);
	       req.send(null);
	   // IE/Windows ActiveX version
	   } else if (window.ActiveXObject) {
	       req = new ActiveXObject("Microsoft.XMLHTTP");
	       if (req) {
	           req.onreadystatechange = function() {GARDO.Request.ahahDone(target);};
	           req.open("GET", url, true);
	           req.send();
	       }
	   }
	},
	
	ahahDone		: 	function(target) {
		// Preloader
		document.getElementById(target).innerHTML += ".";
	    // only if req is "loaded"
	    if (req.readyState == 4) {
	        // only if "OK"
		    if (req.status == 200 || req.status == 304 || req.status == 0) {
		        results = req.responseText;
		        document.getElementById(target).innerHTML = results;
		    } else {
		        document.getElementById(target).innerHTML = "Er is een fout gevonden:\n" +
		                req.statusText;
	        }
	    }
	}
	
	
}

window.onresize = ATHOC.Display.resize;
addLoadEvent(ATHOC.SVG.checkSVG);
addLoadEvent(ATHOC.Display.resize);
addLoadEvent(ATHOC.Delicious.cloud);


