﻿var slideNum = 0;
var repeat = 5000;
var pause = false;
var pausePic = new Image();
var playPic = new Image();
function SlideShow(options)
{
  var reset = false;
  var move = 'next';
  
  if (typeof options.data != 'undefined')
  {
    if (typeof options.data.reset != 'undefined')
    {
      reset = options.data.reset;
    }
    if (typeof options.data.pause != 'undefined')
    {
      pause = options.data.pause;
    }
    if (typeof options.data.move != 'undefined')
    {
      move = options.data.move;
    }
  }
  
  if (reset === true && pause === false)
  {
    RunTimer(false);
    reset = false;
    RunTimer(true);
  }
  
  $.post(
      'http://www.e-motion.ru/scripts/slideshow.html',
      {album:album, slideNum:slideNum, move:move},
      function (result)
      {
          if (result.type == 'error') {return(false);}
          else
          {
            // копируем текущую картинку в нижний слой
            $('#photoCopy').remove();
            var offset = $('#photo').offset();
            var w = $('#photo').width();
            var h = $('#photo').height();
            var copy = $('#photo').clone().attr('id', 'photoCopy').css({ 	position: 'absolute', zIndex: 1, width: w, height: h}).prependTo('#photoBg');
            
            // прячем текущую картинку и подгружаем новую
            $('#photo').hide();
            $('#photo').attr('src', result.slide.link);
            slideNum = result.slide.num;
          }
      },
      "json"
  );
}
function RunTimer(enable)
{
  if (enable === true)
  {
    $(document).everyTime(repeat, 'slideTimer', SlideShow);
  }
  else
  {
    $(document).stopTime('slideTimer');
  }
}
function ShowPause(enable)
{
  if (enable)
  {
    if (pause)
    {
      $('#pause img').attr('src', playPic.src);
    }
    else
    {
      $('#pause img').attr('src', pausePic.src);
    }
    $('#pause').css({'display':'inline', 'position':'absolute', 'left':'560px', 'top':'365px'});
  }
  else
  {
    $('#pause').css({'display':'none'});
  }
}
function Pause(e)
{
  var x = e.pageX;
  var y = e.pageY;
  var s = $('#photo').offset();
  var w = $('#photo').width();
  var h = $('#photo').height();
  
  ShowPause((x >= s.left) && (x <= s.left + w) && (y >= s.top) && (y <= s.top + h));
}
$(document).ready(function(){
  pausePic.src = '/i/pause.png';
  playPic.src = '/i/play.png';
  $('#photoBack').bind('click', {move:'back', reset:true}, SlideShow);
  $('#photoNext').bind('click', {move:'next', reset:true}, SlideShow);
  $('#photo').load(function() {$('#photo').fadeIn('slow');});
  $(document).mousemove(Pause);
  $('#ph').click(function() {pause = !pause; RunTimer(!pause); ShowPause(true);});
  $('#ph2').click(function() {pause = !pause; RunTimer(!pause); ShowPause(true);});
  $('a#portfolio').click(function() {$('#portfolio').css('visibility', 'hidden'); $('#gallery').css('display', 'block'); return false;});
  $('a#portfolioHide').click(function() {$('#portfolio').css('visibility', 'visible'); $('#gallery').css('display', 'none'); return false;});
  $('#gallery ul a').click(function() {$('#portfolio').css('visibility', 'visible'); $('#gallery').css('display', 'none'); album = $(this).text(); $('#gallery li').each(function() {$(this).css('list-style-type', 'none');}); $(this).parent().css('list-style-type', 'disc'); $('#photoNext').click(); return false;});
  $('#photoNext').click();
});
$(document).everyTime(repeat, 'slideTimer', SlideShow);
