if (Drupal.jsEnabled) {
  vepass_product_context = null;

  // Force a thickbox to open
  // Pass the <a> dom element as parameter
  function tbForceOpenNode(nid) {
    var myLink = document.createElement("a");
    myLink.setAttribute('href', vepass_product_url.nodebody + "/" + nid);
    var t = myLink.title || myLink.name || null;
    var a = myLink.href || myLink.alt;
    var g = myLink.rel || false;
    tb_show(t, a, g);
    myLink.blur();
    return false;
  }

  // Called when fiche produit is displayed
  function nodePrepare(nid) {
    _bindNodeCloseButton();
    _printNodeContext(nid);
    _prepareTabs();
    _updateMediaPager();

    // Simple hack to make sure Ajaxsubmit gets here
    Drupal.behaviors.ajaxSubmit(null);

    // Get comments
    nodeGetComments(nid, 0);
  }

  function _updateMedia(data) {
    if (data) {
      result = Drupal.parseJson(data);
      var media_container = $('#fiche_produit_galerie .vepass-product-medias-media-container');
      media_container.html();
      media_container.html(result.data);
    }
  }

  function _updateMediaPager(data) {
    // Data here, we have to update pager HTML code
    if (data) {
      result = Drupal.parseJson(data);
      var container = $('#fiche_produit_galerie .vepass-product-medias-nav-container');
      container.html();
      container.html(result.data);
    }
    // Update pager links
    var pager_links = $('#fiche_produit_galerie .vepass-product-medias-nav-container a.vepass-product-media-pager');
    pager_links.unbind('click');
    pager_links.click( function() {
      $.get(this.href, null, _updateMediaPager);
      return false;
    });
    // Update media links
    var media_links = $('#fiche_produit_galerie .vepass-product-medias-nav-container a.vepass-product-media-media');
    media_links.unbind('click');
    media_links.click( function() {
      $.get(this.href, null, _updateMedia);
      return false;
    });
  }

  function _nodeGetCommentsCallBack(data) {
    var result = Drupal.parseJson(data);
    $('.vepass-product-node #fiche_produit_avis div.avis-comments-ajax').html("");
    $('.vepass-product-node #fiche_produit_avis div.avis-comments-ajax').append(result.data);
    $(".vepass-product-node #fiche_produit_avis div.avis-comments-ajax div.pager a").click( function() {
      var page = this.href.replace(/.*page=([0-9]*).*/, '$1');
      $.get(this.href, null, _nodeGetCommentsCallBack);
      return false;
    });
  }

  function nodeGetComments(nid) {
    if ((nid = parseInt(nid)) && !isNaN(nid)) {
      $.get(vepass_product_url.comments_ajax + '/' + nid, null, _nodeGetCommentsCallBack)
    }
  }

  function getNidInPath(path) {
    return path.replace(/.*node\/([0-9]*).*/, '$1');
  }

  function changeContext(context) {
    vepass_product_context = context;
  }

  function _prepareTabs() {
    var tabs = {
      '.vepass-product-node #fiche_produit' :'.vepass-product-node div#onglet div.product',
      '.vepass-product-node #fiche_produit_avis' :'.vepass-product-node div#onglet div.avis',
      '.vepass-product-node #fiche_produit_galerie' :'.vepass-product-node div#onglet div.galerie'
    };
    // Code
    $('.vepass-product-node div#onglet div.product').click( function() {
      for (_divClass in tabs) {
        $(_divClass).hide();
        $(tabs[_divClass]).each( function() {
          $(this).removeClass('visible');
          $(this).addClass('hidden');
        });
      }
      // As i wont ever ever ever know why, i just cant get the div class
        // dynamically (maybe var scope???), so this code
        // will be cut/pasted 2 other times..
        $('.vepass-product-node #fiche_produit').show();
        $(tabs['.vepass-product-node #fiche_produit']).each( function() {
          $(this).removeClass('hidden');
          $(this).addClass('visible');
        });
      });
    // CC 2 (gruick)
    $('.vepass-product-node div#onglet div.avis').click( function() {
      for (_divClass in tabs) {
        $(_divClass).hide();
        $(tabs[_divClass]).each( function() {
          $(this).removeClass('visible');
          $(this).addClass('hidden');
        });
      }
      $('.vepass-product-node #fiche_produit_avis').show();
      $(tabs['.vepass-product-node #fiche_produit_avis']).each( function() {
        $(this).removeClass('hidden');
        $(this).addClass('visible');
      });
    });
    // CC 3 (GRUICK)
    $('.vepass-product-node div#onglet div.galerie').click( function() {
      for (_divClass in tabs) {
        $(_divClass).hide();
        $(tabs[_divClass]).each( function() {
          $(this).removeClass('visible');
          $(this).addClass('hidden');
        });
      }
      $('.vepass-product-node #fiche_produit_galerie').show();
      $(tabs['.vepass-product-node #fiche_produit_galerie']).each( function() {
        $(this).removeClass('hidden');
        $(this).addClass('visible');
      });
    });
    

    // jpoesen 15/10/2008: add click event to video thumbnails
    $('.video-thumb').click(function(){
      // extract nid from wrapper div's id attribute
      var id_attrib_params = this.id.split("_");
      var nid = id_attrib_params[1];
      $.get("vepass/product/ajax/media/video/" + nid, function(data){
        // replace media-container area with flashvideo content
        $('#fiche_produit_galerie .vepass-product-medias-media-container').html(data);
      });
    });
    
    // jpoesen 15/10/2008: usability tweak
    // make sure mouse arrow changes to pointer to indicate target has become clickable
    $('.video-thumb').css("cursor","pointer");
  }

  function _bindNodeCloseButton() {
    // Dependant of thickbox, so we need to check function exists
    if (typeof (tb_remove) != 'undefined' && tb_remove instanceof Function) {
      $('.TB_closeWindowButton').click(tb_remove);
    }
  }

  function _printNodeContext(nid) {
    var context_name = '';
    if (vepass_product_context) {
      context_name = vepass_product_context;
    }
    else {
      context_name = 'view';
    }
    var url = vepass_product_url.context_ajax + '/' + context_name + '/' + nid;
    $.ajax( {
      data :'&random=' + (new Date().getTime()),
      type :'GET',
      async :false,
      url :url,
      success : function(data) {
        var result = Drupal.parseJson(data);
        $('.vepass-product-ajax-context-' + nid).html(result['html']);
      },
      cache :false
    });
  }
}
