
st = {};


/*
{
  "HOME": 
  {
  "":"/page/1"
  },
  
  "COMPANY": 
  {
  "":"company"
  },
  
  "TECHNOLOGY": 
  {
  "":"/page/2",
  "Products" : "/page/1"
  },
  
  "NEWS": 
  {
  "":"news2006",
  "Archive 2006":"news2006",
  "Archive 2007": "news2007"
  },
  
  "CONTACT": 
  {
  "":"contact"
  }
};
*/
/*
st.header_images = new Array();
st.header_images["/page/1"] = "/static/img/" + "bwchip" + ".jpg";
st.header_images["/page/2"] = "/static/img/" + "dual" + ".jpg";
st.header_images["company"] = "/static/img/" + "growchip" + ".jpg";
st.header_images["tech"] = "/static/img/" + "growchip2" + ".jpg";
st.header_images["news"] = "/static/img/" + "growyellow" + ".jpg";
st.header_images["contact"] = "/static/img/" + "halfchip" + ".jpg";
st.header_images["home"] = "/static/img/" + "handchip" + ".jpg";
st.header_images["products"] = "/static/img/" + "lab" + ".jpg";
st.header_images["9"] = "/static/img/" + "pipe" + ".jpg";
*/


$(document).ready(function() {

  $.getJSON("/menu", function(json) {
    st.menuData = json;
    
    $.getJSON("/headerimages", function(imgjson) {
      
      st.header_images = imgjson;
      
      for(var thing in json) {
        st.populateMenus(thing);
        for (var subthing in json[thing]) {
          st.loadPage(json[thing][subthing]);
          break;
        }
        break;
      }
      
      
    });

  });
});

st.getKey = function (data) {
    var firstKey;
    $.each(data, function (key, val) {
        firstKey = key;
        return false;
    });
    return firstKey;
};

st.getFirstVal = function (data) {
    var firstVal;
    $.each(data, function (key, val) {
        firstVal = val;
        return false;
    });
    return firstVal;
};

st.renderSubmenu = function(submenu_name) {
  var menu = st.menuData[submenu_name];
  var yh = $("#yellowbanner_holder");
  yh.html("");
  for (var j in menu) {
    var url = menu[j];
    var item = $('<div class="submenu_item"><a href="javascript:" class="submenu_link">' + j + '</a></div> ');
    yh.append(item);
    item.data("target", url);
    item.click(function(){
      st.loadPage($(this).data('target'));
    });
  }
}

st.populateMenus = function(menu_name) {
  var th = $("#topmenu_holder");
  th.html("");

  for (var i in st.menuData) {
    var menuName = st.getKey(st.menuData[i]);
    var item = $('<div class="topmenu_item" id="topmenu_'+i+'"><a href="javascript:" class="menu_link">' + i + '</a></div>');
    th.append(item);
    $('#topmenu_' + i).data("gotopage", st.getFirstVal(st.menuData[i]));
    $('#topmenu_' + i).data("menuname", i);
    item.click(function(){
      st.renderSubmenu($(this).data("menuname"));
      st.loadPage($(this).data("gotopage"));
    });
  }
}


st.loadPage = function(page) {
  ph = $('#page_holder');
  hi = $('#header_img');
  ph.slideUp('fast',function(){    
    ph.load(page, function() {
        hi.fadeOut('fast',function(){
          if (st.header_images[page]) {
            hi.attr('src', st.header_images[page]).fadeIn('slow');;
          } else {
            hi.attr('src', '/uploads/pipe.jpg').fadeIn('slow');;
          }
        });
        ph.slideDown('slow');
    });
  });
}

