
maxlks = {};
maxlks.ui = {};

/**
 *
 * --- UI Page object
 *
 */

maxlks.ui = function (config)
{
  this.base_uri = location.pathname;  
  this.hash = location.hash.substr(1);
  this.querystring = location.search.substr(1);
  this.config = config;
  this.timerId = null;
  
  this.initialize();
};

maxlks.ui.prototype.initialize = function()
{
  var that = this; 
  
  $('.js-arrowz').css({ opacity: 0.4 });
  $('.js-arrowz').bind('click', function(e){
    e.preventDefault();
  });
  
  /*that.homedesc_p = $('.js-homedesc p');  
  $('.js-homedesc').find('p').hide();
  $('.js-homedesc').find('p').eq(0).show();
  this.initAnimateDesc();*/
  
  var custom_h = $('#content .lcol .inner').height() - 210;
  $('#content .rcol .inner').css({ height: custom_h });
  
  $('#backhome').css({ opacity: 0.7 });
  $('#backhome').bind('mouseenter', function(){
    //if($(this).is(':animated')) return;
    $(this).animate({ marginTop: '-16px', opacity: 1 }, 300, function(){
        //$(this).css({ opacity: 1 });stop().
    });   
  });
  $('#backhome').bind('mouseleave', function(){
    //if($(this).is(':animated')) return;
    $(this).dequeue().animate({ marginTop: '-78px', opacity: 0.6 }, 1000, function(){
    });   
  });
  
  $('.socials ul li a').css({ opacity: 0.7 });
  $('.socials ul li a').bind('mouseenter', function(){
    $(this).css({ opacity: 1 }); 
  });
  $('.socials ul li a').bind('mouseleave', function(){
    $(this).css({ opacity: 0.7 });   
  });
  
  $('#footer .js-footer a:not(.selected)').bind('mouseenter', function(){
    //if($(this).is(':animated')) return;
    var params_fshow = {
      marginTop: '-79px',
      height: '88px',
      backgroundColor: '#66CCFF' // url(../images/footer_corner.png) 2px 2px no-repeat',
    };
    $(this).dequeue().stop().animate(params_fshow, 300, function(){
      $(this).css({ backgroundColor: '#66CCFF', color: '#333333' });
    });   
  });
  $('#footer .js-footer a:not(.selected)').bind('mouseleave', function(){    
    var params_fhide = {
      marginTop: '-7px',
      height: '16px',
      backgroundColor: '#FF0099' // none 2px 2px no-repeat',
      //color: '#FFFFFF'    
    };
    $(this).animate(params_fhide, 1000, function(){
      $(this).css({ backgroundColor: '#FF0099', color: '#FFFFFF' }).dequeue();
    });   
  });

  /* fancybox images */
  that.fancyBoxImages("a.lightboxable, dl.spip_documents dt a");
      
}; // end ui.initialize


maxlks.ui.prototype.initAnimateDesc = function()
{
  var that = this;   
  this.timerId = window.setInterval(function(){
    that.timedAnimateDesc(); 
  }, 10000);
};

maxlks.ui.prototype.timedAnimateDesc = function()
{
  var that = this; 
  
  var currp = $('.js-homedesc').find('p:visible');
  var currp_idx =  currp.index('.js-homedesc p');
  
  if(currp_idx == (that.homedesc_p.length - 1))
    nextp_idx = 0;
  else
    nextp_idx = currp_idx + 1;  
  
  //console.log('currp_idx: ' + currp_idx + 'nextp_idx' + nextp_idx );
  
  currp.fadeOut('slow', function() {
    //console.log('p: ' + that.homedesc_p.eq(nextp_idx).text());
    that.homedesc_p.eq(nextp_idx).fadeIn('slow');
  });  
  //window.clearInterval(this.timerId); 
};


/**
 *
 * utils
 *
 */

maxlks.ui.prototype.fancyBoxImages = function(selector)
{
  /*$('.lightboxable-trigger').bind('click', function(){    
    var target = $(this).attr('rel');
    $(target).trigger('click');
    return false;  
  });*/
  $(selector).each(function(){
    var subtitle = $(this).attr('rev');
    $(this).fancybox({
      'transitionIn'	:	'fade',
      'transitionOut'	:	'fade',
      'speedIn'		:	400, 
      'speedOut'		:	200,
      'overlayOpacity':	0.7,
      'overlayColor':	'#333',
      'titleShow':	true,
      'titlePosition':	'inside',
      'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
        //console.log($(this.orig).toSource());
        var title = '<span id="fancybox-title-wrap">' + title + '</span>';
        title += '<span id="fancybox-subtitle-wrap">' + subtitle + '</span>';
        title += '<span id="fancybox-title-nav">' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
        
        return title;
       },    
      'padding' :	20,
      'margin': 60,
      'onStart': function () { 
        //window.clearInterval(maxlks.ui.timer); window.clearInterval(maxlks.ui.timerSlideshow); 
      }
    });
  });
};








