var lasturl = "";
var default_content = "";
$(document).ready( function() {
   $.ga.load("UA-3387241-11", function(pageTracker) {
      activateLinks (pageTracker);
   });
   checkURL();
   $('ul#rounded li a').click(function(e) {
      checkURL(this.hash);
      return e.stopPropagation();
   });
   $('ul#footer li a').click(function(e) {
      checkURL(this.hash);
      return e.stopPropagation();
   });
   default_content = $('#pageContent').html();
   setInterval("checkURL()", 150);
   $("#block_sites").empty().load("./guest/includes/load_sinc.php?switch=all_sites", {limit : 1005}, function() {});
   $("#block_news").empty().load("./guest/includes/load_sinc.php?switch=last_news", {limit : 1005}, function() {});
   $("#block_comments").empty().load("./guest/includes/load_sinc.php?switch=last_comments", {limit : 1005}, function() {});
   $("#block_articles").empty().load("./guest/includes/load_sinc.php?switch=last_articles", {limit : 1005}, function() {});
});
function checkURL(hash) {
   if (!hash) hash = window.location.hash;
   if (hash != lasturl) {
      lasturl = hash;
      // FIX - if we've used the history buttons to return to the homepage,
      // fill the pageContent with the default_content
      if (hash == "") $('#pageContent').html(default_content);
      else loadPage(hash);
   }
}
function loadPage(url) {
   var pageTracker = _gat._getTracker("UA-3387241-11");
   $('#loading').css('visibility', 'visible');
   qhash = url.split('?');
   uhash = typeof qhash[0] != 'undefined' ? qhash[0] : '#Index';
   phash = typeof qhash[1] != 'undefined' ? '&' + qhash[1] : '';
   url = uhash.replace('#', '');  
   $.ajax({
      type : "POST",
      url : "./guest/includes/load_page.php",
      data : 'page=' + url + phash,
      dataType : "html",
      success : function(msg) {
         if (parseInt(msg) != 0) {
            if (msg[msg.length - 1] == '^') {
               $('#aside').hide();
               msg = msg.replace('^', '');
               pageTracker._trackEvent('Internal', 'Click-Error', url);
            } else {
               $('#aside').show();
               pageTracker._trackEvent('Internal', 'Click', 'Sezione ' + url);
               // console.log(url);
            }
            document.title = 'Sezione ' + url + ' @ Rhadrix';
            $('#pageContent').html(msg);
            activateLinks (pageTracker);
         }
         $('#loading').css('visibility', 'hidden');
      }
   });
}
function activateLinks (pageTracker) {
   $('a').each(function() {
      var href = $(this).attr('href');
      var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
      if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
         if (href.match(href.match(/\/\/goo\.gl\//))) {
            $(this).click(function(e) {
                  var extLink = href.replace(/^https?\:\/\//i, '');
                  pageTracker._trackEvent('Goolge','Click', extLink);
                  return e.stopPropagation();
            });
         } else {
            $(this).click(function(e) {
               var extLink = href.replace(/^https?\:\/\//i, '');
               pageTracker._trackEvent('External','Click', extLink);
               return e.stopPropagation();
            });
         }
      } else if (href.match(/^mailto\:/i)) {
         $(this).click(function(e) {
            var mailLink = href.replace(/^mailto\:/i, '');
            pageTracker._trackEvent('Email', 'Click', mailLink);
            return e.stopPropagation();
         });
      } else if (href.match(filetypes)) {
         $(this).click( function(e) {
            var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
            var filePath = href.replace(/^https?\:\/\/(www.)rhadrix\.net\//i, '');
            pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
            return e.stopPropagation();
         });
      } else if (href.match(href.match(/search\.php\?/))) {
         $(this).click( function(e) {
            var qword = (/[\?q\=]/.exec(href)) ? /[^\?q\=]+$/.exec(href) : undefined;
            var filePath = href.replace(/^https?\:\/\/(www.)rhadrix\.net\//i, '');
            pageTracker._trackEvent('Search', 'Click - ' + qword, filePath);
            return e.stopPropagation();
         });
      }
   });
}

