
$(document).ready(function()
{
  var expandable_photos = new ExpandablePhotos();

  $('.ArrowLeft').click(function(){roll('left')});
  $('.ArrowRight').click(function(){roll('right')});




  $('.WorkContent .thumb').click(function()
      {
//        var thumbName = $(this).attr('src');
//        $('.WorkContent #screenshot').attr('src',thumbName.substr(0, thumbName.length-11) + thumbName.substr(thumbName.length-4));
        $('.WorkContent #screenshot').attr('src',$(this).attr('bigimg'));

      });


  $('.NewsList .News .Title').click(function(){
    if ($(this).parent().hasClass('active')) return false;
    $('.NewsList .News').removeClass('active');
    $(this).parent().addClass('active');
  });


  $('.DateTabs span.link').click(function(){
//    alert('ok');
    if ($(this).parent().hasClass('active')) return false;
    $('.DateTabs .Date').removeClass('active');
    $(this).parent().addClass('active');
    $('#newsWindow').html($(this).find('.Invisible').html());
  });

  $('.expandable').click(function(){
    $(this).toggleClass('selected');
    $(this).parent().next().toggle(200);
  });


  $('.Toggler div[mode]').click(function(){
    if ($(this).hasClass('active')) return false;
    $(this).siblings().removeClass('active');
    $(this).addClass('active');
    $('.Articles>div').hide();
    $('.Articles>div.'+$(this).attr('mode')).show();
  });

  $(window).load(function(){
    tv_resize()

    if (location.hash.indexOf('#')==0) {
      $(location.hash+'anchor').each(function(){
        var destination = $(this).offset().top;
        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
      });
    }
  });

  $(window).resize(function(){
    expandable_photos.resize();
    tv_resize()
  });

});

function tv_resize()
{
  if ($('#TVset')) TVresize();
}

function ExpandablePhotos() {
  var self = this;
  var popup_speed = 300;

  this.images = $('.ExpandablePhotos img');
  this.fader = $('.Fader');
  $('#Layout').append(this.fader);
  this.current_img = 0;
  this.photo_id = new Array();
  this.count = this.images.size();

  this.images.each(function(index){
    self.photo_id[index] = $(this).attr('id');
  });

  this.popup = function(){
    this.fader.fadeIn(popup_speed);
  }

  this.close = function(){
    this.fader.fadeOut(popup_speed);
  }

  this.load_current_img = function(){
    var thumb = $('#'+self.photo_id[self.current_img]);
    var img = $('<img>');
    img.attr("src", thumb.attr('bigimg'));
    img.bind('load', function(){
      $('.Fader .PictureFrame img').replaceWith(img);
      $('.Fader .PicDescr').html(thumb.siblings('div.d').html());
      self.resize();
    });
  }

 this.resize = function(){
//    this.fader.width($(window).width());
//    this.fader.height($(window).height());
    var img = this.fader.find('.PictureFrame img');
    var container = this.fader.find('table:first');
    img.css('height', 'auto');
    img.css('width', 'auto');
    var aspect_ratio = img.width()/img.height();
    if (container.height() > $(window).height()) {
      var new_img_height = img.height() - (container.height()-$(window).height());
      img.height(new_img_height);
      img.width(new_img_height*aspect_ratio);
    }
    if (container.width() > $(window).width()) {
      var new_img_width = img.width() - (container.width()-$(window).width());
      img.width(new_img_width);
      img.height(new_img_width/aspect_ratio);
    }
  }

  this.images.click(function(){
    for (var j=0; j<self.count; j++ ) if (self.photo_id[j] == $(this).attr("id")) { self.current_img = j; break; }
    self.load_current_img();
    self.popup();
  });

  this.fader.find('.LeftArrow').hover(
    function(){$(this).find('.Arrow').css("left", "-48px");},
    function(){$(this).find('.Arrow').css("left", "0px");}
  );

  this.fader.find('.RightArrow').hover(
    function(){$(this).find('.Arrow').css("left", "-72px");},
    function(){$(this).find('.Arrow').css("left", "-24px");}
  );

  this.fader.find('.LeftArrow').click(function(){
    self.current_img = (self.current_img <= 0 )?(self.count-1):(self.current_img-1);
//console.log('%d', self.current_img);
    self.load_current_img();
  });

  this.fader.find('.RightArrow').click(function(){
    self.current_img = (self.current_img >= self.count-1)?0:(self.current_img+1);
//console.log('%d', self.current_img);
    self.load_current_img();
  });

  this.fader.find('.Close').click(function(){
    self.close();
  });

}




