// =======================================================================
// VRNeu - JavaScript Library
// =======================================================================
// Distrubute:
// Creative Commons Attribution-Noncommercial-Share Alike 3.0 License
// http://creativecommons.org/licenses/by-nc-sa/3.0/
// 
// Creator: John Martin - johnamartin.net - [JM]
// Contributors: None
// =======================================================================
// core.js
// core functions
// =======================================================================

// =======================================================================
// browser: Browser detection
// based on: http://www.quirksmode.org/js/detect.html
// =======================================================================
var browser={
	detect:function(){
		this.browser=this.searchString(this.dataBrowser)||"An unknown browser";
		this.version=this.searchVersion(navigator.userAgent)
			||this.searchVersion(navigator.appVersion)
			||"an unknown version";
		this.OS=this.searchString(this.dataOS)||"an unknown OS";
	},
	searchString:function(data){
		for (var i=0;i<data.length;i++){
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString && dataString.indexOf(data[i].subString) != -1) return data[i].identity;
			else if (dataProp) return data[i].identity;
		}
	},
	searchVersion:function(dataString){
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser:[
		{string:navigator.userAgent,subString: "OmniWeb",versionSearch: "OmniWeb/",identity: "OmniWeb"},
		{string: navigator.vendor,subString: "Apple",identity: "Safari"},
		{prop: window.opera,identity: "Opera"},
		{string: navigator.vendor,subString: "iCab",identity: "iCab"},
		{string: navigator.vendor,subString: "KDE",identity: "Konqueror"},
		{string: navigator.userAgent,subString: "Firefox",identity: "Firefox"},
		{string: navigator.vendor,subString: "Camino",identity: "Camino"},
		{string: navigator.userAgent,subString: "Netscape",identity: "Netscape"},
		{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE"},
		{string: navigator.userAgent,subString: "Gecko",identity: "Mozilla",versionSearch: "rv"},
		{string: navigator.userAgent,subString: "Mozilla",identity: "Netscape",versionSearch: "Mozilla"}
	],
	dataOS:[
		{string: navigator.platform,subString: "Win",identity: "Windows"},
		{string: navigator.platform,subString: "Mac",identity: "Mac"},
		{string: navigator.platform,subString: "Linux",identity: "Linux"}
	]
};
browser.detect();
var dom={
	ready:function(fn_function,b_return) {
		if (typeof b_return=='undefined') b_return=false;
		this.i=typeof this.i=='undefined'?0:this.i+1;
		if (typeof document!='undefined' && typeof document.getElementsByTagName!='undefined' && document.getElementsByTagName('body')[0]!=null) {
			if (b_return==true) return true;
			else fn_function();
		} else if (this.i<60) setTimeout(function(){dom.ready(fn_function)},250);
	},	
	node:function(s_mode) {
		i_parent_pos=(s_mode.indexOf(':')!==-1)?i_parent_pos=s_mode.indexOf(':'):0;
		o_parent=(i_parent_pos!==0)?document.getElementById(s_mode.substr(0,i_parent_pos)):document;
		if (i_parent_pos!==0) s_mode=s_mode.substr(i_parent_pos+1,s_mode.length);
		if (s_mode.indexOf('.')!==-1) {
			var s_tag=s_mode.substr(0,s_mode.indexOf('.')),s_class=s_mode.substr(s_mode.indexOf('.')+1,s_mode.length);
			var a_elements=(s_tag=='*'&&o_parent.all)?o_parent.all:o_parent.getElementsByTagName(s_tag);
			var a_return=new Array(),o_element;
			for (i=0;i<a_elements.length;i++) {
				if (a_elements[i].className==s_class) a_return.push(a_elements[i]);
			}
			return a_return;
		} else return document.getElementById(s_mode.substr(s_mode.indexOf('#')+1,s_mode.length));
	},
	node_add:function(o_container,s_node,a_params,s_position) {
		var o_element;
		if (typeof a_params['id']=='undefined') var s_id='tmp_'+Math.round(Math.random()*1000);
		else var s_id=a_params['id'];
		if (o_container && !document.getElementById(s_id)) {
			o_element=document.createElement(s_node);
			o_element.setAttribute('id',s_id);
			if (typeof a_params['class']!=='undefined') o_element.className=a_params['class'];
			if (s_position=='inner_start') o_container.insertBefore(o_element,o_container.firstChild);
			else if (s_position=='inner_end') o_container.appendChild(o_element);
			else if (s_position=='outer_start') o_container.parentNode.insertBefore(o_element,o_container.previousSibling);
			else if (s_position=='outer_end' && o_container.parentNode) o_container.parentNode.insertBefore(o_element,o_container.nextSibling);
			else alert("Error\nCannot create DOM node at: "+s_position);
		} else if (o_container) o_element=document.getElementById(s_id);
		return o_element;
	},
	node_delete:function(o_element) {
		if (o_element) o_element.parentNode.removeChild(o_element);
	},
	node_style:function(o_element,s_css) {
		var s_value;
		if (document.defaultView && document.defaultView.getComputedStyle) s_value=document.defaultView.getComputedStyle(o_element,"").getPropertyValue(s_css);
		else if (o_element.currentStyle) {
			s_css=s_css.replace(/\-(\w)/g,function(s_match,s_char){return s_char.toUpperCase();});
			s_value=o_element.currentStyle[s_css];
		}
		return s_value;
	},
	node_pos:function(o_element,s_mode) {
		var i_return;
		if (o_element==window) {
			if (window.innerHeight) {
				i_top=window.pageYOffset;
				i_left=window.pageXOffset;
				i_height=window.innerHeight;
				i_width=window.innerWidth;
			} else if (document.documentElement) {
				i_top=document.documentElement.scrollTop;
				i_left=document.documentElement.scrollLeft;
				i_height=document.documentElement.clientHeight;
				i_width=document.documentElement.clientWidth;
			} else if (document.body) {
				i_top=document.body.scrollTop;
				i_left=document.body.scrollLeft;
				i_height=document.body.clientHeight;
				i_width=document.body.clientWidth;
			}
			if (s_mode=="width") i_return=i_width;
			else if (s_mode=="height") i_return=i_height;
			else if (s_mode=="top") i_return=i_top;
			else i_return=i_left;
		} else if (o_element==document) {
			if (self.innerHeight) {
				i_height=self.innerHeight;
				i_width=self.innerWidth;
			} else if (document.documentElement && document.documentElement.clientHeight) {
				i_height=document.documentElement.clientHeight;
				i_width=document.documentElement.clientWidth;
			} else if (document.body) {
				i_height=document.body.clientHeight;
				i_width=document.body.clientWidth;
			}
			if (i_height<dom.node_pos(document.getElementsByTagName('body')[0],'height')) i_height=dom.node_pos(document.getElementsByTagName('body')[0],'height');
			if (i_width<dom.node_pos(document.getElementsByTagName('body')[0],'width')) i_width=dom.node_pos(document.getElementsByTagName('body')[0],'width');
			if (s_mode=="width") i_return=i_width;
			else i_return=i_height;
		} else {
			if (s_mode=="width"||s_mode=="height") {
				if (s_mode=="width") {
					i_offset=parseInt(dom.node_style(o_element,'padding-left').replace(/[^0-9]+/g,''))+
						parseInt(dom.node_style(o_element,'padding-right').replace(/[^0-9]+/g,''));
					i_return=o_element.offsetWidth-i_offset;
				} else if (s_mode=="height") {
					i_offset=parseInt(dom.node_style(o_element,'padding-top').replace(/[^0-9]+/g,''))+
						parseInt(dom.node_style(o_element,'padding-bottom').replace(/[^0-9]+/g,''));
					i_return=o_element.offsetHeight-i_offset;
				}
			} else {
				var i_value=0;
				do {
					if (s_mode=="top" && o_element.offsetTop!="NaN") i_value+=o_element.offsetTop||0;
					else if (s_mode=="left" && o_element.offsetLeft!="NaN") i_value+=o_element.offsetLeft||0;
					else if (s_mode=="right" && o_element.offsetRight!="NaN") i_value+=o_element.offsetRight||0;
					else if (s_mode=="bottom" && o_element.offsetBottom!="NaN") i_value+=o_element.offsetBottom||0;
					o_element=o_element.offsetParent;
				} while (o_element);
				i_return=i_value;
			}
		}
		return i_return;
	}
};
var anim={
	opacity:function(o_element,i_opacity) {
		if (i_opacity==100 && browser.browser=='Explorer' && browser.version>=7) {o_element.style.filter='';o_element.style.backgroundColor='#fff';}
		else {
			i_opacity=(i_opacity==100)?99.999:i_opacity;
			o_element.style.filter="alpha(opacity:"+i_opacity+")";
			o_element.style.KHTMLOpacity=i_opacity/100;
			o_element.style.MozOpacity=i_opacity/100;
			o_element.style.opacity=i_opacity/100;
		}
	},
	fade_in:function(o_element,fn_function,i_opacity) {
		if (typeof i_opacity=='undefined') i_opacity=0;
		o_element.style.display='';
		i_end=100;i_opacity+=10;
		anim.opacity(o_element,i_opacity);
		if (i_opacity<i_end) setTimeout(function(){anim.fade_in(o_element,fn_function,i_opacity);},50);
		else if (typeof fn_function!='undefined' && fn_function!=null) fn_function(o_element);
	},
	fade_out:function(o_element,fn_function,i_opacity) {
		if (typeof i_opacity=='undefined') i_opacity=100;
		i_end=0;i_opacity-=10;
		anim.opacity(o_element,i_opacity);
		if (i_opacity>i_end) setTimeout(function(){anim.fade_out(o_element,fn_function,i_opacity);},50);
		else if (typeof fn_function!='undefined' && fn_function!=null) {o_element.style.display='none';fn_function(o_element);}
		else o_element.style.display='none';
	},
	slide_down:function(o_element,fn_function,s_mode,a_data){
		if (typeof s_mode=='undefined') {
			o_element.style.display="block";o_element.style.visibility="hidden";
			o_element.style.overflow="hidden";
			var i_height=dom.node_pos(o_element,'height'),a_data=new Array();
			a_data['max']=i_height;
			a_data['increment']=i_height/15;
			a_data['height']=0;
		} else o_element.style.visibility="visible";
		if (a_data['height']+a_data['increment']<a_data['max']) {
			a_data['height']+=a_data['increment'];
			o_element.style.height=a_data['height']+"px";
			setTimeout(function(){anim.slide_down(o_element,fn_function,'anim',a_data)},50);
		} else {
			o_element.style.height=a_data['max']+'px';
			if (typeof fn_function!='undefined' && fn_function!=null) fn_function(o_element);
		}
	},
	slide_up:function(o_element,fn_function,s_mode,a_data){
		if (typeof s_mode=='undefined') {
			o_element.style.display="block";o_element.style.visibility="hidden";
			o_element.style.overflow="hidden";
			var i_height=dom.node_pos(o_element,'height'),a_data=new Array();
			a_data['max']=0;
			a_data['increment']=i_height/15;
			a_data['height']=i_height;
		} else o_element.style.visibility="visible";
		if (a_data['height']+a_data['increment']>=a_data['max']) {
			a_data['height']-=a_data['increment'];
			o_element.style.height=a_data['height']+"px";
			setTimeout(function(){anim.slide_up(o_element,fn_function,'anim',a_data)},50);
		} else {
			o_element.style.display='none';
			if (typeof fn_function!='undefined' && fn_function!=null) fn_function(o_element);
		}
	},
	explode:function(o_element,fn_function,s_mode,a_data) {
		if (typeof s_mode=='undefined') {
			var i_width=dom.node_pos(o_element,'width'),i_height=dom.node_pos(o_element,'height');
			var i_left=dom.node_pos(o_element,'left'),i_top=dom.node_pos(o_element,'top');
			var a_data=new Array();
			a_data['width']=i_width; a_data['height']=i_height;
			a_data['width_end']=i_width+40; a_data['height_end']=i_height+40;
			a_data['left']=i_left; a_data['top']=i_top;
			a_data['left_end']=i_left-20; a_data['top_end']=i_top-20;
			o_element.style.position='absolute';
			anim.explode(o_element,fn_function,1,a_data);
			anim.fade_out(o_element);
		} else {
			if (a_data['width']<a_data['width_end']) {
				a_data['width']+=4; a_data['height']+=4;
				a_data['left']-=2; a_data['top']-=2;
				o_element.style.top=(a_data['top'])+'px';
				o_element.style.left=(a_data['left'])+'px';
				o_element.style.width=a_data['width']+'px';
				o_element.style.height=a_data['height']+'px';
				o_element.style.fontSize=a_data['height']+'px';
				setTimeout(function(){anim.explode(o_element,fn_function,1,a_data)},50);
			} else if (typeof fn_function!='undefined' && fn_function!=null) fn_function(o_element);
		}
	},
	highlight:function(o_element,a_colour,fn_function,i_anim){
		if (typeof i_anim=='undefined') {
			var a_start=a_colour['start'].split(","),a_end=a_colour['end'].split(",");
			for (i=0;i<3;i++) {
				if (a_end[i]==a_start[i]) a_colour['part'+i]=0;
				else if (a_end[i]>a_start[i]) a_colour['part'+i]=Math.ceil(((a_end[i]-a_start[i])/a_end[i])*5);
				else a_colour['part'+i]=Math.ceil(((a_end[i]-a_start[i])/a_start[i])*5);
				a_colour['colour'+i]=a_start[i];
			}
			anim.highlight(o_element,a_colour,fn_function,0);
		} else {
			o_element.style.backgroundColor="rgb("+a_colour['colour0']+","+a_colour['colour1']+","+a_colour['colour2']+")";
			for (i=0;i<3;i++) {
				a_end=a_colour['end'].split(",");
				if ((parseInt(a_colour['part'+i])>0 && a_colour['colour'+i]<a_end[i]) || (parseInt(a_colour['part'+i])<0 && a_colour['colour'+i]>a_end[i]))
					a_colour['colour'+i]=parseInt(a_colour['colour'+i])+parseInt(a_colour['part'+i]);
			}
			i_anim++;
			if (i_anim<51) setTimeout(function(){anim.highlight(o_element,a_colour,fn_function,i_anim);},50);
			else {
				a_end=a_colour['end'].split(",");
				o_element.style.backgroundColor="rgb("+a_end[0]+","+a_end[1]+","+a_end[2]+")";
				if (typeof fn_function!='undefined' && fn_function!=null) fn_function(o_element);
			}
		}
	},
	show:function(o_element){
		o_element.style.display='';
	},
	hide:function(o_element){
		o_element.style.display='none';
	},
	blankout:function(s_colour){
		o_blackout=dom.node_add(document.getElementsByTagName('body')[0],'div',{'id':'blackout'},'inner_end');
		if (typeof s_colour!=='undefined') o_blackout.style.backgroundColor=s_colour;
		//o_blackout.style.height=dom.node_pos(window,'height')+'px';
		o_blackout.style.height=dom.node_pos(document,'height')+'px';
		anim.opacity(o_blackout,85);
	}
};
var event={
	add:function(o_element,s_mode,fn_function) {
		if (!o_element.attachEvent) {
			o_element.addEventListener(s_mode,fn_function,false);
			return true;
		} else {
			o_return=o_element.attachEvent('on'+s_mode,fn_function);
			return o_return;
		}
	},
	remove:function(o_element,s_mode,fn_function) {
		if (!o_element.attachEvent) {
			o_element.removeEventListener(s_mode,fn_function,false);
			return true;
		} else {
			o_return=o_element.detachEvent('on'+s_mode,fn_function);
			return o_return;
		}
	},
	stop:function(o_event) {
		if (o_event.preventDefault) o_event.preventDefault();
		o_event.returnValue=false;
	},
	target:function(o_event) {
		var o_target=o_event['srcElement']||o_event.target;
		return o_target;
	},
	bubble:function(o_event) {
		if (!o_event) var o_event=window.event;
		o_event.cancelBubble=true;
		if (o_event.stopPropagation) o_event.stopPropagation();
	}
};
var ajax={
	request:function(s_url,s_vars,s_mode,fn_function) {
		var o_req=false;
		/*@cc_on @*/
		/*@if (@_jscript_version>=5)
		try {
			o_req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				o_req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				o_req=false;
			}
		}
		@end @*/
		if (!o_req && typeof XMLHttpRequest!='undefined') o_req=new XMLHttpRequest();
		o_req.open(s_mode,s_url+"?nocache="+ajax.hash(Math.random())+s_vars,true);
		o_req.onreadystatechange=function() {
			if (o_req.readyState==4) fn_function(o_req.responseText.replace("\n",""));
		}
		if (s_mode=="POST") {
			o_req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			o_req.setRequestHeader("Content-length",s_vars.length);
			o_req.setRequestHeader("Connection","close");
			o_req.send(s_vars);
		} else o_req.send(null);
	},
	hash:function(s_string) {
		
		return s_string;
	},
	throbber:function(b_mode) {
		if (dom.ready(null,true)==true) {
			if (b_mode==true) {
				o_element=dom.node_add(document.getElementsByTagName('body')[0],'div',{'id':'vrneu_throbber'},'inner_end');
				o_element.innerHTML='<img src="/_lib/throbber.gif">';
			} else dom.node_delete(document.getElementById('vrneu_throbber'));
		}
	}
};
