﻿
	// CREATIONAL
	
	// COPYRIGHT  : 2005-2010 : CREATIONAL : WWW.CREATIONAL.NL : ALL RIGHTS RESERVED
	
	// JAVASCRIPT : General Scripts
	
	
	// 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(' ',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(' '); } 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
	// ClassNames: 'search_input_active' and '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); }
	// addEvent(window,'load',CL_searchInputSet);

	// JAVASCRIPT: DROPDOWN
	// ClassName: 'over'
	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", tt_x = 0, tt_y = 15, tt_s = null, 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 (); };
	// addEvent(window,'load',CL_tooltipStart);
		
	// JAVASCRIPT : EXPAND / COLLAPSE
	// ClassNames: 'expandcollapse', 'expand' and 'expanded'
	// Required: Prototype.js
	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'); } } }; }); }
	document.observe('dom:loaded',CL_expandCollapse);
	
	// JAVASCRIPT : MODULE TOGGLE LIST	
	// Required: Prototype.js
	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
	// Required: Protoaculous.js	
	var CL_getFeed = Class.create({
		initialize: function(feed) { if(!document.getElementById || !feed) { return; } 	var pars='', hash, m=1; this.feed = feed; if ($(this.feed.loading)) { $(this.feed.loading).show(); } this.feed = Object.toJSON(this.feed); this.feed = this.feed.evalJSON(); if (!this.feed.id) { return false; } if (!$(this.feed.id)) { return false; } this.feed.mode = 'rss'; if ((this.feed.type && this.feed.keywords) || (this.feed.url && this.feed.url.include('twitter'))) { this.feed.mode = 'twitter'; if (this.feed.url) { if (this.feed.limit) { hash = new Hash(); if (!this.feed.url.include('include_rts')){ m = 3; } hash.set('count',this.feed.limit*m); pars = hash.toQueryString(); } } else { 
			this.feed.url_fu = "http://search.twitter.com/search.json?q=from%3A"; 
			this.feed.url_fm = "http://search.twitter.com/search.json?from%3A"; 
			this.feed.url_tu = "http://search.twitter.com/search.json?q=to%3A"; 
			this.feed.url_tm = "http://search.twitter.com/search.json?to%3A"; 
			this.feed.url_sw = "http://search.twitter.com/search.json?q="; this.twitterFeed(); } 
			this.feed.url_ts = "http://twitter.com/search?q="; 
			this.feed.url_tp = "http://twitter.com/"; 
			this.feed.url_tr = "http://twitter.com/?status=@%NAME%%20&in_reply_to_status_id=%ID%&in_reply_to=%NAME%"; this.feed.target = (this.feed.target) ? ' target="'+this.feed.target+'"': ''; } if (!this.feed.url) { return false; } var cacheobj = this; 
			if (this.feed.callback_param && this.feed.url.include('.json')) {
				var ajaxFeed = new Ajax.JSONRequest(this.feed.url, { callbackParamName: this.feed.callback_param, parameters: pars, onSuccess: function(oResponse) { var data = oResponse.responseText.evalJSON(true); if (data == 'ERROR') { cacheobj.reloadFeed(); } else { cacheobj.showFeed(data); } }, onFailure: function() { cacheobj.reloadFeed(); } }); 
			} else { 
				var file = '/inc/cl_feeds.inc.php'; if (this.feed.file) { file = this.feed.file; } hash = new Hash(); hash.set(rss.url_query_link,this.feed.url); if (this.feed.limit) { hash.set(rss.url_query_limit,this.feed.limit); } if (this.feed.desc) { hash.set(rss.url_query_desc,'TRUE'); } if (this.feed.date) { hash.set(rss.url_query_date,this.feed.date); } if (rss.tree) { hash.set(rss.url_query_tree,rss.tree); } if (rss.sites) { hash.set(rss.url_query_sites,rss.sites); } if (rss.path) { hash.set(rss.url_query_path,rss.path); } pars = hash.toQueryString();
				var ajaxFeed = new Ajax.Request(file,{ method: 'get', parameters: pars, onSuccess: function(oResponse) { var data = oResponse.responseText.evalJSON(true); if (data == 'ERROR') { cacheobj.reloadFeed(); } else { cacheobj.showFeed(data); } }, onFailure: function() { cacheobj.reloadFeed(); } }); 
			} if (this.feed.live) { setTimeout(function() { this.CL_getFeed(); },this.feed.live*1000); } },
		reloadFeed: function () { if (!this.feed.count) { this.feed.count = 0; } if (!this.feed.timeout) { this.feed.timeout = 0; } if (!this.feed.retry) { this.feed.retry = 5; } this.feed.count = this.feed.count+1; this.feed.timeout = this.feed.timeout+this.feed.count*3000; if (this.feed.count < this.feed.retry) { setTimeout(function() { this.CL_getFeed(); },this.feed.timeout); } },
		twitterFeed: function() { this.feed.url = ""; var t = this.feed.type; var kw = this.feed.keywords; var sm = kw.search(/,/); if (sm > 0) { kw = "&ors="+kw.replace(/,/g,"+"); } if (t == "fromUser" && sm <= 0) { this.feed.url = this.feed.url_fu+kw; } else if (t == "fromUser" && sm >= 0) { this.feed.url = this.feed.url_fm+kw; } else if (t == "toUser" && sm <= 0) { this.feed.url = this.feed.url_tu+kw; } else if (t == "toUser" && sm >= 0) { this.feed.url = this.feed.urltm+kw; } else if (t == "searchWord") { this.feed.url = this.feed.url_sw+kw; var lang = (this.feed.lang) ?  this.feed.lang : ''; if (lang.length > 0) { this.feed.url+= "&lang="+lang; } } this.feed.url += "&rpp="; if (this.feed.limit) { this.feed.url += this.feed.limit; } else { this.feed.url+= 5; } },
		filterFeed: function (txt) { if (this.feed.filter == '') { return txt; } var s, sw, si, pattern; sw = this.feed.filter; sw = sw.split(","); if (sw.length > 0) { $A(sw).each(function(si) {	sA = si.split("->"); if (sA.length > 0) { s = sA[0]; r = sA[1]; pattern = eval('/'+s+'/gi'); txt = txt.replace(pattern, r); } }); } return txt; },
		formatFeed: function (txt) { var pattern,kw,s,w; pattern = /(\b(((https?|ftp|file):\/\/)|www.)[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; txt = txt.replace(pattern,'<a href="$1"'+this.feed.target+' class="twitter_link">$1</a>'); txt = txt.replace('href="www.','href="http://'); pattern = /[\@]+([A-Za-z0-9-_]+)/ig; txt = txt.replace(pattern,'<a href="'+this.feed.url_tp+'$1"'+this.feed.target+' class="twitter_link_profile">@$1</a>'); pattern = /[\#]+([A-Za-z0-9-_]+)/ig; txt = txt.replace(pattern,'<a href="'+this.feed.url_ts+'#$1"'+this.feed.target+' class="twitter_link_hash">#$1</a>'); if (this.feed.type == "searchWord") { kw = this.feed.keywords; kw = kw.replace(/&ors=/,""); kw = kw.split("+"); $A(kw).each(function(w){ pattern=eval('/'+w+'/gi'); s = new String(' <strong>'+w+'</strong> '); txt = txt.replace(pattern, s); }); } return txt; },
		relativeTime: function (date) { if (this.feed.date_rel !== true) { return date; } var v, pd, rt, d, txt = ''; v = date.split(" "); date = v[1] + " " + v[2] + ", " + v[5] + " " + v[3]; pd = Date.parse(date); rt = (this.feed.currentdate) ? this.feed.currentdate : new Date(); d = parseInt((rt.getTime() - pd) / 1000); d = d + (rt.getTimezoneOffset() * 60);			
			if (d < 60) { txt = 'less than a minute ago'; if (this.feed.strings) { txt = this.feed.strings.txt1; } } else if (d < 120) { txt = 'about a minute ago'; if (this.feed.strings) { txt = this.feed.strings.txt2; } } else if (d < (60*60)) { txt = (parseInt(d / 60)).toString()+' '; if (this.feed.strings) { txt += this.feed.strings.txt3; } else { txt += 'minutes ago'; } return txt; } else if (d < (120*60)) { txt = 'about an hour ago'; if (this.feed.strings) { txt = this.feed.strings.txt4; } } else if(d < (24*60*60)) { txt = 'about'; if (this.feed.strings) { txt = this.feed.strings.txt5_1; } txt += ' '+(parseInt(d / 3600)).toString()+' '; if (this.feed.strings) { txt += this.feed.strings.txt5_2; } else { txt += 'hours ago'; } } else if(d < (48*60*60)) { txt = '1 day ago'; if (this.feed.strings) { txt = this.feed.strings.txt6; } } else { txt = (parseInt(d / 86400)).toString()+' '; if (this.feed.strings) { txt += this.feed.strings.txt7; } else { txt += 'days ago'; } } 
			if (txt !== '') { return txt; } return date; },		
		showFeed: function (data) {	var cacheobj = this; var list = Builder.node('ul'); var li; if (this.feed.mode == 'twitter') { var html, results, limit, i = 0; data.results ? results = data.results : results = data; this.feed.limit ? limit = this.feed.limit : limit = 10; $A(results).each(function(di) { if ((di.text || di.title) && i < limit) { html = CL_formatTwitterFeed(di,cacheobj); li = Builder.node('li'); $(li).update(html); list.appendChild(li); i++; } }); } else { $A(data).each(function(d) { li = CL_formatFeed(d); list.appendChild(li); }); } $(this.feed.id).update(); $(this.feed.id).appendChild(list); if ($(this.feed.loading)) { $(this.feed.loading).hide(); } }
	});
	
	function CL_formatFeed(d) {
	
		return 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)]
				));
		
	}
	
	function CL_formatTwitterFeed(di,cacheobj) {

		var a, txt, user_id, img, img_src, br, href_name, name, href_reply, reply, href_date, date, html;

		if (di.from_user_id) { user_id = di.from_user_id; } else if (di.user && di.user.id) { user_id = di.user.id; }
		if (di.from_user) { name = di.from_user; } else if (di.user && di.user.screen_name) { name = di.user.screen_name; } else if (cacheobj.feed.keywords) { name = cacheobj.feed.keywords; } else { name = ''; }
		if (di.profile_image_url) { img_src = di.profile_image_url; } else if (di.user && di.user.profile_image_url) { img_src = di.user.profile_image_url; }
			
		href_name = cacheobj.feed.url_tp+name;						
		if (cacheobj.feed.status) { href_name += "/status/"+di.id; }
		a = '<a href="'+href_name+'"'+cacheobj.feed.target+' class="twitter_user">'+name+'</a>';						
		img = '<a href="'+href_name+'"'+cacheobj.feed.target+' class="twitter_avatar"><img src="'+img_src+'" alt="'+name+'" /></a>';						
		br = '<br />';					

		if (di.text) { txt = di.text; } else { txt = di.title; if (name !== '') { txt = txt.replace(name+': ',''); } }
		if (txt.indexOf('RT ') === 0) { txt = txt.replace('RT ','<span class="twitter_retweet">RT</span> '); }
		txt = cacheobj.filterFeed(txt);
		txt = cacheobj.formatFeed(txt);

		if (cacheobj.feed.avatar && !di.title) {
			html = img;
			if (cacheobj.feed.username) { html += a+' '; }
			html += txt;							
		} else if (cacheobj.feed.username) {
			html = a + ' '+ txt;
		} else {
			html = txt;
		}

		if (cacheobj.feed.date) { 
			if (di.created_at) { date = di.created_at; } else { date = di.date; }
			date = cacheobj.relativeTime(date);
			href_date = cacheobj.feed.url_tp+name+'/status/'+di.id;
			date = '<a href="'+href_date+'"'+cacheobj.feed.target+' class="twitter_date">'+date+'</a>';
			html += br+date; 
		}
		
		if (cacheobj.feed.reply) { 
			href_reply = cacheobj.feed.url_tr.replace(/%NAME%/gi,name);
			href_reply = href_reply.replace(/%ID%/gi,di.id);
			reply = '<a href="'+href_reply+'"'+cacheobj.feed.target+' class="twitter_reply">'+cacheobj.feed.strings.reply+'</a>';
			html += ' '+reply; 
		}	
		
		return html;
	}
	
	// JAVASCRIPT : JSONP AJAX REQUEST FOR PROTOTYPE
	// Required: Prototype.js
	Ajax.JSONRequest = Class.create(Ajax.Base, (function() { var id = 0, head = document.getElementsByTagName('head')[0]; return { 
		initialize: function($super, url, options) { $super(options); this.options.url = url; this.options.callbackParamName = this.options.callbackParamName || 'callback'; this.options.timeout = this.options.timeout || 10; this.options.invokeImmediately = (!Object.isUndefined(this.options.invokeImmediately)) ? this.options.invokeImmediately : true ; if (this.options.invokeImmediately) { this.request(); } }, 
		_cleanup: function() { if (this.timeout) { clearTimeout(this.timeout); this.timeout = null; } if (this.transport && Object.isElement(this.transport)) { this.transport.remove(); } }, 
		request: function() { var response = new Ajax.JSONResponse(this); var key = this.options.callbackParamName, name = '_prototypeJSONPCallback_' + (id++), complete = function() { if (Object.isFunction(this.options.onComplete)) { this.options.onComplete.call(this, response); } }.bind(this); this.options.parameters[key] = name; var url = this.options.url + ((this.options.url.include('?') ? '&' : '?') + Object.toQueryString(this.options.parameters)); 
		window[name] = function(json) { this._cleanup(); window[name] = undefined; if (Object.isFunction(this.options.onSuccess)) { response.status = 200; response.statusText = "OK"; response.setResponseContent(json); this.options.onSuccess.call(this, response); } complete(); }.bind(this); this.transport = new Element('script', { type: 'text/javascript', src: url }); if (Object.isFunction(this.options.onCreate)) { this.options.onCreate.call(this, response); } head.appendChild(this.transport); this.timeout = setTimeout(function() { this._cleanup(); window[name] = Prototype.emptyFunction; if (Object.isFunction(this.options.onFailure)) { response.status = 504; response.statusText = "Gateway Timeout"; this.options.onFailure.call(this, response); } complete(); }.bind(this), this.options.timeout * 1000); }, 
		toString: function() { return "[object Ajax.JSONRequest]"; } }; })());
	Ajax.JSONResponse = Class.create({ initialize: function(request) { this.request = request; }, request: undefined, status: 0, statusText: '', responseJSON: undefined, responseText: undefined, setResponseContent: function(json) { this.responseJSON = json; this.responseText = Object.toJSON(json); }, getTransport: function() { if (this.request) return this.request.transport; }, toString: function() { return "[object Ajax.JSONResponse]"; } });

	// JAVASCRIPT : TABLE STRIPING
	function CL_tableStripe() {
	
		var tables = document.getElementsByTagName("table");	
	
		for (var i=0; i!=tables.length; i++) {
			
			var table = tables[i];			
			if (!table) { return; }
			
			if (table.className.indexOf("nostyle") > -1) { return; }
			
			var pos = table.className.indexOf("noruler");			
			var tbodies = table.getElementsByTagName("tbody");
			
			for (var j = 0; j < tbodies.length; j++) {
				
				var odd = true;
				var trows = tbodies[j].getElementsByTagName("tr");
				
				for (var k = 0; k < trows.length; k++) {
					if (pos == -1) {
					
						trows[k].onmouseover = function(){
							this.addClassName('ruled'); return false;
						}
						trows[k].onmouseout = function(){
							this.removeClassName('ruled'); return false;
						}
					}
					
					if (odd) { trows[k].addClassName('odd'); }
					
					odd = !odd;
				}
			}
	
		}
	}
	document.observe('dom:loaded', CL_tableStripe);	

