function imageOpacityOff() {
  $("#bigImage").animate({opacity: 1}, 500 ); 
};
function imageOpacityOn() {
  $("#bigImage").animate({opacity: 0.6}, 500 ); 
};    

function openThumbs() {
  closeContent();
  imageOpacityOn();
  $('#thumbs').fadeIn();
};
function closeThumbs() {
  imageOpacityOff();
  $('#thumbs').fadeOut(); 
};
function toggleThumbs() {
 if ( $('#thumbs').is(':visible') ) {
  closeThumbs();  
 } else if ( $('#thumbs').is(':hidden') ) {
  openThumbs();  
 } 
};

function openContent(element_index) {
 if ($("#content div").eq(element_index).is(':hidden')) {
   contentresize();
   $('#content').slideDown();
   if ( $('#thumbs').is(':visible') ) {
    closeThumbs();
   }

   $("#content div").eq(element_index).delay(300).fadeIn();   
   
 } else { 
  closeContent(element_index); 
 }
};

function closeContent(element_index) {
   $('#content').slideUp();
   $("#content div").fadeOut();          
};

function imageresize() {
	var containerHeight = $('#sizer').height();
   $('#sizer').text(containerHeight);	
  if ((containerHeight) < '580') {
	    $('#bigImage').css('height','330px');
	} else if ((containerHeight) < '620') {
	    $('#bigImage').css('height','370px');
	} else if ((containerHeight) < '670'){
	    $('#bigImage').css('height','420px');
	} else if ((containerHeight) < '720'){
	    $('#bigImage').css('height','470px');
	} else {
	    $('#bigImage').css('height','520px');
	}
}

function contentresize() {
  // dostosowanie wysokosci slidera z contentem do wysokosci zdjęcia
  var imageHeight = $("img#bigImage").height();
  var imageWidth = $("img#bigImage").width();    
  $("#content").css('height',imageHeight);
  $("#content").css('width',imageWidth+20);		  			
}

function resizer() {
  // Zaraz po załadowaniu ustaw rozmiary
	imageresize();	
  contentresize();
  // Ustawiaj wymiary podczas zmiany rozmairu okna
	$(window).bind("resize", function(){
		imageresize();
    contentresize();		
	});
};

jQuery(document).ready(function() {

  // jCarousel
  jQuery('#mycarousel').jcarousel({
      // Uncomment the following option if you want items
      // which are outside the visible range to be removed
      // from the DOM.
      // Useful for carousels with MANY items.
      scroll: 11,
      visible: 11
  });

  // zmiana rozmiaru zdjęcia oraz okienka z terscia zakładek tekstowych
  resizer();	

  // Po najechaniu na duze zdjecie wymusza nieprzezroczystość
  $("#bigImage").hide();   
  $("#bigImage").fadeIn('slow'); 

  // Pasek z miniaturami domyślnie ukrywamy
  //$('#thumbs').hide(); 


  // Pokazuje i chowa minaitury, jednoczensie dodając do duzego zdjęcia przezroczystość
  $('#iconThumbs').click( function() { toggleThumbs(); return false; } );   

  // Po najechaniu na duze zdjecie wymusza nieprzezroczystość
  $("#bigImage").hover( function() { imageOpacityOff(); } ); 

  // Po najechaniu msyzka na miniature przyciemnienie
  $(".jcarousel-skin-simple .jcarousel-item img").hover( function() {
    $(".jcarousel-skin-simple .jcarousel-item").css('background', '#ffffff');
    $(this).css('background', 'none');
    $(this).animate({opacity: 0.6}, 300 ); 
   }, function() {
    $(this).animate({opacity: 1}, 300 );
   } 
  ); 

  // Obsluga stron tekstowych
  $("a.tabSwitch").click(function()
	 {
		var element_index = $("a.tabSwitch").index(this);
		if ($("#content div").eq(element_index).is(':hidden')) $("#content div:visible").ready(function() { closeContent(element_index); } );
		$("#content div").ready(function() { openContent(element_index); } );
		return false;
	});

  // Pokazuje i chowa minaitury, jednoczensie dodając do duzego zdjęcia przezroczystość
  $('#bigImage').click( function() { closeContent(); } );   

});

