// This is the list of main menu items
// The dictionary key is for code reference; the text string in the
// constructor is the actual title that will appear on the menu.
// Ensure that the URL is EXACTLY as it would be in document.location, or else
// the link won't be marked as the current link (no highlight).
var menu = {
    'Home': new MenuItem('Home', 'http://www.ayogo.com/'),
    'About': new MenuItem('About', 'http://www.ayogo.com/about/'),
    'Work': new MenuItem('Work', 'http://www.ayogo.com/work/'),
    'Jobs': new MenuItem('Jobs', 'http://www.ayogo.com/jobs/'),
    'Press': new MenuItem('Press', 'http://www.ayogo.com/press-media/'),
    'SocialBlog': new MenuItem('Ideas', 'http://www.ayogo.com/social-game-design/'),
    //'News': new MenuItem('News', 'http://www.ayogo.com/news/'),
    //'Partnerships': new MenuItem('Partnerships', 'http://www.ayogo.com/news/'),
    'Cont': new MenuItem('Contact', 'http://www.ayogo.com/contact/')
};
// Add subitems to the menu here.

/*menu['About'].addSubText('Innovation in Social Gaming - Ayogo is a gaming... <u>more</u>');
menu['About'].addSubItem('Wikipedia', 'http://www.wikipedia.com');
menu['About'].addSubItem('Blog', 'http://www.ayogo.com/blog/index.php');
/*menu['About'].addSubItem('Facebook', 'http://www.facebook.com');
menu['About'].addSubItem('Myspace', 'http://www.myspace.com');
menu['About'].addSubItem('Twitter', 'http://www.twitter.com');*/

//menu['News'].killLink();
//menu['About'].subitems[3].makePopup();

// Draws the menu in the HTML and sets the links to change the pages in the
// iframe.
$(document).ready(function() {
    drawMenu(menu);
/*    $('#widget .presence').click(function(e) {
      $('#frame').attr('src',$(this).attr('href'));
      $('#widget #menu a').removeClass('currentlink');
      $(this).addClass('currentlink');
      return false;
    });*/
});




// This is an abstraction of a single item in a navigation menu
function MenuItem(title, url) {

    this.title = title;
    this.url = url;
    this.target = null;
    this.isDead = false;
    this.subitems = new Array();
    this.subtext = "";
    
    this.addSubItem = addSubItem;
    this.addSubText = addSubText;
    this.killLink = killLink;
    this.makePopup = makePopup;
}

// Adds a sub MenuItem to a MenuItem
function addSubItem(title, url) {
    
    var subitem = new MenuItem(title, url);
    this.subitems.push(subitem);
}

// Adds a blurb to the submenu
function addSubText(txt) {

    this.subtext = txt;
}

// Turns a menu item into a dead link
function killLink() {
    
    this.isDead = true;
}

// Sets a menu item to pop up in a new window when clicked
// DOESN'T WORK YET!
function makePopup() {

    this.target = '_blank';
}

// Formats the menu with HTML and writes it to the document.
function drawMenu(menu) {

  $('#menu').append(document.createElement('ul'));
  $('#menu ul').attr('id', 'main_menu');
  
  for(menu_item in menu) {
    var item = menu[menu_item];
    
    var new_li = '<li>'
    new_li = new_li+'<a href="'+item.url+'" rel="nofollow" class="';
    if(item.isDead) {
        new_li = new_li+'deadlink" onclick="return false';
    } else {
        new_li = new_li+'presence';
        if(item.url == document.location) {
            new_li = new_li+' currentlink';
        }
    }
    new_li = new_li+'"';
    if(item.target != null) {
        new_li = new_li+' target="'+item.target+'"';
    }
    new_li = new_li+'>'+item.title+'</a>';
    
    if(item.subitems.length > 0){
        new_li = new_li+'<ul>';
        new_li = new_li+'<div class="sub_top"></div>';
        new_li = new_li+'<p>'+item.subtext+'</p>';
        for(menu_subitem in item.subitems) {
            var subitem = item.subitems[menu_subitem];
            new_li = new_li+'<li><a href="'+subitem.url
                     +'" rel="nofollow" class = "presence">'
                     +subitem.title+'</a></li>';
        }
        new_li = new_li+'</ul>';
    }
    
    new_li = new_li+'</li>';
    $('#main_menu').append(new_li);
  }
  
  $('#main_menu').append('</ul>');
  $('#main_menu li:last-child a').addClass('last');
}
