//========================================================================
/**
 *Add ability to retrieve elemets using a an attribtue and value
 *ie.  getElementsByAttribute( 'alt', 'Test' );
 *-> returns all elements in the document that have an 'alt' Attrubute set to 'Test'.
 *@author Brian Kulyk w/ help from text book
**/
function getElementsByAttribute( attribute, attributeValue ){
	var Es = new Array();
	var Matches = new Array();
	if( document.all ){
		Es = document.all;
	}else
		Es = document.getElementsByTagname( '*' );
	for( var i=0; i<Es.length; i++ ){
		if( attribute == 'class' ){
			var pattern = new RegExp( "(^| )" + attributeValue + "( |$)");
			if( pattern.test( Es[i].className ) )
				Matches[ Matches.length ] = Es[i];
		}else if( attribute == 'for' ){
			if( Es[i].getAttribute( 'htmlFor' ) || Es[i].getAttribute( 'for' ) )
				if( Es[i].htmlFor == attributeValue )
					Matches[ Matches.length ] = Es[i];
		}else if( Es[i].getAttribute( attribute ) == attributeValue )
			Matches[ Matches.length ] = Es[i];
	}
	return Matches;
}
//========================================================================
/**
 * Adds function to execute to the onload event of the document/window.
 * @author Brian Kulyk w/ help from text book.
**/
function addLoadListener( function_name ){
	//check for browser type and attach function.
	if( typeof window.addEventListener != 'undefined' )	 //windows
		window.addEventListener( 'load', function_name, false );
	else if( typeof document.addEventListener != 'undefined' )  //fire fox
		document.addEventListener( 'load', function_name, false );
	else if( typeof window.attachEvent != 'undefined' ) //dom0 browsers
		window.attachEvent( 'onload', function_name );
	else{ //outdated browsers
		var old_function = window.onload;
		if( typeof window.onload != 'function' )
			window.onload = function_name;
		else{
			window.onload = function(){
				old_function();
				function_name();
			};
		}
	}
}
//========================================================================
/**
 *Removes events from an element.
 *-Remember to use event types like mouseover not onmouseover
 *@author Brian Kulyk w/ help from text book.
**/
function detachEventListener( target, event_type, function_name, capture ){
	//this stuff was remove because firefox wasn't obaying the commands.  So it's removed and fire fox is working.
	/*if( typeof target.removeEventListener != 'undefined' ){ // is for Fire Fox.  Dosn't seem to work.
		target.removeEventListener( event_type, function_name, capture );
	}else */
	if( typeof target.detachEvent != 'undefined' ){ //works for ie6.
		target.detachEvent( 'on'+event_type, function_name );
	}else // works for firefox.
		target[ 'on'+ event_type ] = null;
	return true;
}
//========================================================================
/**
 *Should prevent default actions of an element, if possible.
 *@author Brian Kulyk w/ help from text book.
**/
function stopDefaultAction( e ){
	e.returnValue = false;
	if( typeof e.preventDefault != 'undefined' )
		e.preventDefault();
	return true;
}
//========================================================================
/**
 *Will return the size of a given element in an array [width, height].
 *@author Brian Kulyk w/ help from text book.
**/
function initSizeElement( element_id )
{
  var starShip = document.getElementById( element_id );
  var pixelWidth = starShip.offsetWidth;
  var pixelHeight = starShip.offsetHeight;

  return [ pixelWidth, pixelHeight ];
}
//========================================================================
function getPosition( target ){
	var my_x = 0;
	var my_y = 0;
	while( target != null ){
		my_x += target.offsetLeft;
		my_y += target.offsetTop;
		target = target.offsetParent;
	}
	return [ my_x, my_y ];
}
//========================================================================
/**
 *Returns element using specified event. ( Very Usefull )
 *@usage attachEventListener( target, 'onmouseover', test_function, capture ) function test_function( event ){ var element = getEventTarget( event);  }
 *@author Brian Kulyk w/ help from text book.
**/
function attachEventListener( target, event_type, function_name, capture ){
	if( typeof target.addEventListenter != 'undefined')
		target.addEventListener( event_type, function_name, capture );
	else if( typeof target.attachEvent != 'undefined' )
		target.attachEvent( "on"+event_type, function_name );
	else{
		event_type = "on"+event_type;
		if( typeof target[ event_type ] == 'function' ){
			var old_listener = target[ event_type ];
			target[ event_type ] = function(){
				old_listener();
				return function_name();
			}
		}else
			target[ event_type ] = function_name;
	}
}
function getEventTarget( event ){
	var target = null;
	if( typeof event.target != 'undefined' )
		target = event.target;
	else
		target = event.srcElement;
	while( target.nodeType == 3 && target.parentNode != null )
		target = target.parentNode;
	return target;
}
//========================================================================
function GetCrazyEventTarget( e ){
	if( typeof e == 'undefined' )
		e = window.event;
	var theelement = getEventTarget( e );
	theelement = $( theelement );
	return theelement;
}
//========================================================================
function is_array( obj ) {
   if( obj.constructor.toString().indexOf("Array") == -1 )
      return false;
   else
      return true;
}
//========================================================================
String.extend( {
	alert: function() {
		alert( this );
		return this;
	}
} );
Array.extend( {
	alert: function() {
		alert( "Length:\t" + this.length + "\nElements:\n\t[" + this.join( "]\n\t[" ) + "]" );
		return this;
	}
} );
Element.extend( { 
	alert: function() {
		alert( this.innerHTML );
	}
} );

Array.extend( { 
	in_array: function( needle ) {
		for( var i=0; i<this.length; i++ ){
			if( this[i] === needle )
				return true;
		}
		return false;
	}
} );
Element.extend( { 
	getPosition: function(){
		return getPosition( this );
	}
} );
