// JavaScript Document

(function($) {
    $(function(){
        /* Document is ready
        ---------------------------------------- */

        $('.mycarousel').jcarousel({
        //Cacher le carrousel s'il contient moins de 2 éléments
        initCallback: function() {
          if($('.mycarousel').children('li.jcarousel-item').length < 2)
            $('.jcarousel-skin-atlantic').show();
        }
        });

        $('.associate_video a').live('click', function (ev) {
            ev.preventDefault();
            viewer_show (this.title || null, this.href, this.rel);
            //this.blur();
            //return false;
        });

        $('a.viewer-link').live('click', function () {
            viewer_show (this.title || null, this.href, this.rel);
            this.blur();
            return false;
        });
        if ($('a.viewer-link')[0]) {
            viewer_show($('a.viewer-link')[0].title, $('a.viewer-link')[0].href, $('a.viewer-link')[0].rel);
        }

        function viewer_show (caption, url, rel) {

            var monitor = $('#viewer-screen');
            var monitorVideo = $('#videocontent');
            var monitorLink = monitor.find('#imgcontent');
            var baseURL;

            // Pause d'une éventuelle vidéo
            if ('play' in document.createElement('video')) {
                var video = monitorVideo.find('video')[0].pause();
            }

            if (/^video:/.test(url)) {
                var u = '/video/get?';
                if (typeof easytoog_admin !== 'undefined')
                {
                    u = '/sites/atlantic_v2/index.php/video/get?admin=true&';
                }
                url = u + 'src=' + url.substr(6) + '.video';
            }

            var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$|\.flv|\.xml|\.video$|\.pdf/;
            var urlType = url.toLowerCase().match(urlString);

            // Code to show images
            if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp') {
                var monitorImg = monitorLink.find('img:first');

                monitorVideo.hide();
                monitorLink.show();
                monitorLink.attr({
                    href: rel,
                    title: caption
                });
                monitorLink.css({cursor:'auto'});
                monitorImg.attr({
                    src: url,
                    alt: caption,
                    title: caption
                });

                monitorLink.unbind('click');
            }

            else if (urlType == '.pdf') {
                var monitorImg = monitorLink.find('img:first');

                monitorVideo.hide();
                monitorLink.show();
                monitorLink.attr({
                    href: url,
                    title: caption
                });
                monitorLink.css({cursor:'pointer'});
                monitorImg.attr({
                    src: rel,
                    alt: caption,
                    title: caption
                });

                monitorLink.click(function(ev){
                  ev.preventDefault();
                  document.location.href = $(this).attr('href');
                });

            }

            // video
            else if (urlType == '.video') {
                var supportVideo = 'play' in document.createElement('video');

                monitorLink.hide();
                $.getJSON(url, { supportVideo: supportVideo } , function(data) {
                  if (data) {
                    if (supportVideo) {

                      var links = $('#liens');
                      links.empty();
                      $.each(data.links, function(i, href){
                          var cl = 'masque' + data.links.length + '_' + i;
                          if (href.substr(-6) == '.video') cl += ' viewer-link';
                          links.append($('<a href="' + href + '" class="' + cl + '">').html('&nbsp;'));
                      });

                      var video = monitorVideo.find('video');
                      video.empty();
                      $.each(data.formats, function(format, src){
                        video.append($('<source src="' + src + '" type="' + format + '" />'));
                      });
                      video.attr('width', data.width).attr('height', data.height)
                      monitorVideo.show();
                      VideoJS.setupAllWhenReady();
                      video[0].load();
                      video[0].play();
                      video.bind('ended', function(ev){ $(this).prev().show(); });
                      video.bind('play', function(ev){ $(this).prev().hide(); });
                    } else {
                      var $video = monitorVideo.find('video');

                      var flashvars = {urlXml: data.xml},
                          params = {
                            wmode: "transparent",
                            allowScriptAccess: 'always'
                          },
                          attributes = {};
                          playerURL = typeof easytoog_admin == 'undefined' ? '/images/atlantic_player.swf' : '/sites/atlantic_v2/images/atlantic_player.swf';

                      swfobject.embedSWF(playerURL, 'flv-player', data.width, data.height, "9.0.0", "", flashvars, params, attributes);

                      /*AC_FL_RunContent(
                        'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
                        'flashvars',data.flv,
                        'width', data.width,
                        'height', data.height,
                        'src', 'atlantic_player',
                        'quality', 'high',
                        'pluginspage', 'http://www.adobe.com/go/getflashplayer_fr',
                        'align', 'middle',
                        'play', 'true',
                        'loop', 'true',
                        'scale', 'showall',
                        'wmode', 'window',
                        'devicefont', 'false',
                        'id', 'atlantic_player',
                        'bgcolor', '#000000',
                        'name', 'atlantic_player',
                        'menu', 'true',
                        'allowFullScreen', 'true',
                        'allowScriptAccess','sameDomain',
                        'movie', 'atlantic_player',
                        'salign', ''
                      ); //end AC code*/
                    }
                  }
                });
            }

            else if (url) {

                var monitorImg = monitorLink.find('img:first');

                monitorVideo.hide();
                monitorLink.show();
                monitorLink.attr({
                    href: url,
                    title: caption
                });
                monitorLink.css({cursor:'pointer'});
                monitorImg.attr({
                    src: rel,
                    alt: caption,
                    title: caption
                });

                monitorLink.click(function(ev){
                  ev.preventDefault();
                  document.location.href = $(this).attr('href');
                });

            }
        }

    });
})(jQuery);

