(function($) {
	
	var homeFeature = {
        listing: null,

        init: function() {
			var parent = $('#homeFeatureContent');
			parent.children('a').remove();
			homeFeature.listing = parent.children('div.genericFeature');

            if (homeFeature.listing.length == 0) return;

            var listingLength = homeFeature.listing.length;
            homeFeature.populateNav();

            parent.cycle({
                fx: 'fade',
                timeout: 10000,
                speed: 600,
                after: homeFeature.onStoryTransition
            });

            $('#homeFeatureNav a#homeFeaturePause').click(homeFeature.onPause);
        },
        
        populateNav : function()
        {
        	var colours = ['red', 'yellow', 'green', 'darkblue', 'lightblue'];
        	var length = colours.length;
        	var ul = $('#homeFeatureNav ul');
        	ul.empty();
        	
        	homeFeature.listing.each(function(i)
        	{
        		var index = i % length;
        		var text = $(this).find('span.tabName').text();
        		
        		ul.append('<li class="' + colours[index] + '"><a href="#"><span>' + text + '</span></a></li>');
        	});
        	
        	ul.find('a').click(homeFeature.onClick);
        },
        
        onClick: function(event)
        {
        	var li = $(this).parent().get(0);
        	$(li).parent().find('li').each(function(i)
        	{
        		if (li == this)
        		{
        			$('#homeFeatureContent').cycle(i);
        			return false;
        		}
        	});
        	
        	event.preventDefault();
        },

        onPause: function(event) 
        {
            var btn = $(this);

            if (btn.hasClass('paused')) 
            {
                btn.removeClass('paused');
                homeFeature.listing.parent().cycle('resume');
            }
            else 
            {
                btn.addClass('paused');
                homeFeature.listing.parent().cycle('pause');
            }

            event.preventDefault();
        },

        onStoryTransition: function(currSlideElement, nextSlideElement, options, forwardFlag) 
        {
            //$('#UBCHeadlineWidget div.UbcHeadlineNav span').text((options.currSlide + 1).toString() + ' / ' + homeFeature.listing.children('li').length.toString());
        	var left = (options.currSlide) * 96 + options.currSlide/*Accounts for the 1px margin on each button*/ + 7;
        	$('#homeFeatureNavHighlight').css('left', left.toString()+'px');
        }
    };
	
	var fontSize = {
		currentSize : 1.0,
	
		init : function()
		{
			$('#fontSizeDecrease').click(function(event)
			{
				event.preventDefault();
				
				if (fontSize.currentSize >= 1.3)
					return;
				
				fontSize.currentSize += 0.1;
				$('#mainContainer').css('font-size', fontSize.currentSize.toFixed(2)+'em');
			});
			
			$('#fontSizeIncrease').click(function(event)
			{
				event.preventDefault();
				
				if (fontSize.currentSize <= 0.7)
					return;
				
				fontSize.currentSize -= 0.1;
				$('#mainContainer').css('font-size', fontSize.currentSize.toFixed(2)+'em');
			});
		}
	};
	
	var pdfDownloadTracker = {
		init : function()
		{
			$('a.pdfDownload').click(function()
			{
				if (!window.pageTracker)
					return;
				
				var title = $(this).attr('title');
				
				if (title.indexOf('Download ') == 0)
				{
					title = title.substring(9);
				}
				
				var page = '/pdf-downloads/' + title;
				pageTracker._trackPageview(page);
			});
			
			$("a").click(function(event)
			{
				if (!window.pageTracker)
					return;
				
				if ($(this).hasClass('pdfDownload'))
					return;
				
				var match = this.href.match(/[^\.]+.pdf$/i);
				if (match == null)
					return;
				
				var index = this.href.lastIndexOf('/');
				index++; //increment to get rid of the slash.
				var fileName = this.href.substring(index);
				
				var page = '/pdf-downloads/' + fileName;
				pageTracker._trackPageview(page);
			});
		}
	};
	
	$(document).ready(function() {
		
		var isIE6 = $.browser.msie && $.browser.version.substr(0,1) < 7;
		var isIE7 = $.browser.msie && $.browser.version.substr(0,1) < 8;
		
		//fix IE6/7 z-index bug the brute force way.
		if(isIE6 || isIE7){
			$(function(){
				var zIndexNumber = 1000;
				$('div').each(function(){
					$(this).css('zIndex', zIndexNumber);
					zIndexNumber -= 10;
				});
			});
		}
		
		//get menu working for IE6
		if (isIE6) {
			$('#nav li').hover(
				function() {
					$(this).addClass('over');
				},
				function() {
					$(this).removeClass('over');
				}
			);
		}
		
		//facebook share btn
		var shareUrl = 'http://www.facebook.com/share.php?';
		var url = 'u=' + encodeURIComponent(window.location);
		var text = 't=' + encodeURIComponent(document.title);
		shareUrl = shareUrl + url + '&' + text;
		$('#footerShare a.facebook').attr('href', 'http://www.facebook.com/pages/2010-Legacies-Now/43418980884').attr('target', '_blank');
		
		$('#print a').click(function(event)
		{
			event.preventDefault();
			window.print();
		});
		
		homeFeature.init();
		fontSize.init();
		pdfDownloadTracker.init();
	});
})(jQuery);

