var lastAddress = "";

$.address.change(function(event) {
  // load the proper content depending on event.value
  // change the title after loading or immediately depending on event.value by using 
  //$.address.title(variable);
    
    var rre = /jeanmarcel\.com\/$/;
    var rooturl = rre.test(document.location.href);
    
    if (event.value == "/")
    {
      if (rooturl)
        PlayIntro();
    }
    else
    {
      
      // adjust the language selectors so they pass on the address
      var langBaseUrl = "/language/xx/";
      $("a.language-selector").each(function() {
        var thisLangBaseUrl = $(this).attr("href").substr(0, langBaseUrl.length);
        $(this).attr("href", thisLangBaseUrl + "#" + event.value);
      });
      
      // check if the whole content or only the text_content will be changed
      var changeNormalContent = true;
      var changeTextContent = false;
      var changeSeriesContent = false;
      
      var subnavRegexp = /^\/profile/;
      if (subnavRegexp.test(lastAddress) && subnavRegexp.test(event.value)) {
        changeTextContent = true;
        changeNormalContent = false;
      } else {
        if (lastAddress.match(/^\/collection\/$/) && event.value.match(/^\/collection\/\w+\/info\/$/))
        {
          changeSeriesContent = true;
          changeNormalContent = false;
          
        }
        
        // lastAddress.match(/^\/collection\/\w+\/$/) && event.value.match
      }
      
      if (changeNormalContent) {
        // remove the interval if some is set
        if (SlideShowInterval != null) {
          SlideShowIndex = 0;
          window.clearInterval(SlideShowInterval);
          SlideShowInterval = null;
        }
        
        // hide current content using some effect
        $("#headline").fadeOut(700);
        $("#content").fadeOut(700);
      }
      else if (changeSeriesContent) {
        // let the other images slide away so only the chosen one remains.
        var itemrel = event.value;
        var remitemrel = "[rel!=address:" + itemrel + "]";
        var remitems = $(".item-image" + remitemrel + ", .item-text" + remitemrel);
        
        remitems.animate({ width: 0, opacity: 0 }, "900", function() { remitems.remove(); });
        //remitems.hide("blind", { direction: 1}, 700);
      }
      
      // mark the new tab as selected
      var regexp = /(\/.*?\/)/;
      var baseAddress = regexp.exec(event.value)[1];
      baseAddress = "address:" + baseAddress;
      
      if ($("#navigation1 a.selected").attr("rel") != baseAddress)
      {
        // if the hover.js is not included, this is false, so the color changing
        // of the navigation links is done without js automatically
        if (HoverActive)
        {
          // change the color of the currently selected tab to normal color
          $("#navigation1 a.selected").animate({ backgroundColor: "#5E2735", color: "#D9D1BD" }, 300);
        }
        
        
        $("#navigation1 a.selected").removeClass("selected");
        
        var newSelected = $("#navigation1 a[rel=" + baseAddress + "]");
        if (newSelected.length > 0)
        { 
          newSelected.addClass("selected");
          if (HoverActive)
          {
            newSelected.animate({ backgroundColor: "#E8DDCD", color: "black" }, 300);
          }
        }
      }
      
      // read the new page with ajax
      $.get(event.value, null, function(data, textStatus) {
        if (changeTextContent) {
          // only change text content
          $("#text_content").empty();
          $(data).find("#text_content div").appendTo("#text_content");
          
        } else if (changeSeriesContent) {
          
          // the item must be given the new link address
          var newitem = $(data).find(".item-image");
          $(".item-image, .item-text").attr({ 
            href : newitem.attr("href") , 
            rel : newitem.attr("rel"), 
            title : newitem.attr("title")
          });
          
          // change text content
          $(data).find("#text_content").appendTo("#content");
          $("#text_content").find("a[rel]").address();
          
        } else {
          // clear current content
          $("#content-container").empty();
          
          // take content from ajax and insert it into the page
          $(data).find("#content-container>*").appendTo("#content-container");
          $("#content").hide();
          
          // all contained links must be updated to have the address functionality
          $("#content-container").find("a[rel]").address();
        }
        // some pages need initialization, which would have been in the html
        // head, if the page had not been loaded with ajax
        
        // test if url starts with '/collection/'
        if (event.value.match(/^\/collection\//))
          InitSeriesHover();
          
        // test if url is '/collection/some_series_777/'
        if (event.value.match(/^\/collection\/\w+\/$/))
          InitSeriesScroll();
        
        // test if url is '/collection/123.456.78/large/'
        if (event.value.match(/^\/collection\/.+\/large\/$/))
          InitLargeScroll();
        
        // test if url starts with '/profile/'
        if (event.value.match(/^\/profile\//)) {
          if (!changeTextContent) 
            // installs the slideshow, only once when profile is loaded
            InitSlideshow(10);
          else
            // when only a subpage is loaded, the subnav has to be marked
            InitSideNavigation(event.value);
        }
        
        // test if url is '/dealers/' or '/contact/'
        if (event.value.match(/^\/dealers\/$/))
          InitDealers("dealers");
        if (event.value.match(/^\/contact\/$/))
          InitDealers("contact");
        
        if (changeTextContent) {
          $("#text_content").show("slide", 800);
        } else if (changeSeriesContent) {
          $("#text_content").show("slide", 1000);
        } else {
          // using some effect, show the headline...
          $("#headline").show("blind", { direction: 0 }, 200, function () {
            // ... and then the content
            $("#content").show("blind", { direction: 0 }, 500);
          });
        }
        
        // the title has to be extracted from the html string, as IE doesn't 
        // support dom access on the title
        var regexp = /<title>(.*)<\/title>/im;
        var title = regexp.exec(data)[1];
        $.address.title(title);
        
        lastAddress = event.value; 
      });
    }
}); 