
$(function() {
	
	initAjaxContentLoader();
	
	$("div#Header div#Logo img").contextMenu(null, new Array());
	
	fixTplPngs();
	
	iconifyExternalLinks();
	
	// $("div#Nav img").reflect({height:0.2,opacity:0.2});
	
	Shadowbox.init({
		handleOversize: 'drag',
		modal: true,
		overlayOpacity: 0.9
	});
	
	$("a[rel=example_group_manual]").live('click', function(event) {
		
		$.fancybox([
				'/img/iStock/small/iStock_000001190540Small.jpg',
				'/img/iStock/small/iStock_000001865845Small.jpg',
				{
					'href'	: '/img/iStock/small/iStock_000002662526Small.jpg'
					// , 'title'	: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
				}
			], {
				padding: 0,
				transitionIn: 'none',
				transitionOut: 'none',
				type: 'image',
				changeFade: 0,
				titlePosition: 'over',
				autoScale: true,
				titleFormat: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
				}
		});
		
	});
	
	return true;
	
});

function fixTplPngs() {
	
	if (isIE6()) {
		
		var selectors = [
			'div#Logo a img'
			, 'a.external'
			// , '#Nav ul ul li a'
		];
		
		if (selectors.length)
			$(selectors.join(',')).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();
	
	fixTplPngs();
	
}

function initAjaxContentLoader() {
	
	// Load history plugin - fixes history (back button), broken by AJAX
	$.historyInit(pageload, 'home');
	
	$('a').live('click', function(event) {
		
		var $href = $(this).attr('href'); 
		
		if (!isset($href) || $href.length < 1)
			return true;
		
		// Check for rel=history or content query var
		if ($(this).attr('rel') == 'history' && isInternal($href)) {
			
			// Prevent default http page load
			event.preventDefault();
			
			$.historyLoad(getRequestUri($href));
			
		}
		
		return true;
		
	});
	
	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) {
	
	if (!hash)
		return false;
	
	var ajaxLoad = 'http://' + document.location.hostname + '/ajax/load.php';
	var page = '';
	
	$('div#Content').hide().empty();
	$('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();
		
	});
	//*/
	
}


