if (typeof OV == "undefined" || !OV) {
    var OV = {};
}

OV.argumentPage = {};

// This is used to look up slugs and ids for the various dialog items
// the property name should be the same as the state name used
// when calling YAHOO.util.History.navigate
//
// This table is populated in page after the navigation element tags
OV.argumentPage.stateTable = {};

OV.argumentPage.initHistoryManager = function (initialState) {
  var bookmarkedSection, querySection, initSection;

  function initializeNavigation() {
    // don't make the ajax call if the state is the default for the page
    if(window.location.href.match('#dialogState')) {
      var currentState = YAHOO.util.History.getCurrentState("dialogState");
      OV.argumentPage.loadDialogItem(currentState);
    }
  }

  bookmarkedSection = YAHOO.util.History.getBookmarkedState("dialogState");
  initSection = bookmarkedSection || initialState || 'argument_state';

  YAHOO.util.History.register("dialogState", initSection, function(state) {
				OV.argumentPage.loadDialogItem(state);
			      });

  YAHOO.util.History.onReady(function () {
			       initializeNavigation();
			     });
  try {
    YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
  } catch (e) {
    OV.argumentPage.loadDialogItem(initSection);
  }

};

OV.argumentPage.clearSelections = function () {
  var els = YAHOO.util.Selector.query('li', 'userList');
  YAHOO.util.Dom.removeClass(els, 'selected');
};

OV.argumentPage.setSelection = function (dom_id) {
  var el = YAHOO.util.Selector.query('#' + dom_id, '');
  YAHOO.util.Dom.addClass(el, 'selected');
};

OV.argumentPage.showSpinner = function () {
  var argumentContainer = YAHOO.util.Selector.query('.argumentContainer', '', true);
  argumentContainer.innerHTML = '<div id="loadingMessage">Content Loading<br /><img alt="loading" src="/lib/img/loading.gif"/></div>';
};



OV.argumentPage.loadDialogItem = function (state) {
  var state_obj = OV.argumentPage.stateTable[state];
  OV.argumentPage.showSpinner();
  OV.argumentPage.clearSelections();
  if(state_obj.type == 'counter') {
    OV.argumentPage.setSelection('counter_nav_' + state_obj.id);
  } else {
    OV.argumentPage.setSelection('argOwner');
  }
  var cObj = YAHOO.util.Connect.asyncRequest('GET', '/' + state_obj.type + 's/' +  state_obj.slug + '.json?v=brick', OV.argumentPage.showDialogItemCallback);
};

OV.argumentPage.showDialogItemClick = function (e, state) {
  YAHOO.util.Event.preventDefault(e);
  try {
    YAHOO.util.History.navigate("dialogState", state);
  } catch (e) {
    OV.argumentPage.loadDialogItem(state);
  }
};

OV.argumentPage.setAttachedImageEvents = function (dom_id, path, height, width) {
      YAHOO.util.Event.addListener(dom_id, "click", function(e){
	window.open(path, dom_id,"height=" + height + ",width=" + width);
      } );
};

OV.argumentPage.setVoteEvents = function(dom_id, votable_id, base_class ) {
  YAHOO.util.Event.addListener(dom_id, "click", OV.voter.castVote, [votable_id, base_class]);
};

OV.argumentPage.showDialogItemCallback = {
  success: function(o) {
    var argumentContainer = YAHOO.util.Selector.query('.argumentContainer', '', true);
    var rs = YAHOO.lang.JSON.parse(o.responseText);
    argumentContainer.innerHTML = rs.html;
    for(var i = 0; i < rs.support_images.length; i++) {
      OV.argumentPage.setAttachedImageEvents(rs.support_images[i].dom_id, rs.support_images[i].path, rs.support_images[i].height,  rs.support_images[i].width);
    }
    for(var i = 0; i < rs.votable.length; i++) {
      OV.argumentPage.setVoteEvents(rs.votable[i].dom_id, rs.votable[i].votable_id, rs.votable[i].base_class);
    }
  },
  failure: function(o) {
    alert('error');
  }
};

