/* Configuration variables */
/*
var conf_reflection_p = 0.5;         // Sets the height of the reflection in % of the source image 
var conf_focus = 4;                  // Sets the numbers of images on each side of the focussed one
var conf_slider_width = 14;          // Sets the px width of the slider div
var conf_images_cursor = 'pointer';  // Sets the cursor type for all images default is 'default'
*/
var conf_images_cursor = 'pointer';  // Sets the cursor type for all images default is 'default'

/* Id names used in the HTML */
var conf_details = 'boite_texte';	// 
var conf_text = 'texte';
var conf_images = 'boite_thumbs';	//
var conf_zoom= 'boite_image';		//
var conf_text= 'commentaire';
var conf_vid= 'boite_video';

var conf_up = 'fleche_haut';
var conf_dn = 'fleche_bas';

var array_images = new Array();
var count;
var target = 0;
var current = 0;
var timer = 0;
var last_zoomed = -1;
var scrolling = 0;
var direction = 0;


var text;
var text_box;

/* Hide loading bar, show content and initialize mouse event listening after loading */
window.onload = function()
{
	if(document.getElementById(conf_images))
	{
		// hide(conf_loading);
		refresh(true);
		zoom(0);
		
		//show(conf_images);
		//initMouseWheel();
		//initMouseDrag();
	}
}
/*
function scroll_text()
{
	if (scrolling != 0) {		

	if (direction < 0) // up
		{
			delta = text.top + text.height - text_box.height;
			if (delta >= 0) 
			{
				text.top = parseInt(text.top) + direction + 'px';
				window.setTimeout(scroll_text, 50);
			}
			else
			{
				scrolling = 0;
			}
		}
		else if (direction > 0) // down
		{
			delta = text.top;
			if (delta >= 0) 
			{
				text.top = parseInt(text.top) + direction + 'px'
				window.setTimeout(scroll_text, 50);
			}
			else
			{
				scrolling = 0;
			}
		}
	}
}

function scroll_up ()
{
	direction = -5;
	scrolling = 1;
	window.setTimeout(scroll_text, 50);
	
}

function scroll_down ()
{
	direction = 5;
	scrolling = 1;
	window.setTimeout(scroll_text, 50);
}

function scroll_stop()
{
	direction = 0;
	scrolling = 0;
}
*/

function refresh(init)
{
	/*
	up_arrow = document.getElementById(conf_up);
	dn_arrow = document.getElementById(conf_dn);

	text = document.getElementById(conf_text).style;
	text_box = document.getElementById(conf_details).style;

	up_arrow.onmouseover = function() { scroll_up(); }
	up_arrow.onmouseout = function() { scroll_stop(); }

	dn_arrow.onmouseover = function() { scroll_down(); }
	dn_arrow.onmouseout = function() { scroll_stop(); }
	*/

	img_div = document.getElementById(conf_images);

	count = img_div.childNodes.length;
	
	var i = 0;
	for (var index = 0; index < count ; index++)
	{ 
		var image = img_div.childNodes.item(index);
		if (image.nodeType == 1)
		{
			array_images[i] = index;
			
			/* Set image onclick by adding i and x_pos as attributes! */
			image.url = image.getAttribute('longdesc');
			image.widescreen = image.getAttribute('widescreen');
			image.isvideo = image.getAttribute('isvideo');
			image.i = i; // stores the image position
			image.id = 'img_id' + i;
			image.onclick = function() { zoom(this.i); }
			
			/* Set image cursor type */
			image.style.cursor = conf_images_cursor;

			/* fade out then unhide detail divs */
			// hide_text_instant(i);
			// document.getElementById("details"+i).style.visibility = 'visible';
			
			i++;
		}
	}
	count = array_images.length;
}

function highlight(number)
{
	var img_div = document.getElementById(conf_images);
	var images = img_div.childNodes;
	for (var index=0 ; index<count ; index++)
	{
		if (index == number) 
			currentOpac('img_id' + index, 100, 100)
		else
			currentOpac('img_id' + index, 50, 100);		
	}	
}

/*
function jumpToPic(picture_number)
{
	var img_div = document.getElementById(conf_images);
	img_div.style.left = conf_center_offset - (picture_number - 0.5) * (conf_image_width + conf_image_spacing) + 'px';
}

function jumpToX(offset)
{
	var img_div = document.getElementById(conf_images);
	img_div.style.left = (offset) + 'px';
	current = offset;
}
*/

function zoom(picture_number)
{
	/* Animate gliding to new x position */
	if (last_zoomed != picture_number)
	{
		// TODO: Hide last text
		// hide_text(last_zoomed);
		
		last_zoomed = picture_number;
		
		highlight(picture_number);
		
		var thumb = document.getElementById('img_id' + picture_number);
		
        if (thumb.isvideo != "0")
        {
			hide (conf_text);
            hide (conf_zoom);
            show (conf_vid);
		}
		else
		{
            hide (conf_vid);
            show (conf_zoom);
            
            if (thumb.widescreen != "0")
            {
                hide (conf_text);
            }
            else
            {
                show(conf_text);
            }
		}
		
        
        document.getElementById(conf_zoom).src = thumb.url;
		
		// TODO : show text box
		// show_text(picture_number);
	}
}


/* Show/hide element functions */
function show(id)
{
	var element = document.getElementById(id);
	element.style.visibility = 'visible';
	element.style.display = 'inline';
}
function hide(id)
{
	var element = document.getElementById(id);
	if (element != null){
        element.style.visibility = 'hidden';
        element.style.display = 'none';
    }  
}


/**************************************************** OPACITY *****************************************************/

function show_text(num) {
	opacity("text"+num, 0, 100, 250); 
}

function hide_text(num) {
	opacity("text"+num, 100, 0, 250); 
}

function hide_text_instant(num) {
	opacity("text"+num, 10, 0, 1); 
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}


