
// BOOK IMAGES

function initGallery()
{
	var container = $('livre_thumbs');
	if (!container) return;
	$$('ul#livre_thumbs img.thumb').each(function(s){Event.observe(s, 'mouseover', showImage) });
}

function showImage(e)
{
	var thumb = e.element();
	var li = thumb.up();
	var ordre = thumb.getAttribute('rel');
	var images = $$('div#image_container img');
	images.each(function(s){s.hide();})
	$('image_'+ordre).show();
  var lis = $$('ul#livre_thumbs li');
	lis.each(function(s){s.removeClassName('li_active');});
	li.addClassName('li_active');
}



// STYLE SWITCHER

function switchStyle(e)
{
	var title = e.innerHTML;
	var styles = $$('head link[title][rel~=stylesheet]');
	styles.each(function(s){
    s.disabled = true;
    if(s.getAttribute("title") == title) s.disabled = false;
	});
	$$('#styleSwitcher ul a').each(function(s){s.removeClassName('bold')});
	e.addClassName('bold');
}

function loadStyles()
{
	var ul = $('styleLinks');
	if(ul)
	{
		var styles = $$('head link[title][rel~=stylesheet]');
		styles.each(function(s){
			title = s.readAttribute('title');
			var html = '<li><a href="#" onclick="switchStyle(this);return false;"';
			if (! s.disabled) html += ' class="bold"';
			html += '>'+title+'</a></li>'
			Element.insert(ul,html);
	  });
	}
}

// cf http://www.htmldog.com/articles/suckerfish/dropdowns/
function menuForIE6()
{
  var sfEls = document.getElementById("nav").getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++) {
    sfEls[i].onmouseover=function() {
      this.className+=" sfhover";
    }
    sfEls[i].onmouseout=function() {
      //this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
      this.className="";
    }
  }
  
  if($('scroll'))
  {
	  var sfEls = document.getElementById("scroll").getElementsByTagName("LI");
	  for (var i=0; i<sfEls.length; i++) {
	    sfEls[i].onmouseover=function() {
	      this.className+=" sfhover";
	    }
	    sfEls[i].onmouseout=function() {
	      //this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
	      this.className="";
	    }
	  }  	
  }
}


var randomCnt = 1;

function randomImage(url)
{
  var oldContainer = $('random'+randomCnt);
  var oldImage = oldContainer.down('img[rel]');
  if(oldImage != undefined) url += '&last_id='+oldImage.readAttribute('rel');

  randomCnt++;
  if(randomCnt>2) randomCnt=1;
  var newContainer = $('random'+randomCnt);
  
  newContainer.hide();
  
	new Ajax.Updater(
	  {success: newContainer}, 
	  url, 
	  {
	    asynchronous:true, 
	    evalScripts:false,
	    onSuccess: function(transport) {
		    new Effect.Fade(oldContainer, {});
		    new Effect.Appear(newContainer, {queue: 'end'});	   	
	    },
	    onFailure: function(transport) {
		   randomCnt++;
		   if(randomCnt>2) randomCnt=1; 
	    }	   
	  }
	);

} 


// ONLOAD SCRIPTS

function windowLoad()
{
	menuForIE6();
	//loadStyles();
	initGallery();
	
	if($('randomContainer'))
	{
	  var collection_id = $('collection_id').innerHTML;
	  var ajaxUrl = '/images/ajax?type=collection&collection_id=' + collection_id;
	  var perexec = new PeriodicalExecuter(function(pe){randomImage(ajaxUrl)}, 6);
	}	
}




Event.observe(window, 'load', windowLoad, false);
