//Globals
var path;
var base_path;
var pid;
var default_image;
var get_directory;
var widgets;
var fetch;
var loader_image;
var randomSeed;
var gallerix_settings;
var gallerix_loaded;
var gallerix_grid_loaded;
var gallerix_mode;

$(document).ready(function(){

  path = window.location.href;
  base_path = path.split('#')[0]; //We always want to know the base node path, so we can tack on anchors.
  pid = path.split('#')[1]; //We want to know what pid the user provided, if any.
  

  //Load in the settings.
  gallerix_settings = Drupal.settings['gallerix'];
  default_image = gallerix_settings['default'];
  get_directory = gallerix_settings['get'];
  gallerix_loaded = false;
  gallerix_grid_loaded = false;
  gallerix_mode = 'viewer';
      
  loader_image = $('#gallerix-loader').css('background-image'); 
  randomSeed = "";
  
  
  
  gallerix_initialize();
  
   
  fetch = function (data) {
    var image = Drupal.parseJson(data);
  
    var thumbnails = image['thumbnails'];
    var thumbnail_count = image['thumbnail_count'];    
  
    //Reset the message box.
    $('#gallerix-message').html('');
    
    var load_image = function () {
      //var height = parseInt(image['height']) + 10;
      
      $('#gallerix-display').fadeTo(500, 0.01, function() {
        $('#gallerix-full').attr('src', image['path'] + randomSeed);
        $('#gallerix-full').attr('height', image['height']).attr('width', image['width']); 
        $('#gallerix-display').fadeTo(500, 1, function() {      fullContent = $('#gallerix-loader').html();        });   
      });  

            
      $('#gallerix-loader').show().css('background-image', 'none');
      
      var thumbnail_buffer = new Image();
      thumbnail_buffer.src = thumbnails['gallerix-thumbnail-link-' + (thumbnail_count - 1)][1];
      
      
      //$(thumbnail_buffer).load(function () {
      //thumbnail_buffer.onload = function () {
        for (thumbnail in thumbnails) {
          $('#' + thumbnail).attr('href', thumbnails[thumbnail][0]).find('img').attr('src', thumbnails[thumbnail][1] + randomSeed).attr('title', thumbnails[thumbnail][2]);
        } 
      //}         
      //});
      
      pid = image['key'];
      //document.location.href = base_path + '#' + pid;  
      window.location.hash = '#' + pid;
      
      if (gallerix_settings['widgets_enabled']) {
        update_gallerix_widgets(image);
      }
    }
    
          
    image_buffer = new Image(image['width'], image['height']);
    image_buffer.onload = load_image;
    image_buffer.src= image['path'] + randomSeed;
    

    
    gallerix_attach_events(image);
  }
  
  
  
  gallerix_activate_links();
  
  if (gallerix_settings['view'] == 'grid' && !pid) {
    gallerix_switch_modes();
    gallerix_fetch_picture(get_directory + (pid ? pid : default_image));    
  }
  else {

    $('#gallerix-viewer').show();
    gallerix_fetch_picture(get_directory + (pid ? pid : default_image));    
    
  }
  


  
 
});



function gallerix_initialize() {
  $('#gallerix-grid-link').click(function () {
      
    
    gallerix_switch_modes();

    return false;
  });
}



function gallerix_reload_grid() {
  gallerix_grid_loaded = false;
  
}




function gallerix_attach_events() {
    var ajax_link = function (data) {
      eval(data);
    }

    $('.gallerix-ajax-link').click(function() {
      $.get(this.href, null, ajax_link);
      
      return false;
    });

       

}


function gallerix_activate_links() {
  $('.gallerix-picture-link').each(function() {
      if (!$(this).is('.activated')) {
        $(this).click(function() {

          if (gallerix_mode == 'grid') {
            gallerix_switch_modes(false, true);
          }
          gallerix_fetch_picture(this.href);
          return false;
        });
        
        $(this).css('opacity', .6);
        
        
        
        var mouseover = function () {
          $(this).css('opacity', 1);
          //window.status = 'ok';//$(this).attr('href');
          return true;
        }
        var mouseout = function () {
          $(this).css('opacity', .6);
          return true;
        }       
        
        $(this).mouseover(mouseover);
        $(this).mouseout(mouseout);
        $(this).hover(mouseover, mouseout);
        $(this).addClass('activated');
        
      }
      
      
  });
  
  
}


function gallerix_switch_modes() {
  gallerix_mode = (gallerix_mode == 'viewer') ? 'grid' : 'viewer';
  
  if (!gallerix_grid_loaded) {
    $.get($('#gallerix-grid-link').attr('href'), null, function (data) {
      $('#gallerix-grid').html(data);
      
      $('#gallerix-viewer').hide();
      $('#gallerix-grid').show();
      gallerix_grid_loaded = true;
      
      
      
      if (gallerix_mode == 'viewer') {
        $('#gallerix-grid-link').html('All Photos');
        window.location.hash = "#" + (pid ? pid : "");
          
      }
      else {
        $('#gallerix-grid-link').html('Single Picture');  
        window.location.hash = "#";    
      }
        
      gallerix_activate_links();                     
    
    });
    

    
    
  }
  else {
    $('#gallerix-viewer').toggle();
    $('#gallerix-grid').toggle();
    
    if (gallerix_mode == 'viewer') {
      $('#gallerix-grid-link').html('All Photos');
      window.location.hash = "#" + (pid ? pid : "");
        
    }
    else {
      $('#gallerix-grid-link').html('Single Picture');  
      window.location.hash = "#";    
    }
  }
  


}





function gallerix_fetch_picture(location, refresh) {
  //randomSeed = refresh ? '?rand=' + Math.random() : "";
  
  randomSeed = '?rand=' + Math.random();

  $('#gallerix-loader').css('background-image', loader_image);
  //$('#gallerix-full').attr('height', '').attr('width', '');  
  $('#gallerix-display').fadeTo(500, 0.001);  
  $.get(location, null, fetch);
}




