(function($) {
   $.fn.bevmenu = function(options) {
	   /* set default options */
      var options = $.extend({
			bkgnd: 'transparent',
			menuid: "bevmenu",
			height:"30px",
			spacing: "10px",
			month: "This Month",
			retrolatest: 1975
      }, options);
      
    function makeHSWidget(title)
    {
       var s = '<span title="'+title+'" class="wmenu">'
       s += '<img id="hs-bk" style="vertical-align:-25%; border:none; cursor:pointer;" ';
       s += 'src="back_enabled.jpg" alt=""  />';
       s += ' '+options.month+' ';
       s += '<img id="hs-fwd" style="vertical-align:-25%; border:none; cursor:pointer" ';
       s += 'src="forward_enabled.jpg" alt="" /></span>';
       var item = $(s);
       return item;
    }
    
    function makeNavWidget(title)
    {
       var months = [ "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" ];
       var s = '<span class="wmenu" title="'+title+'" style="padding: 0px 3px 0px 0px;">Past';
       s += '<select title="'+title+'" style="color:#cc6666; margin:4px 3px 0px 4px;" id="yearto">';
       d = new Date();
       y = d.getFullYear();
       s += '<option selected="selected" value="'+y+'">'+y+'</option>';
       y--;
       for (var i = y; i >= 2003; i--, y--)
          s += '<option value="'+y+'">'+y+'</option>';
       y = options.retrolatest;
       for (var i = y; i >= 1942; i--, y--)
          s += '<option value="'+y+'">'+y+'</option>';
       s += '</select>';
       s += '<select title="'+title+'" style="color:#cc6666; margin:4px 3px 0px 3px;" id="monthto">';
       for (var i = 0; i < 12; i++)
       {
          var cm = months[i].substr(0,1).toUpperCase() + months[i].substr(1);
          s += '<option value="'+months[i]+'">'+cm+'</option>';
       }
       s += '</select>Go';
       s += '<img id="gobutton" src="forward_enabled.jpg" style="padding-left:4px; vertical-align:-25%; border:none; cursor:pointer;" alt="" />';
       s += "</span>";
       var item = $(s);
       return s;
    }
    
    function delimit(that, i, a)
    {
       if (i < a.length-1)
       {
          var item = $('<span style="padding:2px 0px 2px '+options.spacing+'; border-right:solid 2px #55aa44; margin-right:'+options.spacing+'"></span>');
          that.append(item);
       }
    } 
	
	 /* iterate over the matched elements passed to the plugin */
	 $(this).each(function() {
       var _ = $(this);
       $(_).attr("style", "width:100%; height:30px; padding:10px 0px 0px 5px; background-color:"+ options.bkgnd+";");
       var it = $(_).html();
       _.html("");
       var a = it.split("|");
       for (var i = 0; i < a.length; i++)
       {
          a[i] = a[i].replace(/^\s+|\s+$/g,"");
          var aa = a[i].split("..")
          var title = "";
          if (aa.length > 1)
             title = aa[1];
          var t = aa[0].split(",");
          var s = '<span id="%1" alt="%2" style="cursor:pointer; padding:7px 0px 9px 0px;" class="mainmenu%4">';
          if (t.length == 3)
          {
             // third item is an id, or a class, or a widget
             if (t[2] == "hswidget")
             {
                if (!title)
                   title = "Horizontal scrolling.";
                var w = makeHSWidget(title)
                _.append(w);
                delimit(_, i, a);
                continue;
             }
             
             if (t[2] == "navwidget")
             {
                if (!title)
                   title = "Navigate to archive pages (Jan 2003 to last month), and to BEV retrospective.";
                var w = makeNavWidget(title)
                _.append(w);
                delimit(_, i, a);
                continue;
             }
             
             var t3 = t[2].substr(1);
             if (t[2].substr(0, 1) == "#")
             {
                s = s.replace("%1", t3);
                s = s.replace("%4","");
             }
             else if (t[2].substr(0, 1) == ".")
             {
                s = s.replace("%1", "mm"+i);
                s = s.replace("%4", " "+t3);
             }
          }
          else
          {
             s = s.replace("%1", "mm"+i)
             s = s.replace("%4", "");
          }
          s = s.replace("%2", t[1]);
          s = s.replace("%3", options.spacing);
          s += t[0]+"</span>";
          var item = $(s);
          if (title)
             item.attr("title", title);
          _.append(item);
          if (t.length < 3)
          {
             if (t[1].substr(1,1) == '#')
                item.click(function() { document.location.hash = $(this).attr("alt").substr(1); });
             else
                item.click(function() { document.location = $(this).attr("alt"); });
          }

          item.hover(function () {
                $(this).css("text-decoration", "underline");
             }, function () {
                $(this).css("text-decoration", "none");
             });
             
          delimit(_, i, a);
       }
       _.show();
			
    });
  }
})(jQuery);

