/**
 * Show a random tip
 * @author Ignacio Coloma
 */

/*
 * Array methods extracted from Math-extend: http://code.google.com/p/math-extend/
 * @author Lauren Fortin 
 */
Object.extend(Array.prototype, {

    swap: function(index1, index2) {
        var swap = this[index1];
        this[index1] = this[index2];
        this[index2] = swap;
        return this;
    },
    shuffle: function(times) {
        $R(1,((times || this.length) * 3)).each(function(n) {
            this.swap((Math.random() * this.length).floor(),(Math.random() * this.length).floor());
        }.bind(this));
        return this;
    }

}); 
 
var tips = $A([
'LocaleAwareException can be <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/exception-handling#I18nException">automatically translated</a> into a form validation error, transparently.', 
'Javascript and CSS files can be <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/javascript-libraries">concatenated, minified and gzipped</a> automatically at deployment time',
'HTML 5 attributes are used wherever possible',
'The server side is 100% isolated from the javascript library.',
'Remember: there is a "View source" link you can click at any time.',
'<a href="http://loom.extrema-sistemas.org/doc/1.x/ref/form-tags#javascript-validations">Javascript validations</a> work out-of-the-box. We tend to think that\'s cool.',
'<a href="http://loom.extrema-sistemas.org/doc/1.x/ref/annotations">Pluggable annotations</a> means that you can use JPA, Loom or Hibernate annotations to validate user input.',
'Here, <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/persistent-files">file uploads</a> are treated as first-class citizens.',
'Javascript validations are repeated on the server. Yep. The same validations.',
'No animals were hurt while automagically calculating the maxlength and size attributes.',
'Set <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/configuration#development">Config.development</a> to false to disable javascript compression and other optimizations.',
'Performance, performance, performance...',
'Support for JAX-RS annotations: @Path, @GET, @POST, @DELETE and @PUT',
'Automatic MD5-based caching for js and css resources. Open firebug and check it out (even in development mode!)',
'Labels get rendered automatically. Or not. <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/form-tags#decorators">Your choice</a>.',
'You can use <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/form-tags#tuning">renderAsText</a> instead of disabled to render plain text forms.',
'Strongest. I18N. Support. Ever.',
'Third-party jars may include <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/javascript-libraries">their own javascript and css files</a> to be used on-the-fly. Just add to your spring file, and go!',
'<a href="http://loom.extrema-sistemas.org/doc/1.x/ref/accessibility-and-508-compliance">Accessibility</a> is a must. We try our best to ease it under the hoods.',
'Did I say that <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/property-annotations#jpa">JPA annotations</a> can be used to validate forms?', 
'Your changes get redeployed on-the-fly thanks to some <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/using-javarebel">JavaRebel magic</a>.',
'All the best practices in a nutshell. If you find room for improvement, please share!',
'Checkboxes can be used to select <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/form-tags#checkbox-to-set">Set contents</a>.', 
'All your missing i18n resources can be found at <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/internationalization#missing-messages">${temp}/missing-resources.properties</a>.',
'Check the addons project for additional features such as confirmation messages, multiple file uploading, tree components...',
'Use <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/property-annotations#RetrieveEntity">RetrieveEntity</a> to enjoy some db persistence magic.',
'The browser cache can be <a href="http://loom.extrema-sistemas.org/doc/1.x/ref/cache">optimized</a> in a snap',
"Still there? You are looking for the <a href=\"http://extrema-sistemas.com/en/contact\">commercial support</a>, aren't you?"
]).shuffle();

var tipsContainer = $('tips');
var tipCount = 0;
tipsContainer.update(''); 

var updateTips = function() {
  if (tipsContainer.immediateDescendants().length >= 8) {
    var first = tipsContainer.firstDescendant();
    first.identify(); 
    Effect.Fade(first.id);
    Effect.SlideUp(first.id, { duration: 1.5, afterFinish: function(s) {
      s.element.remove();
    }});
    first = null;
  } 
  
  // get next tip
  tipsContainer.insert('<div class="important" style="display:none"><span>' + tips[tipCount++ % tips.length] + '</span></div>');
  new Effect.Appear(tipsContainer.immediateDescendants().last(), { delay: .2 });
};

// check if the right column should be removed
if (/(Action)|(view-source)/.match(window.location.href)) {
  $('tips').remove();
  $('main').setStyle({ marginRight: 0, borderRight: 'none' });
} else {
  new PeriodicalExecuter(updateTips, 10);
}

//Remove the fouc css class once the file has been loaded
// TODO: this could be done in a more efficient way by removing the display attribute 
// of the CSS style instead of using this selector
$$('.fouc').invoke('removeClassName', 'fouc');
