// default menu img path
var site_section = 'tcsa'
var menu_img_path = "/lib/template2/img/tcsa-menu/";
var navigation_block = "#navigation-block>ul";

//ready set go! 
$(document).ready(function(){
  // build base path for rollover states 
  if($('body').hasClass('twpz'))
  {
    site_section = 'twpz';
  }    
  if($('body').hasClass('tz'))
  {
    site_section = 'tz';
  }    
  if($('body').hasClass('tcsa'))
  {
    site_section = 'tcsa';
  }
  menu_img_path = "/lib/template2/img/" + site_section;

  // set height of next button in side bar
  hiboxs = $('.highlight-box .next');
  for(x = 0; x < hiboxs.length; x++)
  {
    $(hiboxs[x]).height( $(hiboxs[x]).parent().height() );
  }

  // some hacks for ie :(
  if($.browser.msie)
  {
    //round corners for msie
    $(".highlight-box").corner("tl 15px").corner("bl 15px");

    if($.browser.version <= 6)
    {
      //fix logo png for msie6
      $("#logo img").ifixpng();
      $("#nswdotgov").ifixpng();
    }
  }

  //rollover on search button
  $(".search-box-top .button").mouseover(function()
  {
    this.src = menu_img_path + "-layout/search-top-over.gif";
  });
  $(".search-box-top .button").mouseout(function()
  {
    this.src = menu_img_path + "-layout/search-top.gif";
  });
  $(".search-box .button").mouseover(function()
  {
    this.src = menu_img_path + "-layout/search-over.gif";
  });
  $(".search-box .button").mouseout(function()
  {
    this.src = menu_img_path + "-layout/search.gif";
  });
  // make menus go
  $(navigation_block + " .navigation-level1-body").siblings(".navigation-level1-head").prepend("<div class='navigation-level1-control'>&nbsp;</div>");
  
  // set class to collapsed on menus with children
  $(navigation_block + " .navigation-level1-control").parent().parent().addClass("collapsed");
  // and remove from parent (already expanded)
  $(navigation_block + " .expanded").removeClass("collapsed");
  // set height of control boxs
  hiboxs = $(navigation_block + " .navigation-level1-control");
  for(x = 0; x < hiboxs.length; x++)
  {
    $(hiboxs[x]).height( $(hiboxs[x]).parent().height() );
  }
  
  $(navigation_block + " .navigation-level1-control").click(function()
  {
    $($(this).parents().get(1)).children('.navigation-level1-body').slideToggle(750, function()
    {
      $($(this).parent()).toggleClass("expanded");
      $($(this).parent()).toggleClass("collapsed");
      $(navigation_block + ">li").removeClass("navigation-active");
      if( $(this).css("display") == "block" )
      {
        $($(this).parent().get(0)).addClass("navigation-active");
      }
    });
  });

  $(".navigation-level1-head").mouseover(function()
  {
    $(this).addClass('hover');
  });
  
  $(".navigation-level1-head").mouseout(function()
  {
    $(this).removeClass('hover');
  });
  
  $('#menu-block>ul>li').mouseover( function(){
    $(this).children('.popup-menu').css('left', 'auto');
    the_img = $(this).find("a img");
    src = the_img.attr('src');
    name = the_img.attr('id');
    new_src = menu_img_path + "-menu/" + name + '-on.png';
    the_img.attr('src', new_src);
  });
  $('#menu-block>ul>li').mouseout( function(){
    $(this).children('.popup-menu').css('left', '-999px');
    the_img = $(this).find("a img");
    src = the_img.attr('src');
    name = the_img.attr('id');
    new_src = menu_img_path + "-menu/" + name + '-off.png';
    the_img.attr('src', new_src);
  });
  //social network links
  share_page = encodeURIComponent(window.location);
  share_title = encodeURIComponent(document.title);
  submit2digg = "http://digg.com/submit?url=" + share_page + "&title=" + share_title;
  submit2reddit = "http://www.reddit.com/submit?url=" + share_page;
  submit2newsvine = "http://www.newsvine.com/_tools/seed?popoff=0&u=" + share_page;
  submit2fbook = "http://www.facebook.com/share.php?u=" + share_page;
  submit2deli = "http://del.icio.us/post?v=4;url=" + share_page + ";title=" + share_title;
  submit_me = 
    "Share : <a href="+submit2digg+"><img src='/lib/template2/img/share/digg.gif'/></a>" +
    "<a href="+submit2reddit+"><img src='/lib/template2/img/share/reddit.gif'/></a>" +
    "<a href="+submit2newsvine+"><img src='/lib/template2/img/share/newsvine.gif'/></a>" +
    "<a href="+submit2fbook+"><img src='/lib/template2/img/share/fbook.gif'/></a>" +  
    "<a href="+submit2deli+"><img src='/lib/template2/img/share/deli.gif'/></a>";
  $("#share-bar").append(submit_me);

  
  //add fancy bg to link-buttons
  link_buttons = $(".link-button");
  for(x=0;x<link_buttons.length;x++)
  {
    $(link_buttons[x]).wrap("<span class='link-button-wrap'></span>");
  }
  link_buttons = $(".link-button-cream");
  for(x=0;x<link_buttons.length;x++)
  {
    $(link_buttons[x]).wrap("<span class='link-button-cream-wrap'></span>");
  }
});
