//trasforma gli url dentro un testo in link funzionanti

function Linkify(inputText) {
    //URLs starting with http://, https://, or ftp://
    var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with www. (without // before it, or it'd re-link the ones done above)
    var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links
    var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
    var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
    }

//esecuzione del filmato flash (dell'ID passato)

function runSwfObject(videoID) {
  
    var flashvars = {
            
            clip_id: videoID,
            show_portrait: 1,
            show_byline: 1,
            show_title: 1,
            js_api: 1, // required in order to use the Javascript API
            };
                
    var params = {
            
            allowscriptaccess: 'always',
            allowfullscreen: 'true'
            };
                
    var attributes = {};
    
    var swfUrl = "http://vimeo.com/moogaloop.swf?clip_id="+videoID+"&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=00adef&fullscreen=1";
    var id = "video-swfobject";
    var width = "560";
    var height = "315";
    var version = "9.0.0";
    var expressInstallSwfurl = "expressInstall.swf";
    
    swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes);
    }

//script del documento

$(document).ready(function() {
    
    /* per la HOME */

    $('#maincontent-home blockquote .blockquote-sign').before('<span class="dquote-right">&nbsp;</span>');
    $('#maincontent-home h4').after('<div class="text-gallery"></div>');
    $('#maincontent-home .text-gallery').append( $('#maincontent-home blockquote') );
    
    $('.text-gallery').cycle({
      timeout:  13000,
      speed:    4500,
      delay:    -8000,
      pause:    1,
      sync:     0
      });

    /* per le pagine DIRECTOR, DOP, EDITING, SKETCHES */

    if ( $('#video-description').length ) {

      //sostituisce i link in video-description
      oldvideodescription = $('#video-description').html();
      newvideodescription = Linkify(oldvideodescription);
  
      $('#video-description').html( newvideodescription );      
      }

    //attivo jcarousel per la lista di thumb dei film
    
    $('#videoList').jcarousel({
        scroll : 2
        });

    //gestione richiesta ajax del main video
    
    $('.videoThumb').click(function(e) {
        
        e.preventDefault();
        
        $("#videoList img").removeClass("videoSelected");
        $(this).children("img").addClass("videoSelected");
        
        $.blockUI({
          
          message:  '<span>Please wait ...</span>',
          css: {
                  border: 'none',
                  padding: '5px',
                  width: '20%',
                  backgroundColor: '#666',
                  '-webkit-border-radius': '10px',
                  '-moz-border-radius': '10px',
                  opacity: .8,
                  color: '#fff'
                  }
          });
  
        var videoId  = $(this).attr("id");
        var vimeoReq = "http://vimeo.com/api/v2/video/"+videoId+".json?callback=?";
        
        $.ajax({
          
          url: vimeoReq,
          
          complete: function() { $.unblockUI(); },
          
          success: function(videoObj){
                  
                  var videoObj_ID    = videoObj[0]["id"];
                  var videoObj_TITLE = videoObj[0]["title"];
                  var videoObj_DESCR = videoObj[0]["description"];
                  
                  runSwfObject(videoObj_ID);
                  
                  $('#video-footage #video-title').html(videoObj_TITLE);
                  document.title = videoObj_TITLE;

                  $('#video-footage #video-description').html( Linkify(videoObj_DESCR) );
                  
                  },
          
          dataType: "jsonp"
          });
                        
        });

     });

