
$(function() {
   
   initAjaxContentLoader();
   
   $("div#Header div#Logo img").contextMenu(null, new Array());
   
   fixPNGs();
   
   iconifyExternalLinks();
   
   // $("div#Nav img").reflect({height:0.2,opacity:0.2});
   
   Shadowbox.init({
      handleOversize: 'drag',
      modal: true,
      overlayOpacity: 0.9
   });
   
   return true;
   
});

function fixPNGs() {
   
   if (isIE6()) {
      
      $("div#Header").fixPNGs();
      $("div#Content").fixPNGs();
      $("a.external").fixPNGs();
      
   }
   
}

function initializeCyclePlugin() {
	
	$.preloadImages(preload_images);
	
	var cycle_dim = ({"width": $("div#Cycle").width(), "height": $("div#Cycle").height()});
	
	// Generate images on page
	for (var i in preload_array) {
		$("div#Cycle").append("<img alt='' src='"+preload_array[i]+"' width='"+cycle_dim.width+"' height='"+cycle_dim.height+"' />");
	}
	
	// Start cycle slideshow
	$("div#Cycle").cycle({
		fx: "fade"
	});
	
   return true;
   
}

function iconifyExternalLinks() {
   
   // Apply "compliant" 'new window/tab' attribute to external anchor tags
   $("a.external").attr("target", "_blank");
   
   // Add 'external link' icon to anchor tags with external class (and without nested images)
   $("a.external").each(function() {
      if ($(this).find("img").length == 0) {
         $(this).css({
            background: "url(../img/external.png) top right no-repeat",
            padding: "0 15px 0 0"
         });
      }
   });
   
   return true;
   
}

function initAjaxContent() {
   
   $("div#Content div#Main img").contextMenu(null, new Array());
   
   iconifyExternalLinks();
   
   fixPNGs();
   
}

function initAjaxContentLoader() {
   
   // Load history plugin - fixes history (back button), broken by AJAX
   $.historyInit(pageload, 'home');
   
   $('a').live('click', function(event) {
      if (typeof $(this).attr('href') == 'undefined' || $(this).attr('href').length < 1) return true;
      
      var split = $(this).attr('href').split('?');
      var query = split.length > 1 ? split[1] : '';
      
      // Check for rel=history or content query var
      if ($(this).attr('rel') == 'history' || query.match('content=')) {
         
         // Prevent default http page load
         event.preventDefault();
         
         var matched = query.match('content=([^&]*)&?(.*)');
         
         if (matched.length > 1) {
            
            var content = matched[1];
            
            if (matched.length > 2 && matched[2].length > 0) {
               
               content += '&' + matched[2];
               
            }
            
            $.historyLoad(content);
            
         }
         
      }
      
   });
   
   return true;
   
}

// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
   
   var ajaxLoad = 'http://' + document.location.hostname + '/ajax/load.php';
   var page = '';
   
   $('div#Content').hide();
   $('div#Loading').show();
   
   // alert("pageload: " + hash);
   
   // Hash doesn't contain the first # character.
   if (hash) {
      
      // restore ajax loaded state
      if($.browser.msie) {
         
         // jQuery's $.load() function does't work when hash include special characters like aao.
         // hash = encodeURIComponent(hash);
         
      }
      
      page = ajaxLoad + '?content=' + hash;
      
   }
   else {
      page = ajaxLoad + '?content=home';
   }
   
   // alert(page);
   //*
   $('div#Content').load(page, function() {
      
      initAjaxContent();
      
      $('div#Loading').hide();
      $('div#Content').show();
      
   });
   //*/
   
}

