$(document).ready(function() {
  
  document.body.splashgallery_activeid = 1 ;
  document.body.splashgallery_autoscroll_interval = 6000;
  document.body.splashgallery_autoscroll_block_interval = 18000;
  document.body.splashgallery_autoscroll_animating = false;
  
  document.body.splashgallery_autoscroll_queue = -1;
    
  document.body.splashgallery_autoscroll_block_timeout = 0;
  setTimeout('gallery_next_auto()', document.body.splashgallery_autoscroll_interval);

  $('.property_text h2:first, .property_text h3:first').animate({opacity: 0.0}, function() {
    $('.property_text h2:first, .property_text h3:first').css('display', 'block');
    $('.property_text h2:first, .property_text h3:first').animate({opacity: 1.0});      
  });
    
});

function gallery_select(id) {
  
  if(id == document.body.splashgallery_activeid)
    return;
    
  if(document.body.splashgallery_autoscroll_animating) {
    if(document.body.splashgallery_autoscroll_queue == -1)
      document.body.splashgallery_autoscroll_queue = id;    
  
    return;
  }
  else
    if(document.body.splashgallery_autoscroll_queue != -1)
      document.body.splashgallery_autoscroll_queue = -1;  
    
  document.body.splashgallery_autoscroll_block_timeout = document.body.splashgallery_autoscroll_block_interval;
   
  _gallery_select(id);  
}

function _gallery_select(id) {

  if(document.body.splashgallery_autoscroll_animating)
    return;

  $('a').blur();
             
  document.body.splashgallery_autoscroll_animating = true;
  
  document.body.splashgallery_activeid = id;
    
  //$('.property_main_photo_container').css('background-image', 'none');
  //$('.property_main_photo_container').css('background-image', 'url(' + $('#thumb_' + id + ' img:first').attr('src') + ')');
  
  $('.property_main_photo_container').css('background-image', 'url(' + $('#property_main_photo').attr('src') + ')');

  $('#property_main_photo').hide();

  $('#property_main_photo').attr('src', $('#thumb_' + id + ' img:first').attr('src'));
  
  $('#property_main_photo_link').attr('href', $('#thumb_' + id + ' a:first').attr('href')); 
       
  $('.property_text h2:first, .property_text h3:first').fadeOut(function() {
                 
    $('#property_main_photo').fadeIn(function() {
     
      $('.property_text h2:first').html($('#thumb_' + id + ' #case_category:first').html());
      $('.property_text h3:first').html($('#thumb_' + id + ' #case_title:first').html());
      $('.property_text h2:first, .property_text h3:first').fadeIn();
      
      $('.property_right .property_thumbnails img').each(function() {
        if($(this).attr('src') == $('#thumb_' + id + ' img:first').attr('src')) {
          $(this).addClass('active');          
          //$('a').blur();
        }
        else
          $(this).removeClass('active');
                
        if($(this).hasClass('thumb_caption')) {
          if($(this).attr('src').substring(31, 32) == id)
            $(this).attr('src', $(this).attr('src').substring(0, 32) + 'a.png');
          else
            $(this).attr('src', $(this).attr('src').substring(0, 32) + '.png');
        }
      });      
      
      document.body.splashgallery_autoscroll_animating = false;
      
      if(document.body.splashgallery_autoscroll_queue != -1) {            
        if(!document.body.splashgallery_autoscroll_animating == 'next')
          gallery_next();
        else
          gallery_select(document.body.splashgallery_autoscroll_queue);
      }
    });
   }); 
}

function gallery_next() {
  
  if(document.body.splashgallery_autoscroll_animating) {
    if(document.body.splashgallery_autoscroll_queue == -1)
      document.body.splashgallery_autoscroll_queue = 'next';    
  
    return;                                                                         
  }
  else
    if(document.body.splashgallery_autoscroll_queue != -1)
      document.body.splashgallery_autoscroll_queue = -1;
  
  document.body.splashgallery_autoscroll_block_timeout = document.body.splashgallery_autoscroll_block_interval;
   
  _gallery_next();  
}

function gallery_next_auto() {
  
  if(!document.body.splashgallery_autoscroll_animating && document.body.splashgallery_autoscroll_block_timeout <= 0)
    _gallery_next();
  else
    document.body.splashgallery_autoscroll_block_timeout -= document.body.splashgallery_autoscroll_interval;
      
  setTimeout('gallery_next_auto()', document.body.splashgallery_autoscroll_interval);    
}

function _gallery_next() {

  if(!document.body.splashgallery_activeid)
    document.body.splashgallery_activeid = 1;
    
  document.body.splashgallery_activeid++;
  
  if(document.body.splashgallery_activeid > 4)
    document.body.splashgallery_activeid = 1;
    
  _gallery_select(document.body.splashgallery_activeid, true); 
}


function highlight(img) {
  $(img).attr('src', '/Resource/Images/' + $(img).attr('id') + '_h.png');
}

function unhighlight(img) {
  $(img).attr('src', '/Resource/Images/' + $(img).attr('id') + '.png');
}


