
/* Helpers */

	var currT;
	var currL;
	var mArray= [4,5,6,7,12,13,14,15,21,22,23,24]; 
	var currP;

$(this).load(function(){
	
	if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}
	
	var hashPage = window.location.hash.substring(1,window.location.hash.length);

	if ( hashPage.length > 0 ) {
		$('#ar10nav li a').removeClass('current');
		$('#ar10nav li.'+hashPage+' a').addClass('current');
		var hashPageURL = $('#ar10nav li.'+hashPage+' a').attr('href');
		$("#ar10pageContent").load(hashPageURL+'/index.html div.ajax-content',function(){
			if ( hashPage == "Milestones") {
				$('#milestonesPage').scrollHoriz({
					viewSize: 8,
					paneWidth: 113
				});
				$('#ar10grid #milestonesPage #content-holder ol li li:first-child').addClass('first');
			}
		});
	}
	
	$('#ar10nav li a').click(function(){
		var curPage = $(this).closest('li').attr('class');
		$('#ar10nav li a').removeClass('current');
		$('#ar10nav li.'+curPage+' a').addClass('current');
		var curPageURL = $(this).attr('href');
		$("#ar10pageContent").load(curPageURL+'/index.html div.ajax-content',function(){
			if ( curPage == "Milestones") {
				$('#milestonesPage').scrollHoriz({
					viewSize: 8,
					paneWidth: 113
				});
				$('#ar10grid #milestonesPage #content-holder ol li li:first-child').addClass('first');
			}
		});
		window.location.hash = curPage;
		return false;
	});
	
	//## Expectation and Value
	
	$('#annualReport2010:not(.animating) #ar10grid #valuePage div.article').each(function(){
		var bg = $('a.thumb img',this).attr('src');
		var bg = $('a.thumb img',this).attr('src');
	})
		
	$('#annualReport2010:not(.animating) #ar10grid #valuePage div.first a.thumb').live('mouseover',function(){
		overArticle($(this));
	});
	
	$('#annualReport2010:not(.animating) #ar10grid #valuePage div.first a.thumb').live('mouseout',function(){
		offArticle($(this));
	});
	
	$('#annualReport2010:not(.animating) #ar10grid #valuePage div a.thumb').live('click',function(){
		$(this).die('mouseout');
		openArticle($(this));
		return false;
	});
	
	$('#annualReport2010:not(.animating) #ar10grid #valuePage div.ajax-content .close').live('click',function(){
		closeArticle($(this));
		return false;
	});
	
	//## Numbers Page
	
	$('#annualReport2010:not(.animating) #ar10grid #numbersPage div img.thumb').live('mouseover',function(){
		var p = $(this).closest('div');
		window.setTimeout(function(){
			$('h4',p).stop(true,true).animate({
				width: "100px"
				},'medium');
			p.stop(true,true).animate({
				width: "213px"
				},'medium');
			}, 100);
	});
	
	$('#annualReport2010:not(.animating) #ar10grid #numbersPage div img.thumb').live('mouseout',function(){
		var p = $(this).closest('div');
		window.setTimeout(function(){$('h4',p).stop(true,true).animate({
			width: 0
		},'medium')}, 100);
	});
	
	$('#annualReport2010:not(.animating) #ar10grid #numbersPage div img.thumb').live('click',function(){
		var which = $(this).closest('div')
		openStat(which);
	});
	
	//## Milestones Page
	
	$('#ar10grid #milestonesPage #content-holder .content-item:not(.current)').live('click',function(){
		var z = $(this);
		openMilestone(z);
	});
	
	$('#ar10grid #milestonesPage #content-holder .current .close').live('click',function(){ closeMilestone(); });
	
	//## Omniture Tracking
	
	$('.track').click(function(){
		var y = $(this).text();
		//s=s_gi('deloittecomvgngxen');
		s.pageName = "2010 Annual Report "+y;
		s.t();
		//alert("2010 Annual Report: "+y);
		//alert(s);
		return false;
	});
	
});

function overArticle(which){
	var p = which.closest('div.article');
	var c = p.attr('class').split(' ');
	$('#valueMap img.'+c[0]).fadeIn();
	window.setTimeout(function(){
		$('dl',p).stop(true,true).animate({
			width: "339px"
			},'medium');
		}, 100);
}

function offArticle(which){
	var p = which.closest('div.article');
	var c = p.attr('class').split(' ');
	$('#valueMap img.'+c[0]).fadeOut();
	window.setTimeout(function(){
		$('dl',p).stop(true,true).animate({
			width: "0"
			},'medium');
		}, 100);
}


function openArticle(which) {
	
	offArticle(which);
	
	// Cast of Characters
	var link = which.attr('href');
	var parent = which.closest('div.article');
	
	// Get the current position of the one we opened
	var currP = parent.position();
	currT = currP.top;
	currL = currP.left;
	
	//Set a flag indicating an animation is in-progress
	$('#annualReport2010').addClass('animating');
	
	//Action
	window.setTimeout(function(){
		parent.animate({
			top: 0,
			left: 0
		},'medium',function(){
			parent.css({zIndex:'1000'});
			offArticle(which);
			$('div.second',parent).load(link+' div.ajax-content',function(){
				$('div.second',parent).show(0).css({
					width: '902px',
					height: '462px'					
				});
				parent.animate({
					width: '902px',
					height: '462px'
				},'medium',function(){
					$('.scrollable').jScrollPane({
						showArrows:true,
						scrollbarWidth: 19,
						dragMaxHeight: 150
					});
					$('#annualReport2010').removeClass('animating');
				});
			});
		});
	}, 100);
}


function closeArticle(which) {
	
	//Cast of Characters
	var content = which.closest('div.second');
	var parent = which.closest('div.article');
	
	//Set a flag indicating an animation is in-progress
	$('#annualReport2010').addClass('animating');

	content.animate({
		width: '111px',
		height: '115px'
	},'medium',function(){
		$('div.ajax-content',content).remove();
	});
	parent.animate({
		width: "111px",
		height: '115px'
	},'medium',function(){
		content.hide(0);
		parent.animate({
			top: currT,
			left: currL
		},'medium',function(){
			parent.css({zIndex:'10'})
			$('#annualReport2010').removeClass('animating');
			$('div.first a.thumb',parent).live('mouseout',function(){
				offArticle($(this));
			});
		});
	});
}

function openStat(which) {
	
	//Close any that are open
	closeStat();
	
	// Get the current position of the one we opened
	// so we can go back to that position when we close
	var currP = which.position();
	currT = currP.top;
	currL = currP.left;
	
	// Cast of Characters
	var parent = which.closest('div');
	var h = $('h4',parent);
	var thumb = $('img.thumb',parent);
	var full = $('img.full',parent);
	var targetT = full.css('top');
	var targetL = full.css('left');
	var targetW = full.css('width')
	var targetH = full.css('height');
	
	//Set a flag indicating an animation is in-progress
	$('#annualReport2010').addClass('animating');
	
	// Action
	h.stop(true,true).animate({
		width: 0
	},'medium',function(){
		thumb.fadeOut('fast',function(){
			full.css({
					top: 0,
					left: 0,
					width: '111px',
					height: '114px' 
				}).fadeIn('slow',function(){
					parent.animate({
						top: targetT,
						left: targetL,
						width: targetW,
						height: targetH
					},'slow');
					full.animate({
						width: targetW,
						height: targetH
					},'slow',function(){
						parent.addClass('current');
						$('span.close',parent).fadeIn('medium').bind('click',function(){ closeStat(); });
						$('#annualReport2010').removeClass('animating');
					});
				});
		});
	});
}

function closeStat() {
	
	// Cast of Characters
	var current = $('#ar10grid #numbersPage div.current');
	var h = $('h4',current);
	var thumb = $('img.thumb',current);
	var full = $('img.full',current);
	
	//Set a flag indicating an animation is in-progress
	$('#annualReport2010').addClass('animating');

	$('span.close',current).hide(0);
	current.animate({
		top: currT,
		left: currL,
		width: '111px',
		height: '114px'
	},'slow');
	full.animate({
		width: '111px',
		height: '114px'
	},'slow',function(){
		current.removeClass('current');
		thumb.fadeIn('slow');
		full.fadeOut('medium',function(){
			full.removeAttr('style');
			$('#annualReport2010').removeClass('animating');
		});
	});
}

function openMilestone(z){
	if ( $('#ar10grid #milestonesPage #content-holder .current').length > 0 ) { 
		var t = 750;
		closeMilestone(); 
	} else { var t = 0 }
	var listener = setInterval(function(){
		if ( $('#ar10grid #milestonesPage #content-holder .current').length == 0 ) {
			var role = $('#content-holder .content-item').index(z);
			if ( mArray.indexOf(role) != -1 ) { var d = 339 }
			else { var d = 0 }
			$('.film').animate({
				"left" : '-='+d,
				"width" : '3051px'
			},900)
			$('img',z).animate({left:0},900);
			z.css({cursor:'auto'}).animate({
				width: '450px'
			},900,function(){
				$('p',z).css({visibility:'visible'}).hide().fadeIn(600);
				z.addClass('current');
			});
			clearInterval(listener);
		}
	}, t);
}

function closeMilestone(){
	var y = $('#ar10grid #milestonesPage #content-holder .current');
	var role = $('#content-holder .content-item').index(y);
	if ( mArray.indexOf(role) != -1 ) { var d = 339 }
	else { var d = 0 }
	$('.film').animate({
		"left" : '+='+d,
		"width" : '2712px'
	},700)
	$('img',y).animate({left:'-113px'},700);
	$('p',y).fadeOut(700,function(){
		$(this).css({visibility:'hidden'}).show();
	});
	y.css({cursor:'pointer'}).animate({
		width: '111px'
	},700,function(){ 
		y.removeClass('current'); 
		
	});
}

function handleSliderChange(e, ui)
{
  var maxScroll = $("#content-scroll").attr("scrollWidth") - $("#content-scroll").width();
  $("#content-scroll").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = $("#content-scroll").attr("scrollWidth") - $("#content-scroll").width();
  $("#content-scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

(function($){
	
	$.fn.scrollHoriz = function(options) {
	
		var defaults = {
			viewSize: 1,
			slideTime: 1500,
			paneWidth: null,
			slideLength: null
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			
			var obj = $(this);

			if (options.paneWidth == null){
				var q = $('.content-item:eq(1)', obj).position();
				var paneWidth = q.left;
			} else { var paneWidth = options.paneWidth; }

			var slideWidth = options.viewSize*paneWidth;
			var slideTime = options.slideTime;
			var slideLength = options.slideLength;

			var paneNum;
			if (slideLength == null){
				paneNum = $('.content-item', obj).length;
			} else { paneNum = slideLength; }
			
			var featureWidth = paneNum*paneWidth; // modded for Deloitte
		
			var viewer = $('.viewer', obj);
			var film = $('.film', obj);
			var prev = $('#prevSlide', obj);
			var next = $('#nextSlide', obj);

			film.width(featureWidth);
			
			if ($.browser.msie && $.browser.version.substr(0,1)==8) {
				var hack = 1
			} else {
				var hack = 0
			}
			
			// Unfortunately, the above is exactly what it looks like.

			$(prev).click(function(){
				if ( $('#ar10grid #milestonesPage #content-holder .current').length > 0 ) { 
					var t = 750;
					closeMilestone(); 
				} else { var t = 0 }
				var listener = setInterval(function(){
					if ( $('#ar10grid #milestonesPage #content-holder .current').length == 0 ) {
						var position = film.position();
						if ( film.is(':animated') ) { return }
						else if ( position.left < 0 ) {  // Are we at the beginning?  Is the list still animating?
							film.animate({
								"left": position.left + slideWidth 
							}, { queue: true, duration: slideTime });
						} else if ( position.left >= 0 ) {
							film.animate({
								"left" : (0-(featureWidth-slideWidth))
							}, { queue: true, duration: slideTime });
						}
						clearInterval(listener);
					}
				}, t);
				return false;
			});
			
			$(next).click(function(){
				if ( $('#ar10grid #milestonesPage #content-holder .current').length > 0 ) { 
					var t = 750;
					closeMilestone(); 
				} else { var t = 0 }
				var listener = setInterval(function(){
					if ( $('#ar10grid #milestonesPage #content-holder .current').length == 0 ) {
						var position = film.position();
						if ( film.is(':animated') ) { return }
						else if ( position.left > (0-(featureWidth-slideWidth)) ) {  // Are we at the end?  Is the list still animating?
							film.animate({
								"left": position.left - slideWidth - hack
							}, { queue: true, duration: slideTime });
						} else if ( position.left <= (0-(featureWidth-slideWidth)) ) {
							film.animate({
								"left" : 0
							}, { queue: true, duration: slideTime });
						}
						clearInterval(listener);
					}
				}, t);
				return false;
			});
		});
	};
})(jQuery);


