var slideshow = false;
var slideshow_running = false;
var current_image = 0;
var background = null;
var eizik = null;

// Array.indexOf polyfill
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

$(document).ready(function() {
    // http://www.schillmania.com/projects/soundmanager2/
    soundManager.url = 'swf/';
    soundManager.useFlashBlock = false;
    soundManager.debugFlash = false;
    soundManager.debugMode = false;
    soundManager.onready(function() {
	eizik = soundManager.createSound({
	    id: 'eizik',
	    url: '/music/Olivier Blanchard - eiphotographies.mp3'
	});
	eizik.play({
	    onfinish: function(){
		this.play()
	    }
	});
    });
    initEI();
});


function initEI() {

    start_preload();

    $('.gallery_image').live('click',
        function() {
            show_image(current_image);
        });


	$('#prev_image').click(function() {
		current_image -= 2;
		show_image(current_image)
	});
	$('#next_image').click(function() {
		show_image(current_image)
	});



// Initialisation des boutons de navigation du Slide Show
	$('#pause_slide').click(function() {
		if (slideshow_running) {
			clearTimeout(slideshow_timer);
			slideshow_running = false;
			$(this).attr({
				'src': 'img/Btn_MiniAvant[bold].png',
				'title': 'Démarrer le défilement auto.'
			});
			$('#prev_image').fadeIn(300);
			$('#next_image').fadeIn(300);
			images_navigation();
		} else {
			/* Démarrage de slideshow */
			slideshow_running = true;
			$(this).attr({
				'src': 'img/Btn_Pause[bold].png',
				'title': 'Stopper le défilement auto.'
			});
			//$('#prev_image').fadeOut(300);
			//$('#next_image').fadeOut(300);
			images_navigation();
			//current_image += 1;
			slideshow_timer = setTimeout(function() {
					show_image(current_image)
			}, SLIDESHOW_DELAY);

		}

	});

	$('#button_laisserunmot').hover(
		function() {
			$(this).attr('src', 'img/Lien_laisser_un_mot[bold].png');
	},
		function() {
			$(this).attr('src', 'img/Lien_laisser_un_mot[light].png');
	});

	$('#prev_image').hover(
		function() {
			$(this).attr('src', 'img/Btn_Arriere[bold].png');
	},
		function() {
			$(this).attr('src', 'img/Btn_Arriere[light].png');
	});

	$('#next_image').hover(
		function() {
			$(this).attr('src', 'img/Btn_Avant[bold].png');
		},
		function() {
			$(this).attr('src', 'img/Btn_Avant[light].png');
	});

	$('#pause_slide').hover(
		function() {
			if (slideshow_running) {
				$(this).attr('src', 'img/Btn_Pause[bold].png');
			} else {
				$(this).attr('src', 'img/Btn_MiniAvant[bold].png');

			}
		},
		function() {
			if (slideshow_running) {
				$(this).attr('src', 'img/Btn_Pause[light].png');
			} else {
				$(this).attr('src', 'img/Btn_MiniAvant[light].png');
			}
	});


	reset_chap_buttons();

    $('.image_chap').live('click', function(){
		clearTimeout(slideshow_timer);
        var chapitre = $(this)[0].id[$(this)[0].id.length - 1]
        current_image = index_chapitres[chapitre - 1];
        show_image(current_image);
    });

// Initialisation des boutons rubriques
	$('#logo').click(function() {
        transition();
        reset_buttons();
	});

	$('#button_folio').hover(
		function() {
		    $(this).attr('src', 'img/Folio[bold].png');
	    },
		function() {
			if (!$(this).hasClass('selected')) {
				$(this).attr('src', 'img/Folio[light].png');
			}
	});

	$('#button_folio').click(function() {
		/* passage en mode slideshow */

		$('.text').fadeOut(100).not('#txt_livre').remove();
        $('.music').fadeOut(100);
		$('#menu').fadeOut(500,
		    function() {
		        $('#menu').css('background-image', 'none');
		        $('#button_credits').hide();

	        }).fadeIn(300);

		$('#background').fadeOut(500,
			function(){
				slideshow = true;
				slideshow_running = true;
				$('#background').hide();
				// $('#button_credits').hide();
				$('#buttons_nav').show();
				show_image(current_image);
			});
		reset_buttons();
		$(this).addClass('selected').attr('src', 'img/Folio[bold].png');
	});



	$('#button_cv').hover(
		function() {
		$(this).attr('src', 'img/Curriculum[bold].png');
	},
		function() {
			if (!$(this).hasClass('selected')) {
				$(this).attr('src', 'img/Curriculum[light].png');
			}
	});
	$('#button_cv').click(function() {
		$(document.body).append($('<img>').attr({
			'id': 'txt_contact',
			'class': 'text',
			'src': 'img/Txt_Curriculum.png'
		}).css({
			'border': 'none',
			'position': 'absolute',
			'top': $('#main').offset().top + 516,
			'left': $('#main').offset().left + 404,
			'display': 'none'
		}))
		reset_buttons();
		$(this).addClass('selected').attr('src', 'img/Curriculum[bold].png');
		transition('txt_contact');
	});


	$('#button_contact').hover(
		function() {
		$(this).attr('src', 'img/Contact[bold].png');
	},
		function() {
			if (!$(this).hasClass('selected')) {
				$(this).attr('src', 'img/Contact[light].png');
			}
	});
	$('#button_contact').click(function() {
		$(document.body).append($('<img>').attr({
			'id': 'txt_cv',
			'class': 'text',
			'src': 'img/Txt_Contact.png',
			'usemap': '#Txt_Contact_Map'
		}).css({
			'border': 'none',
			'position': 'absolute',
			'top': $('#main').offset().top + 500,
			'left': $('#main').offset().left + 544,
			'display': 'none'
		}))
		reset_buttons();
		$(this).addClass('selected').attr('src', 'img/Contact[bold].png');
		transition('txt_cv');
	});



	$('#button_livre').hover(
		function() {
		$(this).attr('src', 'img/Livre_d\'or[bold].png');
	},
		function() {
			if (!$(this).hasClass('selected')) {
				$(this).attr('src', 'img/Livre_d\'or[light].png');
			}
	});
	$('#button_livre').click(function() {
		$('#txt_livre').css({
			'top': $('#main').offset().top + 260,
			'left': $('#main').offset().left + 495 - 30,
			'display': 'none'
		});
		reset_buttons()
		$(this).addClass('selected').attr('src', 'img/Livre_d\'or[bold].png');
		transition('txt_livre');
	});


	$('#button_credits').hover(
		function() {
		$(this).attr('src', 'img/Credits[bold].png');
	},
		function() {
			if (!$(this).hasClass('selected')) {
				$(this).attr('src', 'img/Credits[light].png');
			}
	});
	$('#button_credits').click(function() {
		$(document.body).append($('<img>').attr({
			'id': 'txt_credits',
			'class': 'text',
			'src': 'img/Txt_Credits.png',
			'usemap': '#Txt_Credits_Map',
			'target': '_blank'
		}).css({
			'position': 'absolute',
			'border': 'none',
			'top': $('#main').offset().top + 618,
			'left': $('#main').offset().left + 774,
			'display': 'none'
		}))

		reset_buttons()
		$(this).addClass('selected').attr('src', 'img/Credits[bold].png');
		transition('txt_credits');
	});

    $('#music_control').click(function() {
        if ($(this).hasClass('playing')) {
            $(this).removeClass('playing');
            $('#music_control_stop').fadeIn(200);
            eizik.pause();
        } else {
            $(this).addClass('playing');
            $('#music_control_stop').fadeOut(200);
            eizik.play();
        }
    });


    // initialize scrollable
    $("div.scrollable").scrollable({
        vertical:true,
        size: 1
    // use mousewheel plugin
    }).mousewheel();
}





function initChapitre(chapitre) {
    $('#chap'+chapitre).show();
}

function start_preload() {
	// préchargements du fond
	background = $("<img>").attr({
			"src": 'img/background.jpg',
			'width': 1024,
			'height': 716,
			'id': 'background'
			}).css('display', 'none')

	setTimeout(preload, 1000);
}

function preload() {
	// préchargement des images de la galerie
	var width;
	var height;

	if (current_preload < images.length) {
		switch (images[current_preload][0]) {
			case 0: { // paysage
				width = 1020;
				height = 676;
			}
			break
			case 1: { // portrait
				width = 466;
				height = 700;
			}
			break
			case 2: { // pano
				width = 1024;
				height = 317;
			}
			break
			case 3: { // pano
				width = 1024;
				height = 358;
			}
			break
			case 4: { // pano
				width = 1024;
				height = 512;
			}
			break
			case 5: { // carré
				width = 700;
				height = 700;
			}
			break
			case 6: { // paysage
				width = 933;
				height = 700;
			}
			break
		}
    	var img = $("<img>").attr({
			"src": 'photos/'+images[current_preload][1],
			'width': width,
			'height': height,
			'id': 'slideshow',
			'class': 'gallery_image'
		}).css({
			'display': 'none',
			'margin-top': (716 - height) / 2
		});
        images[current_preload][3] = img;
		// a-t-on un callback ?
        if (images[current_preload][2]&&images[current_preload][2]());

		current_preload += 1;
		setTimeout(preload, PRELOAD_TIMEOUT);

	}

}

function reset_buttons() {
	$('.button').removeClass('selected');
	$('#button_folio').attr('src', 'img/Folio[light].png');
	$('#button_cv').attr('src', 'img/Curriculum[light].png');
	$('#button_contact').attr('src', 'img/Contact[light].png');
	$('#button_livre').attr('src', 'img/Livre_d\'or[light].png');
	$('#button_credits').attr('src', 'img/Credits[light].png');
}

function reset_chap_buttons() {
    // Initialisation des boutons de chapitre Slide show
    $('.image_chap').attr('src', 'img/Btn_Chapitre[light].png');
    $('.image_chap').hover(function(){},
        function(){
		    $(this).attr('src', 'img/Btn_Chapitre[light].png');
        });

    $('#chap1').hover(
        function() {
		    $(this).attr('src', 'img/Btn_Chapitre_1.png');  // Bucarest
        });

    $('#chap2').hover(
        function() {
		    $(this).attr('src', 'img/Btn_Chapitre_2.png'); // Afrique
        });
    $('#chap3').hover(
        function() {
		    $(this).attr('src', 'img/Btn_Chapitre_3.png'); // Multiple
        });
    $('#chap4').hover(
        function() {
		    $(this).attr('src', 'img/Btn_Chapitre_4.png'); // Musique
        });
    $('#chap5').hover(
        function() {
		    $(this).attr('src', 'img/Btn_Chapitre_5.png'); // Gowest
        });
    $('#chap6').hover(
        function() {
		    $(this).attr('src', 'img/Btn_Chapitre_6.png'); // Montage
        });
}


function transition(id){
	/* fonction appelée en transition d'une rubrique à une autre */
	if (slideshow) {
		/* retour du mode slideshow */
		clearTimeout(slideshow_timer);
		$('#music_control').fadeIn(200);
		if (!$('#music_control').hasClass('playing')) {
            $('#music_control_stop').fadeIn(200);
	    }

		$('#buttons_nav').hide()

		$('#menu').fadeOut(300,
		    function() {
		        $('#menu').css('background-image', 'url(img/bar.jpg)');
		        $('#button_credits').show();
	        }).fadeIn(400);


		$('#slideshow').fadeOut(300,
			function() {
				$('#main').html($(background).fadeIn(400,
					function(){
						slideshow = false;
						$('#main').append($('<img>').attr({'id': 'slideshow'}));
                        if (id) {
						    $('#'+id).fadeIn(500);
						}

					}
				))
			}
		)
	} else {
		$('.text').not('#'+id).fadeOut(400, function() {
			$('#'+id).fadeIn(400);
		});
	}
}


function show_image(idx) {
	clearTimeout(slideshow_timer);
	$('#slideshow').fadeOut(300,
		function(){
			$('#main').html($(images[idx][3]).fadeIn(300));
			images_navigation();
			current_image += 1;
			if (current_image == images.length) {
				current_image = 0;
			}
			if (slideshow_running) {
				slideshow_timer = setTimeout(function() {
					show_image(current_image)
				}, SLIDESHOW_DELAY);
			}
		}
	)
	if (index_chapitres.indexOf(idx) > -1) {
	    reset_chap_buttons();
	    var chapitre = index_chapitres.indexOf(idx) + 1;
	    var src = 'img/Btn_Chapitre_'+ chapitre +'.png';
	    var id = '#chap' + chapitre;
	    $(id).attr('src', src);
        $(id).hover(function(){},
        function(){
		    $(this).attr('src', src);
        });

    }
}

function images_navigation() {
	/* gestion de l'affichage des boutons de nav
	   en fonction de la position dans la liste de photos */
	if (current_image == 0) {
		$('#prev_image').fadeOut(300);
	} else if ($('#prev_image').css('display') == 'none') {
		$('#prev_image').fadeIn(300);
	}

	if (current_image == images.length-1) {
		$('#next_image').fadeOut(300);
	} else if ($('#next_image').css('display') == 'none') {
		$('#next_image').fadeIn(300);
	}

}

