/*global AZCAT,YAHOO,object,portal_url */

/*jslint browser: true, unparam: true, sloppy: true, maxlen: 80, indent: 2 */

var blankImg;
if (typeof blankImg === 'undefined') {
  blankImg = 'spacer.gif';
}

var f = 'DXImageTransform.Microsoft.AlphaImageLoader';

function filt(e, s, m) {
  if (e.filters[f]) {
    e.filters[f].enabled = s ? true : false;
    if (s) {
      e.src = s;
      e.sizingMethod = m;
    }
  } else if (s) {
    e.style.filter = [
      'progid:',
      f,
      '(src="',
      s,
      '",sizingMethod="',
      m,
      '")'
    ].join("");
  }
}

function doIEPNGFix(e) {
  var bgImg, s, n;
  if (e) {
    // Assume IE7 is OK.
    if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {
      return;
    }
    bgImg =
      (e.currentStyle && e.currentStyle.backgroundImage) ||
      (e.style && e.style.backgroundImage) ||
      null;

    if (e.tagName === 'IMG') {
      if ((/\.png$/i).test(e.src)) {
        if (e.currentStyle.width === 'auto' &&
            e.currentStyle.height === 'auto') {
          e.style.width = e.offsetWidth + 'px';
        }
        filt(e, e.src, 'scale');
        e.src = blankImg;
      } else if (e.src.indexOf(blankImg) < 0) {
        filt(e);
      }
    } else if (bgImg && bgImg !== 'none') {
      if (bgImg.match(/^url[("']+(.*\.png)[)"']+$/i)) {
        s = RegExp.$1;
        if (e.currentStyle.width === 'auto' &&
            e.currentStyle.height === 'auto') {
          e.style.width = e.offsetWidth + 'px';
        }
        e.style.backgroundImage = 'none';
        filt(e, s, 'crop');
        // IE link fix.
        for (n = 0; n < e.childNodes.length; n += 1) {
          if (e.childNodes[n].style) {
            e.childNodes[n].style.position = 'relative';
          }
        }
      } else {
        filt(e);
      }
    }
  }
}

function doIEPNGFixImgFrom(rootEle) {
  var i, ilen,
    imgs = rootEle.getElementsByTagName('IMG');
  for (i = 0, ilen = imgs.length; i < ilen; i += 1) {
    doIEPNGFix(imgs[i]);
  }
}

AZCAT.siteman.init = function (e, a, root_data) {
  var config, viewtype;
  if (AZCAT.siteman.dataView) {
    AZCAT.siteman.dataView = null;
  }

  if (AZCAT.siteman.data) {
    AZCAT.siteman.data = null;
  }

  if (AZCAT.siteman.prefs) {
    if (AZCAT.siteman.Data) {
      if (AZCAT.siteman.DataView) {
        config = object(AZCAT.siteman);
        AZCAT.siteman.data = new AZCAT.siteman.Data(config, root_data);
        AZCAT.siteman.dataView = new AZCAT.siteman.DataView(config);

        viewtype = AZCAT.siteman.prefs.getPreference('lastViewMethod',
                                                     root_data.prefs_id);
        AZCAT.siteman.dataView.setViewType(viewtype, true);
        AZCAT.siteman.data.sortList(
          AZCAT.siteman.prefs.getPreference(
            'lastSortedOn',
            root_data.prefs_id
          ),
          AZCAT.siteman.prefs.getPreference(
            'lastSortedOrder',
            root_data.prefs_id
          )
        );
        AZCAT.siteman.data.updateFromServer(
          AZCAT.siteman.dataView.updateDisplay,
          AZCAT.siteman.dataView,
          true
        );
      } else {
        throw new Error('Azcat.siteman.DataView is missing.');
      }
    } else {
      throw new Error('AZCAT.siteman.Data is missing.');
    }
  } else {
    throw new Error('AZCAT.siteman.prefs is missing.');
  }
};

/*
   -++++++++++-
   |waitStatus|
   -++++++++++-
   + This function will let the user know if the system is busy or not.
   Passing true will add to the counter and false will remove from the counter. 
   When the counter is 1 or above the loading logic will run, otherwise ready
   logic is run.

   + This is included here because anonymous users do not get
   admin_javascript.js or admin_widget_javascript.js when viewing the sitemap.
   Once the dialogs have been consolidated into azcat_panel.js, this can be
   removed.

*/
var waitStatus = (function () {
  var myDialog, waitStatus;

  waitStatus = function (loading) {
    if (loading && !myDialog) {
      myDialog = new AZCAT.Dialog(
        false,
        "waitStatus",
        {"close" : false, "width" : "240px", "draggable" : false}
      );
      myDialog.setHeader(
        [
          myDialog.headerleft,
          "...",
          myDialog.headerright
        ].join("")
      );
      myDialog.setBody(
        "<div style=\"text-align: center;\"><img src=\"/loading.gif\"/></div>"
      );
      myDialog.setFooter(myDialog.footerleft + myDialog.footerright);
      myDialog.render(document.body);
      myDialog.show();
    } else if (!loading && myDialog) {
      if (myDialog) {
        if (myDialog.cfg) {
          myDialog.hide();
        }
        myDialog = null;
      }
    }
  };

  return waitStatus;
}());

var show_action_buttons = false;

var sitemap_url;
if (YAHOO.env.ua.ie) {
  sitemap_url = document.getElementsByTagName('BASE')[0].href;
} else {
  sitemap_url = document.baseURI;
}
sitemap_url = sitemap_url.replace(new RegExp('^' + portal_url), '');
sitemap_url = sitemap_url.replace(/\/$/, '');

YAHOO.util.Event.onDOMReady(AZCAT.siteman.init, {path: '',
                                                 cmf_uid: 0,
                                                 id: sitemap_url,
                                                 prefs_id: 'sitemap'});

