$(function(){
  
  // Keep a mapping of url-to-container for caching purposes.
  var cache = {  };
    // If url is '' (no fragment), display this page.
	
    if(window.location.hash) {
      // Fragment exists
    } else {
      window.location.replace("#/home");
    };
  
  // 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) {
    
    // 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();
    
    // Remove .atlastweb-current class from any previously "current" link(s).
    $( 'a.selected' ).removeClass( 'selected' );
    
    // Hide any visible ajax content.
    $( '.atlastweb-content' ).children( ':visible' ).hide();
    
    // Add .atlastweb-current class to "current" nav link(s), only if url isn't empty.
    url && $( 'a[href="#' + url + '"]' ).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 ].fadeIn(500);
	  tracker();
      
    } else {
      // Show "loading" content while AJAX content loads.
      $( '.atlastweb-loading' ).show();
      
      // Create container for this url's content and store a reference to it in
      // the cache.
      cache[ url ] = $( '<div class="atlastweb-item"/>' )
        
        // Append the content container to the parent container.
        .appendTo( '.atlastweb-content' )
        
        // 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 + ' #hastag_content', function(){
          $( '.atlastweb-content' ).hide().fadeIn(500);
          // Content loaded, hide "loading" content.
          $( '.atlastweb-loading' ).hide();
		  
		  tracker();
		  
		  $('a').click(function(){
					
					$(this).addClass('local');
					$("a:urlExternal").removeClass('local').addClass('external');
					$("a.hashtag").removeClass('local');
					elemclass = $(this);
					if (elemclass.hasClass ("local")) {
					
					// Do some stuff
					$('html, body').stop().animate({scrollTop:0}, 300);
					// Stop doing stuff
				
				    // Wait 300ms before loading the url
					var myurl = ('#' + $(this).attr("href"));	
						
					setTimeout(function() { window.location = myurl }, 300 ); 
					return false;
					} else if (elemclass.hasClass ("external")) {
					$(this).attr('target', '_blank');
					} else {
					}
				});
		  
				$(".overlay_button").click(function(){
				var overlink = $(this).attr("id");
				$(".iframe_overlay").attr("src", "/includes/overlay" + overlink);
				$('.iframe_overlay').load(function() {
				        $(".iframe_overlay").show();
				    	$("#overlay").fadeIn(500);
						$("body *").addClass("blur");
						$("body *").css("color", "transparent");
				    });
				});
		  
		  if (url == "/home") {
			  
			  $( '#logo' ).hide();
			  $( '#logo' ).delay(2000).fadeIn(2000);
			  $( '#newsload' ).fadeIn(500);
			  $('.newsitems_1').css('font-weight', 'bold');
			  $('.newsitems_1').css('text-decoration', 'underline');
    		  var newsurl  = "includes/fetch_news.php?page=1";
			  $('#newsitems').delay(2000).load( newsurl, function(){
						  $("#newsitems").css({ opacity: 0 });
						  $('#newsitems').hide();
						  $('#newsitems').animate({
						    opacity: 0.4,
						    height: 'toggle'
						  }, 500, function() {
							$('#newsitems').animate({
						      opacity: 1
						    }, 500, function() {
						    $( '#newsload' ).fadeOut(500);
						  });
						  });
			            
    		  });
			  
			  $('.newsnavigate').click(function() {
			  var newstop = $("#leftcontainer_news");
			  var position = newstop.position();
			  var top_position = position.top - 28;
			  var newspage = $(this).attr("id");
			  var newsurl  = "includes/fetch_news.php?page=" + newspage;
			  $('html, body').stop().animate({scrollTop: top_position}, 300, function(){
			  $( '#newsitems' ).fadeOut(500);
			  $( '#newsload' ).fadeIn(500);
			  $('.newsnavigate').css('font-weight', 'normal');
			  $('.newsnavigate').css('text-decoration', 'none');
			  $('.newsitems_' + newspage).css('font-weight', 'bold');
			  $('.newsitems_' + newspage).css('text-decoration', 'underline');
			  $('#newsitems').load( newsurl, function(){
						$( '#newsitems').fadeIn(500);
			            // Content loaded, hide "loading" content.
			            $( '#newsload' ).fadeOut(500);
    		  });
			  });
			  });
			  jQuery(document).ready(function($) {
        	    $(".tweet").tweet({
                join_text: "auto",
                username: twitteruser,
                avatar_size: 48,
                count: 6,
                auto_join_text_default: artist + " says:", 
                auto_join_text_ed: artist,
                auto_join_text_ing: artist + " is",
                auto_join_text_reply: artist + " replies to",
                auto_join_text_url: artist + " is checking out",
                loading_text: loadingtweets + "..."
              });
              $("#empty").tweet({
                avatar_size: 48,
                count: 6,
                query: "cieR8oog",
                loading_text: "searching twitter..."
              }).bind("empty", function() { $(this).append("No matching tweets found"); });
              })
		  }
		  if (url == "/contact") {
			  $('#mail_1').click(function() {
			    $("#mail_1").css("color","#690073");
			    $("#mail_2").css("color","#999");
			    $("#mail_3").css("color","#999");
			    $("#mail_4").css("color","#999");
			    $("#us_change").text("Sascha de Jong");
			    $("#phone_change").text("Phone");
			    $("#mailform").attr("action","mailconfirm_sascha");
			    $('#organisation').animate({opacity: 1.0}, 500, function() { });
			    $('#function').animate({opacity: 1.0}, 500, function() { });
			    $('#organisation_hide').animate({opacity: 1.0}, 500, function() { });
			    $('#function_hide').animate({opacity: 1.0}, 500, function() { });
			    $("#organisation").removeAttr("disabled","disabled");
			    $("#function").removeAttr("disabled","disabled");
			  });
			  $('#mail_2').click(function() {
			    $("#mail_1").css("color","#999");
			    $("#mail_2").css("color","#690073");
			    $("#mail_3").css("color","#999");
			    $("#mail_4").css("color","#999");
			    $("#us_change").text("@Last! Management");
			    $("#phone_change").text("Phone");
			    $("#mailform").attr("action","mailconfirm");
			    $('#organisation').animate({opacity: 1.0}, 500, function() { });
			    $('#function').animate({opacity: 1.0}, 500, function() { });
			    $('#organisation_hide').animate({opacity: 1.0}, 500, function() { });
			    $('#function_hide').animate({opacity: 1.0}, 500, function() { });
			    $("#organisation").removeAttr("disabled","disabled");
			    $("#function").removeAttr("disabled","disabled");
			  });
			  $('#mail_3').click(function() {
			    $("#mail_1").css("color","#999");
			    $("#mail_2").css("color","#999");
			    $("#mail_3").css("color","#690073");
			    $("#mail_4").css("color","#999");
			    $("#us_change").text("Carmen Wijnen");
			    $("#phone_change").text("Phone");
			    $("#mailform").attr("action","mailconfirm_carmen");
			    $('#organisation').animate({opacity: 1.0}, 500, function() { });
			    $('#function').animate({opacity: 1.0}, 500, function() { });
			    $('#organisation_hide').animate({opacity: 1.0}, 500, function() { });
			    $('#function_hide').animate({opacity: 1.0}, 500, function() { });
			    $("#organisation").removeAttr("disabled","disabled");
			    $("#function").removeAttr("disabled","disabled");
			  });
			  $('#mail_4').click(function() {
			    $("#mail_1").css("color","#999");
			    $("#mail_2").css("color","#999");
			    $("#mail_3").css("color","#999");
			    $("#mail_4").css("color","#690073");
			    $("#us_change").text(domain);
			    $("#phone_change").text("Download link");
			    $('#organisation').animate({opacity: 0.4}, 500, function() { });
			    $('#function').animate({opacity: 0.4}, 500, function() { });
			    $('#organisation_hide').animate({opacity: 0.4}, 500, function() { });
			    $('#function_hide').animate({opacity: 0.4}, 500, function() { });
			    $("#organisation").attr("disabled","disabled");
			    $("#function").attr("disabled","disabled");
			    $("#mailform").attr("action","mailconfirm_artist");
			  });
		  }
        });
    }
  })
  
  // 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' );
  
});
