$(document).ready(function() {
   //ie7 fix
   $("li #underline[:last]").each(function() {
      if ($(this).width() == 0) {
      $(this).css("width",$(this).parent().width());} });
   //handle external links
   $("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").addClass("external")
   $("a.external").attr("target","_blank");
   $("a[href$='.mov']").click(function() { window.open($(this).attr('href'), 'Quicktime', 'height=600,width=800,toolbar=false,scrollbars=auto,status=false,resizable=true,location=false,menubar=false').focus();	return false;});
   /*
   $("ul#submenu2 li a").click(function() {
      var url = $(this).attr('href');
      $("#maincontainer").load(url + " #maincontainer");
      $(this).parent().siblings().removeClass("selected");
      $(this).parent().addClass("selected");
      return false;
      });
   
   */
   /*
   $('a[rel=lightbox]').live('click', function() {
      $.fn.colorbox({href:$(this).attr('href'), open:true, transition:"elastic",
        width:"75%",
        height:"75%",
        slideshow:true,
        slideshowStart: "Spela",
        slideshowStop: "Paus",
        current: "Bild {current} av {total}",
        previous: "&lt;",
        next: "&gt;",
        close: "Stäng",
        slideshowSpeed: 3500});
   return false;
   });
   */
  //param = $.deparam.querystring();
  
  function post_load() {
   $('a[rel=lightbox]').removeClass('cboxElement');
   $('a[rel=lightbox]:visible').colorbox({transition:"elastic",
        width:"75%",
        height:"75%",
        slideshow:true,
        slideshowStart: "Spela",
        slideshowStop: "Paus",
        current: "Bild {current} av {total}",
        previous: "&lt;",
        next: "&gt;",
        close: "Stäng",
        slideshowSpeed: 3500});
  }
  
  var in_edit = false;
  //if( ('edit' in param)) { in_edit = true; }
  if ($("#cms_toolbar.cms_toolbar_editmode_toggle_on").length>0) { in_edit = true; }
  //console.log(in_edit);
  if ( in_edit == false ) {
  $("ul#submenu2 li a").each(function() {
      var url = $(this).attr('href').replace('#c','');
      //console.log(document.location.pathname)
      if (url!=document.location.pathname) {
         url = url.replace(document.location.pathname, '');
      }
      $(this).attr('href',"#"+url);
   });
  }
  // Keep a mapping of url-to-container for caching purposes.
  var cache = {
    // If url is '' (no fragment), display this div's content.
    '': $('#maincontainer')
  };
  
  // Bind an event to window.onhashchange that, when the history state changes,
  // gets the url from the hash and displays either our cached content or fetches
  // new content to be displayed.
  $(window).bind( 'hashchange', function(e) {
    
    if ( in_edit == true ) { return; } 
    
    // Get the hash (fragment) as a string, with any leading # removed. Note that
    // in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
    var url = $.param.fragment();
    //console.log(url);
    if (url == '') {
      if ($("#content.winter_root,#content.summer_root").length>0) {
         sel = $("ul#submenu2 li a");
         if (sel.length==0) return;
         u = sel.eq(0).attr('href');
         url = $.param.fragment(u);
      }
      if (url == '') return;
    }
    
    // Remove .bbq-current class from any previously "current" link(s).
    $("ul#submenu2 li").removeClass('selected');
    //$( 'a.bbq-current' ).removeClass( 'bbq-current' );
    
    // Hide any visible ajax content.
    $( '#maincontainer' ).children( ':visible' ).hide();
    
    // Add .bbq-current class to "current" nav link(s), only if url isn't empty.
    url && $( '^a[href="#' + url + '"]' ).parent().addClass("selected");
    
    if ( cache[ url ] ) {
      // Since the element is already in the cache, it doesn't need to be
      // created, so instead of creating it again, let's just show it!
      cache[ url ].show();
      post_load();
      
    } else {
      // Show "loading" content while AJAX content loads.
      $( '.bbq-loading' ).show();
      
      // Create container for this url's content and store a reference to it in
      // the cache.
      cache[ url ] = $( '<div class="bbq-item"/>' )
        
        // Append the content container to the parent container.
        .appendTo( '#maincontainer' )
        
        // Load external content via AJAX. Note that in order to keep this
        // example streamlined, only the content in .infobox is shown. You'll
        // want to change this based on your needs.
        .load( url +" #maincontainer", function(){
          // Content loaded, hide "loading" content.
          $( '.bbq-loading' ).hide();
          
          post_load();
        });
    }
    
  })
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).trigger( 'hashchange' );
  

   
});
