/**
  * Author: Pras Sarkar (pras@headphono.us)
  * Released: Aug 30, 2009
  * Code is released on GitHub - http://github.com/mxz/citation-helper/tree/master
**/

YUI().use('node', 'json', function(Y) {


  // Create the radio select options from the JSON array
  Y.Node.get('#select-container').appendChild( Y.Node.create('<label for="select-control">Choose a source for your MLA citation:</label>') );
  for(i=0;i<mla_data.length;i++) {
    Y.Node.get('#select-container').appendChild( Y.Node.create("<p><input type=\"radio\" name=\"choices\" value='" + [i] + "'>" + mla_data[i].label + "</p>" ) );
  }


  // Function that's used to generate the citation based on the input fields
  var createCite = function() {
    var formatChosen = Y.get('#format_chosen').get('value');

    var fValue = mla_data[formatChosen].format;
    var result = '';
    if (fValue != null && fValue != '') {
      result = fValue;
      for(i=0;i<mla_data[formatChosen].fields.length;i++) {
        var fName = mla_data[formatChosen].fields[i].name;
        var regex = new RegExp( fName );
        result = result.replace( regex, Y.get('#' + fName).get('value'));
      }
    } else {
      for(i=0;i<mla_data[formatChosen].fields.length;i++) {
        var fName = mla_data[formatChosen].fields[i].name;
        var fFormat = mla_data[formatChosen].fields[i].format;
        var regex = new RegExp( fName );
        var value = Y.get('#' + fName).get('value');
        if (value != null && value != '') {
          result += fFormat.replace( regex, value );            
        }
      }
    }


    Y.all('#result code').remove();
    Y.get('#result').appendChild( Y.Node.create( '<code>' + result + '</code>' ) );

    Y.get( '.container h3' ).removeClass('hide');
    Y.get( '#result' ).removeClass('hide');
    
  }

  // Function to render the form depending on the type of citation source chosen
  var createForm = function( event ) {
    
    var formatType = event.target.get('value');    
    resetForm();

    Y.get('#form').appendChild( Y.Node.create( '<input id="format_chosen" type="hidden" value="' + formatType + '">' ));

    for(i=0;i<mla_data[formatType].fields.length;i++) {
      var fLabel = mla_data[formatType].fields[i].label;
      var fType = mla_data[formatType].fields[i].type;
      var fName = mla_data[formatType].fields[i].name;
      switch( fType ) {
        case 'text':
          Y.get('#form').appendChild( Y.Node.create( '<p><label for="' + fName + '">' + fLabel + ':</label><input id="' + fName + '" name="' + fName + '" type="text" size="20" /></p>' ) );
          break;
        case 'date':
          var today = new Date();
          Y.get('#form').appendChild( Y.Node.create( '<p><label for="' + fName + '">' + fLabel + ':</label><input id="' + fName + '" name="' + fName + '" type="text" size="20" value="' + today.getMonth() + '/' + today.getDate() + '/' + today.getFullYear() + '"/></p>' ) );
          break;
        case 'url':
          Y.get('#form').appendChild( Y.Node.create( '<p><label for="' + fName + '">' + fLabel + ':</label><input id="' + fName + '" name="' + fName + '" type="text" size="20" value="http://"/></p>' ) );
          break;
      }
    }
    
    Y.get('#form').appendChild( Y.Node.create( '<label for="cite">If the result does not auto-update, use this button</label><button id="cite">Create citation</button>' ) );
    Y.on( 'click', createCite, '#cite');
    Y.on( 'blur', createCite, '#form input' );
    
  }
  
  // Resets the form container, removes all the form elements (inputs, buttons, etc.), also clears the results container
  var resetForm = function() {

    Y.all('#form p').remove();
    Y.all('#form label').remove();
    Y.all('#form button').remove();
    Y.all('#form input').remove();
    Y.all('#result code').remove();
    
  }


  // Attaches an EventListener to each input element in the form
  Y.on( 'delegate', createForm, '#select-container', 'click', 'input' );


});
