cookie_name = 'blogorama_bar';

// redimensionne toutes les images de la page
function redimAllImagesDescriptif() {
	return redimAllImagesBySize('div.header',740,99999);
}	
function redimAllImages() {
	return redimAllImagesBySize('div.text',525,99999);
}
function redimAllImagesBySize(name,s1,s2) {	
	if (navigator.appName!="Microsoft Internet Explorer") 
		return false;

	$$(name).each( function(divtext) {
		list_des = divtext.descendants();	// pour chaque descandants de la div article
		list_des.each( function(item) {
			if ( item.src != '' && item.src != undefined ) {
				// si item est une image, on redimensionne
				var currentTime = new Date();
				id = currentTime.getTime();
				item.id='insertedimage-'+id; 
				redimImage(item,s1,s2);
			}
		});
	});
	return true;
}
// redimensionne l'objet image inImg avec width-max=inMW height-max=inMH
function redimImage(inImg, maxWidth, maxHeight) {
	// ratio
	var dW = 0;
	var dH = 0;
	oImg=inImg;

	if ( !oImg ) return;

	var dim = oImg.getDimensions();
	var h = dim.height;
	var w = dim.width;

	// non ie ou non compatible, on quitte
	if ( h == 0 || w == 0 ) return;

	var dW=0;
	var dH=0;
	// Si la largeur ou la hauteur depasse la taille maximale
	if ((h > maxHeight) || (w > maxWidth)) {
		// Si la largeur et la hauteur depasse la taille maximale
		if ((h >= maxHeight) && (w >= maxWidth)) {
			// On cherche la plus grande valeur
			if (h > w) {
				dH = maxHeight;
				// On recalcule la taille proportionnellement
				dW = parseInt((w * dH) / h, 10);
			} else {
				dW = maxWidth;
				// On recalcule la taille proportionnellement
				dH = parseInt((h * dW) / w, 10);
			}
		} else if ((h > maxHeight) && (w < maxWidth)) {
			// Si la hauteur depasse la taille maximale
			dH = maxHeight;
			// On recalcule la taille proportionnellement
			dW = parseInt((w * dH) / h, 10);
		} else if ((h < maxHeight) && (w > maxWidth)) {
			// Si la largeur depasse la taille maximale
			dW = maxWidth;
			// On recalcule la taille proportionnellement
			dH = parseInt((h * dW) / w, 10);
		}
	}
	if ( dW == 0 || dH == 0 ) 
		return;

	// redimensionnement
	oImg.width=dW;
	oImg.height=dH;
	oImg.show();

	return -1;
}
// appel avec delai
function delayredim(image,inMW,inMH) {
	redimImage($(image),inMW,inMH);
}
/**
 * Show a popup
 * @param	String	url
 * @param	String	name	Title of the page
 * @param	Int	width	Width of the popup
 * @param	Int	height	Height of the popup
 */
function popup(url,name,width,height) {
	window.open(url,name,'scrollbars=yes,width='+width+',height='+height+''); 
	return true;
}
/**
 * Toggle the basic theme header
 */
function descToggle() {
	$('infoBlogOFF').toggle();
	$('infoBlogON').toggle();

	// We need to hide the objects because our deer Internet Explorer don't like that when we hide a block
	$$('#infoblogON object').each( function(Element) {
		Element.toggle();
	});
	$$('#infoblogON embed').each( function(Element) {
		Element.toggle();
	});
}
function replaceVid(id,type) {
	obj = $('flv'+id);
	flv = $("val"+id).value;

	if ( type == 'webcam' ) {
		url = '/webcam/'+flv+'/';
	}
	else {
		url = '/movie/'+flv+'/';
	}
	/* Ajax call */
	var ajax = new Ajax.Updater(
		'flv'+id,
		url ,
		{ 
			method:'get',
			asynchronous: true 
		}
	);
}
function externalLinks() {
	$$('a').each( function(Element) {
		if( Element.getAttribute("href") && Element.getAttribute("rel") == "external") {
			Element.target = "_blank";
		}
	});
}
/* VOTE */
function starOver(note,text) {
	$('starbul').innerHTML=text;
	starInitJs(note);
}
function starOut() {
	starInitJs('');
	$('starbul').innerHTML='&nbsp;';
}
function starInitJs(forcenote) {

	urlbase = $('vote_urlbase').value;

	if ( forcenote == '') {
		x=document.getElementsByName('oldnote')[0];
		note = x.value;
	}
	else {
		note = forcenote;
	}

	for (i=1;i<=5;i++) {
		y=document.getElementsByName('note'+i)[0];
		if ( note < ( i - 0.6 ) ) {
			val=0;
			noteFinale = i;
		}
		else if ( note >= (i-0.6) && note < (i-0.3) ) {
			val=1;
		}
		else {
			val=2;
		}
		change = "background-image: url("+urlbase+val+".gif);";
		if ( change != 	y.style.cssText )
			y.style.cssText="background-image: url("+urlbase+val+".gif);";
	}
}
/* END VOTE */

function comment_show(type) {
  if(typeof(type) == 'string') {
    if(type == 'texte') {
      if($('codeCommWebcam') != null) { $('codeCommWebcam').hide(); }
      if(Prototype.Browser.IE6) {
        $('texteTD').show();
        $('texte').show();
        $('codeCommTxt').setStyle('display','none');
      } else {      
        $('codeCommTxt').show();
      }
      if($('block-smileys') != null && $('block-smileys').hasClassName('shown')) { $('block-smileys').show(); }
      if($$('.errormsg')[0] != null) { $$('.errormsg')[0].show(); }
      if($$('.bt-submit-form-webcam')[0] != null) { $$('.bt-submit-form-webcam')[0].hide(); }
      if($$('.bt-submit-form-texte')[0] != null) { $$('.bt-submit-form-texte')[0].show(); }
      $$('.comment-tabs-texte')[0].toggleClassName('selected'); 
      $$('.comment-tabs-webcam')[0].toggleClassName('selected');
      $('hidden_type').value = 'texte';
      if($$('.known-user-webcam')[0] != null) { $$('.known-user-webcam')[0].className = 'known-user-texte'; }
    } else {
      if($('codeCommWebcam') != null) { $('codeCommWebcam').show(); }
      if(Prototype.Browser.IE6) {
        $('texteTD').hide();
        $('texte').hide();
        $('codeCommTxt').setStyle('display','block');
      } else {      
        $('codeCommTxt').hide();
      }
      if($('block-smileys') != null && $('block-smileys').hasClassName('shown')) { $('block-smileys').hide(); }
      if($$('.errormsg')[0] != null) { $$('.errormsg')[0].hide(); }
      if($$('.bt-submit-form-webcam')[0] != null) { $$('.bt-submit-form-webcam')[0].show(); }
      if($$('.bt-submit-form-texte')[0] != null) { $$('.bt-submit-form-texte')[0].hide(); }
      $$('.comment-tabs-texte')[0].toggleClassName('selected'); 
      $$('.comment-tabs-webcam')[0].toggleClassName('selected');
      $('hidden_type').value = 'webcam'; 
      if($$('.known-user-texte')[0] != null) { $$('.known-user-texte')[0].className = 'known-user-webcam'; }
    }
  } else {
    return false;
  }
}

function showSmileys () {
  if($('block-smileys').hasClassName('shown')) { $('block-smileys').removeClassName('shown'); }
  else { $('block-smileys').addClassName('shown'); }
  new Effect.toggle($('block-smileys'),'blind',{duration: 0.4});
  return false;
}

function rvb2hex(val){
  if (val.indexOf("rgb") >= 0)  {
    var rgb_string = val.slice(val.indexOf('(') + 1,val.indexOf(')'));
    var rgb_val = rgb_string.split(",");
    val = "#";
    var hexChars = "0123456789ABCDEF";
    for (var i = 0; i < 3; i++)  {
      var v = rgb_val[i].valueOf();
      val += hexChars.charAt(v/16) + hexChars.charAt(v%16);
    }
  }
  return val;
}

function format_ads() {
  var ctitle = $$('span.article-title-main')[0].getStyle('color');
  if(ctitle.charAt(0)!='#') { ctitle = rvb2hex(ctitle); }
  
  $$('span.killer_titulo1').each(function(elt) {
    elt.setStyle({ color : ctitle });
  }); 
}

function loadBar() {

  if(!document.all) {
    win_w = window.innerWidth;
  } else {
    win_w = document.documentElement.clientWidth;
  }
  
  if($('toolbar') != null) {
   
    inner_bar_w = $('tablebar_ctx').getWidth();
    bar_w = (parseInt(win_w) - inner_bar_w) / 2;
    $('toolbar').down('div.arrow').style.width = bar_w + 'px';
    
    if($('toolbar').hasClassName('closed')) {
      $('toolbar_closed').setStyle({'bottom': '0px'});
    } else {
      $('toolbar').style.bottom = '0px';
      $('toolbar_closed').style.bottom = '-'+ $('toolbar').getHeight() + 'px';
    }
     
  }
  
}

function bar_alert_left(tab) {
  if($('bar_alert_left') != null) {
      if($('bar_alert_left').hasClassName('closed')) {
      
        $('bar_alert_'+ tab).removeClassName('hide');
        alert_h = $('bar_alert_left').getHeight() + 1; // on ajoute 1 pour caler correctement la div
        $('bar_alert_left').removeClassName('closed').addClassName('opened').setStyle({'top': -alert_h+'px'});
        alert_l = $('toolbar_td').positionedOffset().left;  
        $('bar_alert_left').setStyle({'left': alert_l +'px'});

        alert_w = $('toolbar_td').getWidth() - 1 - 10; /* - bordure - padding */
        $('bar_alert_left').setStyle({'width':alert_w+'px'});

        $$('#bar_alert_left div.alert_close')[0].setStyle({'width':alert_w+'px'});
        $$('#bar_alert_left .input_extend').each(function(elt) {
            elt.setStyle({'width':alert_w-10+'px'});
        });
        
      } else {
      
        $('bar_alert_left').removeClassName('open').addClassName('closed').setStyle({'top': ''});
        $('bar_alert_msg').addClassName('hide');
        $('bar_alert_add').addClassName('hide');
      }

  }
}

function bar_alert() {
  if($('bar_alert') != null) {
  if($('bar_alert').hasClassName('closed')) {
  
    alert_h = $('bar_alert').getHeight() + 1; // on ajoute 1 pour caler correctement la div
    $('bar_alert').removeClassName('closed').addClassName('opened').setStyle({'top': -alert_h+'px'});
    $$('strong.bar_alert')[0].removeClassName('bar_alert').addClassName('bar_closed');
    
  } else {
  
    $('bar_alert').removeClassName('open').addClassName('closed').setStyle({'top': ''});
    $$('strong.bar_closed')[0].removeClassName('bar_closed').addClassName('bar_alert');
  
  }
  }
}

function moveBar() {

  if($('toolbar').getStyle('top') == null) {
    $('toolbar').style.top = document.documentElement.offsetHeight - $('toolbar').getHeight() + 'px';
  }
  if($('toolbar_closed').getStyle('top') == null) {
    $('toolbar_closed').style.top = document.documentElement.offsetHeight - $('toolbar_closed').getHeight() + 'px';
  }
  t_top = document.documentElement.scrollTop + document.documentElement.clientHeight - $('toolbar').getHeight();
  new Effect.Parallel([
    new Effect.Morph($('toolbar'), { sync: true, style: 'top:'+ t_top + 'px' }), 
    new Effect.Morph($('toolbar_closed'), { sync: true, style: 'top:' + t_top + 'px' }) 
    ], { 
    duration: 0.25
  });
}

function bar(action, cookie_domain) {
      
  t_top = document.documentElement.scrollTop + document.documentElement.clientHeight;

  if(action == 'closed') {
    
      new Effect.Parallel([
        new Effect.Morph($('toolbar'), { sync: true, style: 'bottom:-'+ ($('toolbar').getHeight() + 1) + 'px' }), 
        new Effect.Morph($('toolbar_closed'), { sync: true, style: 'bottom:0px' }) 
        ], { 
        duration: 0.5
      });
    
  } else {
  
    if(!document.all) {
      win_w = window.innerWidth;
    } else {
      win_w = document.documentElement.clientWidth;
    }
  
    inner_bar_w = $('tablebar_ctx').getWidth();
    bar_w = (parseInt(win_w) - inner_bar_w) / 2;
    
    new Effect.Parallel([
      new Effect.Morph($('toolbar_closed'), { sync: true, style: 'bottom:-'+ ($('toolbar').getHeight() + 1) + 'px' }), 
      new Effect.Morph($('toolbar'), { sync: true, style: 'bottom:0px' }) 
      ], { 
      duration: 0.5
    });
        
  }
  
  document.cookie = 'toolbar='+ action+'; domain='+ cookie_domain +'; path=/;'
}

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==7;

document.observe("dom:loaded" , function() {
	redimAllImages();
	externalLinks();
  if(!Prototype.Browser.IE7) { loadBar(); }
});

window.onload = function() {
  format_ads();
  if(Prototype.Browser.IE7) {
    loadBar();
  }
}

Event.observe(window, 'resize', function() {
  if($('toolbar') != null) {
  if($('toolbar').hasClassName('open')) {

    if(!document.all) {
      win_w = window.innerWidth;
    } else {
      win_w = document.documentElement.clientWidth;
    }
  
    imgs = $('tablebar_ctx').getElementsBySelector('img');
    if(win_w < 1024) {
      imgs.each(function(elt) {
        elt.style.display = 'none';
      });
    } else {
      imgs.each(function(elt) {
        elt.style.display = 'block';
      });
    }
  
    inner_bar_w = $('tablebar_ctx').getWidth();
    bar_w = (parseInt(win_w) - inner_bar_w) / 2;
    $('toolbar').down('div.arrow').style.width = bar_w + 'px';
  
  }  
  }
});
