//Generic cookie getter
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
 
//This handles most of the page display elements - CJS 1/16/09
	
var currentAnchor = null;
//check if there are anchor changes, if there are, update content
function checkAnchor(){
	//Check if it has changed
	if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
		//if there is no anchor, load first page
		if(!currentAnchor){page = "0";}
		
		// This should allow for us to send strings within the anchor - haven't tried it yet
			//Creates the  string callback. This converts the url URL/#main&id=2 in URL/?page=main&id=2
			var splits = currentAnchor.substring(1).split('&');
			//Get the section
			var page = splits[0];
			delete splits[0];
			//Create the params string
			var params = splits.join('&');
		
		//Check if page is odd - if true start spread on previous page
		if(page%2){page--;}
		
		//Pages for previous & next spread
		var query1 = "page=" + page + "&side=left&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML + "&num_pages="+ document.getElementById('pages').innerHTML + params;
		page++;
		var query2 = "page=" + page + "&side=right&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML + "&num_pages="+ document.getElementById('pages').innerHTML + params;
		page--;

		
		//Update the pages
		
		//Simple page update - no animation
		//$.get("getpage.php",query1, function(data){
		//	$("#left_page").html(data); })
			
		//$.get("getpage.php",query2, function(data){
		//	$("#right_page").html(data); })
		
		
		//Page update with animation
		pageHeight = document.getElementById('page_height').innerHTML
		
		//getCookie("currentpage") = '' || 
		
		if(page == getCookie("currentpage") || getCookie("currentpage") == null) { 
			$.get("getpage.php",query1, function(data){
			$("#left_page").html(data); })
			$("#left_flip").fadeOut("fast");
			
			$.get("getpage.php",query2, function(data){
			$("#right_page").html(data); })
			$("#right_flip").fadeOut("fast");
			
			$("#loading").fadeIn("fast", function(){
				$("#loading").animate({opacity: 1.0}, 4000, function(){
					$("#loading").fadeOut("slow");
					});
				});
			
			}
		
		if(page < getCookie("currentpage")) {
			$("#left_flip").fadeIn("fast");
			$("#right_flip").load("getflip.php?page="+page+"&side=right&direction=back&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML, function(){
				$("#right_flip img").animate({marginRight: "455px", width: "0px", height: pageHeight}, 1 );
				$("#right_flip").fadeIn("fast");
				});
			
			$("#left_flip").load("getflip.php?page="+page+"&direction=back&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML, function(){
				$("#left_flip img").animate({marginLeft: "455px", width: "0px", height: pageHeight}, 500, "easeInSine", function(){
					$("#right_flip img").animate({marginRight: "0px", width: "455px", height: pageHeight}, 500, "easeOutSine", function(){
						$.get("getpage.php",query2, function(data){
							$("#right_page").html(data);
							$("#right_flip").fadeOut("fast");
							$("#left_flip").fadeOut("fast");
							});
						});
					});
				});

			$.get("getpage.php",query1, function(data){
				$("#left_page").html(data);
				});
			
			}
			
		if(page > getCookie("currentpage")) {
		
			$("#right_flip").fadeIn("fast");
		
			$("#left_flip").load("getflip.php?page="+page+"&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML, function(){
				$("#left_flip img").animate({marginLeft: "455px", width: "0px", height: pageHeight}, 1 );
				$("#left_flip").fadeIn("fast");
				});
		
			$("#right_flip").load("getflip.php?page="+page+"&side=right&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML, function(){
				$("#right_flip img").animate({width: "0px", marginRight: "455px", height: pageHeight}, 500, "easeInSine", function(){
					$("#left_flip img").animate({marginLeft: "0px", width: "455px", height: pageHeight}, 500, "easeOutSine", function(){

						$.get("getpage.php",query1, function(data){
							$("#left_page").html(data);
							$("#left_flip").fadeOut("fast");
							$("#right_flip").fadeOut("fast");
							});
						});
					});
				});
		
			$.get("getpage.php",query2, function(data){
				$("#right_page").html(data);
				});
			}
		
		
		
			//Update the next and prev buttons for max and min
			page = parseFloat(page);
			prevLink = page-2;
			nextLink = page+2;
			if(page<=0){
				$("#previous").fadeOut("slow");
				$("#prev_arrow").fadeOut("slow");
				}else{
				$("#previous").fadeIn("slow");
				$("#previous").html("<div id=\"tab\" onclick=\"location.href=\'#" + prevLink + "\';\"></div>");
				$("#prev_arrow").fadeIn("slow");
				$("#prev_arrow").html("<a href=\"#" + prevLink + "\"><img src=\"/images/arrow_left.gif\"></a>");
				}
				
			maxpages = parseFloat(document.getElementById('pages').innerHTML);
			if(page>=maxpages){ 
				$("#next").fadeOut("slow");
				$("#next_arrow").fadeOut("slow");
				}else{
				$("#next").fadeIn("slow");
				$("#next").html("<div id=\"tab\" onclick=\"location.href=\'#" + prevLink + "\';\"></div>");
				$("#next_arrow").fadeIn("slow");
				$("#next_arrow").html("<a href=\"#" + prevLink + "\"><img src=\"/images/arrow_left.gif\"></a>");
				}
			
			$("#next").html("<div id=\"tab\" onclick=\"location.href=\'#" + nextLink + "\';\"></div>");
			$("#next_arrow").html("<a href=\"#" + nextLink + "\"><img src=\"/images/arrow_right.gif\"></a>"); 
			
			//Set cookie with current page
			document.cookie = 'currentpage'+'='+page;
			
			$("#precache").animate({opacity: 1.0}, 1000, function(){
				precache(page);
				});
	}
}


// All-purpose hide and show functions
function showstuff(boxid){$("#"+boxid).fadeIn("slow");}
function hidestuff(boxid){$("#"+boxid).fadeOut("slow");}

//Fade in/out the zoomed page and hide prv/next buttons
function zoom(page) {
	$("#zoom").load("getzoom.php?page="+page+"&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML);
	$("#zoom").fadeIn("slow");
	$("#previous").fadeOut("slow");
	$("#next").fadeOut("slow");
	$("#prev_arrow").fadeOut("slow");
	$("#next_arrow").fadeOut("slow");

	}
function zoomOut() {
	$("#zoom").fadeOut("slow");
	$("#previous").fadeIn("slow");
	$("#prev_arrow").fadeIn("slow");
	$("#next_arrow").fadeIn("slow");
	$("#next").fadeIn("slow"), function(){
		$("#zoom").html(""); // Clear for next zoom
		}
	}

//right-click to zoom out - NOT WORKING
function zoomClick(event) { 
	if (event.button==2) { 
			zoomOut(); 
		} 
	} 

//Check current page and load up the next spread into page display divs
function nextPage(gotopage) {
	currentPage = location.hash.replace('#','');
	//alert(currentPage);
	nextPage = currentPage++;
	$("#right_page").load("getpage.php?page="+nextPage+"&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML);
	nextPage++;
	$("#left_page").load("getpage.php?page="+nextPage+"&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML);	
	}

//Show/hide the search result div
function showResults() {
	$("#search_results").slideDown("slow");
	}	
function hideResults() {
	$("#search_results").slideUp("slow");
	}
	
//Precache the next spread
function precache(page) {
	$("#precache").load("getprecache.php?page="+page+"&client_id=" + document.getElementById('client_id').innerHTML + "&issue_id="+ document.getElementById('issue_id').innerHTML);
	}

//Find X/Y Coords
xpos = 0;
ypos = 0;

function coords(e) {
	xpos = e.layerX? e.layerX :
		e.offsetX? e.offsetX : 0;
	ypos = e.layerY? e.layerY :
		e.offsetY? e.offsetY  : 0;
 
	if(xpos!=0 && ypos !=0) {	
		window.status = 'X = '+xpos+' - Y = '+ypos;
		}
 	}
