var curr_parent_node = '1';

function initPage() {
  hashListener.onHashChanged = hashChanged;
  hashListener.init();
}

var hash_changing = false;

function hashChanged() {

  if(hash_changing) {

    hash_changing = true;

    var node_id = hashListener.getHash();
    doAjaxRequest("CoMeT_function=run_module&module=DynamicContent&function=get_node_level&node_id=" + node_id, '', hashChangedResponse);

  }

}

function hashChangedResponse(html) {

  hash_changing = false;

  var ids = html.split('|');
  gotoPage(ids[0], ids[1]);

}

function gotoPage(node, parent) {
  if (parent != curr_parent_node) {
    curr_parent_node = parent;
    loadPage(parent, node);
  }
  getDynamicContent(node);
}

if (window.attachEvent) window.attachEvent("onload", initPage);
else window.addEventListener("load", initPage, false);


function getDynamicContent(node_id) {

  jQuery('#left_nav a').removeClass('current');
  if (jQuery('#leftmenu_' + node_id)) {
    jQuery('#leftmenu_' + node_id).addClass('current');
  }

  doAjaxRequest("CoMeT_function=run_module&module=DynamicContent&function=render_dynamic_content&node_id=" + node_id, '#content_container', getDynamicContentResponse);

  hashListener.setHash(node_id); //hashlistener

  //update vine images
  updateLeftVine('left');
  updateRightVine('right');

}

function getDynamicContentResponse(html) {
  jQuery('#content_container').html(html);
}

function loadPage(parent_id, node_id) {

  jQuery('#main_nav a').removeClass('current');
  if (jQuery('#topmenu_' + parent_id)) {
    jQuery('#topmenu_' + parent_id).addClass('current');
  }

  doAjaxRequest("CoMeT_function=run_module&module=Menu&function=my_submenu_ul&div_id=left_nav&level=1&depth=4&type=leftmenu&node_id=" + node_id, '#left_navigation_container', loadPageResponse);

}

function loadPageResponse(html) {
  jQuery('#left_navigation_container').html(html);
}
