//--------------------------------------------------------------------
// IPS Global JS functions
//--------------------------------------------------------------------
// Matt Mecham Woz Here
// (c) 2005 Invision Power Services, Inc.
// http://www.invisionpower.com
//--------------------------------------------------------------------


function ipsclass()
{
	//=============================================================================
	// EDITOR FUNCTIONS
	//=============================================================================
	
	/**
	* Get contents from an open editor
	*/
	this.get_editor_contents = function( editor_id, editor_array )
	{
		var aIPS_editor = editor_array ? editor_array : IPS_editor;
		
		if ( ! editor_id )
		{
			//-----------------------------------------
			// Assume we don't care, and get first box
			//-----------------------------------------
			
			for( var i in aIPS_editor )
			{
				editor_id = i;
				break;
			}
		}
		
		return aIPS_editor[ editor_id ].editor_get_contents;
	};
	
	/**
	* Add contents to an open editor
	*/
	this.add_editor_contents = function( text, editor_id, editor_array )
	{
		var aIPS_editor = editor_array ? editor_array : IPS_editor;
	
		if ( ! editor_id )
		{
			//-----------------------------------------
			// Assume we don't care, and get first box
			//-----------------------------------------
			
			for( var i in aIPS_editor )
			{
				editor_id = i;
				break;
			}
		}
		
		return aIPS_editor[ editor_id ].insert_text( text );
	};
	
	/**
	* Add contents to an open text area / input
	*/
	this.add_to_textinput =  function( text, div_id, from_popup )
	{
		try
		{
			var ta_obj    = from_popup ? opener.document.getElementById( div_id ) : document.getElementById( div_id );
			var _document = from_popup ? opener.document : document;
			
			if (typeof(ta_obj.selectionStart) != 'undefined')
			{
				var open = ta_obj.selectionStart + 0;
				var st   = ta_obj.scrollTop;
				
				ta_obj.value = ta_obj.value.substr(0, ta_obj.selectionStart) + text + ta_obj.value.substr(ta_obj.selectionEnd);

				ta_obj.selectionStart = open;
				ta_obj.selectionEnd   = open + text.length;
				ta_obj.scrollTop      = st;
			}
			else if ( ( _document.selection && _document.selection.createRange ) || this._ie_cache )
			{
				sel.text = text.replace(/\r?\n/g, '\r\n');
				sel.select();
			}
			else
			{
				ta_obj.value += text;
			}
		}
		catch(error)
		{
			//alert( error );
		}
	};
	
	/**
	* Convert saved tags to display tags
	*/
	this.convert_saved_tags_to_display_tags = function( text )
	{
		text = text.replace( /(<|&lt;|&#60;)!--/, "{" );
		text = text.replace( /--(>|&gt;|&#62;)/ , "}" );
		
		return text;
	};
	
	//=============================================================================
	// PHP EMULATION
	//=============================================================================
	
	/**
	* Emulate PHPs trim function
	*/
	this.trim = function( text )
	{
		if( typeof(text) == 'undefined' )
		{
			return '';
		}
				
		text = text.replace(/^\s+/, '');
		return text.replace(/\s+$/, '');
	};
	
	/**
	* Emulate PHPs in_array function
	*/
	this.in_array = function( needle, haystack )
	{
		if ( ! haystack.length )
		{
			return false;
		}
		
	    for ( var i = 0 ; i < haystack.length ; i++)
		{
	        if ( haystack[i] === needle )
			{
	            return true;
	        }
	    }
	
	    return false;
	};
	
	/**
	* Emulate PHPs htmlspecial chars function
	*/
	this.htmlspecialchars = function( text )
	{
		text = text.replace( /</g, '&lt;' );
		text = text.replace( />/g, '&gt;' );
		text = text.replace( /"/g, '&quot;' );
		
	    return text;
	};
	
	/**
	* Emulate PHPs html_entity_decode function
	*/
	this.html_entity_decode = function( text )
	{
		if( typeof ca == 'undefined' )
		{
			var ca = document.createElement( 'textarea' );
		}
		
		ca.innerHTML = text.replace( /</g, '&lt;' ).replace( />/g, '&gt;' );

		return ca.value;
	};
	
	/**
	* Emulate PHPs un htmlspecial chars function
	*/
	this.un_htmlspecialchars = function( text )
	{
		text = text.replace( /&lt;/g  , '<' );
		text = text.replace( /&gt;/g  , '>' );
		text = text.replace( /&quot;/g, '"' );
		
	    return text;
	};
	
	//=============================================================================
	// MISC FUNCTIONS
	//=============================================================================
	
	/**
	* Fade in colour...
	*/
	this.fade_in_element = function( div ) 
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		var hash        = '#';
		var color_items = "0123456789abcdef";
		var start_color = '#ffff66';
		var orig_color  = document.getElementById( div ).style.backgroundColor;
		var temp_end    = '#ffffff';
		var iter        = 20;
		var time        = 80;

		var rbeg = color_items.indexOf(start_color.substr(1,1))*16 + color_items.indexOf(start_color.substr(2,1));
		var gbeg = color_items.indexOf(start_color.substr(3,1))*16 + color_items.indexOf(start_color.substr(4,1));
		var bbeg = color_items.indexOf(start_color.substr(5,1))*16 + color_items.indexOf(start_color.substr(6,1));
		var rend = color_items.indexOf(temp_end.substr(1,1))*16    + color_items.indexOf(temp_end.substr(2,1));
		var gend = color_items.indexOf(temp_end.substr(3,1))*16    + color_items.indexOf(temp_end.substr(4,1));
		var bend = color_items.indexOf(temp_end.substr(5,1))*16    + color_items.indexOf(temp_end.substr(6,1));
		
		for ( i = 1, r = rbeg, g = gbeg, b = bbeg;
				i <= iter;
				r = Math.round(rbeg + i * ((rend - rbeg) / (iter-1))),
				g = Math.round(gbeg + i * ((gend - gbeg) / (iter-1))),
				b = Math.round(bbeg + i * ((bend - bbeg) / (iter-1))), i++
			)
		{
			hstr = '#' + color_items.charAt(Math.floor(r/16)) + color_items.charAt(r%16) + color_items.charAt(Math.floor(g/16)) + color_items.charAt(g%16) + color_items.charAt(Math.floor(b/16)) + color_items.charAt(b%16);
			
			setTimeout('var div = document.getElementById("' + div + '"); div.style.backgroundColor = "' + hstr + '";', i * time);
		}
		
	   	setTimeout('var div = document.getElementById("' + div + '"); div.style.backgroundColor = "' + orig_color + '";', (i+1) * time);
	};
	
	/**
	* Similar to sprintf
	*/
	this.lang_build_string = function()
	{
		if ( ! arguments.length || ! arguments )
		{
			return;
		}

		var string = arguments[0];

		for( var i = 1 ; i < arguments.length ; i++ )
		{
			var match  = new RegExp('<%' + i + '>', 'gi');
			string = string.replace( match, arguments[i] );
		}

		return string;
	};
	
	/**
	* Get element which may either be a parent or local
	*/
	this.get_element = function( id )
	{
		var element = '';
		
		try
		{
			element = document.getElementById( id );
		}
		catch( error )
		{
			element = parent.document.getElementById( id );
		}
		
		return element;
	};
	
	/**
	* Return the ID from an ID title string
	*
	* @param	string	ID title
	* @return	integar	ID
	*/
	this.get_id_from_text = function( id )
	{
		return id.replace( /.*(\-|_)(\S+)/, "$2" );
	};
	
	/**
	* Return the name from an ID string
	*
	* @param	string	ID title
	* @return	integar	ID
	*/
	this.get_name_from_text = function( id )
	{
		return id.replace( /^(.*)(\-|_)(\S+)/, "$1" );
	};
	
	/**
	* Location jump
	* @param	string	URL to redirect to
	* @param	boolean	Do not prepend normal URL
	*/
	this.location_jump = function( url, full )
	{
		url = url.replace( /&amp;/g, '&' );
		
		if ( full )
		{
			window.location.href = url;
		}
		else
		{
			window.location.href = ipb_var_base_url + url;
		}
	};
	
	/**
	* Confirm an action
	*/
	this.confirm_action = function(url, msg, iframe_id)
	{
		if ( ! msg )
		{
			msg = 'PLEASE CONFIRM:\nOK to proceed?';
		}

		if ( confirm( msg ) )
		{
			if ( ! iframe_id )
			{
				window.location.href = url;
			}
			else
			{
				window.frames[ iframe_id ].location.href = url;
			}
		}
		else
		{
			alert ( 'OK, action cancelled!' );
			return false;
		} 
	};
	
	/**
	* Create pop-up window
	*/
	this.pop_up_window = function(url, width, height, name)
	{
		if ( ! name )
		{
			var mydate = new Date();
			name = mydate.getTime();
		}
		
		var Win = window.open( url, name, 'width='+width+',height='+height + ',resizable=1,scrollbars=1,location=no,directories=no,status=no,menubar=no,toolbar=no');
		
		return false;
	};
	
	/**
	* Set stuff unselectable
	*/
	this.set_unselectable = function(obj)
	{
		if ( ! is_ie4 && typeof( obj.tagName ) != 'undefined' )
		{
			if ( obj.hasChildNodes() )
			{
				for (var i = 0; i < obj.childNodes.length; i++)
				{
					this.set_unselectable( obj.childNodes[i] );
				}
			}
		
			obj.unselectable = 'on';
		}
	};

	/**
	* GET LEFT OBJECT POSITION
	* Safari friendly version...
	*/
	this.get_obj_leftpos = function(obj)
	{
		var curleft = 0;

		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft;
				obj      = obj.offsetParent;
			}
		}
		else if (obj.x)
		{
			curleft += obj.x;
		}

		return curleft;
	};

	/**
	* GET TOP OBJECT POSITION
	* Safari friendly version...
	*/
	this.get_obj_toppos = function(obj)
	{
		var curtop = 0;

		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{ 
				curtop += obj.offsetTop;
				obj     = obj.offsetParent;
			}
		}
		else if (obj.y)
		{
			curtop += obj.y;
		}

		return curtop;
	};
	
	//=============================================================================
	// CANCEL BUBBLE FUNCTIONS
	//=============================================================================
	
	/**
	* Stop the browser bubbling events
	*/
	this.cancel_bubble = function(obj, extra)
	{
		if ( extra !== false )
		{
			extra = true;
		}

		if ( ! obj || is_ie)
		{
			if ( extra )
			{
				window.event.returnValue = false;
			}

			window.event.cancelBubble = true;

			return window.event;
		}
		else
		{
			obj.stopPropagation();

			if ( extra )
			{
				obj.preventDefault();
			}

			return obj;
		}
	};

	this.cancel_bubble_all = function( obj )
	{
		return ipsclass.cancel_bubble( obj, true );
	};

	this.cancel_bubble_low = function( obj )
	{
		return ipsclass.cancel_bubble( obj, false );
	};
	
	//=============================================================================
	// COOKIE FUNCTIONS
	//=============================================================================
	
	/**
	* Cookie: Get Cookie
	*/
	this.my_getcookie = function( name )
	{
		cname = ipb_var_cookieid + name + '=';
		cpos  = document.cookie.indexOf( cname );

		if ( cpos != -1 )
		{
			cstart = cpos + cname.length;
			cend   = document.cookie.indexOf(";", cstart);

			if (cend == -1)
			{
				cend = document.cookie.length;
			}

			return unescape( document.cookie.substring(cstart, cend) );
		}

		return null;
	};

	/**
	* Cookie: Set Cookie
	*/
	this.my_setcookie = function( name, value, sticky )
	{
		expire = "";
		domain = "";
		path   = "/";

		if ( sticky )
		{
			expire = "; expires=Wed, 1 Jan 2020 00:00:00 GMT";
		}

		if ( ipb_var_cookie_domain != "" )
		{
			domain = '; domain=' + ipb_var_cookie_domain;
		}

		if ( ipb_var_cookie_path != "" )
		{
			path = ipb_var_cookie_path;
		}

		document.cookie = ipb_var_cookieid + name + "=" + value + "; path=" + path + expire + domain + ';';
	};
	
	//=============================================================================
	// ARRAY FUNCTIONS
	//=============================================================================
	
	/**
	* Array: Get stacksize
	* @param	array	Array
	* @return	int		Array length
	*/
	this.array_stacksize = function( thearray )
	{
		for (i = 0 ; i < thearray.length; i++ )
		{
			if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') )
			{
				return i;
			}
		}

		return thearray.length;
	};

	/**
	* Array: Add element to the stack
	* @param	array	Array
	* @param	mixed	New value
	*/
	this.array_pushstack = function ( thearray, newval )
	{
		var arraysize = this.array_stacksize(thearray);
		thearray[arraysize] = newval;
	};

	/**
	* Array: Remove last array element
	* @param	array	Array
	* @return	int		Array length
	*/
	this.array_popstack = function( thearray )
	{
		var arraysize = this.array_stacksize( thearray );
		var theval    = thearray[arraysize - 1];
		
		delete thearray[arraysize - 1];
		return theval;
	};
	
}

