﻿/**

 **/
//http://jqueryfordesigners.com/image-loading/
var first_run=1;
var curr_podpis='';
function show_img(myanchor, abs_path,iwidth,iheight,podpis) {
	var cur_img;//used to test if the previous img was set
	curr_podpis=podpis;
	$('#loader').empty().addClass('loading');
	$('.mythumb').addClass('tmpclass');
	$(myanchor).removeClass('tmpclass');
	$(myanchor).fadeTo('slow', 1.0);
	$('.tmpclass').fadeTo('slow', 0.7);
	
	container_width=$('#loader').width();
	new_h=iheight;//image height is calculated depending on fixed width
	new_w=iwidth;
	//calculate only if the source image width is wider than container
	if(iwidth>0 && iwidth>container_width){
		new_h=Math.round(container_width*iheight/iwidth);
		new_w=container_width;
	}
	//only for the first run set the height earlier to avoid very small top img height
	if(first_run==1){
		$('#loader').css({'height':new_h});
		first_run=0;
	}else{
		//$('#loader').animate({height:new_h});
	}
	//fadeout the previous(here still the current) image
	if(cur_img!= undefined){$(cur_img).fadeOut();}
	$('#big_podpis').empty().append('&nbsp;');//clear the title
	
	
	var img = new Image();
	// wrap our new image in jQuery, then:
	$(img)
    // once the image has loaded, execute this code
    .load(function () {
		cur_img=$(this);
		
		//use the delay to see the loader
		$(this).oneTime(mydelay,'noname', function() {
			// set the image hidden by default    
			$(this).hide();    
			// with the holding div #loader, apply:
			$('#loader')
				// remove the loading class (so no background spinner), 

				// then clear and insert our image
				.empty().append(this);
				$('#loader').animate({height:new_h});
				//$('#loader').css({'height':new_h});
				$('#loader').removeClass('loading');
				$('#big_podpis').empty().append(podpis).fadeIn();
			// fade our image in to create a nice effect
			$(this).fadeIn();
		});
    })    
    // if there was an error loading the image, react accordingly
    .error(function () {
		$('#big_podpis').empty().append(curr_podpis).fadeIn();
		$('#loader')
			.removeClass('loading')
			.empty().append('<p>Brak większego obrazka</p>')
			.animate({height:$('#loader p').height()+$('#big_podpis').height()});
      // notify the user that the image could not be loaded
    })
    // *finally*, set the src attribute of the new image to our image
    .attr('src', abs_path).attr('width', new_w).attr('height', new_h);
	//expand to full only if the source width is wider than container
	if( iwidth>container_width)
	{
		$(img)
		.attr('title','Kliknij aby powiększyć')
		.addClass('highslide')
		.css('cursor','pointer')
		.click( 
		function(){
		return hs.expand(this,{src: abs_path,wrapperClassName: 'wide-border', captionOverlay: {position: 'rightpanel'}});
		}
		);
	}else{
		$(img)
		.attr('title','Nie ma powiększenia');
	}
}

