function deplace_to_image(index_img){
  // Get some informations
  var image_width = 81
  var images = $(".other-view")
  var nb_images = images.size()
  var new_pos = 0;

  // Calcul deplacement
  if(index_img>=2){
      new_pos = (1 - index_img) * image_width;
  }
  if (index_img>=2 && index_img>=nb_images-2){
      new_pos = -((nb_images-3) * image_width);
  }

  // Move picture with animation
  $('#thumb-images').animate({left: new_pos}, 500);

  // Set image as current image
  current_image = index_img

  // Remove css selected.
  images.removeClass('selected');

  // Get selected image
  image_selected = images.eq(index_img)

  // Add css selected to current img
  image_selected.addClass('selected');

  // Show image
  var img = image_selected.children("img");
  var new_src = img.attr("src");
  new_src = new_src.replace("width=65", "width=323");
  new_src = new_src.replace("height=65", "height=280");
  $("#cover").attr("src", new_src);

  // Update zoom link
  var new_dnl = img.attr("src");
  new_dnl = new_dnl.replace(/;.*$/, ';download');
  $("#zoom-img").attr("href", new_dnl);
}



function add_article_to_favorite(caller) {
  var url = window.location.href;
  var title = document.title;

  if ($.browser.msie) {
    window.external.AddFavorite(url, title);
  }
  if ($.browser.mozilla) {
    window.sidebar.addPanel(title, url, "");
  }
  if ($.browser.safari) {
    // FIXME does not work
    window.sidebar.addPanel(title, url, "");
  }
}



function previous_page(anchor) {
    var history = window.history;
    if (history.length) {
        history.back();
    }
    else {
        window.location = anchor.href;
    }
    return false;
}


function show_all_families(div) {
    var arrPageSizes = ___getPageSize();
    var arrPageScroll = ___getPageScroll();
    var left, top, hdelta;

    top = arrPageScroll[1] + (arrPageSizes[3] / 10);
    hdelta = arrPageSizes[3] - div.height() - (arrPageSizes[3] / 10);
    if (hdelta)
        top += hdelta / 2;
    left = (arrPageSizes[0] - div.width()) / 2;
    div.css('top', top);
    div.css('left', left);
    div.show('slow');
}

function hide_all_families(div) {
    div.hide('fast');
}


/* home */
function toggle_corner_list() {
  var div = $("#corner-list");
  if (div.css('display') == 'none') {
    div.slideDown("slow");
  } else {
    div.slideUp("slow");
  }
  return false;
}

function mycarousel_initCallback(carousel) {

    $('#home-redactions #toolbar #previous-button').bind('click', function() {
        carousel.prev();
        return false;
    });
    $('#home-redactions #toolbar #next-button').bind('click', function() {
        carousel.next();
        return false;
    });
};

/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
};
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScroll,yScroll);
    return arrayPageScroll;
};



$(document).ready(function(){
  $(".boite").hover(function(){
      $(this).children(".hoverable").addClass("hover");
    },function(){
      $(this).children(".hoverable").removeClass("hover");
    });

  /* Thumbnails */
  var current_image = 0;
  $("#thumb-block .other-view").click(function(event) {
      var index_img = $("#thumb-block .other-view").index(this);
      deplace_to_image(index_img);
  });

  $("#nextButton").click(function(event){
    nb_images = $(".other-view").size()
    if (current_image<(nb_images-1)){
        current_image += 1;
        deplace_to_image(current_image);
    }
  });

  $("#previousButton").click(function(event){
    if (current_image!=0){
      current_image -= 1;
      deplace_to_image(current_image);
    }
  });

  $("a#zoom-img").lightBox();

});


/* JQuerydefault value */
jQuery.fn.DefaultValue = function(){
    return this.each(function(){
		//Make sure we're dealing with text-based form fields
		if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
			return;
        // Get the value from the title
        var text = $(this).attr("title");
		//Store field reference
		var fld_current=this;
		//Set value initially if none are specified
        if(this.value=='') {
			this.value=text;
		} else {
			//Other value exists - ignore
			return;
		}
		//Remove values on focus
		$(this).focus(function() {
			if(this.value==text || this.value=='')
				this.value='';
		});
		//Place values back on blur
		$(this).blur(function() {
			if(this.value==text || this.value=='')
				this.value=text;
		});
		//Capture parent form submission
		//Remove field values that are still default
		$(this).parents("form").each(function() {
			//Bind parent form submit
			$(this).submit(function() {
				if(fld_current.value==text) {
					fld_current.value='';
				}
			});
		});
    });
};
