//var ratio = 1050 / 1680
//var ratio = 1007 / 1920
var ratio = 900 / 1600
  , $win = $(window)
  , $doc = $(document)
  ;

if (typeof(CYCLE_FX) == 'undefined'){
  // Efecto de cambio entre fotos.
  // Ver más aquí: http://www.malsup.com/jquery/cycle/browser.html
  var CYCLE_FX = 'fade';
}

if (typeof(CYCLE_SYNC) == 'undefined'){
  // Efecto de salida al mismo tiempo que el de entrada?
  var CYCLE_SYNC = false
}

if (typeof(CYCLE_SPEED) == 'undefined'){
  // Velocidad del cambio
  var CYCLE_SPEED = 600 //ms
}

if (typeof(CYCLE_TIMEOUT) == 'undefined'){
  // Tiempo para cambiar automáticamente. 0 para desactivar el cambio automático
  var CYCLE_TIMEOUT = 0 //ms
}

if (typeof(CYCLE_EASING) == 'undefined'){
  // Función de la velocidad del cambio
  // Ver http://gsgd.co.uk/sandbox/jquery/easing/
  //, CYCLE_EASING = 'easeInOutExpo'
  var CYCLE_EASING = 'easeInOutExpo'
}

if (typeof(CYCLE_RANDOM_START) == 'undefined'){
  // Empezar con una foto al azar
  var CYCLE_RANDOM_START = 0
}

if (typeof(CYCLE_BEFORE) == 'undefined'){
  var CYCLE_BEFORE = function(){}
}

$doc.ready(function() {
    var $photos = $('#photos')
      , $items = $photos.find('div')
      , $images = $photos.find('img')
      , footer_height = $('#credits').height()
      
      , $details = $('#photo_details')
      , $photo_menu = $('#photo_menu')
      , $details_text =  $photo_menu.find('span')
      //, $photo_submenu = $photo_menu.find('ol.submenu')
      , $photo_submenu = $('#photo_submenu')
      , $menu_items = $photo_submenu.find('li')
      , $details_link = $('#photo_view')
      
      , $photo_nav_next = $('#photo_nav_next')
      , $photo_nav_prev = $('#photo_nav_prev')
      , options = { 
            fx: CYCLE_FX
          , sync: CYCLE_SYNC
          , speed: CYCLE_SPEED
          , timeout: CYCLE_TIMEOUT
          , easing: CYCLE_EASING
          , random: CYCLE_RANDOM_START
          , before: CYCLE_BEFORE
        }
      ;
      
    if ($photo_nav_next.length){
        options.next = '#photo_nav_next';
    }
    
    if ($photo_nav_prev.length){
        options.prev = '#photo_nav_prev';
    }      
    
     
    if ($details_text.length){
        options.before = function () {
            $details_text.text(this.children[0].title);
            $details_link.attr('href', this.children[0].alt);
            $menu_items.show();
            $menu_items.eq($items.index(this)).hide();
        }
    }
    
    function resizePhotos(){
        var ww = $win.width()
          // Esta línea fue reemplazada por la siguiente [, wh = $win.height() - footer_height] para que vaya acorde al diseño
          , wh = $win.height()
          , img_w, img_h
          ;
        
        $photos.css({width: ww, height: wh});
        
        img_w = ww
        img_h = ww * ratio;
        if (img_h < wh){
            img_h = wh;
            img_w = img_h / ratio;
        }
        
        $images.css({
            width: img_w + 'px'
          , height: img_h + 'px'
          // Centrar la imagen
          , left: Math.min(0, (ww - img_w) >> 1)
          , top: Math.min(0, (wh - img_h) >> 1)
        });
    }
    resizePhotos();
    window.onresize = resizePhotos;
    
    $items.eq(0).show();
    $items.show();
    $details_text.text($images[0].title);
    $details.show();
    $photos.cycle(options);
    

    $photo_menu.hover(
        function(){ $photo_submenu.slideDown('fast', {queue:false}); },
        function(){return false;}
    );

    $('#photo_nav_prev').hover(
        	function(){$photo_submenu.slideUp('fast', {queue:false});}, 
        	function(){return false}
        );
    $('#photos').hover(
    	function(){$photo_submenu.slideUp('fast', {queue:false});}, 
    	function(){return false}
    );
    $('#photo_view').hover(
        	function(){$photo_submenu.slideUp('fast', {queue:false});}, 
        	function(){return false}
        );
    

    $menu_items.find('a').click(function(e){
        e.preventDefault();        
        var idx = $menu_items.index(this.parentNode);    
        $photos.cycle(idx);        
        return false;
    });
    
    if($("#currentCollection").val()){
	    cc = $('#currentCollection').val().toLowerCase();
	    cx = $('a[href$="'+cc+'"]');
	    
	    li = cx.parent();
	    n = li.parent().children().index(li);            
	    $photos.cycle(parseInt(n));
    }
    
});

