/* fonctions relatives à la barre */

// formulaire d'identification
function autologinbar() {
	if (jQ('#loginblogbar input#loginCompte').val() == 'Pseudo'
      || jQ('#loginblogbar input#loginCompte').val() == ''
      || jQ('#loginblogbar input#passwdCompte').val() == '') {
		jQ('#error_login_bar').show();
		return false;
	}
	jQ('#error_login_bar').hide();
	jQ('#loginblogbar').attr('action', jQ('#siteBlogoramaBar').val());
  return true;
}

// format du titre de la box
function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div><span class="caption" style="width: 438px;">' + (title && title.length ? title : '' ) + '</span></div>';
}

// chargement de la barre
function loadBar() {
  if(!document.all) {
    win_w = window.innerWidth;
  } else {
    win_w = document.documentElement.clientWidth;
  }
  if(jQ('#toolbar').length > 0) {  
    inner_bar_w = jQ('#tablebar_ctx').width();
    bar_w = (parseInt(win_w) - inner_bar_w) / 2;
    jQ('#toolbar div.arrow').css('width',bar_w +'px');
    if(jQ('#toolbar').hasClass('closed')) {
      jQ('#toolbar_closed').css('bottom','0px');
    } else {
      jQ('#toolbar').css('bottom','0px');
      jQ('#toolbar_closed').css('bottom','-'+ jQ('#toolbar').height()+'px');
    }
  }
}

function bar_alert_left(tab) {

  if(tab != undefined) {

    if(jQ('#bar_alert_left').length > 0) {
        if(jQ('#bar_alert_left').hasClass('closed')) {
        
          jQ('#bar_alert_'+ tab).removeClass('hide').addClass('opened');
          alert_ph = parseInt(jQ('#bar_alert_left').css('padding-top')) +parseInt(jQ('#bar_alert_left').css('padding-bottom'));   
          alert_pw = parseInt(jQ('#bar_alert_left').css('padding-left')) + parseInt(jQ('#bar_alert_left').css('padding-right'));
          
          alert_h = jQ('#bar_alert_left').height() + 1 + alert_ph ; // on ajoute 1 pour caler correctement la div
          
          jQ('#bar_alert_left').removeClass('closed').addClass('opened');
          alert_l = jQ('#toolbar_td').offset().left + (jQ('#toolbar_td').width() - jQ('#bar_alert_left').width() - alert_pw - 1);
          alert_w = jQ('#bar_alert_msg').width() - 2 - alert_pw; /* - bordure - padding */
          
          jQ('#bar_alert_left').attr('style', 'left:'+ alert_l + 'px !important; top:-' + alert_h + 'px !important;');
          
          if(ie7) { // la propriété left ne s'applique pas donc on la force
            jQ('#bar_alert_left').setAttribute('style', 'left:'+ alert_l + 'px !important; top:-' + alert_h + 'px !important');
          }
          
          jQ('#bar_alert_left .input_extend').each(function(ui,elt) {
            jQ(elt).attr('style', 'width:'+ alert_w + 'px !important;');
          });
          if(jQ.browser.msie) {
            jQ('#bar_alert_left').attr('style','display:block !important');
          }
          
        } else {
          if(tab == 'msg') { 
            if(!jQ('#bar_alert_add').hasClass('hide')) { 
              jQ('#bar_alert_add').removeClass('opened').addClass('hide'); 
            }
            jQ('#bar_alert_msg').removeClass('hide').addClass('opened');
          } else {
            if(!jQ('#bar_alert_msg').hasClass('hide')) { 
              jQ('#bar_alert_msg').removeClass('opened').addClass('hide'); 
            }
            jQ('#bar_alert_add').removeClass('hide').addClass('opened');
          }
          
          alert_p = jQ('#bar_alert_left').height() + 1; // on ajoute 1 pour caler correctement la div          
          if(ie7) {
            jQ('#bar_alert_left').attr('style', 'top:-'+ alert_p + 'px !important;left:'+ jQ('#bar_alert_left').css('left') + ' !important;');
          }

        }
      }
  
  } else {
  
    jQ('#bar_alert_left').removeClass('opened').addClass('closed').css('top','');
    jQ('#bar_alert_msg').removeClass('opened').addClass('hide');
    jQ('#bar_alert_add').removeClass('opened').addClass('hide');
    
  }
  
}

function bar_alert() {

  if(jQ('#bar_alert').length > 0) {
    if(jQ('#bar_alert').hasClass('closed')) {    
      alert_h = jQ('#bar_alert').height() + 1; // on ajoute 1 pour caler correctement la div
      jQ('#bar_alert').removeClass('closed').addClass('opened');
      jQ('#bar_alert').attr('style', 'top:-'+ alert_h + 'px !important;');
      jQ('strong.bar_alert').removeClass('bar_alert').addClass('bar_closed');
      
    } else {
    
      jQ('#bar_alert').removeClass('opened').addClass('closed').css({'top': ''});
      jQ('strong.bar_closed').removeClass('bar_closed').addClass('bar_alert');
    
    }
  }
}

function moveBar() {
  if(jQ('#toolbar').css('top') == null) {
    jQ('#toolbar').css('top',document.documentElement.offsetHeight - jQ('#toolbar').height() + 'px');
  }
  if(jQ('#toolbar_closed').css('top') == null) {
    jQ('#toolbar_closed').css('top', document.documentElement.offsetHeight - jQ('#toolbar_closed').height() + 'px');
  }
  t_top = document.documentElement.scrollTop + document.documentElement.clientHeight - jQ('#toolbar').height();
  
  jQ('#toolbar').animate({top:t_top}, 500);  
  jQ('#toolbar_closed').animate({top:t_top}, 500);
}

/* fonction pour position left sous IE <= 7 */
function getX(oElement) { 
  var iReturnValue = 0;
  while( oElement != null ) {
  iReturnValue += oElement.offsetLeft;
  oElement = oElement.offsetParent;
  }
  return iReturnValue;
}

function bar(action, cookie_domain) {
      
  t_top = document.documentElement.scrollTop + document.documentElement.clientHeight;

  if(action == 'closed') {
  
    if(jQ('#bar_alert_left').length > 0 && jQ('#bar_alert_left').hasClass('opened')) {
      jQ('#bar_alert_left').removeClass('opened').addClass('closed');
      jQ('#bar_alert_msg').removeClass('opened').addClass('hide');
      jQ('#bar_alert_add').removeClass('opened').addClass('hide');
      jQ('#bar_alert_left').attr('style','');
      if(jQ.browser.msie) {
        jQ('#bar_alert_left').attr('style','display:none !important');
      }
    }
    
    if(jQ('#bar_alert').hasClass('opened')) {
      jQ('#bar_alert').attr('style','');
      jQ('strong.bar_closed').removeClass('bar_closed').addClass('bar_alert');
      jQ('#bar_alert').removeClass('opened').addClass('closed');
    }
    
    jQ('#toolbar').animate({bottom: -(jQ('#toolbar').height() + 1)}, 500);
    jQ('#toolbar_closed').animate({bottom: 0}, 500);
  
  } else {
  
    if(!document.all) {
      win_w = window.innerWidth;
    } else {
      win_w = document.documentElement.clientWidth;
    }
  
    inner_bar_w = jQ('#tablebar_ctx').width();
    bar_w = (parseInt(win_w) - inner_bar_w) / 2;
  
    jQ('#toolbar_closed').animate({bottom: -(jQ('#toolbar').height() + 1)}, 500);
    jQ('#toolbar').animate({bottom:0}, 500);        
  }
  
  document.cookie = 'toolbar='+ action+'; domain='+ cookie_domain +'; path=/;'
}


jQ(window).resize(function() {

  if(jQ('#toolbar').length > 0) {
  
    if(!document.all) {
      win_w = window.innerWidth;
    } else {
      win_w = document.documentElement.clientWidth;
    }

    alert_ph = parseInt(jQ('#bar_alert_left').css('padding-top')) +parseInt(jQ('#bar_alert_left').css('padding-bottom'));   
    alert_pw = parseInt(jQ('#bar_alert_left').css('padding-left')) + parseInt(jQ('#bar_alert_left').css('padding-right'));
    if(jQ('#bar_alert_left').length > 0 && !jQ('#bar_alert_left').hasClass('closed')) {      
      /*
      if(ie7) {      
        place = 'left:' + (getX(jQ('#toolbar_td')) + (jQ('#toolbar_td').width() - jQ('#bar_alert_left').width())) +'px !important; top:' + parseInt(jQ('#bar_alert_left').css('top')) + 'px !important';
      } else {
      */
        place = 'left:' + (jQ('#toolbar_td').offset().left + (jQ('#toolbar_td').width() - jQ('#bar_alert_left').width() - alert_pw - 1)) +'px !important; top:' + parseInt(jQ('#bar_alert_left').css('top')) + 'px !important';
      //}
      jQ('#bar_alert_left').attr('style',place);
      if(ie7) { // la propriété left ne s'applique pas donc on la force
        jQ('#bar_alert_left').setAttribute('style', place);
      }
    }
    imgs = jQ('#tablebar_ctx a.blog_avatar');
    if(win_w < 1024) {
      imgs.each(function(n,elt) {
        jQ(elt).hide();
      });
    } else {
      imgs.each(function(m,elt) {
        jQ(elt).show();
      });
    }
    inner_bar_w = jQ('#tablebar_ctx').width();
    bar_w = (parseInt(win_w) - inner_bar_w) / 2;
    jQ('#toolbar div.arrow').css('width', bar_w + 'px');
  
  }  
});

