/**
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *
 * http://www.gnu.org/licenses/gpl.txt
 *
 *
 * Creates a image like div with the image as background with ability to
 * pan it with mouse.
 *
 * @name        panFullSize
 * @author      Esa-Matti Suuronen
 * @contact     esa-matti [a] suuronen [dot] org
 * @license     GPL
 * @version     1.1
 *
 *
 *
 * Takes only img-elements (and custom pan-divs). Otherwise exception is thrown.
 *
 * Example: $("img#mypic").panFullSize(300,200);
 *
 *
 *
 */
 
var alturaDivfull = 0;
 
jQuery.fn.panFullSize = function(x, y, afterLoaded){

    this.each(function(){

        var pic;
        var pic_real_width;
        var pic_real_height;
        var picX_start = 0;
        var picY_start = 0;
        var prevX = 0;
        var prevY = 0;
        var newX = 0;
        var newY = 0;
        var posicaoY = 0;
        var contador = 0;
        var alturaDiv = 0;
        var pic_orig_height = 0;
        alturaDivfull = 0;
        var mousedown = false;
        var activoSlider = 0;


		
        if ( $(this).is("img") ) {
            pic = $(this);
            if($(this).height() != 0) {
            
              alturaDivfull = jQuery(pic).height();
            
            }
        }
        else if ( $(this).is("div.panFullSize")  ) { // from custom pan-div
            pic = $(this).prev("img"); // Get the real pic
            
            if(j$(this).prev("img").height() != 0) {
           
                alturaDivfull = jQuery(pic).height();
             
            }
        }
        
        else {
            throw "Not an image! panFullSize can only be used with images.";
        }

        var pan = getPan();
        // Already have pan div?
        var exists = pan.is("*");

        if (exists) {
            x = x || pan.width();
            y = y || pan.height();
        }
        else {
            // Defaults from img-element
            x = x || pic.width();
            y = y || pic.height();
        }

        var box_width = x;
        var box_height = y;


        function initialize(){

            pic.hide();
            clearTimeout(intervaloEspera101);
            if(!$.browser.chrome) {
              jQuery(".imagem_fundo, .panFullSize, .divAvolta, .imagemPannel, .imagemPannel img").css("cursor", "url(sysimages/blank.cur),none");
            } else {
              jQuery(".imagemove").hide();
              jQuery(".imagem_fundo, .panFullSize, .divAvolta, .imagemPannel, .imagemPannel img").css("cursor", "move");
            }
            
            pan.css( 'width', jQuery(document).width()).css( 'height', box_height + "px" );
            jQuery(".imagemPannel, .imagemPannel img").css("width", jQuery(document).width());
            pan.css( 'display', 'none');
            jQuery(".panFullSize img").css("display", "block");
            jQuery(".panFullSize").fadeIn(700, function(){
              
              animaImagemFull_new();
              
              if(!$.browser.chrome) {
                jQuery(".imagem_fundo, .panFullSize, .divAvolta, .imagemPannel, .imagemPannel img").css("cursor", "url(sysimages/blank.cur),none");
              } else {
                jQuery(".imagemove").hide();
                jQuery(".imagem_fundo, .panFullSize, .divAvolta, .imagemPannel, .imagemPannel img").css("cursor", "move");
              }
              
              
              
            });       
            
            
            alt1 = jQuery(window).height() - (jQuery(".topo").outerHeight(true) + jQuery(".footer").outerHeight(true) + 35);
            alt2 = jQuery(".imageNone").height();
            if(alt1 >= 522 && alt1 <= alt2) {
              jQuery(".imagem_fundo, .panFullSize, .divAvolta, .imagemPannel").height(alt1);
            }
            
            if ( exists ) {
                return;
            }
            
            var temp_ = '<div class="baseHotspot"></div>';
            
            pan.append("<div class='divAvolta'></div><div class='imagemPannel'>"+temp_+"<img src='" + pic.attr("src") + "'/></div>");
            
            
            
            var pic_orig_width = pic.width();
            pic_orig_height = pic.height();
            
            if(alturaDivfull == 0) {
              alturaDivfull = pic.height();
            }
            
            
            pic.removeAttr("width");
            pic.removeAttr("height");
            pic_real_width = pic.width();
            pic_real_height = pic.height();
            pic.width(pic_orig_width).height(pic_orig_height);



            pan.mousedown(function(e){
                e.preventDefault();
                
                window.clickImagem = 1;
                
                mousedown = true;
                clearInterval(intervaloCatalogo);
                                
                if(jQuery.browser.msie == false) {
				          $('div.imagemove').css('background', 'url(sysimages/move_sel.png) no-repeat left center');
				        }
                box_width = pan.width();
                box_height = pan.height();

                picX_start = e.clientX;
                picY_start = e.clientY;
                
                jQuery(".imagemPannel").stop();
                
                
            });
		
		
		
      			pan.mousemove(function(e){
      			  
      				$('div.imagemove').css({
      					top : ((e.pageY) - 3) + 'px',
      					left : (e.pageX + 15) + 'px'
      				});
      				
      			}).mouseenter(function(){
      			
      			 if(jQuery.browser.chrome == false) {
      				  $('div.imagemove').css('display', 'block');
      				}
      				
      			}).mouseleave(function(){
      			
      				$('div.imagemove').css('display', 'none');
      				
      			}); 
      
             
             
            $(document).mousemove(onpan);
    
                $(document).mouseup(function(e){
                    onpan(e);
                    mousedown = false;
            				
            				  $('div.imagemove').css('background', 'url(sysimages/move_unsel.png) no-repeat left center');
            				
                    prevX = newX;
                    prevY = newY;
               
                });
    
                if (afterLoaded) {                
                    afterLoaded();
                }
                
            }
    
    
            if ( !exists ) {
                // new pan-div
                // The space in div is required
                //console.log(pic.height());
                
                pic.after('<div id="pan' + pic.attr("id") +'" class="panFullSize" altura=""></div>');
                pan = getPan().hide();
                // On creating pan-div, we need to wait for the image to load before we can initialize pan-div.
                // Otherwise getting the real image width&heigth fails...
                //pic.load(initialize);
                pic.get(0).complete ? initialize() : pic.load(initialize);
            }
            else {            
                initialize();
            }
    
    
    
    
    
            function onpan(e){
              
                var diffX = e.clientX - picX_start;
                //var diffY = e.clientY - picY_start;
               
                 if ( mousedown ){
                 
                 if(window.activoSlider == 1) {
                      
                      jQuery(".imagemPannel").stop();
                      clearTimeout(intervaloEspera101);
                      
                      var in_areaX = true;
                      var in_areaY = true;
                      var arrstr_new = new Array();
                      
                      
                      str = jQuery(".imagemPannel").css("top");
                      
                      if(jQuery.browser.mozilla == true) {
                        arrstr_new = str.split(".");
                        str_new = arrstr_new[0];
                      } else {
                        str_new = str.substring(0, str.length - 2);
                      }
                      
                      str_int = str_new;
                      str_int++;
                      str_int--;
                      
            
                      if(prevY == 0 && fimanimacaoFull == 999999) {
                        prevY = str_int;
                        var diffY = e.clientY - picY_start;
                      } else if(prevY == 0 && fimanimacaoFull < 0) {
                        prevY = fimanimacaoFull;
                        var diffY = e.clientY - picY_start;
                      }
                      
                      
                      var diffY = e.clientY - picY_start;
                      
                      if ( prevY + diffY >= 0 ){
                          in_areaY = false;
                      }
                      
                      var propriadade = pic_real_height - box_height;
                      
                      if($.client.os == "Mac") {
                        fim = propriadade - 70;
                      } else {
                        fim = propriadade;
                      }
                      
                      
                      if ( -(prevY + diffY) > fim ){
                          in_areaY = false;
                      }
                      
                      
                      if (in_areaY){
                          newY = prevY + diffY;
                      }
                      
                      
                      contador = 1;
                      
                      alturaimg = Math.abs(jQuery(".imagemPannel img").height() - (jQuery(".panFullSize").height() + 20));
                      newYNew = Math.abs(newY);
                      //console.log("alturaimg :: " + alturaimg + " newY :: " + newYNew);
                      if(newYNew < alturaimg) {
                        jQuery(".imagemPannel").css({top: newY + "px"});
                      } else {
                        newY = -alturaimg;
                      }
                    }
    
                 }
    
             }
    
             function getPan(){
                 return pic.next("div.panFullSize");
             }
    
      
        });
        
        //jQuery(".panFullSize").fadeIn(500);
        
        //var filhos = jQuery(".imagem_fundo").children("div");
        //jQuery(filhos[0]).css("display", "block");
    
        // Lets return the new pan-divs    
        return $(this).next("div.panFullSize");


};

 

/**
 * Restores normal image view
 */
jQuery.fn.normalView = function(){
 this.each(function(){

    if ( $(this).is("div.panFullSize") ){
      $(this).hide();
      $(this).prev("img").show();
    }
    else if ( $(this).is("img") && $(this).next("div.panFullSize").is("*") ) {
      $(this).show();
      $(this).next("div.panFullSize").hide();
    }

 });


if ( $(this).is("div.panFullSize")  )
    return $(this).prev("img");
return $(this);

};

