$(document).ready(function(){
  // imageSwitch params
isw = '#tiled_fader_big img';
if (typeof images != 'undefined' && $('#tiled_fader_big').length)
{
  zi = getZindex(isw);
  iswTop = $('#tiled_fader_big').attr('offsetTop');
  iswLeft = $('#tiled_fader_big').attr('offsetLeft');
  
  iswContainer = Array('isw_first', 'isw_second');
  iswFirst = 'isw_first';
  iswSecond = 'isw_second';

  // element to switch next (0/1)
  img2switch = 1;

  // counter to loop through images, if only 2 images are there don't bother counting
  iswCount = images.length > 2 ? 2 : -1;

  // add first image
  $(isw).attr({'src':imgPath+'/'+images[0]});

  // set css for first image
  $(isw).css({'position':'absolute'});

  // 2nd image-container for appending
  var iswApp = '<img src="'+imgPath+'/'+images[1]+'" id="isw_second" style="opacity:1;z-index:'+(zi-5)+';position:absolute;"/>';
  
  $(isw).attr({id:'isw_first'});
  $(isw).parent().append(iswApp);
  
  window.setInterval('imgswitch()', iswTimer);
}  
});

function imgswitch()
{
  // make former hidden image visible
  $('#'+iswContainer[img2switch]).css({opacity:1})
  
  // set which element to switch
  img2switch = img2switch == 1 ? 0 : 1;
  
  // fade out currently shown image
  $('#'+iswContainer[img2switch]).animate({opacity:0},iswSwitchTime,function(){
    // get z-index for first image and set it
    var iswZi = img2switch == 0 ? (zi-10) : zi;
    $('#'+iswContainer[0]).css({'z-index':iswZi});
    // change imagesource of hidden image if more than 2 images are to be switched
    if (iswCount != -1)
      $(this).attr({'src':imgPath+'/'+images[iswCount]});
  });
  
  // set the counter
  if (iswCount != -1)
    iswCount = (iswCount == images.length-1) ? 0 : iswCount+1;
    
  return false;
}

function getZindex(i)
{
  var zfirst = 100;
  
  if ($(i).css('z-index') == 'auto')
    $(i).css({'z-index':zfirst});
  else
    zfirst = $(i).css('z-index');
    
  return zfirst;
}
