
/**
*	File:		im_gui.js
*	Description:	d
*	Written By:	RonB, inManage
*	Date:		November 21, 2007
*	Variables:
*		[private]_dlgWin	-	inner window object
*				-	
*	Methods:
*		obj			-	return html object element (id[string],type[tag,name,id],parent[object])
*/

var im_gui = {
	
	// public
	// ~~~~~
	ie: /msie/i.test(navigator.userAgent),
	ff: false,
	version: '0.0.1',
	isLoaded: false,
	
	// private
	// ~~~~~
	_dlgWin: null,
	
	
	// functions
	// ~~~~~~~~
	
	init: function(){
		if (document.all){
			this.ie = true;
		}
		isLoaded = true;
	},
	
	kill: function(){
		isLoaded = false;
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	obj: function(id,type,parent){
		switch(type){
			case "tag":
				if(parent && parent.getElementsByTagName(id)){
					return parent.getElementsByTagName(id);
				}
			break;
			case "name":
				if(document.getElementsByName(id)){
					return document.getElementsByName(id)[0];
				}
			break;
			case "id":
			default:
				if(document.getElementById(id)){
					return document.getElementById(id);
				}
			break;
		}
		return (false);
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	show: function(id){
		if(id && id.style && id.style.display){
			id.style.display = 'block';
		}else{
			if(this.obj(id)){
				this.obj(id).style.display = 'block';
			}
		}
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	hide: function(id){
		if(id && id.style && id.style.display){
			id.style.display = 'none';
		}else{
			if(this.obj(id)){
				this.obj(id).style.display = 'none';
			}
		}
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	switchDisplay: function(id){
		if(id && id.style && id.style.display){
			id.style.display = (id.style.display=='' ? 'none' : '');
		}else{
			this.obj(id).style.display = (this.obj(id).style.display == '' ? 'none' : '');
		}
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	innerData: function(id, data, addingPos){
		if(typeof(id)=='object'){
			if(typeof(addingPos)=='undefined' || !addingPos){
				id.innerHTML = data;
			}else if(addingPos=='begin' || addingPos=='before'){
				id.innerHTML = data + id.innerHTML;
			}else if(addingPos=='end' || addingPos=='after'){
				id.innerHTML += data;
			}
		}else{
			if(typeof(addingPos)=='undefined' || !addingPos){
				this.obj(id).innerHTML = data;
			}else if(addingPos=='begin' || addingPos=='before'){
				this.obj(id).innerHTML = data + this.obj(id).innerHTML;
			}else if(addingPos=='end' || addingPos=='after'){
				this.obj(id).innerHTML += data;
			}
		}
		resizeDivs();
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	gotoURL: function(url){
		document.location.href = url;
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	newWindow: function(mypage,myname,w,h,scroll,pos){
		if (typeof pos == 'undefined') pos='center';
		if (typeof scroll == 'undefined') scroll='auto';
		if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
		if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
		else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
		settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
		if (this._dlgWin){ this._dlgWin.close(); }
		
		this._dlgWin = window.open(mypage,myname,settings);
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	invertValue: function(objInput, evntRaised, strTxt){
		switch(evntRaised){
			case "focus":
				if(objInput.value==strTxt){
					objInput.value='';
				}
			break;
			case "blur":
				if(objInput.value==''){
					objInput.value=strTxt;
				}
			break;
		}
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	initTxtInputs: function(){
		var inps = this.obj("input","tag",document);
		for(i=0;i<inps.length;i++){
			if(inps[i].getAttribute("type")=='text'){
				if(inps[i].getAttribute("invertValue")=='yes'){
					inps[i].onfocus = function (){
						im_gui.invertValue(this,"focus",this.defaultValue);
					}
					inps[i].onblur = function (){
						im_gui.invertValue(this,"blur",this.defaultValue);
					}
				}
			}
		}
		var inps = obj("textarea","tag",document);
		for(i=0;i<inps.length;i++){
			if(inps[i].getAttribute("invertValue")=='yes'){
				inps[i].onfocus = function (){
					invertValue(this,"focus",this.defaultValue);
				}
				inps[i].onblur = function (){
					invertValue(this,"blur",this.defaultValue);
				}
			}
		}
	},
	
	/**
		type:		public event
		name:		raiseEvent_Change
		params:	strNewUrl
		return:		-
		desc:		this event is raised from the iframe. when the iframe changes its source, 
				the event is called and the strNewUrl is passed. the CallBack function is being called.
	*/
	setOpacity: function(objElement,intOpacity){
		if (this.ie){
			if(typeof(objElement)=='object'){
				objElement.style.filter = "alpha(opacity="+intOpacity+")";
			}else{
				this.obj(objElement).style.filter = "alpha(opacity="+intOpacity+")";
			}
		}else{
			if(typeof(objElement)=='object'){
				objElement.style.opacity = intOpacity/100;
			}else{
				this.obj(objElement).style.opacity = intOpacity/100;
			}
		}
	},
	
	getObjOpacity: function(obj){
		var opc;
		if(ie){
			if(typeof obj=='object'){
				if (obj && obj.filters && obj.filters.alpha && obj.filters.alpha.opacity){
					opc = obj.filters.alpha.opacity;
				}else{
					setObjOpacity(obj,100);
					opc = 100;
				}
			}else{
				if (d(obj) && d(obj).filters && d(obj).filters.alpha && d(obj).filters.alpha.opacity){
					opc = d(obj).filters.alpha.opacity;
				}else{
					setObjOpacity(d(obj),100);
					opc = 100;
				}
			}
		}else{
			if(typeof obj=='object'){
				opc = obj.style.opacity*100;
			}else{
				opc = d(obj).style.opacity*100;
			}
		}
		// if opacity not found, it means it was not set by code, so do so by code
		if (opc==''){
			if(typeof obj=='object'){
				setObjOpacity(obj,100);
			}else{
				setObjOpacity(d(obj),100);
			}
			opc = 100;
		}
		return (opc);
	},
	
	winInnerWidth: 0,
	winInnerHeight: 0,
	getWindowInnerDim: function(){
		if (self.innerHeight) // all except Explorer
		{
			this.winInnerWidth = self.innerWidth;
			this.winInnerHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
		{
			this.winInnerWidth = document.documentElement.clientWidth;
			this.winInnerHeight = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			this.winInnerWidth = document.body.clientWidth;
			this.winInnerHeight = document.body.clientHeight;
		}
	},
	
	strLimit: function(str,maxLen){
		if(str.length<=maxLen) return str;
		return (str.substr(0,maxLen));
	},
	
	sectotime: function(secs){
		var h,m;
		h = Math.floor(secs / 3600);
		secs -= h*3600;
		m = Math.floor(secs / 60);
		secs -= m*60;
		secs = Math.round(secs);
		if (secs==60) secs = 0;
		return ((h>0?(h<10?'0'+h:h)+':':'')+(m<10?'0'+m:m)+':'+(secs<10?'0'+secs:secs));
	},
	
	findPosX: function(obj){
		var curleft = 0;
		if (obj.offsetParent){
			while (obj.offsetParent){
				curleft += obj.offsetLeft;
				//curtop += obj.currentStyle.borderWidth;
				obj = obj.offsetParent;
			}
		}else{
			if (obj.x) curleft += obj.x;
		}
		return curleft;
	},
	
	findPosX: function(obj){
		var curtop = 0;
		if (obj.offsetParent){
			while (obj.offsetParent){
				curtop += obj.offsetTop;
				//curtop += obj.currentStyle.borderHeight;
				obj = obj.offsetParent;
			}
		}else{
			if (obj.y) curtop += obj.y;
		}
		return curtop;
	},
	
	toInt: function(val){
		if (val=='' || val==null || val==NaN) val=0;
		return (parseInt(val));
	},
	
	setSize: function(base,inc){
		return ((this.toInt(base)+inc)+'px');
	},
	
	getTagContent: function(str,tag){
		return ( str.substring( ( str.indexOf( '<'+tag+'>' ) + tag.length ) , str.indexOf( '</'+tag+'>' ) ) );
	}
	
}

