// rozmiary zdjęcia dla galerii publikacji img.pmi
var width_max = 500; 
var height_max = 340;



function replaceImage(hash, imageNr)
{ 
  if (active_hash < 0)
  {
	  var select = "div#pi"+hash+" #publication_main_image img.pmi";  //publications image
	  var width_max = 500; 
	  var height_max = 340;
  }
  else
  {
	  var select = "div.full_gallery div.image img.pmi"; // full gallery image
	  var width_max = 900; 
	  var height_max = 600;
  }
  
  var key = 'pit' + hash;
  path = pub_image_tab[key]['el'][imageNr].file;
  $(select).attr('src', path);  
  
  //if (active_hash < 0)
  
	  //$(select).width("auto").height("auto");
	  var w = pub_image_tab[key]['el'][imageNr].nw;
	  var h = pub_image_tab[key]['el'][imageNr].nh;
	  
	  var ratio = w / h; // 600/900 = 0,66
	  var hres = Math.round(width_max / ratio);
	  var wres = Math.round(height_max * ratio);
	  
	  var dw = w / width_max;
	  var dh = h / height_max;
	  
	  //if (dw > 1 && dh > 1)
	  if (1)
	  {
		  if (dw > dh)
		  {
			  $(select).width(width_max).height(hres);
		  }
		  else
		  {
			  $(select).height(height_max).width(wres);
		  }
		  
	  }
	  else if (dw > 1)
	  {
		  $(select).width(width_max).height(hres);
	  }
	  else if (dh > 1)
	  {
		  $(select).height(height_max).width(wres);
	  }
		  
    
  
  check_arrow(hash, imageNr);
}

function correct_dim()
{
	  if (active_hash < 0)
	  {
		  var select = "div#pi"+hash+" #publication_main_image img.pmi";  //publications image
	  }
	  else
	  {
		  var select = "div.full_gallery div.image img.pmi"; // full gallery image
	  }
  
	  
	  if (active_hash < 0)
	  {
		 // $(select).width("auto").height("auto");
		  var w = $(select).width();
		  var h = $(select).height();
		  
		  var dw = w - width_max;
		  var dh = h - height_max;
		  
		  if (dw > 0 && dh > 0)
		  {
			  if (dw > dh)
			  {
				  $(select).width(width_max);
			  }
			  else
			  {
				  $(select).height(height_max);
			  }
			  
		  }
		  else if (dw > 0)
		  {
			  $(select).width(width_max);
		  }
		  else if (dh >0)
		  {
			  $(select).height(height_max);
		  }			  
	  }	
}

// images are loaded asynchronously with no delay
var db = new Array();

function preloading(url)
{
	db.push(url);
	return;
  $.get(url, function(data) {
    var i=new Image();
    i.src = data;    
  });
} 


/*

 	
										        {foreach from=$publication.Elements item=image}
										        	tab[tab.length] = '{$r}module_files/publications/{$image.id}/{$image.publication_id}/full/{$image.file}'; 
										          preloading('{$r}module_files/publications/{$image.id}/{$image.publication_id}/full/{$image.file}');							           							          
										        {/foreach}
										          {assign var=image value=$publication.Elements[0]}
										          //replaceImage('{$r}module_files/publications/{$image.id}/{$image.publication_id}/full/{$image.file}');
										          pub_image_tab['pit{$publication.hash}'] = tab;
										          
										          
*/
