/** FIXME DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT 
 * DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT
 * 
 * POR FAVOR, SEPARAR EN CLASES, ARCHIVOS, LO QUE SEA, ESTO ES ASQUEROSO   
 */

/**
 *  Crea todo 
 * @return
 */
function launch() {

  //********************************** menu ***********************************
  var submenu = $('ul.menu ol');
  submenu.hide();
  $('ul.menu .submenu-container').click(function(){
    submenu.toggle('clip');
    return false;
  });
  submenu.click(function(event){
    event.stopPropagation();
  });  
  
  //*********************************** Catalogo
  $('ul.product-catalog-menu h4').wrapInner('<a href="#"/>');
  $('ul.product-catalog-menu').accordion({
    header: 'h4',
    icons: { 'header': 'ui-icon-circle-triangle-e',
        'headerSelected': 'ui-icon-circle-triangle-s' },
    animated: 'easeInOutSine',
    active: $('h4.activeCriteria')
  });
  
  
  //*************************** Products ---------------------------------------
  if ($('#products-table').length > 0) {
    var table = $('#products-table');
    table.hide();
    var definitionTermsArray = [];
    table.find('th').slice(2).each(function(index, element) {
      definitionTermsArray.push($(element).html());
    });
    
    var contentContainer = $('#content');
    table.find('tbody').find('tr').each(function(index, tr){
      tr = $(tr);
      
      var header = $('<h3/>').append('<span/><span/><a class="ui-icon ui-icon-plus"/>');
      header.addClass('product ui-widget ui-widget-header ui-corner-all');
      header.children().first().append(tr.children().first().html()).
          end().next().first().append(tr.children().next().first().html());
      
      var definitionList = $('<dl class="product ui-widget ui-widget-content"/>');
      tr.children().slice(2).each(function(index, td){
        var term = $('<dt/>').append(definitionTermsArray[index]);
        var description = $('<dd/>').append($(td).html());
        definitionList.append(term).append(description);
        
        term.mouseenter(function(){
          $(this).next().toggleClass('ui-state-highlight');
        }).mouseleave(function(){
          $(this).next().toggleClass('ui-state-highlight');
        });
        
        description.mouseenter(function(){
          $(this).toggleClass('ui-state-highlight');
        }).mouseleave(function(){
          $(this).toggleClass('ui-state-highlight');
        });
      });
      
      contentContainer.append(header);
      contentContainer.append(definitionList);
      definitionList.hide();
      
      header.find('a').click(function(){
        $(this).toggleClass('ui-icon-plus ui-icon-minus');
        definitionList.slideToggle( 1000, 'easeOutQuart', null);
        return false;
      });
      
    });
  }
  //*************************** clients ---------------------------------------
  if ($('#clients-container')) {
    $('#clients-container').find('.flip').click(function(){
      
      var elem = $(this);
      
      if (elem.data('flipped')) {
        elem.revertFlip();
        elem.data('flipped', false);
      } else {
        var newContent = '<p>' + elem.next().html() +
            elem.next().next().html();
        elem.flip({
          direction:'lr',
          speed: 350,
          content: newContent,
          onEnd: function() {
            elem.find('a').click(function(event){
              event.stopPropagation();
            });            
          }
        });
        elem.data('flipped',true);
      }
    });
  }
  
  //*************************** organizations ----------------------------------
  if ($('#organizations-container')) {    
    $('#organizations-container').find('dt').each(function(i,e){
        $(e).nextUntil('dt').wrapAll('<div/>').parent().css({
          clear: 'both',
          margin: '40px 0 0 0'          
        }).hide();
        
        $(e).click( function(){
          showOrganizationDetails($(e))
        });
    });
  }
  
  //*************************** Home Gallery ----------------------------------



  /*
   * Builds the image gallery on the home page.
   * 
   */
  if ($('#gallery').size() > 0) {
    var total = $('#gallery img').length;
    var rand = Math.floor(Math.random()*total);
    $('#gallery').nivoSlider({
      startSlide:rand,
      keyboardNav: false,
      controlNav: false,
      directionNav: false,
      effect: 'fade',
      animSpeed: 1000,
      pauseTime: 4000
    });  
  };
  //*************************** El mas info del HOme----------------------------
  if ($('#more_info_link')) {
    $('#more_info_link').click(function(){
      $('#more_info').show('clip');
      $('#more_info_link').hide();
      return false;
    });
  }
}

$(document).ready(launch);

function showOrganizationDetails(definitionTerm) {
  var definitionList = definitionTerm.parent();
  
  if (definitionList.next().length > 0) {
    definitionList.next().fadeOut(750, function(){
      definitionList.next().replaceWith(definitionTerm.next().clone());
      definitionList.next().show("slide", { direction: "down" }, 1000);
    });
  } else {
    definitionList.after(definitionTerm.next().clone());    
    definitionList.next().show("slide", { direction: "down" }, 1000);
  }
}
