var content_server;

/**
 * Namespace: CONTENT
 *  This namespace is a container for generic Javascript used globally by
 *  PowerIT product installations across all PowerIT web servers.
 *
 * Usage:
 *  In order to take advantage of this namespace's functionality, the following
 *  Javascript files must be included
 *
 *  - http://content.mypowerit.net/_js/mootools.js
 *  - http://content.mypowerit.net/content.js
 *
 *  The namespace is implemented to prevent function names from conflicting with
 *  other Javascript included within the same loaded page
 *
 * Returns:
 *  Object - (_public) The set of publicly available methods and variables from
 *           within the CONTENT namespace
 */
CONTENT = function(){
 
 
  /**
   * Object: _private
   *  This object is a container providing private visibility for methods and
   *  variables that should only be accessed by methods within CONTENT's namespace
   *
   *  These methods are not accessible globally through means like CONTENT._private...
   */
  var _private = {
    
    /**
     * Array: included_js_files
     *  Contains a list of all Javascript files previously included by CONTENT
     *  into the DOM. This prevents a Javascript file from being added more than
     *  once.
     */
    included_js_files: new Array(),
    
    
    /**
     * Function: includeJS
     *  Dynamically loads a Javascript file, injecting a new style tag into the <head>
     *  of the DOM.
     *
     * Usage:
     *  From within a public method, includeJS can be called as
     *  _private.includeJS
     */
    includeJS : function(file){
      var links = document.getElementsByTagName('script');
      for(var i=0; i<links.length; i++)
        if(links[i].getAttribute('src').indexOf(file) != -1)
          return;
      if(document.createElement && this.included_js_files.indexOf(file) == -1) {
        var head = document.getElementsByTagName('head')[0];

        this.included_js_files.push(file);

        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('src', ((content_server) ? content_server : 'http://content.mypowerit.net/') +'/_js/'+file);

        head.appendChild(script);
      }else
        return;
    },
    
    
    /**
     * Array: included_css_files
     *  Contains a list of all CSS files previously included by CONTENT
     *  into the DOM. This prevents a CSS file from being added more than
     *  once.
     */
    included_css_files: new Array(),
    
    
    /**
     * Function: includeCSS
     *  Dynamically loads a CSS file, injecting a new style tag into the <head>
     *  of the DOM.
     *
     * Usage:
     *  From within a public method, includeCSS can be called as
     *  _private.includeCSS
     */
    includeCSS : function(file){
      var links = document.getElementsByTagName('LINK');
      for(var i=0; i<links.length; i++)
        if(links[i].getAttribute('href').indexOf(file) != -1)
          return;
      if(document.createElement && this.included_css_files.indexOf(file) == -1) {
        var head = document.getElementsByTagName('head')[0];
        var stylesheet = document.createElement('link');
        
        this.included_css_files.push(file);
        
        stylesheet.setAttribute('type', 'text/css');
        stylesheet.setAttribute('rel', 'stylesheet');
        stylesheet.setAttribute('href', ((content_server) ? content_server : 'http://content.mypowerit.net/') +'/_css/'+file);

        head.appendChild(stylesheet);
      }else
        return;
    }
  };



  /**
   * Object: _public
   *  This object is a container providing public visibility for methods and
   *  variables that be accessible both internally and external to the CONTENT
   *  namespace
   *
   *  Methods and variables in this _public object are accessible external of the
   *  CONTENT namespace by referencing CONTENT. ....
   */
  var _public = {
        
    /**
     * Function: applieIEPngFix
     *  Applies an IE5.5+ png transparency fix (does not affect other browsers)
     *  allowing PNG images to render 24 bit transparentcy properly.
     *
     * Dependencies:
     *  - iepngfix.htc file (properly referenced below)
     *  - transparent .gif file (see iepngfix.htc for details)
     *
     * Notes:
     *  This does not break validation of stylesheet code.
     */
    applyIEPngFix: function (){
      var arVersion = navigator.appVersion.split("MSIE")
      var version = parseFloat(arVersion[1])
      if ((version >= 5.5 && version < 7) && (document.body.filters))
        _private.includeJS('iepngfix.js');
    },
    
    /**
     * Function: zebra
     *  Applies a zebra stripe dynamically to any table with class attribute
     *  containing 'zebra'
     *
     * Usage:
     *  Apply class="zebra" to a <table> element
     *
     * Requires:
     *  This method requires the zebra.css file be included somewhere within the
     *  application
     *
     * Notes:
     *  Whenever an event causes the row structure/order of a .zebra table to change,
     *  simply call zebra() again and the zebra striping will be updated properly
     */
    /*zebra: function (){
      _private.includeCSS('zebra.css');
      var current_class;
      $$('.zebra').each(function(table){
        current_class = 'zebraLight';
        $ES('tr',$(table)).each(function(child){
          var d = $(child);
          while($(d).getTag() != 'table')
            d = $(d).getParent();
          if($(d).hasClass('zebra'))
            if(!$(child).hasClass('hidden') && $(child).getStyle('display') != 'none'){
              $(child).removeClass('zebraLight');
              $(child).removeClass('zebraDark');
              $(child).addClass(current_class);
              current_class = (current_class == 'zebraLight') ? 'zebraDark' : 'zebraLight';
            }
        });
      });
    },*/
    
    
    zebra: function (){
      _private.includeCSS('zebra.css');
      var current_class;
      $$('.zebra').each(function(table){
        current_class = 'zebraLight';
        $ES('tr',$(table)).each(function(child){
          var d = $(child);
          
          var highlight = (d.hasClass('highlight')) ? '-highlight' : '';
          
          var tbody = false;
          while($(d).getTag() != 'table'){
            if($(d).getTag() == 'tbody') // Yes, this table row is nested inside a tbody
              tbody = $(d); // Store a pointer to the tbody
            d = $(d).getParent();
          }
          if($(d).hasClass('zebra')) // If the table should be zebra striped...
            if(!$(child).hasClass('hidden') && $(child).getStyle('display') != 'none'
                 && ((tbody && !tbody.hasClass('hidden') && tbody.getStyle('display') != 'none')|| !tbody)){ // If the tbody exists and is NOT hidden
              $(child).removeClass('zebraLight'+ highlight);
              $(child).removeClass('zebraDark'+ highlight);
              $(child).addClass(current_class+ highlight);
              current_class = (current_class == 'zebraLight') ? 'zebraDark' : 'zebraLight';
            }
        });
      });
    },
    
    
    /**
     * Function: applyIeNavHover
     *  Creates a pseudo :hover state behavior for version of IE 5+ to allow
     *  for show/hide of the sub-menus in the navigation bar
     */
    applyIeNavHover : function() {
      if($('navigation')){
        _private.includeCSS('navigation.css');
        
        var arVersion = navigator.appVersion.split("MSIE")
        var version = parseFloat(arVersion[1])
        if ((version >= 5.5 && version < 7) && (document.body.filters))
          _private.includeCSS('navigation_ie.css');
        
        if(version >= 5.5)
        var sfEls = $ES('li','navigation').each(function(f){
          $(f).addEvent('mouseover', function(){
            $(this).addClass('nav-hover');
          });
          $(f).addEvent('mouseout', function(){
            $(this).removeClass('nav-hover');
          });
        });
      }
    },
  
  
    /**
     * Function: applyCreateButtons
     *  Applies button icons and styles to <div> tags containing a class attribute
     *  value of 'create-submit'. The application of these styles makes the <div>
     *  and children appear as a large functioning submit button
     *
     * Requires:
     *  This method requires the buttons.css file be included somewhere within the
     *  application
     */
    applyCreateButtons : function(){
      _private.includeCSS('buttons.css');
      $$('.create-submit').each(function(f){
        var url = $E('a',$(f)).getProperty('href');
        $(f).addEvent('click',function(){
          window.location = url;
        });
        if($E('img',$(f))){
          $E('img',$(f)).setProperty('src',((content_server) ? content_server : 'http://content.mypowerit.net/') +'/_img/icons/'+ $E('img',$(f)).getProperty('src'));
          $E('img',$(f)).addEvent('click',function(){
            window.location = url;
          });
        }
        $E('a',$(f)).addEvent('click',function(){
          window.location = url;
        });
      });
    },
    
    
    /**
     * Function: init
     *  Performs required method execution of necessary code within the CONTENT
     *  namespace
     *
     * Notes:
     *  This method is, and should only be called ondomready. This method
     *  should never be executed at any other time
     */
    init : function(){
      _private.includeCSS('common.css');
      _private.included_css_files.push('navigation.css');
      
      _public.zebra();
      _public.applyCreateButtons();
      _public.applyIEPngFix();
      _public.applyIeNavHover();
    }
  };



  return _public;
}();


/**
 * Function: addEvent
 *  Attaches functionality to be executed once the DOM has completed loading
 *  based around CONTENT's code.
 *
 * Notes:
 *  For now this should only call CONTENT's public init() function. All
 *  functionality executed ondomready should be placed within the init()
 *  method inside the CONTENT namespace
 */
window.addEvent('domready', function() {
  CONTENT.init();
});
