
// window.onload instead of document.ready() as it needs doc downloaded to work out dims etc.
window.onload = function() {	

	// browser detection to replace pg corner png with gif
	if ($.browser.msie && $.browser.version <= 6 ) {
		$('#mainnext img').attr({	src:'images/corner-next.gif'});
		$('#mainprev img').attr({	src:'images/corner-prev.gif'});
	}
	
	tooltipMagnify();
	tooltip();
	
	$('#mainnext').click(scrollnext);
	$('#mainprev').click(scrollprev);
	$('#scroller a').click(scroller);
	
	$('#scrollBarNext').click(scrollBarNext);
	$('#scrollBarPrev').click(scrollBarPrev);
	
	// window close
	$('a#close').click(function(event) {
		window.close();
		event.preventDefault();
	});
	
	// Return home
	$('a#home').click(function(event) {
		jumpToPage(1);
		event.preventDefault();
	});
	
	// CONTENTS PAGE functionality
	
	$('a#contents').click(function(event) {
		jumpToPage(2);
		event.preventDefault();
	});
	
	$('li#contents ul li a').click(function(event) {
		var desiredPgNum = $(this).text();
		// formula to find actual chosen page (not literal as per page numbers in PDF)
		desiredPgNum = desiredPgNum/2 + 2;
		jumpToPage(desiredPgNum);
		
		if (desiredPgNum==numListItems) {
			newPos = (0 - pageWidth * (desiredPgNum-1)) + singlePageWidth;
			$('#innercontainer').animate({"left": newPos}, 500, function() {
				$('#container').animate({"width": singlePageWidth}, 500);
			});
			currentPos = newPos;
			highlightedPage = desiredPgNum-1;
			highlightThumb();
		}
		
		event.preventDefault();
	});
	
	// zoomed pages jumpToPage functionality
	$('#lgimgholder span ul li a').click(function(event) {
		var desiredPgNum = $(this).text();
		// formula to find actual chosen page (not literal as per page numbers in PDF)
		desiredPgNum = desiredPgNum/2 + 2;
		
		
		// fade out zoomed version before calling below function
		var jumpToPageRequired = true;
		removeZoom(jumpToPageRequired, desiredPgNum);
		
		if (desiredPgNum==numListItems) {
			newPos = (0 - pageWidth * (desiredPgNum-1)) + singlePageWidth;
			$('#innercontainer').animate({"left": newPos}, 500, function() {
				$('#container').animate({"width": singlePageWidth}, 500);
			});
			currentPos = newPos;
			highlightedPage = desiredPgNum-1;
			highlightThumb();
		}
		
		//jumpToPage(desiredPgNum);
		
		
		event.preventDefault();
	});
	
	prepare();
	
	// CORNER PEELS	
	$('#mainnext').hover(function() {
		$('#mainnext img').animate({"width": "50px","height": "53px"},100);
	}, function() {
		$('#mainnext img').animate({"width": "40px","height": "45px"},100);
	});
	
	$('#mainprev').hover(function() {
		$('#mainprev img').animate({"width": "50px","height": "53px"},100);
	}, function() {
		$('#mainprev img').animate({"width": "40px","height": "45px"},100);
	});
	
	findPageWidths = function() {
		
	}
	findPageWidths();
	
	// ENLARGE
	$('#innercontainer > li > a:not(.abspos)').click(function(event) {
		
		// show contents ul only if zoomed page is a contents page (li class of contents)
		if ($(this).parent().attr("id") == "contents") {
			$('#lgimgholder span ul').show();
		}
		else {
			$('#lgimgholder span ul').hide();
		}
		
		// absolute pos anchors
		$('#lgimgholder span a.abspos').hide();
		// 1. create an array of the link's parent's li's a's that has class of abspos. 2. loop through them and grab their id and store in a new array. 3. loop through this new array of id's and show() all the lgimgholder anchors with matching ids.			
		anchorid = Array();		
		$(this).parent().children('li a.abspos').each(function(i) {
			anchorid[i] = $(this).attr("id");
   	});		
		for (j=0; j<anchorid.length; j++) {
			$('#lgimgholder span a.abspos#' + anchorid[j]).show();
		}
		
		// set lg img attr to whatever is clicked
		var img = $(this).attr("href");
		$('#lgimgholder a img').attr({src:img});
		
		var pageFormat = $(this).children().attr("class"); // returns singlePage or doublePage. Cant retrieve on window.load as zoomed pages are created on the fly so have no physical width
		
		switch (pageFormat) {
			case 'singlePage' :
				var lgImgHolderSpanWidth = '1010px';
				break;
			case 'doublePage' :
				var lgImgHolderSpanWidth = '2020px';
				break;
		}		
		
		$('#lgimgholder span').css({width:lgImgHolderSpanWidth}); // need to find width of page and set span to that size. example width:1010px for single page;
		$('#magnify').hide();
		
		$('#lgimgholder').fadeIn(500, function() {
			$('#magnifyout').show();
		});		
		
		removeZoom = function(jumpToPageRequired, desiredPgNum) {
			$('#magnifyout').hide();
			$('#lgimgholder').fadeOut(500, function() {
				$('#magnify').show();
				$(this).hide();
				// set src back to # to eliminate split second flicker of old page when page zooms back in later
				$('#lgimgholder a img').attr({src:'images/placeholder.gif'});
				
				// jumpToPage called only when jumpToPageRequired is set via the argument above. occurs when user selects page from zoomed contents page
				if (jumpToPageRequired) {
					jumpToPage(desiredPgNum);
				}
			
			});
		}		
		
		// if img is clicked page zooms out
		$('#lgimgholder a:not(.abspos)').click(function(event) {
			removeZoom();
			event.preventDefault();
		});
		
		event.preventDefault();
	});

}

prepare = function() {	

	$('#lgimgholder span ul').hide();
	
	numListItems = $('#innercontainer > li').size();
	// by default fade out previous button
	$('#mainprev').fadeOut(0);
	
	// viewable thumbs
	if (numListItems > 5) {
		pageLimit = 5;
	}
	else {
		pageLimit = numListItems;
		$('#scrollBarNext').hide();
		$('#scrollBarPrev').hide();
	}
	
	pageWidth = $('#innercontainer > li:not(#innercontainer li:eq(0))').width();
	singlePageWidth = $('#innercontainer > li:eq(0)').width();
	pageHeight = $('#innercontainer > li img').height();
	currentPos = '0';
	currentThumbsPos = '0';
	highlightedPage = 0;
	highlightThumb();
	
	$('#container').css("width",singlePageWidth);	
	$('#container').css("height",pageHeight);
	
	var initInnerContWidth = pageWidth * (numListItems - 2) + (singlePageWidth * 2);
	$('#innercontainer').css("width",initInnerContWidth);
	scrollLimit = "-" + ($('#innercontainer').width() - singlePageWidth);
	
	// 10 below is the current margin width
	scrollLimitThumbsMargins = (numListItems) * 10;
	scrollLimitThumbs = ($('#scroller ul li:eq(0)').width() * 2) + (($('#scroller ul li:eq(1)').width()) * (numListItems - 2)) + scrollLimitThumbsMargins;
	scrollLimitThumbs = "-" + scrollLimitThumbs;
	//alert(scrollLimitThumbs); // example -5836
	
	var $i=0;
	liThumbWidth = 0;
	while ($i < numListItems) {
		if ($i < 5) {
			// if the list item exists tally up the width with margins
			if ($('#scroller ul li:eq(' + $i + ')')) {
				liThumbWidth += $('#scroller ul li:eq(' + $i + ')').width() + 10;
			}
		}
		$i++;
	}
	$('#scroller').css("width",liThumbWidth-10);
	
	highlightThumb();
	
}

// SCROLL - - - - - - - - - - - - - - - - - - - - - - - - 

highlightThumb = function() {
	$('#scroller a span').addClass("dim");
	$('#scroller a:eq(' + highlightedPage + ') span').removeClass();
	
	// hide corner peel previous by default
	if (highlightedPage==0) {
		$('#mainprev').fadeOut(200);
	}
	else {
		$('#mainprev').fadeIn(200);
	}
	
	// hide corner peel next by default
	if (highlightedPage==numListItems-1) {
		$('#mainnext').fadeOut(200);
	}
	else {
		$('#mainnext').fadeIn(200);
	}
	
}

// page corners

scrollnext = function(event) {
	
	// first page - single
	if (currentPos==0) {
		newPos = currentPos - singlePageWidth;
		$('#innercontainer').animate({"left": newPos}, 500, function() {
			$('#container').animate({"width": pageWidth}, 500);
		});
		currentPos = newPos;
		highlightedPage++;
		highlightThumb();
	}
	// last page - single
	else if (currentPos == Number(scrollLimit) + pageWidth) {
		newPos = currentPos - pageWidth;
		$('#innercontainer').animate({"left": newPos}, 500, function() {
			$('#container').animate({"width": singlePageWidth}, 500);
		});
		currentPos = newPos;
		highlightedPage++;
		highlightThumb();
	}
	// remaining pages - double
	else {	
		if (currentPos!=scrollLimit) {
			newPos = currentPos - pageWidth;
			$('#innercontainer').animate({"left": newPos}, 500);
			currentPos = newPos;
			highlightedPage++;
			highlightThumb();
		}
	}
	event.preventDefault();

}

scrollprev = function(event) {
	if (currentPos!='0') {
		// first page - single
		if (currentPos == 0 - singlePageWidth) {
			newPos = currentPos + singlePageWidth;
			$('#innercontainer').animate({"left": newPos}, 500, function() {
				$('#container').animate({"width": singlePageWidth}, 500);
			});
			currentPos = newPos;
			highlightedPage--;
			highlightThumb();
		}
		// last page - single
		else if (currentPos == scrollLimit) {
			newPos = currentPos + pageWidth;
			$('#innercontainer').animate({"left": newPos}, 500, function() {
				$('#container').animate({"width": pageWidth}, 500);
			});
			currentPos = newPos;			
			highlightedPage--;
			highlightThumb();
		}
		// remaining pages - double
		else {
			newPos = currentPos + pageWidth;
			$('#innercontainer').animate({"left": newPos}, 500);
			currentPos = newPos;
			highlightedPage--;
			highlightThumb();
		}
	}
	event.preventDefault();
}

// lower scroller bar (thumbs)

scroller = function(event) {
	
	selectedPage = $(this).attr("href");
	
	if (selectedPage==1) {
		newPos = 0 - pageWidth * (selectedPage-1);
		$('#innercontainer').animate({"left": newPos}, 500, function() {
			$('#container').animate({"width": singlePageWidth}, 500);
		});
		currentPos = newPos;
		highlightedPage = selectedPage-1;
		highlightThumb();
	}
	else if (selectedPage==numListItems) {
		newPos = (0 - pageWidth * (selectedPage-1)) + singlePageWidth;
		$('#innercontainer').animate({"left": newPos}, 500, function() {
			$('#container').animate({"width": singlePageWidth}, 500);
		});
		currentPos = newPos;
		highlightedPage = selectedPage-1;
		highlightThumb();
	}
	else {
		newPos = (0 - pageWidth * (selectedPage-1)) + singlePageWidth;
		$('#innercontainer').animate({"left": newPos}, 500, function() {
			$('#container').animate({"width": pageWidth}, 500);
		});
		currentPos = newPos;
		highlightedPage = selectedPage-1;
		highlightThumb();
	}
		
	event.preventDefault();
}

scrollBarNext = function(event) {
	
	var numThumbs = $('#scroller ul li').size();
	firstThumbWithBorderAndMarg = $('#scroller ul li:eq(0)').width() + 10;
	thumbWithBorderAndMarg = $('#scroller ul li:eq(1)').width() + 10;
	//var allthumbsWithBorderAndMarg = (thumbWithBorderAndMarg*(numThumbs-2))+(firstThumbWithBorderAndMarg*2);
	var visibleCombinedThumbWidths = firstThumbWithBorderAndMarg + (thumbWithBorderAndMarg * 4);
	endOfThumbs = Number(scrollLimitThumbs) + visibleCombinedThumbWidths;
	
	if (currentThumbsPos==0) {
		// -10 to remove margin and align centrally
		$('#scroller').animate({"width": liThumbWidth-firstThumbWithBorderAndMarg-10},500);
	}	
	else if (currentThumbsPos == endOfThumbs) {
		// revert to original dims
		$('#scroller').animate({"width": liThumbWidth-10},500);
	}
	
	if (currentThumbsPos!=endOfThumbs) {
		if (currentThumbsPos == 0) {
			newThumbsPos = currentThumbsPos - (firstThumbWithBorderAndMarg+thumbWithBorderAndMarg);		
			$('#scroller ul').animate({"left": newThumbsPos},500);
			currentThumbsPos = newThumbsPos;
		}
		else {
			newThumbsPos = currentThumbsPos - thumbWithBorderAndMarg;					
			$('#scroller ul').animate({"left": newThumbsPos},500);		
			currentThumbsPos = newThumbsPos;
		}		
	}
	event.preventDefault();
}

scrollBarPrev = function(event) {
	
	if (currentThumbsPos==Number("-" + firstThumbWithBorderAndMarg)) {
		// revert to original dims
		$('#scroller').animate({"width": liThumbWidth-10},500);
	}
	else if (currentThumbsPos == endOfThumbs) {
		$('#scroller').animate({"width": liThumbWidth-firstThumbWithBorderAndMarg-10},500);
	}	
	
	if (currentThumbsPos!='0') {		
		if (currentThumbsPos==-firstThumbWithBorderAndMarg) {
			newThumbsPos = currentThumbsPos + firstThumbWithBorderAndMarg;
			$('#scroller ul').animate({"left": newThumbsPos},500);
			currentThumbsPos = newThumbsPos;
		}		
		else {
			newThumbsPos = currentThumbsPos + thumbWithBorderAndMarg;
			$('#scroller ul').animate({"left": newThumbsPos},500);
			currentThumbsPos = newThumbsPos;
		}		
	}
	event.preventDefault();
}

// CONTENTS PAGE functionality

jumpToPage = function(desiredPgNum) {
	 
	var pageLocation = desiredPgNum;
	var totalWidth = 0;
	for (var i=0; i<pageLocation-1; i++) {
		// calculate total width before contents page so we know where container has to slide to
		totalWidth = totalWidth + $('#innercontainer > li:eq(' +i+')').width();
	}	
	// target contents page
	newPos = Number("-" + totalWidth);
	$('#innercontainer').animate({"left": newPos}, 0, function() {
		if (newPos==0) {
			$('#container').animate({"width": singlePageWidth}, 0);
		}
		else {
			$('#container').animate({"width": pageWidth}, 500);
		}
	});
	currentPos = newPos;
	highlightedPage=desiredPgNum-1;
	highlightThumb();
	
}

// - - - - - - - - - - - - - - - - - - - - - - - - 

tooltipMagnify = function() {
	
	xOffset = 50;
	yOffset = 10;
	
	$("#innercontainer li a img").mouseover(function(e) {
		$('#magnify').show();
		$("#magnify").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
  });	
	
	$("#innercontainer li a img").mousemove(function(e) {
		$("#magnify").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
	});
	
	// zoom out
	
	$('#lgimgholder a img').mouseover(function(e) {
		$('#magnifyout').show();
		$("#magnifyout").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
  });	
	
	$('#lgimgholder a img').mousemove(function(e) {
		$("#magnifyout").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
	});
	
	// mouseouts
	
	$("#innercontainer li a img").mouseout(function() {
		$('#magnify').hide();
	});
	
	$('#lgimgholder a img').mouseout(function() {
		$('#magnifyout').hide();
	});

};

tooltip = function(){	
	/* CONFIG */		
		xOffset = -20;
		yOffset = -60;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
