// gefunden auf https://issues.jboss.org/browse/RF-8282
/*
if (typeof Sarissa != 'undefined') {
	jQuery.ajaxSetup({
	xhr: function() {
	if (Sarissa.originalXMLHttpRequest) { return new Sarissa.originalXMLHttpRequest(); } else if (typeof ActiveXObject != 'undefined') { return new ActiveXObject("Microsoft.XMLHTTP"); } else { return new XMLHttpRequest(); }
	}
	});
	}
*/

function hideBookOverlay(){
	$("#overlay_content_documentscan").hide();
	$("#overlay_documentscan").hide();
}

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

function nextPage(bookId, pageId){
	
	$.ajax({
		  url: "/ajax/bookdata.jsp?_="+new Date().getTime(),
		  dataType: "json",
		  data: {"pageId" : pageId, "direction": "next" , "bookId": bookId},
		  success: function(json){
			  
			  if(json.pageIdRight != "noLink")
			  {
				  //$("#nextPage").attr("onClick", "nextPage('"+bookId+"', '" + (parseInt(pageId) + 2)  + "');");
				  //$("#prevPage").attr("onClick", "prevPage('"+bookId+"', '" + (parseInt(pageId) - 1) + "');");
				  
				  /*
				  $("#nextPage").attr('onclick', '').bind( "click" , function() {
					  nextPage(bookId, (parseInt(pageId) + 2));
				  });
				  $("#prevPage").attr('onclick', '').bind( "click" , function() {
					  prevPage(bookId, (parseInt(pageId) - 1));
				  });
				  */
				  
				  var booksize = $("#overlay_documentscan_booksize").val();
				  $("#overlay_documentscan_footer_left").html("<a id=\"prevPage\" onclick=\"prevPage("+bookId+", " + (parseInt(pageId) - 1) + ");\" href=\"#\"><img src=\"/images/template/bookreader_page_down.png\" alt=\"\" /></a>");
				  if(parseInt(booksize) < parseInt(pageId) + 2)
				  {
					  $("#overlay_documentscan_footer_right").html("<img src=\"/images/template/bookreader_page_empty.png\" alt=\"\" />");
				  }
				  else{
					  $("#overlay_documentscan_footer_right").html("<a id=\"nextPage\" onclick=\"nextPage("+bookId+", " + (parseInt(pageId) + 2)  + ");\" href=\"#\"><img src=\"/images/template/bookreader_page_up.png\" alt=\"\" /></a>");
				  }
				  
				  
				  $("#overlay_documentscan_footer_middle").html(json.pageNumbers);
				  loadPageNextLeft(bookId, pageId);
				  loadPageNextRight(bookId, pageId);
				  
			  }
			  else
			  {
				  //$("#overlay_documentscan_footer_right").html("");
			  }
			  
		        
		  },
		  error: function (xhr, ajaxOptions, thrownError){
			  alert(thrownError);
		  }
		});
	
	
}

function loadPageNextLeft(bookId, pageId){
	$.ajax({
		  url: "/ajax/pages.jsp?_="+new Date().getTime(),
		  dataType: "html",
		  data: {"pageId" : pageId, "bookId": bookId},
		  success: function(html){
			  $("#overlay_documentscan_left").html(html);
		        
		  },
		  error: function (xhr, ajaxOptions, thrownError){
			  alert(thrownError);
		  }
		});
}

function loadPageNextRight(bookId, pageId){
	$.ajax({
		  url: "/ajax/pages.jsp?_="+new Date().getTime(),
		  dataType: "html",
		  data: {"pageId" : (parseInt(pageId) + 1 ), "bookId": bookId},
		  success: function(html){
			  $("#overlay_documentscan_right").html(html);
		        
		  },
		  error: function (xhr, ajaxOptions, thrownError){
			  alert(thrownError);
		  }
		});
}

function loadPagePrevLeft(bookId, pageId){
	$.ajax({
		  url: "/ajax/pages.jsp?_="+new Date().getTime(),
		  dataType: "html",
		  data: {"pageId" : (parseInt(pageId)-1), "bookId": bookId},
		  success: function(html){
			  $("#overlay_documentscan_left").html(html);
		        
		  },
		  error: function (xhr, ajaxOptions, thrownError){
			  alert(thrownError);
		  }
		});
}

function loadPagePrevRight(bookId, pageId){
	$.ajax({
		  url: "/ajax/pages.jsp?_="+new Date().getTime(),
		  dataType: "html",
		  data: {"pageId" : pageId, "bookId": bookId},
		  success: function(html){
			  $("#overlay_documentscan_right").html(html);
		        
		  },
		  error: function (xhr, ajaxOptions, thrownError){
			  alert(thrownError);
		  }
		});
}

function prevPage(bookId, pageId){
	
	$.ajax({
		  url: "/ajax/bookdata.jsp?_="+new Date().getTime(),
		  dataType: "json",
		  data: {"pageId" : pageId, "direction": "prev", "bookId": bookId},
		  success: function(json){
			  
				  
			  if(json.pageIdLeft != "noLink")
			  {
				  //$("#prevPage").attr("onClick", "prevPage('"+bookId+"', '" + (parseInt(pageId) - 2) + "');");
				  //$("#nextPage").attr("onClick", "nextPage('"+bookId+"', '" + (parseInt(pageId) + 1) + "');");
				  /*
				  $("#nextPage").attr('onclick', '').bind( "click" , function() {
					  nextPage(bookId, (parseInt(pageId) + 1));
				  });
				  $("#prevPage").attr('onclick', '').bind( "click" , function() {
					  prevPage(bookId, (parseInt(pageId) - 2));
				  });
				  */
				  if(parseInt(pageId - 2) < 1){
					  $("#overlay_documentscan_footer_left").html("<img src=\"/images/template/bookreader_page_empty.png\" alt=\"\" />");
				  }
				  else{
					  $("#overlay_documentscan_footer_left").html("<a id=\"prevPage\" onclick=\"prevPage("+bookId+", " + (parseInt(pageId) - 2) + ");\" href=\"#\"><img src=\"/images/template/bookreader_page_down.png\" alt=\"\" /></a>");
				  }
				  $("#overlay_documentscan_footer_right").html("<a id=\"nextPage\" onclick=\"nextPage("+bookId+", " + (parseInt(pageId) + 1)  + ");\" href=\"#\"><img src=\"/images/template/bookreader_page_up.png\" alt=\"\" /></a>");
				  
				  
				  $("#overlay_documentscan_footer_middle").html(json.pageNumbers);
				  
				  loadPagePrevLeft(bookId, pageId);
				  loadPagePrevRight(bookId, pageId);
				  
			  }
			  else
			  {
				  //$("#overlay_documentscan_footer_left").html("");
			  }
		        
		  },
		  error: function (xhr, ajaxOptions, thrownError){
			  alert(thrownError);
		  }
		});
	
}


jQuery(function() {
	
	$.ajax({
		  url: "/ajax/documentscan.jsp?_="+new Date().getTime(),
		  dataType: "json",
		  success: function(json){
			   var searchString = json.searchTerms;
			   var termArray = json.termArray;
			   var pageIdArray = json.pageIdArray;
			   
			   if(searchString != ""){
			    	var ausdruck = new RegExp("("+searchString+")", "gi"); 
			        var text = $(".documentscan").html();
			        if(text != null)
			        {
				        var neu = text.replace(ausdruck,"<a style='color:#A1815D;' class='openbook' name='$1' href='$1'>$1</a>");
				        $(".documentscan").html(neu);
					  
				        $(".openbook").each(function(index){
				        	
				        	var link = $(this).attr("name");
				        	var index = termArray.indexOf(link.toLowerCase());
				        	var url = window.location.pathname;
				        	$(this).attr("href", url + "?pageId=" + pageIdArray[index]);
				        });
			        }
			   }
		        
		  },
		  error: function (xhr, ajaxOptions, thrownError){
			  alert(thrownError);
		  }
		});
	
});
