/* Digit ry
* Common javascript library
*
* created: 23.6.2006
* author: Jouni Koivuviita
**/


var Digit = {

	menuHideHandle : null,
	
	init : function() {
		// Search button
		Event.observe($('searchGo'), 'click', function(){ $('searchForm').submit(); }, false);
		
		// Diginfo button
		if(document.getElementById('diginfoGo'))
			Event.observe($('diginfoGo'), 'click', function(){ $('diginfo').submit(); }, false);
		
		// Submenus
		//var buttons = document.getElementsByClassName("button", "mainNav");
		//for(var i=0; i < buttons.length; i++) {
			//Event.observe(buttons[i], 'mouseover', Digit.showMenu);
			//Event.observe($("mainNav"), 'mouseout', function(){Digit.menuHideHandle = setTimeout(Digit.hideMenus, 500);});
		//}
	},
	
	showMenu : function() {
		clearTimeout(Digit.menuHideHandle);
		var name = this.className.substr(7);
		var menus = document.getElementsByClassName(name, "subNav");
		menus[0].className += " show";
		Event.observe(menus[0], 'mousemove', Digit.showMenuSelf);
		Event.observe(menus[0], 'mouseout', Digit.hideMenuSelf);
	},
	
	hideMenus : function() {
		clearTimeout(Digit.menuHideHandle);
		var menus = document.getElementsByTagName("ul", "subNav");
		for(var i=0; i < menus.length; i++) {
			var breakIndex = menus[i].className.indexOf(" ");
			if(breakIndex != -1) menus[i].className = menus[i].className.substring(0, breakIndex);
			Event.stopObserving(menus[i], 'mousemove', Digit.showMenuSelf);
			Event.stopObserving(menus[i], 'mouseout', Digit.hideMenuSelf);
		}
	},
	
	showMenuSelf : function() {
		clearTimeout(Digit.menuHideHandle);
		this.className += " show";
	},
	
	hideMenuSelf : function() {
		var breakIndex = this.className.indexOf(" ");
		if(breakIndex != -1) this.className = this.className.substring(0, breakIndex);
		Event.stopObserving(this, 'mousemove', Digit.showMenuSelf);
		Event.stopObserving(this, 'mouseout', Digit.hideMenuSelf);
	},
	
	
	getPosition : function(anchorname) {
		// This function will return an Object with x and y properties
		var useWindow=false;
		var coordinates=new Object();
		var x=0,y=0;
		// Browser capability sniffing
		var use_gebi=false, use_css=false, use_layers=false;
		if (document.getElementById) { use_gebi=true; }
		else if (document.all) { use_css=true; }
		else if (document.layers) { use_layers=true; }
		// Logic to find position
	 	if (use_gebi && document.all) {
			x=Digit.getPageOffsetLeft(document.all[anchorname]);
			y=Digit.getPageOffsetTop(document.all[anchorname]);
		}
		else if (use_gebi) {
			var o=document.getElementById(anchorname);
			x=Digit.getPageOffsetLeft(o);
			y=Digit.getPageOffsetTop(o);
		}
	 	else if (use_css) {
			x=Digit.getPageOffsetLeft(document.all[anchorname]);
			y=Digit.getPageOffsetTop(document.all[anchorname]);
		}
		else if (use_layers) {
			var found=0;
			for (var i=0; i<document.anchors.length; i++) {
				if (document.anchors[i].name==anchorname) { found=1; break; }
			}
			if (found==0) {
				coordinates.x=0; coordinates.y=0; return coordinates;
			}
			x=document.anchors[i].x;
			y=document.anchors[i].y;
		}
		else {
			coordinates.x=0; coordinates.y=0; return coordinates;
		}
		coordinates.x=x;
		coordinates.y=y;
		return coordinates;
	},
	
	// Functions for IE to get position of an object
	getPageOffsetLeft : function(el) {
		var ol=el.offsetLeft;
		while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
		return ol;
	},

	getWindowOffsetLeft : function(el) {
		return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	},

	getPageOffsetTop : function(el) {
		var ot=el.offsetTop;
		while((el=el.offsetParent) != null) { ot += el.offsetTop; }
		return ot;
	},

	getWindowOffsetTop : function(el) {
		return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	},


	loopTimeout : new Array(0),

	posAndSizeAnim : function(obj,newTop,newLeft,newWidth,newHeight,tableIndex,userSpeed) {
		var index = tableIndex;	
		var object = obj;
		var div = document.getElementById(object);
		if(!div) return;
			
		var new_top = newTop;
		var new_left = newLeft;
		var new_width = newWidth;
		var new_height = newHeight;
		var definedSpeed = userSpeed;
		
		var old_left = Number(div.style.left.split("px")[0]);
		var old_top = Number(div.style.top.split("px")[0]);
		var old_width = Number(div.style.width.split("px")[0]);
		var old_height = Number(div.style.height.split("px")[0]);

		var allDone = true;
		if (!definedSpeed) var speed = 22;
		else var speed = definedSpeed;
		
		var limiter = Math.max( Math.abs(new_top-old_top), Math.abs(new_left-old_left), Math.abs(new_width-old_width), Math.abs(new_height-old_height) );

		var topSpeed = Math.ceil(Math.abs(new_top-old_top)/limiter*speed);
		var leftSpeed = Math.ceil(Math.abs(new_left-old_left)/limiter*speed);
		var widthSpeed = Math.ceil(Math.abs(new_width-old_width)/limiter*speed);
		var heightSpeed = Math.ceil(Math.abs(new_height-old_height)/limiter*speed);
		
		if(new_top != null) {
			if(Math.abs(new_top-old_top) > topSpeed) {
				allDone = false;		
				var direction = (new_top-old_top)/Math.abs(new_top-old_top);
				div.style.top = old_top + direction*topSpeed + "px";
			} else div.style.top = new_top + "px";
		}
		
		if(new_left != null) {
			if(Math.abs(new_left-old_left) > leftSpeed) {
				allDone = false;		
				var direction = (new_left-old_left)/Math.abs(new_left-old_left);
				div.style.left = old_left + direction*leftSpeed + "px";
			} else div.style.left = new_left + "px";
		}
		
		if(new_width != null) {
			if(Math.abs(new_width-old_width) > widthSpeed) {
				allDone = false;		
				var direction = (new_width-old_width)/Math.abs(new_width-old_width);
				div.style.width = old_width + direction*widthSpeed + "px";
			} else div.style.width = new_width + "px";
		}
		
		if(new_height != null) {
			if(Math.abs(new_height-old_height) > heightSpeed) {
				allDone = false;		
				var direction = (new_height-old_height)/Math.abs(new_height-old_height);
				div.style.height = old_height + direction*heightSpeed + "px";
			} else div.style.height = new_height + "px";
		}
			
		
		if (!allDone) Digit.loopTimeout[index] = setTimeout("Digit.posAndSizeAnim('"+object+"',"+new_top+","+new_left+","+new_width+","+new_height+","+index+","+speed+")",1);
		else Digit.loopTimeout[index] = 0;
	},
	
	
	/**
	 * Sets a Cookie with the given name and value.
	 *
	 * name       Name of the cookie
	 * value      Value of the cookie
	 * [expires]  Expiration date (in hours) of the cookie (default: end of current session)
	 * [path]     Path where the cookie is valid (default: path of calling document)
	 * [domain]   Domain where the cookie is valid
	 *              (default: domain of calling document)
	 * [secure]   Boolean value indicating if the cookie transmission requires a
	 *              secure transmission
	 */
	setCookie : function(name, value, expires, path, domain, secure) {
		var today = new Date();
		if(expires && expires > 0) {
			var hours = Math.floor(expires);
			var mins = Math.floor(60*(expires - hours));
			var secs = Math.floor(60*(60*(expires - hours) - mins));
			hours += today.getHours();
			mins += today.getMinutes();
			secs += today.getSeconds();
			today.setHours(hours,mins,secs);
		}
	    document.cookie= name + "=" + escape(value) +
	        ((expires) ? "; expires=" + today.toGMTString() : "") +
	        ((path) ? "; path=" + path : "") +
	        ((domain) ? "; domain=" + domain : "") +
	        ((secure) ? "; secure" : "");
	},

	/**
	 * Gets the value of the specified cookie.
	 *
	 * name  Name of the desired cookie.
	 *
	 * Returns a string containing value of specified cookie,
	 *   or null if cookie does not exist.
	 */
	getCookie : function(name) {
	    var dc = document.cookie;
	    var prefix = name + "=";
	    var begin = dc.indexOf("; " + prefix);
	    if (begin == -1) {
	        begin = dc.indexOf(prefix);
	        if (begin != 0) return null;
	    } else {
	        begin += 2;
	    }
	    var end = document.cookie.indexOf(";", begin);
	    if (end == -1) {
	        end = dc.length;
	    }
	    return unescape(dc.substring(begin + prefix.length, end));
	},

	/**
	 * Deletes the specified cookie.
	 *
	 * name      name of the cookie
	 * [path]    path of the cookie (must be same as path used to create cookie)
	 * [domain]  domain of the cookie (must be same as domain used to create cookie)
	 */
	deleteCookie : function(name, path, domain) {
	    if (getCookie(name)) {
	        document.cookie = name + "=" +
	            ((path) ? "; path=" + path : "") +
	            ((domain) ? "; domain=" + domain : "") +
	            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	    }
	}
	
};

Event.observe(window, 'load', Digit.init, false);
