/* jqimgviewer Versión 0.1-Experimental - 13/08/2009 - 11.02
 * Adaptation for jquery use from "gsv" by Martín Mulone <martin@tecnodoc.com.ar> * 
 * Copyright (c) 2009
 * visitenos en: http://www.tecnodoc.com.ar
 * 
 * 
 * "jqimgviewer" es un visor de imágenes de alta resolución, basado en el código de gsv.
 * Adaptado por Martín Mulone, para reemplazar el behaviour.js por jquery. El cual tiene mayor desarrollo. 
 * 
 * License: The same as GSV 1.0 by Michal Migurski
 * Licencia: La misma que GSV 1.0 por Michal Migurski
 * 
 * 
  GSV 1.0, by Michal Migurski <mike-gsv@teczno.com>
  $Id: gsv.js,v 1.6 2005/06/28 03:30:49 migurski Exp $

  License:
    Copyright (c) 2005 Michal Migurski <mike-gsv@teczno.com>
    
    Redistribution and use in source form, with or without modification,
    are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.
    
    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/   

var jqviewer=new function(){this.idx=0;this.page=1;this.top=0;this.left=0;this.mytop=-150;this.tiles=[];this.pressed=false;this.tileSize=256;this.Width=520;this.Height=470;this.zoomLevel=-1;this.url="/";this.countpages=1;this.minwidth=520;this.minheight=470;this.maxwidth=900;this.maxheight=470;this.init=function(){this.ImageViewer=$(".imageViewer");this.Viewer=$('.diario .visor');this.Width=$(".imageViewer").width();this.Height=$(".imageViewer").height();this.activeSurface=$(".imageViewer .surface");this.tileWell=$(".imageViewer .well");this.PanelImage=$('.imageViewer .well, .imageViewer .surface');this.status=$(".imageViewer .status");this.PanelActions=$(".imageViewer .actions");this.ActionZoomMore=$(".imageViewer .actions .zoommore");this.ActionZoomMinus=$(".imageViewer .actions .zoomminus");this.ActionChangePage=$(".imageViewer .actions .changepage");this.ActionInformation=$(".imageViewer .actions .information");this.ActionPrevious=$(".imageViewer .actions .previous");this.ActionNext=$(".imageViewer .actions .next");this.ActionFullScreen=$(".imageViewer .actions .full");this.PanelPages=$(".imageViewer .pages");this.PanelPagesClose=$(".imageViewer .pages .close");this.PanelPagesList=$(".imageViewer .pages .listpages");this.PanelInfo=$(".imageViewer .info");this.PanelInfoClose=$(".imageViewer .info .close");this.PanelInfoText=$(".imageViewer .info .infotext");this.PanelTitle=$(".imageViewer .titleimage");this.PanelTitleHeader=$(".imageViewer .titleimage h1");this.PermanentLink=$(".url_permanent .permanentlink");this.PanelNews=$(".diario .noticias");this.url=url_site;this.url_full=url_site_full;this.OnEventsActions();this.OnEventsOthers()};this.GetPermanentLink=function(){var urlperm=this.url_full;var dim=this.dim;urlperm+="diario/"+this.idx+'/?page='+this.page+'&zl='+this.zoomLevel+'&xp='+dim.x+'&yp='+dim.y;return urlperm};this.SetPermanentLink=function(){var urlpermanentlink="";urlpermanentlink=this.GetPermanentLink();this.PermanentLink.val(urlpermanentlink);this.SetPermanentLinkFocus()};this.OnEventsActions=function(){jqviewer=this;this.ActionZoomMore.click(function(){jqviewer.zoomImageUp(undefined)}).hover(function(){jqviewer.ActionZoomMore.css({cursor:'pointer'});jqviewer.displayStatus("Acercar la imagen.")},function(){jqviewer.ActionZoomMore.css({cursor:'default'});jqviewer.HideStatus()});this.ActionZoomMinus.click(function(){jqviewer.zoomImageDown(undefined)}).hover(function(){jqviewer.ActionZoomMinus.css({cursor:'pointer'});jqviewer.displayStatus("Aleja la imagen.")},function(){jqviewer.ActionZoomMinus.css({cursor:'default'});jqviewer.HideStatus()});this.ActionChangePage.click(function(){jqviewer.ShowPages()}).hover(function(){jqviewer.ActionChangePage.css({cursor:'pointer'});jqviewer.displayStatus("Muestra el panel de las páginas.")},function(){jqviewer.ActionChangePage.css({cursor:'default'});jqviewer.HideStatus()});this.ActionInformation.click(function(){jqviewer.ShowInfo()}).hover(function(){jqviewer.ActionInformation.css({cursor:'pointer'});jqviewer.displayStatus("Muestra información.")},function(){jqviewer.ActionInformation.css({cursor:'default'});jqviewer.HideStatus()});this.ActionPrevious.click(function(){jqviewer.DisplayPreviousPage()}).hover(function(){jqviewer.ActionPrevious.css({cursor:'pointer'});jqviewer.displayStatus("Se mueve a la página anterior.")},function(){jqviewer.ActionPrevious.css({cursor:'default'});jqviewer.HideStatus()});this.ActionNext.click(function(){jqviewer.DisplayNextPage()}).hover(function(){jqviewer.ActionNext.css({cursor:'pointer'});jqviewer.displayStatus("Se mueve a la página siguiente.")},function(){jqviewer.ActionNext.css({cursor:'default'});jqviewer.HideStatus()});this.ActionFullScreen.click(function(){jqviewer.ShowFullScreen()}).hover(function(){jqviewer.ActionFullScreen.css({cursor:'pointer'});jqviewer.displayStatus("Ampliar area de visor.")},function(){jqviewer.ActionFullScreen.css({cursor:'default'});jqviewer.HideStatus()})};this.SetPermanentLinkFocus=function(){this.PermanentLink.focus(function(){this.select()})};this.OnEventsOthers=function(){jqviewer=this;this.PanelPagesClose.click(function(){jqviewer.ShowPages()}).hover(function(){jqviewer.PanelPagesClose.css({cursor:'pointer'})},function(){jqviewer.PanelPagesClose.css({cursor:'default'})});this.PanelInfoClose.click(function(){jqviewer.ShowInfo()}).hover(function(){jqviewer.PanelInfoClose.css({cursor:'pointer'})},function(){jqviewer.PanelInfoClose.css({cursor:'default'})})};this.getCenter=function(zoomLevel){var fullSize=this.tileSize*Math.pow(2,zoomLevel);do{zoomLevel+=1;fullSize*=2}while(fullSize<Math.max(this.Width,this.Height));var center={'x':((fullSize-this.Width)/-2),'y':((fullSize-this.Height)/-2)};var matrix={'center':center,'zoomLevel':zoomLevel,'fullSize':fullSize};return matrix};this.LoadInViewer=function(idx,tilePage,zoomLevel,Xpos,Ypos){matrix=this.getCenter(zoomLevel);center=matrix.center;if((Xpos==0)&&(Ypos==0)&&(zoomLevel==-1)){Xpos=center.x;Ypos=this.mytop;zoomLevel=matrix.zoomLevel}this.idx=idx;this.page=tilePage;this.zoomLevel=zoomLevel;this.dim={'x':Xpos,'y':Ypos};this.start={'x':0,'y':0};this.PrepareTiles();this.OnEvents(this);this.SetDisplayTitle(idx);this.SetDisplayActions();this.SetDisplayPages(idx);this.SetPermanentLink()};this.CalculateOffset=function(object,x,y){var offsetTop=object.offset().top;var offsetLeft=object.offset().left;x-=offsetLeft;y-=offsetTop;coord={'x':x,'y':y};return coord};this.OnEvents=function(jqviewer){this.activeSurface.bind('mouseup',function(event){event.preventDefault();if(jqviewer.pressed){jqviewer.pressed=false;jqviewer.PanelImage.css({cursor:'default'});dim=jqviewer.dim;coordinates=jqviewer.CalculateOffset($(this),event.pageX,event.pageY);dim.x+=(coordinates.x-jqviewer.start.x);dim.y+=(coordinates.y-jqviewer.start.y);jqviewer.SetPermanentLink()}}).bind('mousedown',function(event){event.preventDefault();jqviewer.pressed=true;jqviewer.PanelImage.css({cursor:'move'});coordinates=jqviewer.CalculateOffset($(this),event.pageX,event.pageY);jqviewer.start={'x':coordinates.x,'y':coordinates.y}}).bind('mousemove',function(event){event.preventDefault();if(jqviewer.pressed){coordinates=jqviewer.CalculateOffset($(this),event.pageX,event.pageY);jqviewer.PositionTiles({'x':coordinates.x,'y':coordinates.y})}})};this.displayStatus=function(message){this.status.text(message);this.ShowStatus()};this.PrepareTiles=function(){tiles=[];this.tiles=tiles;var rows=Math.ceil(this.Height/this.tileSize)+1;var cols=Math.ceil(this.Width/this.tileSize)+1;for(var c=0;c<cols;c+=1){var tileCol=[];for(var r=0;r<rows;r+=1){var tile={'c':c,'r':r,'img':document.createElement('img')};tile.img.className='tile';tile.img.style.width=this.tileSize+'px';tile.img.style.height=this.tileSize+'px';this.SetTileImage(tile,true);$(tile.img).appendTo(this.tileWell);tileCol.push(tile)}tiles.push(tileCol)}this.PositionTiles({'x':0,'y':0})};this.SetTileImage=function(tile,nullOverride){var src=this.url+'tile/'+this.idx+'-'+this.page+'-'+this.zoomLevel+'-'+tile.c+'-'+tile.r+'.jpg';var left=tile.c<0;var high=tile.r<0;var right=tile.c>=Math.pow(2,this.zoomLevel);var low=tile.r>=Math.pow(2,this.zoomLevel);var outside=high||left||low||right;if(nullOverride){src=this.url+'js/null/none.png'}else if(outside){src=this.url+'js/null/none.png'}else if(high&&left){src=this.url+'js/null/top-left.png'}else if(low&&left){src=this.url+'js/null/bottom-left.png'}else if(high&&right){src=this.url+'js/null/top-right.png'}else if(low&&right){src=this.url+'js/null/bottom-right.png'}else if(high){src=this.url+'js/null/top.png'}else if(right){src=this.url+'js/null/right.png'}else if(low){src=this.url+'js/null/bottom.png'}else if(left){src=this.url+'js/null/left.png'}tile.img.src=src};this.PositionTiles=function(mouse){var tiles=this.tiles;var dim=this.dim;var start=this.start;var statusTextLines=[];statusTextLines.push('imageViewer.dimensions x,y: '+dim.x+','+dim.y);for(var c=0;c<tiles.length;c+=1){for(var r=0;r<tiles[c].length;r+=1){var tile=tiles[c][r];var wrappedAround=false;tile.x=(tile.c*this.tileSize)+dim.x+(mouse.x-start.x);tile.y=(tile.r*this.tileSize)+dim.y+(mouse.y-start.y);if(tile.x>this.Width){do{tile.c-=tiles.length;tile.x=(tile.c*this.tileSize)+dim.x+(mouse.x-start.x);wrappedAround=true}while(tile.x>this.Width)}else{while(tile.x<(-1*this.tileSize)){tile.c+=tiles.length;tile.x=(tile.c*this.tileSize)+dim.x+(mouse.x-start.x);wrappedAround=true}}if(tile.y>this.Height){do{tile.r-=tiles[c].length;tile.y=(tile.r*this.tileSize)+dim.y+(mouse.y-start.y);wrappedAround=true}while(tile.y>this.Height)}else{while(tile.y<(-1*this.tileSize)){tile.r+=tiles[c].length;tile.y=(tile.r*this.tileSize)+dim.y+(mouse.y-start.y);wrappedAround=true}}statusTextLines.push('tile '+r+','+c+' at '+tile.c+','+tile.r);this.SetTileImage(tile,wrappedAround);this.SetTileImage(tile,false);tile.img.style.top=tile.y+'px';tile.img.style.left=tile.x+'px'}}};this.ShowTitle=function(){if(this.PanelTitle.is(":hidden")){this.PanelTitle.css({opacity:0.5});this.PanelTitleHeader.css({opacity:1});this.PanelTitle.fadeIn("slow")}};this.ShowStatus=function(){if(this.status.is(":hidden")){this.status.css({opacity:0.5});this.status.fadeIn("slow")}};this.HideStatus=function(){this.status.fadeOut("fast")};this.SetDisplayTitle=function(id){jqviewer=this;$.getJSON(this.url+"json/js_titulo.php",{idx:id},DisplayTitle);function DisplayTitle(data){jqviewer.PanelTitle.html("<h1>"+data["nombre_diario"]+"</h1>");jqviewer.ShowTitle()}};this.SetDisplayActions=function(){if(this.PanelActions.is(":hidden")){this.PanelActions.css({opacity:0.5});this.PanelActions.fadeIn("slow")}};this.ShowPages=function(){if(this.PanelPages.is(":hidden")){this.PanelPages.css({opacity:0.5});this.PanelPagesList.css({opacity:1});this.PanelPages.fadeIn("slow")}else{this.PanelPages.fadeOut("slow")}};this.ShowFullScreen=function(){if(this.PanelNews.is(":hidden")){this.Viewer.css({width:this.minwidth});this.PanelNews.fadeIn("fast");this.SetNewWidth(this.minwidth)}else{this.PanelNews.fadeOut("fast");this.Viewer.css({width:this.maxwidth});this.SetNewWidth(this.maxwidth)}};this.SetNewWidth=function(new_width){this.Width=new_width};this.ShowInfo=function(){if(this.PanelInfo.is(":hidden")){this.PanelInfo.css({opacity:0.5});this.PanelInfoText.css({opacity:1});this.PanelInfo.fadeIn("slow")}else{this.PanelInfo.fadeOut("slow")}};this.SetDisplayPages=function(id){jqviewer=this;$.getJSON(url_site+"json/js_pages.php",{idx:id},DisplayPages);function DisplayPages(data){var countpages=data["cantidad"];jqviewer.countpages=countpages;var currentpage=jqviewer.page;jqviewer.SetPages(countpages,currentpage,id);jqviewer.ShowPages()}};this.DisplayNextPage=function(){var idx=this.idx;var countpages=this.countpages;var currentpage=this.page;var nextpage=currentpage+1;if(nextpage<=countpages){this.LoadInViewer(idx,nextpage,-1,0,0)}};this.DisplayPreviousPage=function(){var idx=this.idx;var countpages=this.countpages;var currentpage=this.page;var previouspage=currentpage-1;if(previouspage>=1){this.LoadInViewer(idx,previouspage,-1,0,0)}};this.zoomImage=function(mouse,direction){var dim=this.dim;if(mouse==undefined){var mouse={'x':this.Width/2,'y':this.Height/2}}var pos={'before':{'x':0,'y':0}};pos.before.x=(mouse.x-pos.before.x)-dim.x;pos.before.y=(mouse.y-pos.before.y)-dim.y;pos.before.width=pos.before.height=Math.pow(2,this.zoomLevel)*this.tileSize;var statusMessage=['at current zoom level, image is '+pos.before.width+' pixels wide','...mouse position is now '+pos.before.x+','+pos.before.y+' in the full image at zoom '+this.zoomLevel,'...with the corner at '+dim.x+','+dim.y];if(this.zoomLevel+direction>=0){pos.after={'width':(pos.before.width*Math.pow(2,direction)),'height':(pos.before.height*Math.pow(2,direction))};statusMessage.push('at zoom level '+(this.zoomLevel+direction)+', image is '+pos.after.width+' pixels wide');pos.after.x=pos.before.x*Math.pow(2,direction);pos.after.y=pos.before.y*Math.pow(2,direction);statusMessage.push('...so the current mouse position would be '+pos.after.x+','+pos.after.y);pos.after.left=mouse.x-pos.after.x;pos.after.top=mouse.y-pos.after.y;statusMessage.push('...with the corner at '+pos.after.left+','+pos.after.top);dim.x=pos.after.left;dim.y=pos.after.top;this.zoomLevel+=direction;this.start=mouse;this.PositionTiles(mouse)}};this.zoomImageUp=function(mouse){this.zoomImage(mouse,1);this.SetPermanentLink()};this.zoomImageDown=function(mouse){this.zoomImage(mouse,-1);this.SetPermanentLink()};this.SetPages=function(totalpage,current,idx){txtpages="";for(counter=1;counter<=totalpage;counter++){link_page='javascript:void(0)" onclick="jqviewer.LoadInViewer('+idx+', '+counter+', -1, 0, 0); return false';if(counter==current){txtpages+='<li><a href="'+link_page+'"><span class="selected">'+counter+'</span></a></li>'}else{txtpages+='<li><a href="'+link_page+'">'+counter+'</a></li>'}}txtpages='<ul>'+txtpages+'</ul>';this.PanelPagesList.html(txtpages)};this.dumpInfo=function(){var dim=this.dim;var tiles=this.tiles;var statusTextLines=['imageViewer '+(i+1),'current window position: '+dim.x+','+dim.y+'.','----'];for(var c=0;c<tiles.length;c+=1){for(var r=0;r<tiles[c].length;r+=1){statusTextLines.push('image ('+c+','+r+') has tile ('+this.zoomLevel+','+tiles[c][r].c+','+tiles[c][r].r+')')}}alert(statusTextLines.join("\n"))}}