/**
 * @author	 		Marc Eley
 * Date created: 	25-04-2006 21:00 
 * Date revised: 	27-09-2006 13:58
*/

/**
 * The App object 
 * @
*/
var App = {
	
	_$:function(s){ return document.getElementById(s)!=null?document.getElementById(s):false; },
	$:function (o){
		if(typeof o=="string"){
			var s=o.split("|"),a=[];
			if(s.length==1) return App._$(s[0]);
			for(var i=0; i<s.length; i++) a.push(App._$(s[i]));
			o=a;
		}
		return o; 
	},

	show:function(o){
		var o=App.$(o),d="block";
		if(!App.Browser.msie){ // normalize display value for table elements
			switch(o.nodeName){
				case "TR":
					d = "table-row";
					break;
				case "TD":
					d = "table-cell";
					break;
				case "TABLE":
					d = "table";
					break;
				case "TBODY":
					d = "table-row-group";
					break;
			}
		}
		if(o.length>1){ 
			for(var i=0; i<o.length; i++) this.setCss(o[i],{display:d}); 
			return o; 
		} else {
			this.setCss(o,{display:d}); 
		}
		return o;
	},

	hide:function(o){
		var o = App.$(o);
		if(o.length>1){
			for(var i=0; i<o.length; i++) this.setCss(o[i],{display:"none"}); 
			return o; 
		} else {
			this.setCss(o,{display:"none"});
		}
		return o;
	},

	getCss:function(o,s){
		var o=App.$(o),r,n;
		if(o.currentStyle){
			var n = s.split("-");
			for(var i=1; i<n.length; i++) n[i] = n[i].charAt(0).toUpperCase()+n[i].substr(1);
			r=o.currentStyle[n.join("")];
		} else if(document.defaultView && document.defaultView.getComputedStyle){
			r=document.defaultView.getComputedStyle(o,null).getPropertyValue(s);
		} else {
			r=o.style[s];
		}
		return r;
	},

	setCss:function(o,s){
		var o=App.$(o);
		for(var p in s) o.style[p]=s[p];
	},
	
	addClass:function(o,s){
		var o=App.$(o);
	    App.removeClass(o,s);
	    o.className=(o.className+" "+s).trim();
	    return o;
	},

	removeClass:function(o,s){
		var o=App.$(o);
		o.className = o.className.replace(s,"").trim();
	    return o;
	},
	
	classContains:function(o,s){
		var o=App.$(o),ss=s.split("|");
		for(var i=0; i<ss.length; i++) if(o.className.indexOf(ss[i])!=-1) return true;
		return false;
	},
	
	/**
	 * Works like getElementsByTagName
	 * Looks for the passed string in the nodes class="" attribute, so the function will also return the element if it has multible selectors like: class="section info"
	 * @param {Node} oParent The object in which you wish to find the elments
	 * @param {String} sClassName The name of the class attribute you wish to find
	 * @returns An array of elements
	*/
	getElementsByClassName:function(p,s){
		var a=p.getElementsByTagName("*"),r=[];
		if(a.length<=0) a=document.all;
		if(a.length>0){
			for(var i=0; i<a.length; i++){
				var sTmp = "#"+a[i].className+"#";
				var re = "[ |#]"+s+"[ |#]";
				if(sTmp.match(new RegExp(re))) r.push(a[i]);
			}
			return r;
		} else {
			return false;
		}
	},
	
	Browser:{
		safari:/webkit/.test(navigator.userAgent.toLowerCase()),
		opera:/opera/.test(navigator.userAgent.toLowerCase()),
		msie:/msie/.test(navigator.userAgent.toLowerCase()) && !/opera/.test(navigator.userAgent.toLowerCase()),
		mozilla: /mozilla/.test(navigator.userAgent.toLowerCase()) && !/(compatible|webkit)/.test(navigator.userAgent.toLowerCase())
	},
	
	Page: {
		/**
		 * onDOMContentLoaded crossbrowser, kudos jquery
		*/
		Loader:{
			readyList:[],
			add:function (func){
				App.Page.Loader.readyList.push(func);
			},
			run:function (){
				for(var i=0; i<App.Page.Loader.readyList.length; i++){
					App.Page.Loader.readyList[i].apply(document);
				}
			},
			prepare:function (){
				if (App.Browser.mozilla || App.Browser.opera){
					document.addEventListener( "DOMContentLoaded", App.Page.Loader.run, false );
				} else if(App.Browser.msie){	
					// If IE is used, use the excellent hack by Matthias Miller
					// http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
				
					// Only works if you document.write() it
					document.write("<scr" + "ipt id=__ie_init defer=true " + 
						"src=//:><\/script>");
				
					// Use the defer script hack
					var script = document.getElementById("__ie_init");
					
					// script does not exist if jQuery is loaded dynamically
					if ( script ) 
						script.onreadystatechange = function() {
							if ( this.readyState != "complete" ) return;
							this.parentNode.removeChild( this );
							App.Page.Loader.run();
						};
				
					// Clear from memory
					script = null;
				} else if (App.Browser.safari){
					// Continually check to see if the document.readyState is valid
					var safariTimer = setInterval(function(){
						// loaded and complete are both valid states
						if ( document.readyState == "loaded" || 
							document.readyState == "complete" ) {
				
							// If either one are found, remove the timer
							clearInterval(safariTimer);
							safariTimer = null;
				
							// and execute any waiting functions
							App.Page.Loader.run();
						}
					}, 10); 
				} else {
					// A fallback to window.onload, that will always work
					App.addLoadEvent(App.Page.Loader.run);
				}
			}	
		}, // @ App.Page.Loader
		
		Sifr:{
			init:function(){
				if(typeof sIFR == "function"){
					var sHex = (App.$("frontpage")?"646564":"273c63");
					sIFR.replaceElement(named({sSelector:"h1.part1",sFlashSrc:"http://www.telecomscandinavia.net/js/sifr/eurostile.swf",sColor:"#"+sHex}));
					sIFR.replaceElement(named({sSelector:"h2.part2",sFlashSrc:"http://www.telecomscandinavia.net/js/sifr/eurostile.swf",sColor:"#"+sHex}));
				};
			}
		},
		
		start:function (){

			App.Page.Sifr.init();
			
		} // @App.Page.start
	} // @App.Page
} // @App

/**
 * Initialize the document
*/
App.Page.Loader.prepare();
App.Page.Loader.add(App.Page.start);

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/,"");
}
if(typeof Array.prototype.push=="undefined"){
	Array.prototype.push=function(k){
		this[this.length]=k; return this.length;
	};
}
