﻿
	// CREATIONAL
	
	// COPYRIGHT  : 2005-2010 : CREATIONAL : WWW.CREATIONAL.NL : ALL RIGHTS RESERVED
	
	// JAVASCRIPT : General Scripts
	
	
	// JAVASCRIPT : START
	// addEvent(window,'load',CL_tooltipStart);
	// addEvent(window,'load',CL_searchInputSet);


	// JAVASCRIPT : ADD OR REMOVE EVENTS

	function addEvent(obj,evType,fn,useCapture) {
		if (obj.addEventListener) { obj.addEventListener(evType,fn,useCapture); return true; } 
		else if (obj.attachEvent) { var r = obj.attachEvent("on"+evType,fn); return r; } 
		else { alert("Handler could not be attached"); }
	}
	
	function removeEvent(obj,evType,fn,useCapture) {
		if (obj.removeEventListener){ obj.removeEventListener(evType,fn,useCapture); return true; } 
		else if (obj.detachEvent){ var r = obj.detachEvent("on"+evType,fn); return r; } 
		else { alert("Handler could not be removed"); }
	}	
	
	// JAVASCRIPT : E-MAIL OBFUSCATION
	function CL_emailExpand(t,e) {

		if (t === null || typeof t == 'undefined') { return ''; }

		if (t.indexOf('@') > -1 || t.indexOf('&#64;') > -1) {
			var p, pe, es;
			p = t.indexOf('&#64;');
			if (p == -1) { p = t.indexOf('@'); }
			pe = t.indexOf(' ',p);
			if (pe == -1) { pe = t.indexOf('%20',p); }
			if (pe == -1) { pe = t.length; } 
			es = t.substring(0,p);
			p = es.lastIndexOf(' ');
			if (p == -1) { p = es.lastIndexOf('%20'); if (p > -1) { p = p+2; } } else { p = p+1; }
			if (p == -1) { p = 0; }
			es = t.substring(p,pe);
			t = t.replace(es,e);
		}
	
		return t;
	}
	
	function rot13init() {
		var m = [];
		var s = "abcdefghijklmnopqrstuvwxyz",i;
		for (i = 0 ; i < s.length ; i++) {
			m[s.charAt(i)] = s.charAt((i+13)%26);
		}
		for (i = 0 ; i < s.length ; i++) {
			m[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
		}
		return m;
	}
	
	function str_rot13(a,m) {
		var s = "";
		for (var i = 0 ; i < a.length ; i++) {
			var b = a.charAt(i);
			s += (b>='A' && b<='Z' || b>='a' && b<='z' ? m[b] : b);
		}
		return s;
	}
	
	function CL_emailObfuscation() {
		
		if (!document.getElementsByTagName) { return false; }
		var lnks = document.getElementsByTagName('a');

		var m = rot13init(), i = 0; 
		
		function geo_decode(a) {

			var href = a.getAttribute('href');
			var d = href.replace(/.*geo=([a-z0-9._%\-]+)\+([a-z0-9._%\-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
			if (href != d) {				
				
				var p, s = '', e = d, l, t;
				
				p = d.indexOf('&');
				if (p > -1) {
					e = d.substr(0,p);
					s = d.substr(p+1);
					s = s.replace('geo_s=','?subject=');
					
				}
				e = str_rot13(e,m);
				s = CL_emailExpand(s,e);
				a.setAttribute('href','mailto:' + e +s);
				
				l = a.innerHTML;				
				a.innerHTML = CL_emailExpand(l,e);
				
				t = a.getAttribute('title');
				if (t) {
					t = CL_emailExpand(t,e);
					a.setAttribute('title',t);
				}
			}
		}
		for (i; i < lnks.length ; i++) {
			var href = lnks[i].getAttribute('href');
			if (href && href.indexOf('geo=') > -1) {
				geo_decode(lnks[i]);
			}
		}
		
	}
	
	


	// JAVASCRIPT : CLEAR AND SET SEARCH INPUT
	function CL_searchInputFocus() {
		if (document.searchForm.searchInput.value == searchInitialValue) {
		   document.searchForm.searchInput.value = "";
		   document.searchForm.searchInput.className = "search_input_active";
		}
	}
	
	function CL_searchInputFocusModule() {
		if (document.searchFormModule.searchInputModule.value == searchInitialValueModule) {
		   document.searchFormModule.searchInputModule.value = "";
		   document.searchFormModule.searchInputModule.className = "search_input_active";
		}
	}
	
	function CL_searchInputSet() {
		if (document.searchForm || document.searchFormModule) {
			var m1 = '', m2 = '', v = ''; 
			if (arguments[0] === true) { m1 = 'Module'; m2 = '_module'; v = searchInitialValueModule; } else { v = searchInitialValue; }
			var d = navigator.userAgent.toLowerCase();
			if (d.indexOf('safari') > -1) {
				var s = document.getElementById('searchInput'+m1);
				s.type = 'search';
				s.setAttribute('results','0');
				s.setAttribute('placeholder',v);
				s = document.getElementById('searchSubmit'+m1);
				s.style.display = 'none';
				s = document.getElementById('search'+m2);
				s.style.background = 'none';
			} else {
				if (m1 == 'Module' && document.searchFormModule.searchInputModule) {
					if (document.searchFormModule.searchInputModule.value == "") {
					   document.searchFormModule.searchInputModule.value = v;
					   document.searchFormModule.searchInputModule.className = "search_input";
					}			
				} else if (document.searchForm.searchInput) {
					if (document.searchForm.searchInput.value == "") {
					   document.searchForm.searchInput.value = v;
					   document.searchForm.searchInput.className = "search_input";
					}			
				}
			}
		}
	}

	function CL_searchInputSetModule() {
		CL_searchInputSet(true);
	}

	// JAVASCRIPT: DROPDOWN
	var menuname = "languages";
	function CL_startList() {
		if (document.getElementById(menuname)) {
			var r = document.getElementById(menuname);
			for (var i=0; i<r.childNodes.length; i++) {
				var n = r.childNodes[i];
				if (n.nodeName=="LI") {
					n.onmouseover=function() {
						this.className+=" over";
					};
					n.onmouseout=function() {
						this.className=this.className.replace("over", "");
					};
				}
			}
		}
	}
	//addEvent(window,'load',CL_startList);
	
	// JAVASCRIPT : TOOLTIP
	// Based on code by Craig Erskine (grayg.com), Multi-tag support by James Crooke (www.cj-design.com), Inspired by code from Travis Beckham (www.squidfingers.com / www.podlob.com)
	
	var tt_tags = "a,label,div,img"; //  tooltip tags: lower case
	var tt_x = 0;
	var tt_y = 15;
	var tt_s = null;
	var tt_e = null;
	
	CL_tooltip = {
	  name : "tooltip",
	  offsetX : tt_x,
	  offsetY : tt_y,
	  tip : null
	};
	
	CL_tooltip.init = function () {
		
		if (!document.getElementById) { return; }
		if (tt_s !== null) { return; }
		tt_s = true;
		var tu = "http://www.w3.org/1999/xhtml";
		var tc = document.getElementById(this.name);
	
		if (!tc) {
			tc = document.createElementNS ? document.createElementNS(tu, "div") : document.createElement("div");
			tc.setAttribute("id", this.name);
			document.getElementsByTagName("body").item(0).appendChild(tc);
		}
	
		this.tip = document.getElementById(this.name);
		if (this.tip) { document.onmousemove = function(event) { CL_tooltip.move(event); }; }
	
		var a, tt, e;
		
		var el = tt_tags.split(",");
		for(var j = 0; j < el.length; j++) {
			if (tt_e === null) {
				e = document.getElementsByTagName(el[j]); 
			} else {
				e = document.getElementById(tt_e).getElementsByTagName(el[j]); 
			}
			if(e) {
				for (var i = 0; i < e.length; i ++) {
					a = e[i];
					tt = a.getAttribute("title");
					if(tt) {
						a.setAttribute("tiptitle", tt);
						a.removeAttribute("title");
						a.removeAttribute("alt");
						a.onmouseover = function() { CL_tooltip.show(this.getAttribute('tiptitle')); };
						a.onmouseout = function() { CL_tooltip.hide(); };
					}
				}
			}
		}
	};
	
	CL_tooltip.move = function (e) {
		var x=0, y=0;
		if (document.all) {
			x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
			y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
			x += window.event.clientX;
			y += window.event.clientY;			
		} else {
			x = e.pageX;
			y = e.pageY;
		}
		this.tip.style.left = (x + this.offsetX) + "px";
		this.tip.style.top = (y + this.offsetY) + "px";
	};
	
	CL_tooltip.show = function (t) {
		if (!this.tip) { return; }
		t = t.replace('[','<strong>');
		t = t.replace(']','</strong>');
		t = t.replace(/ - /g,'<br />');
		this.tip.innerHTML = t;
		this.tip.style.display = "block";
	};
	
	CL_tooltip.hide = function () {
		if (!this.tip) { return; }
		this.tip.innerHTML = "";
		this.tip.style.display = "none";
	};
	
	CL_tooltipStart = function () {
		CL_tooltip.init ();
	};
	
	
	// JAVASCRIPT : EXPAND / COLLAPSE
	
	function CL_expandCollapse() {
	
		var ttls = $$('#content .expandcollapse');
		var txts = $$('#content .expand');
		
		txts.each (function(txt){
			txt.hide();
		});
		
		ttls.each (function(ttl) {
			ttl.onclick = function(ttl)	{
				var obj = this;
				if (this.tagName == "SPAN") { obj = this.parentNode; }
				if (obj.next('.expand')) { 
					var nt = obj.next('.expand');
					if (nt.getStyle('display') == 'none' || nt.getStyle('display') === null) {
						nt.show();
						this.addClassName('expanded');
					} else {
						nt.hide(); 
						this.removeClassName('expanded');
					}
				}
			};
		});
		
	}
	
	// JAVASCRIPT : MODULE TOGGLE LIST
	
	function CL_moduleToggleList(obj) {

		var l = $(obj).next('ul');

		if ($(l)) {
			if ($(l).getStyle('display') == 'none') {
				$(l).setStyle({display:'block'});
				$(obj).update('-');
			} else {
				$(l).hide();
				$(obj).update('+');
			}
		}
	}
	
	// JAVASCRIPT : RSS FEED
	function CL_getRssFeed(feed) {
		
		var file = '/inc/cl_feed.inc.php';
		if (feed.file) { file = feed.file; }
		
		var hash = new Hash();
		hash.set(rss.url_query_link,feed.url);
		if (feed.limit) { hash.set(rss.url_query_limit,feed.limit); }
		if (feed.desc) { hash.set(rss.url_query_desc,feed.desc); }
		if (feed.date) { hash.set(rss.url_query_date,feed.date); }
		
		var pars = hash.toQueryString();

		var getFeed = new Ajax.Request(file,{ 
									   method: 'get', 
									   parameters: pars, 
									   onSuccess: function(oReq, oJSN) {
		
											var data = eval(oReq.responseText);
											
											if (data == 'ERROR') { 
												
												if (!feed.count) { feed.count = 0; }
												if (!feed.timeout) { feed.timeout = 0; }
												if (!feed.retry) { feed.retry = 5; }
												feed.count = feed.count+1;
												feed.timeout = feed.timeout+rss.count*1000;
												if (feed.count < feed.retry) { setTimeout(function() {CL_getRssFeed(feed); },feed.timeout); }
											
											} else {
											
												var list = Builder.node('ul');
												var li;
												
												$A(data).each(function(d){
													
													li = Builder.node('li',
															Builder.node('a',{href:d.link},[
																Builder.node('span',{className:'rss_title'},d.title),
																Builder.node('br'),
																Builder.node('span',{className:'rss_description'},d.description),
																Builder.node('br'),
																Builder.node('span',{className:'rss_date'},d.date)]
													));
													
													list.appendChild(li);
												
												});
												
												$(feed.id).appendChild(list);
											
											}
											
									},
									onFailure: function() {
										
										if (!feed.count) { feed.count = 0; }
										if (!feed.timeout) { feed.timeout = 0; }
										if (!feed.retry) { feed.retry = 5; }
										feed.count = feed.count+1;
										feed.timeout = feed.timeout+rss.count*1000;
										if (feed.count < feed.retry) { setTimeout(function() {CL_getRssFeed(feed); },feed.timeout); }
									}
		
		});
	}
	
	// JAVASCRIPT : CORRECT LEFTNAV
	function CL_correctLeftnav() {
		var listitems = $$('#leftnav li.current');
		listitems.each (function(li){
			if (!li.down('ul')) {
				li.addClassName('nochildren');	
			}										   
		});	
	}
	
	// JAVASCRIPT : POSITION DONATE BUTTON
	function CL_positionDonate() {
		var cH, y;
		var anchors = $$('a.donate');
		anchors.each (function(ai){										   
			cH = ai.up('.content_inner').offsetHeight;
			y = Math.floor(((cH-35)/2)+23);
			if (y < 45) { y = 45; }
			ai.setStyle({top:y+'px'});
		});		
	}
	
	document.observe('dom:loaded',function() { CL_correctLeftnav(); CL_positionDonate(); });

