(function($) {
  $.fn.GestYoutube = function(option) {
    var defaults = {
      array_video_big: new Array(),
      array_video_small: new Array(),
      colore: '3a3a3a',
      coloredue: '999999',
      position_big: 'up'
    };

    $(this)[0].VariabiliObject = $.extend(defaults, option);

    if ($(this)[0].VariabiliObject.array_video_big.length <= 1)
      return;

    $(this)[0].array_percorso_video = new Array();

    for ($(this)[0].i = 0; $(this)[0].i < $(this)[0].VariabiliObject.array_video_big.length; $(this)[0].i++) {
      $(this)[0].array_percorso_video[$(this)[0].i] = $(this)[0].VariabiliObject.array_video_big[$(this)[0].i].substring($(this)[0].VariabiliObject.array_video_big[$(this)[0].i].indexOf('?v=') + 3);
    }

    $("#" + $(this)[0].id).append("<div id='contenitore_video_" + $(this)[0].id + "'><object width=\"400\" height=\"300\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + $(this)[0].array_percorso_video[0] + "&hl=jp&fs=1&color1=0x" + $(this)[0].VariabiliObject.colore + "&color2=0x" + $(this)[0].VariabiliObject.coloredue + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/" + $(this)[0].array_percorso_video[0] + "&hl=jp&fs=1&color1=0x" + $(this)[0].VariabiliObject.colore + "&color2=0x" + $(this)[0].VariabiliObject.coloredue + "\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"400\" height=\"300\"></embed></object></div>");

    if ($(this)[0].VariabiliObject.position_big == 'up') {
      $("#" + $(this)[0].id).append("<br/><div id='small_" + $(this)[0].id + "' ></div>");
    }
    else if ($(this)[0].VariabiliObject.position_big == 'down') {
      $("#" + $(this)[0].id).prepend("<div id='small_" + $(this)[0].id + "' ></div><br/>");
    }

    else if ($(this)[0].VariabiliObject.position_big == 'right') {
      $("#" + $(this)[0].id).prepend("<div id='small_" + $(this)[0].id + "' class='movie_right'></div>");
    }

    for ($(this)[0].i = 0; $(this)[0].i < $(this)[0].VariabiliObject.array_video_small.length; $(this)[0].i++) {
      $("#small_" + $(this)[0].id).append("<img id='img_" + $(this)[0].i + "_" + $(this)[0].id + "' src='" + $(this)[0].VariabiliObject.array_video_small[$(this)[0].i] + "' style='cursor:pointer' width=\"100\" height=\"75\" />");
      $("#img_" + $(this)[0].i + "_" + $(this)[0].id)[0].onclick = function() {
        var temp = $(this)[0].id.substring(4);
        var numero = temp.substring(0, temp.indexOf("_"));
        $("#contenitore_video_" + this.parentNode.parentNode.id).html("<object width=\"400\" height=\"300\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + $("#" + this.parentNode.parentNode.id)[0].array_percorso_video[numero] + "&hl=jp&fs=1&color1=0x" + $("#" + this.parentNode.parentNode.id)[0].VariabiliObject.colore + "&color2=0x" + $("#" + this.parentNode.parentNode.id)[0].VariabiliObject.coloredue + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/" + $("#" + this.parentNode.parentNode.id)[0].array_percorso_video[numero] + "&hl=jp&fs=1&color1=0x" + $("#" + this.parentNode.parentNode.id)[0].VariabiliObject.colore + "&color2=0x" + $("#" + this.parentNode.parentNode.id)[0].VariabiliObject.coloredue + "\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"400\" height=\"300\"></embed></object>");
      }

    } 
  } 
})
(jQuery);
