var diaporama_carousel;

jQuery(document).ready(function () {
    jQuery("#diaporama .image-list").jcarousel({
        scroll: 1,
        initCallback: on_init_callback
    });

    jQuery("#diaporama-view-btn").bind("click", function () {
        $lightbox.openPopup({
            rel: '#popup',
            closeBtnText: 'fermer',
            themeClass: 'DiaparomaPopup',
            callback: function () {
               diaporama_carousel.reload();
            }
        });

        return false;
    }); 


	jQuery("#diaporama-view-img").bind("click", function () {
        $lightbox.openPopup({
            rel: '#popup',
            closeBtnText: 'fermer',
            themeClass: 'DiaparomaPopup',
            callback: function () {
                diaporama_carousel.reload();
            }
        });

        return false;
    });

    jQuery(".media-gallery a").bind("click", function () {

        var index = $(".media-gallery a").index(this);
        var media_id = $(this).attr("media_id");

        var ext = $(this).attr('href').split('.').pop();

        $lightbox.openPopup({
            rel: '#popup',
            closeBtnText: 'fermer',
            themeClass: 'DiaparomaPopup',
            callback: function () {
        		diaporama_carousel.reload();
        		jQuery(".media_id_"+(media_id)+" a").click();
            },
        	end: function (){alert('fin');} 
        });

        return false;
    });

});


// 


function on_init_callback (carousel, item) {
    if ( diaporama_carousel == undefined ) {
        diaporama_carousel = carousel;
    }

    var active_item = carousel.list.find("li#diaporama-image-active > a");
    active_item.parent().addClass("active-item");

    var active_image_text = active_item.parent().find(".intro-text").eq(0).clone(true);
    jQuery("#image-text").empty().append(active_image_text);

    var active_image_infos = active_item.parent().find(".image-infos").eq(0).clone(true);
    jQuery("#diaporama-image-infos").empty().append(active_image_infos);

    var imgObj = new Image();
    //jQuery("#diaporama-image-viewer").empty();

    jQuery(imgObj).bind("load", function () {
        jQuery("#diaporama-image-viewer").html(imgObj);
    });

    imgObj.src = active_item.attr("href");
	resizeImages(imgObj);
	imgObj.alt = active_item.attr("title");
    imgObj.title = active_item.attr("title");

    carousel.list.find("li > a").each(function (media_id) {
        var el = jQuery(this);

		el.bind("click", function () {
			jQuery('#showDMVideo').remove();

            var image_infos = el.parent().find(".intro-text").eq(0).clone(true);
			jQuery("#image-text").empty().append(image_infos);

            var image_infos = el.parent().find(".image-infos").eq(0).clone(true);
			jQuery("#diaporama-image-infos").empty().append(image_infos);

			if ( active_item != undefined ) {
				active_item.parent().removeClass("active-item");
				active_item = el;
			}
			el.parent().addClass("active-item");
            var ext = el.attr('href').split('.').pop();
           
			switch(ext){
			
				// Les images
				case 'jpg':
				case 'jepg':
				case 'gif':					
				case 'png':					
				case 'bmp':

	                var imgURL = el.attr("href");
	                var imgTitle = el.attr("title");
					var imgObj = new Image();
					jQuery("#diaporama-image-viewer").children().fadeOut("fast", function () {
						jQuery("#diaporama-image-viewer").empty();

						jQuery(imgObj).bind("load", function () {
							jQuery("#diaporama-image-viewer").html(this);
							jQuery(this).fadeIn("fast", function (callback) {
								if ( callback != undefined ) {
									callback();
								}
							});
						});

						imgObj.src = imgURL;
						resizeImages(imgObj);
						imgObj.alt = imgTitle;
						imgObj.title = imgTitle;
					});

					
				break;

				// Les musiques
				case 'mp3':
					var link = el.attr('href');
					jQuery("#diaporama-image-viewer").children().fadeOut("fast", function () {
						playAudio(link, this);
					});
				break;
				
				// Les swf
				case 'swf':
					var link = el.attr('href');
					jQuery("#diaporama-image-viewer").children().fadeOut("fast", function () {
						showSwf(link, this);
					});
				break;
				
				// Les videos dailymotion
				default:
					var imgTitle = el.attr("title");
					var link = el.attr('href');
					jQuery("#diaporama-image-viewer").children().fadeOut("fast", function () {
						showDMVideo(link, this);
					});					
				break;
			
			}
				
			return false;
		});
    });
}




function resizeImages(imgObj)
{
	var newW = 700;
	var newH = 500;
	var imgW = imgObj.width;
	var imgH = imgObj.height;

	if ( (imgW < newW) && (imgH < newH) ) {
		$(imgObj).css({
			'padding-top' : (newH - imgH) / 2
		})
	}
	else {
		if (imgW > newW){
			imgObj.width = newW;
			imgObj.height = imgObj.height * (newW/imgW);
			var imgW = imgObj.width;
			var imgH = imgObj.height;

			if (imgH > newH){
				imgObj.height = newH;
				imgObj.width = imgObj.width * (newH/imgH);
				var imgW = imgObj.width;
				var imgH = imgObj.height;
			}

			jQuery(imgObj).css({
				'padding-top' : (newH - imgH) / 2
			})

		}
		else {
			imgObj.height = newH;
			imgObj.width = imgObj.width * (newH/imgH);
			var imgW = imgObj.width;
			var imgH = imgObj.height;

			if (imgW > newW){
				imgObj.width = newW;
				imgObj.height = imgObj.height * (newW/imgW);
			}
		}
	}
}




function showDMVideo(link, where){
	var spLink = link.split("/");
	var newLink = spLink.pop().split('_')[0];
	var fullLink = "http://www.dailymotion.com/swf/video/" + newLink + "&autoPlay=1";
	
	var dmW = 480;
	var dmH = 360;

	jQuery('' +
		'<div id="showDMVideo">' +
			'<object width="' + dmW + '" height="' + dmH + '">' +
				'<param name="movie" value="' + fullLink + '"></param>' +
				'<param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param>' +
				'<embed type="application/x-shockwave-flash" src="' + fullLink +  '"' +
					' width="' + dmW +  '" height="' + dmH + '" allowfullscreen="true" allowscriptaccess="always">' +
				'</embed>' +
			'</object>' +
		'</div>'
	).fadeIn('fast').appendTo( jQuery(where).parent() );
}

function showSwf(fullLink, where){
	var dmW = 480;
	var dmH = 360;	
	jQuery('' +
		'<div id="showDMVideo">' +
			'<object width="' + dmW + '" height="' + dmH + '">' +
				'<param name="movie" value="' + fullLink + '"></param>' +
				'<param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param>' +
				'<embed type="application/x-shockwave-flash" src="' + fullLink +  '"' +
					' width="' + dmW +  '" height="' + dmH + '" allowfullscreen="true" allowscriptaccess="always">' +
				'</embed>' +
			'</object>' +
		'</div>'
	).fadeIn('fast').appendTo( jQuery(where).parent() );
}


function playAudio(fullLink, where){	
	var dmW = 480;
	var dmH = 360;
	jQuery('' +
		'<div id="showDMVideo">' +
			'<object width="' + dmW + '" height="' + dmH + '">' +
				'<param name="movie" value="/swf/player.swf?file='+ fullLink +'&autostart=true"></param>' +
				'<param name="allowFullScreen" value="true"></param> ' +
				'<param name="allowScriptAccess" value="always"></param>' +
				'<embed type="application/x-shockwave-flash" src="/swf/player.swf"' +
					' width="' + dmW +  '" height="' + dmH + '" allowfullscreen="true" allowscriptaccess="always" flashvars="&file='+ fullLink +'&autostart=true&image=/images/player-audio.jpg&screencolor=FFFFFF">' +
				'</embed>' +
			'</object>' +
		'</div>'
	).fadeIn('fast').appendTo( jQuery(where).parent() );
}
