/* V0.05 */


/* Short-circuit para encontrar varios elementos pelo id 
 * Se for passado uma lista, uma lista é retornada
 * Se for passado o elemento e não o id, o elemento é retornado
 * changed from $() to e$() bacause of conflict with mootools
 */
function e$() 
{
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++)
    {
    	var element = arguments[i];
      	if (typeof element == 'string')
      	{
      	    if (document.getElementById) //Moderm browsers
            	element = document.getElementById(element);
	    else if (document.all) // Internet Explorer 4 ou Opera with IE user agent
    		element = document.all[element];
	    else if (document.layers) // Navigator 4
	        element = document.layers[element];
        }   	
    	if (arguments.length == 1)
      	    return element;
    	elements.push(element);
    }
    return elements;
}
 

/* Checkes how the browser is rendering the page.
 * mostly sefull for IE <= 6
 * Returns:
 * - 0 - Doesnt matter
 * - 1 - W3C Standards 
 * - 2 - Almost in standards
 * - 3 - Quirks mode
 */
function swiCheckCompatMode()
{
    var mode=document.compatMode;
    var m=0;
    if(mode)
    {
    	if(mode=='BackCompat')
      	    m=3
    	else if(mode=='CSS1Compat')
	    m=1
    	else 
	    m=2
    }
    return m;
}
    
 

   
/* Borrowed from Scott Andrew
 * Add and event to a listenter in DOM2+ browsers and IE
 * Use Capture defaults to false. Form more info: http://developer.mozilla.org/en/docs/DOM:element.addEventListener
 */
function swiAddEvent(obj, evType, fn, useCapture)
{
  if (obj.addEventListener)
  {
  obj.addEventListener(evType, fn, useCapture);
  return true;
  }
  else if (obj.attachEvent)
  {
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  }
  else 
  {
    return false;
  }
}
    
/* Same as before, used to remove an event */
function swiRemoveEvent(obj, evType, fn, useCapture)
{
  if (obj.removeEventListener)
  {
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  }
  else if (obj.detachEvent)
  {
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  }
  else 
  {
    return false;
  }
}
    
    /* swiGetElementStyle(): gets the active css style
       * needs: e$()
       * elemID: either the string of the element ID or the element object
       * style: The CSS Style Propertie name (as it goes in the stylesheet)
       * ieStyle: The name of the style used by Internet Explorer.
       *          It is the JavaScript format, where the - is supressed and
       *          the next letter is capitalized.
       *          e.g font-size becomes fontSize
       *          This option is optional and if not given style will be tried 
       *          to convert to the ieStyle one.
       *          Note: The conversion hasn't being tested on IE 5.0 (which uses a different code).
       *
       *
       * Tested in:
       *  * FireFox 1.0.6 (OK)
       *  * FireFox 2.0 (OK)
       *  * Opera 9.23 (OK)
       *  * Konqueror 3.4.2 (OK)
       *  * Konqueror 3.5.2 (OK)
       *  * Mozilla 1.7.13 (OK)
       *  * IE 7 (OK)
       *  * IE 6 (OK)
       *  * IE 5.5 (OK)
       *  * Safari 2 (OK)
       * 
       * Not tested on:
       *  * Safari (1)
       *  * IE 4, 5.0
       *  * Mozilla < 1.7
       *  * Firefox < 2
       * 
       * Caveats: getComputedStyle gets the info as it will be used to display
       *          and currentStyle before being processed to be displayed 
       *	  (as it is understood from the CSS), which is not the same thing.
       *	  e.g. font-size will be in px in Gecko and in em in IE.
       */
      function swiGetElementStyle(elemID, style, ieStyle) 
      {
        /* TODO: Estudar pra ver se faz diferença em algum navegador antigo
        if(document.defaultView && document.defaultView.getComputedStyle)
        {
	 return document.defaultView.getComputedStyle(e$(elemID), "").getPropertyValue(style);
	}
    	else 
    	*/
    	if (e$(elemID).currentStyle) 
    	{
    	  if (ieStyle == null)
    	  {
     	    try
     	    {
     	      ieStyle = style.replace(/\-(\w)/g, function (strMatch, p1) {
 	        return p1.toUpperCase();
 	      });
 	    }
 	    catch(e)
 	    {
	      // Used to prevent an error in IE 5.0 (not tested yet)
	      while( -1 != (dashIndex = style.indexOf('-')) )
	      {
		ieStyle = style.substring(0,dashIndex) 
			+ style.substring(dashIndex + 1, dashIndex + 2).toUpperCase() 
			+ style.substring(dashIndex + 2);
	      }
 	    }
    	  }
          return e$(elemID).currentStyle[ieStyle];
	}
	else
	{
	  if (window.getComputedStyle) 
	  {
            var compStyle = window.getComputedStyle(e$(elemID), "");
            return compStyle.getPropertyValue(style);
    	  }
    	  else
    	  {
    	     //Last try (Safari)
    	     if (document.defaultView.getComputedStyle)
    	     {
    	       return document.defaultView.getComputedStyle(e$(elemID),null).getPropertyValue(style);
    	     }
     	     
    	  }
    	}
    	return null; //Not found
      }
      
      /* Return a css value in pixels 
       * if the value is auto, return 0
       * if the value is not defined (cant retrieve), returns 0
       * if the value is not in px (returns 0) FIXME: has to work this out some time
       *
       * needs: e$()
       */
      function swiGetElementStylePx( elemID, style, iestyle )
      {
        var val;
        var type;
      	val = swiGetElementStyle( elemID, style, iestyle )
      	
      	if (val == null)
      	  return 0;
      	
      	if (val == "auto")
      	  return 0;
      	  
      	type = val.substring(val.length-2, val.length);
      	
      	//FIXME: we should findout other units size
      	if (type != "px")
      		return 0;
      		
      	return val.substring(0, val.length-2);
      }
      
      
    
      /* Needs: e$()
       * 	swiGetElementStyle()
       * 	swiGetElementStylePx()
       */
      var swiBoxHeights = {
      maxh: 0,
      boxes: Array(),
      num: 0,
      maxb: 0,
      equalise: function() 
      {
	this.num = arguments.length;
	for (var i=0;i<this.num;i++) 
	  if (!e$(arguments[i]))
	    return;
	this.boxes = arguments;
	this.maxHeight();
	for (var i=0;i<this.num;i++) 
	{
 	   e$(arguments[i]).style.height = this.getNewHeight( arguments[i], this.maxh ) + "px";
 	}
      },
	
      /* This funtion takes the element, and try to apply the new OffsetHeight to
       * it taking into consideration the padding and border
       */
      getNewHeight: function(elemId, newOffsetHeight)
      {
      	var sizeType;
      	var newHeight = newOffsetHeight;

    	/*@cc_on
	@if (@_jscript_version > 5.1) // IE5.01 and up
	/* */
    	
    	newHeight -= swiGetElementStylePx( e$(elemId), "margin-top" );
    	newHeight -= swiGetElementStylePx( e$(elemId), "margin-bottom" );
      	
      	/*@cc_on
      	@end
      	
	@if (@_jscript_version >= 5.6) // IE6 and up
      	if (swiCheckCompatMode() != 3) //IE 6 or higher not in quirks mode
      	{
      	@*/
    	newHeight -= swiGetElementStylePx( e$(elemId), "border-top-width" );
      	newHeight -= swiGetElementStylePx( e$(elemId), "border-bottom-width" );      	
      	newHeight -= swiGetElementStylePx( e$(elemId), "padding-top" );
   	newHeight -= swiGetElementStylePx( e$(elemId), "padding-bottom" );
      	/*@cc_on
      	}
	@end
      	@*/
      	return newHeight;
      },
      
      maxHeight: function()
      {
	var heights = new Array();
	var fullHeight;
	for (var i=0;i<this.num;i++) 
	{
//	  fullHeight = e$(this.boxes[i]).offsetHeight*1;
            fullHeight = e$(this.boxes[i]).offsetHeight*1 
			+ swiGetElementStylePx(this.boxes[i], "margin-top")*1 
			+ swiGetElementStylePx(this.boxes[i], "margin-bottom")*1;
	    //Array.push doesnt work on older browsers (E.g. IE5.01)		
	    //heights.push( fullHeight );
	    heights[heights.length] = fullHeight;
	}
	heights.sort(this.sortNumeric);
	this.maxh = heights[this.num-1];
      },
      sortNumeric: function(f,s) 
      {
        return f-s;
      }
	
    }
    
    
    
    
    
    
    
    
    
    
    
    
/* ********************************************************
 * System to load events after DOM is ready (HTML is loaded) 
 * and before pageload event (which include external files)
 */
     
//Placeholder for the init function (will be redefined later)
function _swiInit()
{
    // quit if this function has already been called
    if (arguments.callee.done) return;
  
    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer (webkit/khtml)
    if (_swiTimer) {
	//killing the timer
    	clearInterval(_swiTimer);
      	_swiTimer = null;
    }
  
    // user init
    if (window.swiInit)
  	swiInit();
}
 
// for Mozilla/Opera9.0b2+ browsers
if (document.addEventListener) 
{
  document.addEventListener("DOMContentLoaded", _swiInit, false);
}


// for Internet Explorer (using conditional comments)
/*@cc_on
@if (@_jscript_version > 4) //IE5 and above
      
//Detect https
var _swiLoc = document.location.toString();
if (_swiLoc.indexOf(":") == "4") 
{ //HTTP
  document.write("<script id=__swiIE_onload defer src=javascript:void(0)><\/script>");
}
else
{ //HTTPS
  document.write("<script id=__ie_onload defer src=//:><\/script>");
}
var script = document.getElementById("__swiIE_onload");
script.onreadystatechange = function() 
{
  if (this.readyState == "complete") 
  {
    _swiInit(); // call the onload handler
  }
};
@end @*/

//Safari && Konqueror3.2.2+ (and other KHTMLs)
if (/WebKit/i.test(navigator.userAgent) || /KHTML/i.test(navigator.userAgent)) 
{ // sniff
  var _swiTimer = setInterval(  function() 
  {
    if (/loaded|complete/.test(document.readyState)) 
    {
      clearInterval(_swiTimer);
      _swiInit(); // call the onload handler
    }
  }, 10);
}

//Attach as event to other browsers that support events
if ( swiAddEvent(window, "load", _swiInit, false) == false)
{

  // for other browsers (older)
  //Note that this might be overwritten by other external scripts
  window.onload = _swiInit;
}

/**** End Of DOMonLoad*******/


