//jsGaleria
function explode(delimiter,item) {
  alert(delimiter+'\n'+item);
  tempArray=new Array(1);
  var Count=0;
  var tempString=new String(item);
  alert(tempString);
  while (tempString.indexOf(delimiter)>0) {
    alert(tempString);
    tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
    tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
    Count=Count+1;
  }
  tempArray[Count]=tempString;
  return tempArray;
}

function imageSwitch(obiekt,indeks,indexlast,myHeight,myWidth){
  var imageSrc=$('#a'+indeks).attr('href');
  $('#'+obiekt).remove();
  $('#fota-full-p').prepend('<img id="'+obiekt+'" src="'+imageSrc+'" alt="" title="Kliknij aby zamknąć"/>');
  $('#'+obiekt).load(function(){ imageFit(indeks,indexlast,myHeight,myWidth); });
  $('#fota-full-info').width(0).hide();
  $('#'+obiekt).hide();
}
function imageFit(indeks,indexlast,myH,myW){
  indexlast++;
  var imageSrc=$('#fota-full-img').attr('src');
  var tmpinfo=$('#p'+indeks).attr('alt');
  var myRatio=myW/myH;
    //skalowanie foty
  var fotaH=$('#fota-full-img').height(); var fotaW=$('#fota-full-img').width();
  var tmpww=0;
  var tmphh=0;
  if(myH<=myW){
    if(fotaW>=myW-100 || fotaH>=myH-100){
      tmphh=myH-100;
    }else{
      tmphh=fotaH;
    }
    tmpww=Math.round((fotaW*tmphh)/fotaH);
  }else{
    if(fotaH>=myH-100 || fotaW>=myW-100){
      tmpww=myW-100;
    }else{
      tmpww=fotaW;
    }
    tmphh=Math.round((fotaH*tmpww)/fotaW);
  }

  $('#fota-full-img').height(tmphh);
  $('#fota-full-img').width(tmpww);
  
  $('#fota-full-p').css({
    "width":(tmpww+40)+'px',
    "height":(tmphh+40)+'px',
    "margin-top":'-'+Math.round((tmphh+40)/2)+'px',
    "margin-left":'-'+Math.round((tmpww+40)/2)+'px'
  });
  
  $('#fota-full-info').html((indeks+1)+'/'+indexlast+' - '+tmpinfo+'<img id="full-fota-info-fade" src="'+_mainURL+'/rg-engine/full-info-fade.png" alt=""//>').width(200).fadeIn('slow');
  $('#fota-full-close').width(24).fadeIn('slow');
  
  $('#fota-full-img').fadeIn('slow',function(){
    $('#fota-full-p').hover(function(){
      $('#fota-full-info').stop();
      $('#fota-full-info').animate({width:"200"},1000);
      $('#fota-full-close').stop();
      $('#fota-full-close').animate({width:"24"},1000);
    },function(){
      $('#fota-full-info').stop();
      $('#fota-full-info').animate({width:"0"},1000);
      $('#fota-full-close').stop();
      $('#fota-full-close').animate({width:"0"},1000);
    });
  });    
  
//  alert('fotaHW '+fotaW+' x '+fotaH+'\nnowe: '+tmpww+' x '+tmphh+'\n      '+$('#fota-full-img').width()+' x '+$('#fota-full-img').height());
  
}

$(document).ready(function(){
  
  var indexthis = -1;
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
  //!IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  //IE 6+
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  //IE 4
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  $(window).resize(function(){
    //zczytanie rozmiaru okna
    if( typeof( window.innerWidth ) == 'number' ) {
  //!IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  //IE 6+
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  //IE 4
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
    }
    //zmiana rozmiaru div#fota-full
    $('body').find('#fota-full').css({'width':myWidth+'px','height':myHeight+'px'});
//    alert(myHeight+' '+myWidth);
    if(indexthis!=-1) imageFit(indexthis,myHeight,myWidth);
  });
// pobranie parametru # z URLa
//   var hrefIdInd = window.location.href.search(/#/);
//   var hrefId = hrefIdInd!=-1?window.location.href.substr(hrefIdInd+1):'';
// -------------------------

// ladowanie
  $('body').ajaxStart(function(){
    $('#galeria').append('<div id="loading"><img id="galeria-loading" src="rg-engine/27-1.gif" alt="Trwa ładowanie..."/></div>');
  });
  $('body').ajaxStop(function(){
    $('#loading').remove();
  });


  $('.photo-a').click(function(){

    var thisphotoaid=$(this).attr('id');
    indexthis=parseInt(thisphotoaid.substr(1));
    var indexlast=$('.photo-a:last').attr('id').substr(1);

    $('#zegarek').css('visibility','hidden');
    $('#fota-full').empty();
    $('#fota-full').remove();
    $('body').append('<div id="fota-full"><p id="fota-full-p"><img id="fota-full-img" style="display:none;" src=""/><span id="fota-full-info" style="display:none;"></span><span id="fota-full-close" style="display:none;"><a href="" title="Zamknij"></a></span><span id="fota-full-navi"><a href="" id="fota-full-a-prev">&laquo;</a> <a href="" id="fota-full-a-zoom">Powiększ</a> <a href="" id="fota-full-a-next">&raquo;</a></span></p></div>');

   // $('#fota-full-img').attr('src',$(this).attr('href')); 
    //$('#fota-full-img').load(function(){ imageFit(indexthis,indexlast,myHeight,myWidth); });
      imageSwitch('fota-full-img',indexthis,indexlast,myHeight,myWidth);
    //---------------

    $('#fota-full-close a').click(function(){
      $('#fota-full').empty();
      $('#fota-full').remove();
      $('#zegarek').css('visibility','visible');
      return false;
    });
    
    $('#fota-full-a-zoom').click(function(){
      $(this).attr({href:$('#fota-full-img').attr('src'),target:'_blank'});
    });

    $('#fota-full-a-prev').click(function(){
      indexthis-= indexthis==0?-indexlast:1;
      $('#fota-full-img').fadeOut('slow');
      imageSwitch('fota-full-img',indexthis,indexlast,myHeight,myWidth);
      return false;
    });

    $('#fota-full-a-next').click(function(){
      indexthis+=indexthis==indexlast?-indexlast:1;
      $('#fota-full-img').fadeOut('slow');
      imageSwitch('fota-full-img',indexthis,indexlast,myHeight,myWidth);
      return false;
    });

    return false;
  });


});