//------------------------------------------------------------------------------
// 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 );
	};
	
	/**
	* 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;
	};
	
	//=============================================================================
	// MISC FUNCTIONS
	//=============================================================================
	
	/**
	* Include a script...
	*/
	this.include_javascript = function( _file, div_id )
	{
		/* INIT */
	    var _doc = ( div_id ) ? document.getElementById( div_id ) : document.getElementsByTagName( 'head' ).item(0);
	    var _js  = document.createElement('script' );
		
		/* Create attribute */
	    _js.setAttribute( 'language', 'javascript' );
	    _js.setAttribute( 'type', 'text/javascript' );
	    _js.setAttribute( 'src', _file );
	    _doc.appendChild( _js );

	    return false;
	};
	
	/**
	* 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);
	};
	
	/**
	* 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( /.*(\-|_)(\d+)/, "$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( /(.*)(\-|_)(\d+)/, "$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)
	{
		if ( ! msg )
		{
			msg = js_lang_confirm;
		}

		if ( confirm( msg ) )
		{
			window.location.href = url;
		}
		else
		{
			alert ( js_lang_cancel );
			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;
	};
	
}