
var HOMEPAGE = Class.create();
HOMEPAGE.prototype = {

  /**
   * Constructor
   * 
   */
  initialize: function(context, prefix, contentTag) {

    YAHOO.log('intialising HOMEPAGE object','grid','homeapge.js');

    // define version
    this.version = '1.0';

    // create utility object
    var oProp = {
      afterLogin: this.initMenu,
      contentTag: contentTag
    };

    // setup GRIDcms
    this.prefix = context + '/' + prefix;
    
    gridcms.setup(this.prefix, oProp);

    this.menuBar = null;

    // create AJAX request
    //    new Ajax.Request( "grid/cms/user?action=login&username=admin&password=21232f297a57a5a743894a0e4a801fc3", {
    //      method: "get",
    //      onComplete: function(transport) {
    //        this.initApplication();
    //      }.bind(this)
    //    });

    // application initialisation
    this.initApplication();
        
    YAHOO.log('initialisation of HOMEPAGE object done','grid','homeapge.js');

  },

  // =========================================================================
  // Initialise DOM elements
  // =========================================================================
  
  // ===========================================================================
  // Application intialisation
  // ===========================================================================

  initApplication: function() {

    YAHOO.log('initialising application','grid','homeapge.js');
    
    // create AJAX request
    new Ajax.Request( this.prefix + "/menubar", {
      method: "get",
      onComplete: function(transport) {
        $('grid_menu').innerHTML = transport.responseText;
        if (this.menuBar==null) {
          this.menuBar = new YAHOO.widget.MenuBar("grid_menu", {
            lazyload: true
          });
          this.menuBar.subscribe("click", function(p_sType, p_aArgs) {
            var oEvent = p_aArgs[0];    // DOM Event 
            var item = p_aArgs[1];     // YAHOO.widget.MenuItem instance 
            if (item) {
              var args = item.id.split('_');
              var type = args[0];
              var id = args[1];
              if (type=='menu') {
                if (id=='new') {
                  this.createMenu(this.initApplication);
                } else if (id=='login') {
                  //                  gridcms.getUser().showLogin(this.initApplication, this.initApplication);
                  gridcms.getUser().showLogin(gridcms.reload, gridcms.reload);
                } else if (id=='logout') {
                  //                  gridcms.getUser().logout(this.initApplication);
                  gridcms.getUser().logout(gridcms.reload);
                } else if (id=='system') {
                } else {
                  this.showMenu(id);                
                }
              } else if (type=='page') {
                if (id=='sysuser') {
                  gridcms.getUser().editUser();
                } else if (id=='sysmanage') {
                  gridcms.viewManage('grid_content');
                } else {
                  this.showPage(id);                  
                }
              }
            }
          }.bind(this));
        }
        this.menuBar.render();
        this.menuBar.show();
      }.bind(this)
    });
    
  },
    
  // ===========================================================================
  // Show page
  // ===========================================================================
  
  showMenu : function (menu) {

    YAHOO.log('show menu','grid','homeapge.js');
    
    // show page details
    new Ajax.Request( this.prefix + "/menu?id="+menu, {
      method: "get",
      onComplete: function(transport) {
        $('grid_submenu').innerHTML = transport.responseText;
      }.bind(this)
    });

    // show page details
    new Ajax.Request( this.prefix + "/page?menuid="+menu, {
      method: "get",
      onComplete: function(transport) {
        $('grid_content').innerHTML = transport.responseText;
      }.bind(this)
    });

  },

  showPage : function (page) {

    YAHOO.log('show page','grid','homeapge.js');
    
    // show page details
    new Ajax.Request( this.prefix + "/menu?pageid="+page, {
      method: "get",
      onComplete: function(transport) {
        $('grid_submenu').innerHTML = transport.responseText;
      }.bind(this)
    });

    // show page details
    new Ajax.Request( this.prefix + "/page?id="+page, {
      method: "get",
      onComplete: function(transport) {
        $('grid_content').innerHTML = transport.responseText;
      }.bind(this)
    });
    
  },

  showNews : function (news) {

    YAHOO.log('show news','grid','homeapge.js');

    // show page details
    new Ajax.Request( this.prefix + "/cms/element?action=load&mode=view&meta_id="+news, {
      method: "get",
      onComplete: function(transport) {
        $('gridcms_news_'+news+'_long').innerHTML = transport.responseText + '<p><a style="cursor: pointer" onclick="homepage.hideNews('+news+')"> [zuklappen] </p></p>';
        $('gridcms_news_'+news+'_long').show();
        $('gridcms_news_'+news+'_short').hide();
      }.bind(this)
    });

  },

  hideNews : function (news) {

    YAHOO.log('hide news','grid','homeapge.js');
    $('gridcms_news_'+news+'_long').hide();
    $('gridcms_news_'+news+'_short').show();

  },

  // ===========================================================================
  // Element management
  // ===========================================================================

  showImageCollectionItem : function (tagid,idlist,currentid,direction) {
    // check if this.currentid is available
    if (!this.currentid) {
      this.currentid = currentid;
    }
    var ids = idlist.split(",");
    var countids = ids.length
    // get index of current id
    var currentindex = -1;
    for (var ii = 0 ; ii < countids; ++ii) {
      if (ids[ii] == this.currentid) {
        currentindex = ii;
      }
    }
    // check current index
    if (currentindex < 0) {
      currentindex = 0;
    }
    // define new id
    var newindex = currentindex + direction;
    if (newindex < 0) {
      newindex = countids - 1;
    } else if (newindex >= countids) {
      newindex = 0;
    }
    // update image source
    var newid = ids[newindex];
    this.currentid = newid;
    $("gridcms_image_"+tagid).src = gridcms.prefix + "/cms/element?action=file&meta_id=" + newid;

  },

  // 
  // 
  // 
  // ===========================================================================
  // Element management
  // ===========================================================================

  createMenu : function (callback) {
    gridcms.createElement('menu', callback);
  },

  createContent : function (type, idpage, callback) {
    gridcms.createElement(type, 'container='+idpage, callback);
  },

  // ===========================================================================
  // Helper functions
  // ===========================================================================

  showUserInfo: function () {
    // create AJAX request
    new Ajax.Request( this.prefix + "/admin/user?action=status", {
      method: "get",
      onComplete: function(transport) {
        // parse XML and select the "room" element
        alert(transport.responseText);
      }
    });
  },

  showAbout : function () {
    
    // Instantiate the Dialog
    var dlg = new YAHOO.widget.Dialog('dlg_about', 
    {
      width : "30em",
      zIndex: 300,
      fixedcenter : true,
      visible : false, 
      constraintoviewport : true,
      buttons: [ 
      {
        text:"ok",
        handler: function() {
          this.cancel();
        },
        isDefault: true
      },
      ]  
    });
    dlg.setHeader('GRID-IT GmbH Homepage');
    dlg.setBody('<div class="hd">GRID-IT GmbH Homepage</div>' +
      '<div class="bd">' +
      'Version:<br>&nbsp;&nbsp;' + this.getVersion() + '<br>' +
      'Layout und Technische Umsetzung:<br>&nbsp;&nbsp; GRID-IT GmbH, Innsbruck <br>' +
      '&nbsp;&nbsp;<a target="_new" href="http://www.grid-it.at">http://www.grid-it.at</a> <br>' +
      '</div>');
    // Render the Dialog
    dlg.render();
    dlg.show();
    
  },
    
  // ===========================================================================
  // Version
  // ===========================================================================

  getVersion: function () {
    return this.version;
  }
};  


// ===========================================================================
// Function calls on startup
// ===========================================================================

function onButtonClick (action, value) {
  if (action=="abcde") {
    
  } else {
    homepage.cms.onButtonClick(action,value);
  }
}


// YAHOO.util.Event.addListener("button_search_lit", "click", egar.searchDoc.bind(egar));

