/* los.js Copyright (C) 2010 LackOfSpark.com 
   This program is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free Software
   Foundation; version 2 of the License. 

   This program is distributed in the hope that it will be useful, but WITHOUT 
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */

var LOS = (function(){
	return ({
		addEvent : (function(){
			if(window.attachEvent){
				return function(o,t,f){o['e'+t+f]=f;o[t+f]=function(){o['e'+t+f](window.event);};o.attachEvent('on'+t,o[t+f]);}
			}else{
				return function(o,t,f){o.addEventListener(t,f,false);}
			}
		})(),

		removeEvent : (function(o,t,f){
			if(window.detachEvent){
				return function(o,t,f){o.detachEvent('on'+t,o[t+f]);o[t+f]=null;}
			}else{
				return function(o,t,f){o.removeEventListener(t,f,false);}
			}
		})(),

		trim : function(s){
			return s.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
		},

		preloadImage : function(s){
			var i=new Image();
			i.src=s;
		},

		isEmail : function(e){
			var r = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; //this sucks. FIXME.
			return r.test(e);
		},

		getPosOffset : function(e, t){
			var to=0;
			while(e!=null){
				to=(t=='top')?to+e.offsetTop:to+e.offsetLeft;
				e=e.offsetParent;
			}
			return to;
		},

		getMousePos : (function(){
			if(window.pageX){
				return function(e){return{x:e.pageX,y:e.pageY}};
			}else{
				return function(e){
					return {x:e.clientX+document.body.scrollLeft-document.body.clientLeft,y:e.clientY+document.body.scrollTop-document.body.clientTop};
				};
			};
		})(),

		getMouseOffset : function(t,e){
			var m=this.getMousePos(e);
			return {x:m.x-this.getPosOffset(t,'left'),y:m.y-this.getPosOffset(t,'top')};
		},

		getParent : function(s, t){
			while(s.nodeName!=t){		
				s=s.parentNode;
				if(s==null||s=='html'){return null;}
			}
			return s;
		},

		makeQueryString : function(){
			var q='';
			for(var i=0;i<arguments.length;i+=2){
				if(arguments[i+1]!==''){
					if(i!=0){q+='&';}
					q+=arguments[i]+'='+encodeURIComponent(arguments[i+1]);
				}
			}
			return q;
		},

		getQueryStringParam : function(p){
			var b=location.search.indexOf(p)+p.length+1;
			var e=location.search.indexOf('&',b);
			if(e==-1){e=location.search.length;}
			return location.search.substring(b,e);
		},
		
		isMouseLeaveOrEnter : function(e,h){
			var r=e.relatedTarget?e.relatedTarget:e.type=='mouseout'?e.toElement:e.fromElement;
			try{
				while(r&&r!=h&&r.nodeName!='BODY'){r=r.parentNode};
			}catch(ex){}
			return r!=h;
		},
		removeClass : function(e,c){
			var r=new RegExp('(\\s|^)'+c+'(\\s|$)');
			e.className=e.className.replace(r,' ');
		},

		hasClass : function(e,c){
			return e.className.match(new RegExp('(\\s|^)'+c+'(\\s|$)'));
		},

		addClass : function(e,c) {
			if (!this.hasClass(e,c)){e.className+=" "+c};
		},

		getElementsByClass : function(s,n,t){
		        var c = new Array();
		        if(n==null){n=document;}
		        if(t==null){t='*';}
		        var e=n.getElementsByTagName(t);
		        var r=new RegExp("(^|\\s)"+s+"(\\s|$)");
		        for(var i=0;i<e.length;i++){
		                if(r.test(e[i].className)){
		                        c[c.length]=e[i];
		                }
		        }
		        return c;
		},

		minutesToTime : function(min,twentyFourHour){
			var hours=Math.floor(min/60);
			var mins=min%60;

			var amPm='';
			if(!twentyFourHour){
				if(hours>=12){
					if(hours>12){hours-=12;}
					amPm='PM';
				}else{
					amPm='AM';
				}
				if(mins<10){mins='0'+mins;}
			}
			time=hours+':'+mins+amPm;
			return time;
		},

		makeSwappableTextInputHint : function(i,r){
			i.value=r;
			this.addClass(i,'hinttext');
			var self=this;
			i.onblur=function(){
				if(i.value==''){
					i.value=r;
					self.addClass(i,'hinttext');
				}
			}
			i.onfocus=function(){
				if(i.value==r){
					i.value='';
					self.removeClass(i,'hinttext');
				}
			}
			i.restoreHintText=function(){
				self.value=r;
				self.addClass(i,'hinttext');
			}
		},

		removeChildren : function(p){
			for(var i=p.childNodes.length-1;i>=0;i--){
				p.removeChild(p.childNodes[i]);
			}
		},

		dropdownOtherHandler : function(d,o){
			var e=function(){ 
				if(d.options[d.selectedIndex].value==''){
					o.style.display='block';
				}else{
					o.style.display='none';
					o.value='';
				}
			};
			this.addEvent(d,'change',e);
		},
		
		inputMakeReadOnly : function(i){
			this.addClass(i,'readonly');
			i.readOnly=true;
		},

		getRadioButtonSelectedValue : function(f,g){
			var e=document.getElementById(f);

			for(var i=0;i<e[g].length;i++){
				if(e[g][i].checked){
					return e[g][i].value;
				}
			}
		},

		radioButtonsOnChange : function(i,r,c){
			var ec=function(e){c(e);};
			var f=document.getElementById(i);
			var b=new Array();
			for(var j in f.elements){
				if(f.elements[j].type=='radio'&&f.elements[j].name==r){
					b[b.length]=f.elements[j];
					this.addEvent(f.elements[j],'change',ec);
				}
			}
		},

		cGlobalMsgSlider : function (opt){
			this.elMsg = document.createElement('div');
			this.elMsg.id = 'globalMsgSlider';
			this.elMsg.style.display = 'none';
			document.body.appendChild(this.elMsg);
			var self = this;

			this.animate = function(opt){
				if(!opt.isLoaded){
					opt.txt = opt.txt || '';
					opt.startPos = opt.StartPos || -50;
					opt.finalPos = opt.finalPos || 0;
					opt.pauseTime = opt.pauseTime || 2000;
					opt.msDelay = opt.msDelay || 33; //1000/33 = 30fps
					opt.msRollSpeed = opt.msRollSpeed || 1000;
			
					opt.pixelJumps = Math.ceil((Math.abs(opt.finalPos - opt.startPos) / (opt.msRollSpeed / 1000)) / (1000 / opt.msDelay), 10);
			
					opt.isLoaded = true;
			
					self.elMsg.style.top = opt.startPos + 'px';
					self.elMsg.innerHTML = opt.txt;
					self.elMsg.style.display = '';
				}

				var newTop = parseInt(self.elMsg.style.top, 10);

				if(newTop == opt.finalPos && opt.pauseTime >= 0){ //pause
					opt.pauseTime -= opt.msDelay;
				}else{
					if(opt.pauseTime > 0){ //move down
						newTop += opt.pixelJumps;
						if(newTop > opt.finalPos){ newTop = opt.finalPos; }

					}else{ //move up
						newTop -= opt.pixelJumps;
						if(newTop < opt.startPos){ newTop = opt.startPos; }
					}
					self.elMsg.style.top = newTop + 'px';
				}
		
				if(opt.pauseTime > 0 || opt.startPos != newTop ){
					setTimeout( function(){ self.animate(opt); }, opt.msDelay);
				}else{
					self.elMsg.style.display = 'none';
				}
			}
	
			if(typeof opt == 'object'){
				this.animate(opt);
			}
		}

	});
})();



