var flash = null;
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preload = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)



$(document).ready(function(){
    jQuery.preload(
      '/images/mask_foto_open.png',
      '/images/mask_foto_hover.png'
    );

    $('#sitemap').hover(
    function(){
      $('#sitemap').find('.bg_img').addClass('hover');
      $('#sitemap').find('a').addClass('hover');
    },
    function(){
      $('#sitemap').find('.bg_img').removeClass('hover');
      $('#sitemap').find('a').removeClass('hover');
    });

    
    if ($('#mainpage_banner').get(0))
    {
      $('#mainpage_banner .text').verticalCentrify();

      var toggle_speed = 500;
      $('#mainpage_banner .text .js_link').click(function(){
        if ($(this).next().is('span')) $(this).next().toggleMainpageFlashText(toggle_speed, $(this), function(){$('#mainpage_banner .text').verticalCentrify(toggle_speed);});
      });


      var popup_speed = 500;
      $('ul.menu_l1 li').hover(
        function(){
          $(this).find('a').addClass('pink');
          var popup = $(this).find('.popup');
          var popup_i = popup.find('i');
          if (popup.is(':visible')) return false;
          var index = _random(popup_i.length);
          popup_i.eq(index).show();
          popup.fadeIn(popup_speed);
        },
        function(){
          $(this).find('a').removeClass('pink');
          $(this).find('.popup').fadeOut(popup_speed, function(){$(this).find('i').hide();});
        }
      );

    }

    $('.interest .js_link').click(function() {
      var speed = 200;
      var obj = $(this).parent().find('.text');
      obj.fadeOut(speed);
      $('.interest .text').load($(this).attr('url'), function(){
        obj.fadeIn(speed);
      });
    });


  var prev_item = null;
  var animating = false;

  $('.persons .item').click(function(){
    if (animating) return;
    if ($(this).parents('.simple').get(0)) return;

    var speed = 250;
    var obj = $(this);

    animating = true;

    if (prev_item==null) $(this).parents('.persons_container').addClass('active');
    if ($(this).is('.open')) {
      if ($('.row2').is('.low')) $('.row2').switchClass('low', '', speed);
      $('.description').fadeOut(speed);
      $(this).hidePhoto(speed, function(){
        obj.parents('.persons_container').removeClass('active');
        prev_item=null;
        animating = false;
      });
      return;
    }


    if ($(this).parent().is('.row2') && prev_item && prev_item.parent().is(':not(.row2)'))
      $('.persons_container .row2').switchClass('low', '', speed);

    if (
      ($(this).parent().is(':not(.row2)') && (prev_item==null))
      ||
      ($(this).parent().is(':not(.row2)') && prev_item && prev_item.parent().is('.row2'))
      )
      $('.persons_container .row2').switchClass('', 'low', speed);
    if (prev_item) prev_item.hidePhoto(speed, function(){obj.expandPhoto(speed, function(){animating = false;});})
    else
    obj.expandPhoto(speed, function(){animating = false;});
    obj.showText(speed);
    prev_item = obj;

  });

  $('.persons_view_toggler.js_link').click(function(){
    var obj = $(this);
    var speed = 200;
    
    $('.persons_container:first').fadeOut(speed, function(){
      obj.find('span').toggle();
      $(this).toggleClass('simple');
      $(this).find('.description').toggle();
      $(this).fadeIn(speed);
//      $('.footer').hide();
//      $('.footer').show();
    });
  });

  $('#question_form').each(function(){
    scroll_to_form();
    $('#flash_notice').each(function(){
      $('#form_block').hide();
      $('#one_more_message').click(function(){
        $('#flash_notice').hide();
        $('#form_block').show();
      });
    });
  });

  $('.expandable .js_link').click(function(){
    $(this).expandText(200, $(this).parents('.expandable'));
  })

});

_random = function(b) {
  return Math.floor(b*Math.random());
}

function scroll_to_form() {
  var destination = $('#form_block').offset().top;
  $("html:not(:animated),body:not(:animated)").animate({scrollTop: destination-20}, 500 );
}


jQuery.fn.extend({
  inlineSlideToggle: function(speed, callback){
    var obj = this;
    var width = obj.outerWidth();
    obj.before("<i></i>");
    if (obj.is(':hidden')) {
      obj.prev().animate({paddingLeft: width+'px'}, speed, 'swing', function(){
        obj.prev().remove();
        obj.fadeIn(speed, callback);
      });
    }
    else {
      obj.fadeOut(speed, function(){
        obj.prev().css('padding-left', width+'px');
        obj.prev().animate({paddingLeft: '0px'}, speed, 'swing', function(){
          obj.prev().remove();
          if (typeof callback == 'function') callback();
        });
      });
    }
  }
});

jQuery.fn.extend({
  showText: function(speed, callback){
    var obj = this;
//    var description = $('#description');
    $('.persons_container .description').fadeOut(speed, function(){
      if (obj.is('.slava, .maxim, .anna2, .elena, .ekaterina, .julia')) $(this).addClass('wide');
//      if (obj.is('.anna2')) $(this).addClass('x_wide');
      else $(this).removeClass('wide x_wide');
      $(this).css('top', obj.position().top+obj.parent().position().top+obj.height()+40+'px');
//console.log(obj.offset().left);
      var left = (obj.offset().left>($(this).width()+30))?(obj.position().left - $(this).width() - 15):(obj.position().left+215);
      $(this).css('left', left+'px');
      $(this).html(obj.find('.text').html());
      $(this).fadeIn();
    });
  }
});

jQuery.fn.extend({
  expandPhoto: function(speed, callback){
    this.animate({width: '200px'}, speed, 'linear', function(){
      $(this).addClass('open');
      $(this).removeAttr('style');
      $(this).find('i i').spriteAnimation({
        numberOfLoops: 0,
        endFrame: 15,
        interval: speed/6,
        onStop: function(){
          if (typeof callback == 'function') callback();
        }
     });
    });
    this.find('img').switchClass('', 'img_open', speed);
    this.find('.img').switchClass('', 'img_open', speed);
  },
//  expandPhoto: function(speed, callback){
//    this.addClass('open');
//    var obj = this;
//    this.find('img').switchClass('', 'img_open', speed, function(){
//      $(obj).find('i i').spriteAnimation({
//        numberOfLoops: 0,
//        endFrame: 15,
//        interval: speed/6,
//        onStop: function(){
//          if (typeof callback == 'function') callback();
//        }
//
//      });
//    });
//    this.find('.img').switchClass('', 'img_open', speed);
//  },
  hidePhoto: function(speed, callback){
    var obj = this;
    
    this.find('.img').switchClass('img_open', '', speed/2);
    obj.animate({width: '130px'}, speed/2);
    this.find('img').switchClass('img_open', '', speed/2, function() {
      obj.removeClass('open');
      obj.removeAttr('style');
      obj.find('i i').css('top', '0px');
      if (typeof callback == 'function') callback();
    });
  }
});

jQuery.fn.extend({
  toggleMainpageFlashText: function(speed, envoker, callback){
    if (envoker.is('.suicide')) {
      var obj = this;
      var width = obj.outerWidth();
      var envoker_width = envoker.outerWidth();
      envoker.fadeOut(speed, function(){
        obj.before('<i style="padding-left:'+envoker_width+'px"></i>');
        obj.prev().animate({paddingLeft: width+'px'}, speed, 'swing', function(){
          obj.prev().remove();
          obj.fadeIn(speed, callback);
//alert($('#mainpage_banner .text').html());
        });

      });

    }
    else this.inlineSlideToggle(speed, callback);
  }
,

  verticalCentrify: function(speed) {
    var height = this.innerHeight();
    var parent_height = this.parent().innerHeight();
    var _speed = (speed)?speed:0;
    this.animate({top: ((parent_height-height)/2)+'px'}, _speed);
  }
});

jQuery.fn.extend({
  expandText: function(speed, envoker, callback){
    var p = envoker.find('p');
    if (p.is(':hidden'))
      p.fadeIn(speed, callback);
    else
      p.fadeOut(speed, callback);
  }
,

  verticalCentrify: function(speed) {
    var height = this.innerHeight();
    var parent_height = this.parent().innerHeight();
    var _speed = (speed)?speed:0;
    this.animate({top: ((parent_height-height)/2)+'px'}, _speed);
  }
});

