﻿/* Toogle text
---------------------------------------------------------------- */

var field = function(){
	return {
		focus:function(f,txt){
			if(f.value==txt){
				f.value='';
			}else{
				f.select();
			}
		},
		blur:function(f,txt){
			if(f.value==''){
				f.value=txt;
			}
		}
	};
}();


/* Link function
---------------------------------------------------------------- */

var url = function(){
	return {
		href:function(url,blank){
			if(blank){
				window.open(url);
			}else{
				location.href = url;
			}
		}
	};
}();

var set = function(){
	return {
		newsletter:function(f){
			var obj = document.getElementById('subscribe');
			obj.value = f;
		}
	};
}();


/* Ajax <|> ajax.load('http://','ajaxdiv',true,['customfunction()','anotherfunction()']);
---------------------------------------------------------------- */

var ajax = function(){
	return {
		nocache:function(){
			var minutes = 1000 * 60;
			var hours = minutes * 60;
			var days = hours * 24;
			var years = days * 365;
			var d = new Date();
			var t = d.getTime();
			return t;
		},
		load:function(url,container,load,functions){
			var obj = document.getElementById(container);
			var loader = '<div class="loading"></div>';
			if(obj && url){
				if(load){obj.innerHTML = loader;}
				ajax.page(url,container,functions);
			}
		},
		page:function(url,container,functions){
			var _url,_no;
			if(url.indexOf("?")==-1){_no = '?~' + ajax.nocache();}else{_no = '&~' + ajax.nocache();}
			_url = url + _no;
			
			if(window.XMLHttpRequest){
				page_request = new XMLHttpRequest();
			}else if(window.ActiveXObject){
				try{
					page_request = new ActiveXObject('Msxml2.XMLHTTP');
				}catch(e){
					try{
						page_request = new ActiveXObject('Microsoft.XMLHTTP');
					}catch(e){}
				}
			}else{
				return false;
			}

			page_request.onreadystatechange = function(){
				ajax.parse(page_request,container,functions);
			}
			page_request.open('GET', _url, true);
			page_request.send(null);
		},
		parse:function(page_request,container,functions){
			if(page_request.readyState==4 && (page_request.status==200 || window.location.href.indexOf('http')==-1)){
				document.getElementById(container).innerHTML = page_request.responseText;
				ajax.completed(functions);
			}
		},
		completed:function(functions){
			// After complete functions here..
			if(functions){
				for(var i=0;i<functions.length;i++){
					try{eval(functions[i]);}catch(e){alert(e.description);}
				}
			}
		}
	};
}();


/* Dark layer
---------------------------------------------------------------- */

var darken = function(){
	return {
		create:function(){
			var dark = document.getElementById('layerdark');
			if(!dark){
				var dot = document.createElement("div");
				dot.id = 'layerdark';
				dot.style.opacity = 0.5;
				dot.style.filter = 'alpha(opacity=50)';
				dot.style.width = '100%';
				dot.style.height = '100%';
				dot.style.position = 'absolute';
				dot.style.zIndex = '1000';
				dot.style.backgroundColor = '#000000';
				dot.style.left = '0px';
				dot.style.top = '0px';
				dot.onclick = function(){darken.kill();}
				document.body.appendChild(dot);
			}
		},
		show:function(){
			darken.create();
			var dark = document.getElementById('layerdark');
			var height = document.documentElement.scrollHeight;
			if(dark){
				dark.style.height = height + 'px';
				dark.style.display = 'block';
			}
		},
		fixed:function(){
			var dark = document.getElementById('layerdark');
			var height = document.documentElement.scrollHeight;
			if(dark){
				dark.style.height = height + 'px';
			}
		},
		kill:function(){
			var dark = document.getElementById('layerdark');
			if(dark){dark.style.display = 'none';}
		}
	};
}();


/* Validation
---------------------------------------------------------------- */

var validate = function(){
	var execute = true;
	return {
		err:function(step,f){
			var obj = document.getElementById(f);
			if(obj){
				if(step=='1'){obj.style.backgroundColor = '#ffabab';setTimeout("validate.err('2','"+f+"');", 50);}
				if(step=='2'){obj.style.backgroundColor = '#ffc5c5';setTimeout("validate.err('3','"+f+"');", 50);}
				if(step=='3'){obj.style.backgroundColor = '#ffdfdf';setTimeout("validate.err('4','"+f+"');", 50);}
				if(step=='4'){obj.style.backgroundColor = '#fef1f1';setTimeout("validate.err('5','"+f+"');", 50);}
				if(step=='5'){obj.style.backgroundColor = '#fff';}
			}
		},
		email:function(str){
			var filter = /^[^\s@]+@[^\s@]+\.[a-z]{2,6}$/i;
			if(filter.test(str)){return true;}else{return false;}
		},
		empty:function(str){
			var filter = /^\s+$/;
			var expression;
			if(filter.test(str) || str==''){expression = false;}else{expression = true;}
			return expression;
		},
		onlynumbers:function(evt){
			var charCode = (evt.which) ? evt.which : event.keyCode;
			if(charCode > 31 && (charCode < 48 || charCode > 57)){return false;}else{return true;}
		},
		search:function(f){
			execute = true;
			if(!validate.empty(f.q.value)){
				execute = false;
			}
			return execute;
		},
		newsletter:function(f){
			execute = true;
			if(!validate.email(f.email.value)){
				execute = false;
				validate.err('1',f.email.id);
			}
			if(!validate.empty(f.name.value)){
				execute = false;
				validate.err('1',f.name.id);
			}
			return execute;
		}
	};
}();


/* Cookies
---------------------------------------------------------------- */

var cookies = function(){
	return {
		create:function(name,value,days){
			if(days){
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		},
		read:function(name){
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i<ca.length;i++){
				var c=ca[i];
				while(c.charAt(0)==' ') c = c.substring(1,c.length);
				if(c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		},
		erase:function(name){
			cookies.create(name,"",-1);
		}
	};
}();


/* Format
---------------------------------------------------------------- */

var format = function(){
	return {
		pretty:function(f){
			// Format: YYYY/MM/DDTHH:MM:SS+ZZ:ZZ
			if(f){
				var _from = new Date();
				var _to = new Date(f);
				if(!isNaN(_to)){
					var _diff = ((_from.getTime()-_to.getTime())/1000);
					var _day_diff = Math.floor(_diff/(60*60*24));
					var _stamp;
					
					// Extract names of month, month + day + time
					var _arr_month = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
					var _arr_day = new Array("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat");
					var _name_month = _arr_month[_to.getMonth()];
					var _name_day = _arr_day[_to.getDay()];
					var _hrs_day = _to.getDate();
					var _hrs_mins = _to.getHours() + ':' + _to.getMinutes();
					
					// Cases
					if(_diff<60){
						_stamp = Math.floor(_diff) + ' seconds ago';
					}else if(_diff<120){
						_stamp = '1 minute ago';
					}else if(_diff<3600){
						_stamp = Math.floor(_diff / 60) + ' minutes ago';
					}else if(_diff<7200){
						_stamp = '1 hour ago';
					}else if(_diff<86400){
						_stamp = Math.floor(_diff / 3600) + ' hours ago';
					}else if(_day_diff==1){
						_stamp = 'Yesterday' + ' at ' + _hrs_mins;
					}else if(_day_diff<5){
						_stamp = _name_day + ' at ' + _hrs_mins;
					}else{
						_stamp = _hrs_day + ' ' + _name_month + ' at ' + _hrs_mins;
					}
				}else{
					_stamp = '';
				}
				return _stamp;
			}
		}
	};
}();


/* Toolbar
---------------------------------------------------------------- */

var toolbar = function(){
	var elmX, elmY, timer;
	return {
		show:function(f,options){
			var pos = library.elementposition(f);
			var _pos = pos.split('/');
			elmX = parseInt(_pos[0]);
			elmY = parseInt(_pos[1]);
			clearTimeout(timer);
			var obj = document.getElementById('toolbar');
			var dyn = document.getElementById('toolbar_dynamic');
			var opts = options, html = '';
			var arr = opts.split(',');
			for(var i=0;i<arr.length;i++){
				html+='<li><a href="#" title=""><span class="'+arr[i]+'" onclick="toolbar.click(this);">&nbsp;</span></a></li>';
			}
			dyn.innerHTML = html;
			obj.style.left = (elmX + 25) + 'px';
			obj.style.top = (elmY + 25) + 'px';
			obj.style.display = 'block';
		},
		delay:function(){
			timer = setTimeout("toolbar.hide();", 3000);
		},
		hide:function(){
			var obj = document.getElementById('toolbar');
			obj.style.display = 'none';
		},
		over:function(){
			var obj = document.getElementById('toolbar');
			obj.className = 'toolbar';
			clearTimeout(timer);
		},
		out:function(){
			var obj = document.getElementById('toolbar');
			obj.className = 'toolbar fxalpha';
			toolbar.delay();
		},
		click:function(f){
			var cls = f.className;
			if(cls){
				switch(cls){
					case 'icon1':
						alert('A function with ICON1 has been click, specify what to do in the script..');
						break;
					case 'icon2':
						alert('A function with ICON2 has been click, specify what to do in the script..');
						break;
					case 'icon3':
						alert('A function with ICON3 has been click, specify what to do in the script..');
						break;
					case 'icon4':
						alert('A function with ICON4 has been click, specify what to do in the script..');
						break;
					case 'icon5':
						alert('A function with ICON5 has been click, specify what to do in the script..');
						break;
					case 'icon6':
						alert('A function with ICON6 has been click, specify what to do in the script..');
						break;
					case 'icon7':
						alert('A function with ICON7 has been click, specify what to do in the script..');
						break;
					case 'icon8':
						alert('A function with ICON8 has been click, specify what to do in the script..');
						break;
					case 'icon9':
						alert('A function with ICON9 has been click, specify what to do in the script..');
						break;
				}
			}
		}
	};
}();


/* Highlight
---------------------------------------------------------------- */

var highlight = function(){
	var elmX, elmY, timer;
	return {
		over:function(f){
			var elmW = f.offsetWidth;
			var elmH = f.offsetHeight;
			var pos = library.elementposition(f);
			var _pos = pos.split('/');
			elmX = _pos[0];
			elmY = _pos[1];
			highlight.build(elmW,elmH);
			clearTimeout(timer);
			toolbar.show(f,'icon1,icon2,icon3,icon4');
		},
		out:function(f){
			highlight.hide();
			toolbar.delay();
		},
		hide:function(){
			var adv = document.getElementById('highlight-adv');
			if(adv){adv.style.display = 'none';}
		},
		build:function(w,h){
			var adv = document.getElementById('highlight-adv');
			if(adv){
				adv.style.display = 'block';
				adv.style.left = elmX + 'px';
				adv.style.top = elmY + 'px';
				adv.style.width = w + 'px';
				adv.style.height = h + 'px';
			}
		}
	};
}();


/* Menus
---------------------------------------------------------------- */

var menu = function(){
	return {
		right:function(event){
			var showcontext;
			showcontext = library.whichelement(event);
			if(showcontext){
				var mouseX, mouseY, winX, winY, menuW, menuH, contX, contY;
				var elm = document.getElementById('contextmenu');
				if(elm){
					var mouse = library.cursorposition(event);
					var _mouse = mouse.split('/');
					mouseX = parseInt(_mouse[0]);
					mouseY = parseInt(_mouse[1]);
					var win = library.documentdimensions();
					var _win = win.split('/');
					winX = _win[0];
					winY = _win[1];
					// Position contextmenu
					menuW = parseInt(elm.offsetWidth);
					menuH = parseInt(elm.offsetHeight);
					var _comX = menuW + mouseX;
					var _comY = menuH + mouseY;
					if(_comX>winX){contX = mouseX - menuW;}else{contX = mouseX;}
					if(_comY>winY){contY = mouseY - menuH;}else{contY = mouseY;}
					elm.style.left = contX + 'px';
					elm.style.top = contY + 'px';
					elm.style.visibility = 'visible';
					return false;
				}
			}
		},
		rightclear:function(e){
			var elm = document.getElementById('contextmenu');
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
			if(elm){elm.style.visibility = 'hidden';}
		}
	};
}();

/* Drop down */
var dropdown = function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n;this.h=[];this.c=[]}
	dd.prototype.init=function(p,c){
		a=c;var w=document.getElementById(p),s=w.getElementsByTagName('ul'),l=s.length,i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode;this.h[i]=h;this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x],h=this.h[x],p=h.getElementsByTagName('a')[0];
		clearInterval(c.t);c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block';c.style.height='';c.mh=c.offsetHeight;c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z;z++;c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,'');c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter='';c.style.opacity=1;c.style.overflow='visible'}
			clearInterval(c.t);return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s),o=h/c.mh;
		c.style.opacity=o;c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();



/* Sliding divs <|> slider.toogle('divname',['customfunction()','anotherfunction()']);
---------------------------------------------------------------- */

var slider = function(){
	var sliding = false,slideAtClose=true,duration=0.6,obj,newHeight,curHeight;
	return {
		init:function(elm){
			obj = document.getElementById(elm);
			if(obj.style.display == 'none'){
				obj.style.display = 'block';
				obj.style.height = parseInt(obj.offsetHeight) + 'px';
				obj.style.display = 'none';
			}else if(obj.style.display == 'block'){
				obj.style.display = 'block';
				obj.style.height = parseInt(obj.offsetHeight) + 'px';
			}
		},
		toogle:function(elm,functions){
			obj = document.getElementById(elm);
			if(obj.style.display=='none'){
				slider.down(elm,functions);
			}else{
				slider.up(elm,functions);
			}
		},
		down:function(elm,functions){
			if(!sliding){
				slider.init(elm);
				newHeight = parseInt(obj.style.height);
				curHeight = '1';
				obj.style.height = '1px';
				obj.style.display = 'block';
				slider.slide(elm,functions);
			}
		},
		up:function(elm,functions){
			if(!sliding){
				slider.init(elm);
				curHeight = parseInt(obj.style.height);
				newHeight = '1';
				var finishTime = slider.slide(elm,functions);
				window.setTimeout("slider.hide();",finishTime);
			}
		},
		slide:function(elm,functions){
			sliding = true;
			var frames = 30 * duration;
			var tinc = (duration * 1000) / frames;
			tinc = Math.round(tinc);
			var sinc = (curHeight - newHeight) / frames;
			var frameSizes = new Array();
			for(var i=0;i<frames;i++){
				if(i < frames / 2){
					frameSizes[i] = (sinc * (i / frames)) * 4;
				} else {
					frameSizes[i] = (sinc * (1 - (i / frames))) * 4;
				}
			}
			for(var i=0;i<frames;i++){
				curHeight = curHeight - frameSizes[i];
				window.setTimeout("document.getElementById('"+elm+"').style.height = '"+Math.round(curHeight)+"px';", tinc * i);
				if((i + 1) == frames){window.setTimeout("slider.complete("+functions+");", tinc * (i + 5));}
			}
			return tinc * i;
		},
		hide:function(){
			obj.style.height = 'auto';
			obj.style.display = 'none';
		},
		complete:function(functions){
			sliding = false;
			if(functions){
				for(var i=0;i<functions.length;i++){
					try{eval(functions[i]);}catch(e){alert(e.description);}
				}
			}
		}
	};
}();


/* Move divs <|> slider.toogle('divname',['customfunction()','anotherfunction()']);
---------------------------------------------------------------- */

var mover = function(){
	var sliding = false,slideAtClose=true,duration=0.6,obj,newLeft,curLeft;
	return {
		init:function(elm,from){
			obj = document.getElementById(elm);
			if(obj){obj.style.left = from + 'px';}
		},
		custom:function(elm,functions){
			obj = document.getElementById(elm);
			var _from = 0;
			_from = parseInt(obj.style.left);
			if(isNaN(_from)){_from = 0}
			var _to = _from - 800;
			mover.init(elm,_from);
			mover.move(elm,_from,_to,functions);
		},
		move:function(elm,from,to,functions){
			if(!sliding){
				mover.init(elm,from);
				newLeft = to;
				curLeft = from;
				mover.slide(elm,functions);
			}
		},
		slide:function(elm,functions){
			sliding = true;
			var frames = 30 * duration;
			var tinc = (duration * 1000) / frames;
			tinc = Math.round(tinc);
			var sinc = (curLeft - newLeft) / frames;
			var frameSizes = new Array();
			for(var i=0;i<frames;i++){
				if(i < frames / 2){
					frameSizes[i] = (sinc * (i / frames)) * 4;
				} else {
					frameSizes[i] = (sinc * (1 - (i / frames))) * 4;
				}
			}
			for(var i=0;i<frames;i++){
				curLeft = curLeft - frameSizes[i];
				window.setTimeout("document.getElementById('"+elm+"').style.left = '"+Math.round(curLeft)+"px';", tinc * i);
				if((i + 1) == frames){window.setTimeout("mover.complete("+functions+");", tinc * (i + 5));}
			}
			//return tinc * i;
		},
		hide:function(){
			obj.style.height = 'auto';
			obj.style.display = 'none';
		},
		complete:function(functions){
			sliding = false;
			if(functions){
				for(var i=0;i<functions.length;i++){
					try{eval(functions[i]);}catch(e){alert(e.description);}
				}
			}
		}
	};
}();


/* Library
---------------------------------------------------------------- */

var library = function(){
	return {
		elementposition:function(obj){
			var curleft=curtop=0;
			if (obj.offsetParent){
				curleft=obj.offsetLeft;
				curtop=obj.offsetTop;
				while(obj=obj.offsetParent){
					curleft+=obj.offsetLeft
					curtop+=obj.offsetTop
				}
			}
			return curleft+'/'+curtop;
		},
		cursorposition:function(e){
			e = e || window.event;
		    var cursor = {x:0, y:0};
		    if(e.pageX || e.pageY){
		        cursor.x = e.pageX;
		        cursor.y = e.pageY;
		    }else{
		        var de = document.documentElement;
		        var b = document.body;
		        cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
		        cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
		    }
		    return cursor.x+'/'+cursor.y;
		},
		documentdimensions:function(){
			var myWidth = 0, myHeight = 0;
			if(typeof(window.innerWidth)=='number'){
				myWidth = window.innerWidth;
				myHeight = window.innerHeight;
			}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
				myWidth = document.body.clientWidth;
				myHeight = document.body.clientHeight;
			}
			return myWidth+'/'+myHeight;
		},
		whichelement:function(e){
			var targ,found=false;
			if(!e){var e=window.event;}
			if(e.target){targ=e.target;}else if(e.srcElement){targ=e.srcElement;}
			if(targ.nodeType==3){targ=targ.parentNode;}
			if(targ.className=='contextmenu'){found=true;}else{found=false;}
			return found;
		},
		keycode:function(evt){
			evt = (evt) ? evt : ((event) ? event : null);
			var evver = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null );
			var keynumber = evt.keyCode;
			if(keynumber){return keynumber;}
		}
	};
}();


/* Auto Suggest
---------------------------------------------------------------- */

var suggest = function(){
	var base, original;
	var collection = new Array(); 
	var tempcollection = new Array(); 
	collection = ['Ramona','Shannon','Jim','Fred','Bob','David','Carrie','Kara','Lobna','James','Jerry','Michael','John','Fola','Lawrence','Marta','Caleb','Nely','Lorenzo','Bobby','Peg','Paul','Edmond','Ronald','Francisco','Philip','Leslie','Nancy','John','Leslie','Maria','Sam','Nikola','Daphne','Timothy','Robert','Sarah','Tish','Patrick','Susan','Clem','Atsumi','April','William','Moses','Arthur','Peter','Jesse','Sally','Mary','Kerry','Tammy','Jason','Hitomi','Dana','Daniel','Gerald','Joseph','Kevin'];
	return {
		initialize:function(){
			var _pops = '', stamp;
			var items = document.getElementsByTagName('input');
			var count = items.length;
			for(var t=0;t<count;t+=1){
				var str = items[t].className;
				if(str.indexOf('suggest')!=-1 && items[t].type == 'text'){
					var pos = library.elementposition(items[t]);
					var _pos = pos.split('/');
					var elmX = parseInt(_pos[0]);
					var elmY = parseInt(_pos[1]);
					var elmW = items[t].offsetWidth - 2;
					var elmH = items[t].offsetHeight;
					var elmT = elmY + elmH;
					if(items[t].id==''){
						items[t].id = 'suggest'+t;
						_pops += '<div class="suggest-items" id="suggest'+t+'_items" style="width:'+elmW+'px;left:'+elmX+'px;top:'+elmT+'px;"><ul id="suggest'+t+'_collection"></ul></div>';
					}else{
						_pops += '<div class="suggest-items" id="'+items[t].id+'_items" style="width:'+elmW+'px;left:'+elmX+'px;top:'+elmT+'px;"><ul id="'+items[t].id+'_collection"></ul></div>';
					}
					items[t].setAttribute('autocomplete', 'off');
					items[t].onfocus = function(){suggest.focus(this,this.title);}
					items[t].onblur = function(){suggest.blur(this,this.title);}
					items[t].onkeyup = function(){suggest.up(this);}
					stamp = t;
				}
				if(str.indexOf('suggest')!=-1 && items[t].type == 'submit'){
					if(items[t].id==''){
						items[t].id = 'suggest'+stamp+'_submit';
					}else{
						items[t].id = items[t].id+'_submit';
					}
				}
			}
			if(_pops){
				var pops = document.createElement("div");
				pops.innerHTML = _pops;
				document.body.appendChild(pops);
			}
		},
		focus:function(f,txt){
			if(f.value==txt){f.value='';}else{f.select();}
			base = f.id;
		},
		blur:function(f,txt){
			if(f.value==''){f.value=txt;}
			setTimeout("suggest.hide('"+f.id+"');", 200);
		},
		up:function(f){
			var key = _keycode;
			if(key!=38 && key!=104 && key!=40 && key!=98){
				suggest.compare(f);
				original = f.value;
			}
		},
		show:function(f){
			var obj = document.getElementById(f.id+'_items');
			if(obj){obj.style.display = 'block';}
		},
		hide:function(f){
			var elm = base;
			var obj = document.getElementById(elm+'_items');
			if(obj){obj.style.display = 'none';}
		},
		compare:function(f){
			var obj = document.getElementById(f.id+'_collection');
			var items = '', count = 0, limit = 10;
			tempcollection = [];
			if(f.value){
				var str = f.value.toLowerCase();
				var len = collection.length;
				for(var i=0;i<len;i++){
					var col = collection[i].toLowerCase();
					if(col.indexOf(str)!=-1){
						if(count<limit){
							items += '<li><a href="#" title="'+collection[i]+'" onmouseover="suggest.over(this,\''+f.id+'\');" onclick="suggest.click(this,\''+f.id+'\');return false;">'+collection[i]+'</a></li>';
							tempcollection.push(collection[i]);
						}
						count++;
					}
				}
			}
			if(items){
				suggest.show(f);
				obj.innerHTML = items;
			}else{
				obj.innerHTML = '';
				suggest.hide(f);
			}
		},
		current:function(moth){
			var elm = document.getElementById(moth+'_collection');
			var items = elm.getElementsByTagName('a');
			var count = items.length;
			var cur = -1;
			for(var t=0;t<count;t+=1){
				if(items[t].className == 'selected'){
					cur = t;
				}
			}
			return cur + '/' + (count - 1);
		},
		over:function(f,moth){
			var elm = document.getElementById(moth+'_collection');
			var items = elm.getElementsByTagName('a');
			var count = items.length;
			for(var t=0;t<count;t+=1){
				items[t].className = '';
			}
			f.className = 'selected';
		},
		click:function(f,moth){
			var obj = document.getElementById(moth);
			var sbt = document.getElementById(moth+'_submit');
			if(obj){obj.value = f.innerHTML;}
			suggest.hide(moth);
			if(sbt){sbt.click();}
		},
		move:function(f){
			var obj = document.getElementById(base);
			var cur = suggest.current(base);
			var _cur = cur.split('/');
			var all, mark, found = 0;
			cur = parseInt(_cur[0]);
			all = parseInt(_cur[1]);
			if(f=='down'){
				mark = cur + 1;
			}else if(f=='up'){
				if(cur==-1){mark = all;}else{mark = cur - 1;}
			}
			var elm = document.getElementById(base+'_collection');
			var items = elm.getElementsByTagName('a');
			var count = items.length;
			for(var t=0;t<count;t+=1){
				if(t==mark){
					items[t].className = 'selected';
					obj.value = items[t].innerHTML;
					found++;
				}else{
					items[t].className = '';
				}
			}
			if(found==0){obj.value = original;}
		},
		tricker:function(f){
			suggest.hide();
		}
	};
}();


/* Key code navigation
---------------------------------------------------------------- */

// Global keycode
var _keycode;

// Get document events
document.onkeyup = function(event){

	// Get character code
	var charCode = library.keycode(event);
	_keycode = charCode;
	switch(charCode){
		
		// Forward arrows
		case 38: // Arrow -> Up
			suggest.move('up');
			break;
		case 104: // Numpad -> Up
			suggest.move('up');
			break;
		case 40: // Arrow -> Down
			suggest.move('down');
			break;
		case 98: // Numpad -> Down
			suggest.move('down');
			break;
		case 9: // Tab
			suggest.tricker('tab');
			break;
		case 13: // Enter
			suggest.tricker('tab');
			break;
		
	}
	
}


/* Window events
---------------------------------------------------------------- */

window.onresize = function(){
	darken.fixed();
}

/* Event: Contextmenu
---------------------------------------------------------------- */

document.oncontextmenu = menu.right;
document.onclick = menu.rightclear;


/* Window handler // eg. windows.load or myfunction (windows.load(alert('test')))
---------------------------------------------------------------- */

var windows = function(){
	return {
		load:function(functions){
			if(functions){
				if(window.addEventListener){
					window.addEventListener("load", functions, false);
				}else if(window.attachEvent){
					window.attachEvent("onload", functions);
				}else if(document.getElementById){
					window.onload = functions;
				}
			}
		}
	};
}();
