(function($){
   var nickCache = {};
   var defaultRenderFunction = function(nick, params) {
      var html = '<div tabindex="0" class="ss-nickview-wrapper">';
      html += '<h3>' + nick['username'] + '</h3>';
      
      if (nick['isdisabled'] != true) {
         var tstamp = parseInt(nick['membersince'], 10);
         if (!isNaN(tstamp)) {
            var registredSince = new Date(tstamp);
            var regString = [registredSince.getDate(),
                          (registredSince.getMonth() + 1),
                          registredSince.getFullYear()].join(".");
         
            html += '<p>Registriert seit: ' + regString + '</p>';
         }
         
         if (nick['showemail'] == true) {
            html += '<p><a class="normalButton small emailaction" href="http://my.orf.at' + (nick['emailaction'] || "") + '">E-Mail</a></p>';
         }
      } else {
         html += '<p>User ist inaktiv.</p>';
      }
      
      
      html += '</div>';
      return html;
   };
   
   var methods = {
      'show':  function(toView, renderFunctionParams, renderFunction) {
                  if (toView == null || typeof toView !== 'string') {
                     $.error('Invalid call!');
                     return;
                  }
                  var nick = nickCache[toView];
                  if (nick == null) {
                     $.getJSON("http://my.orf.at/jsonP?uname=" + toView + "&callback=?",
                        function(data) {
                           nick = data;
                           if (nick != null) {
                              nickCache[toView] = nick;
                              $this.after((renderFunction || defaultRenderFunction)(nick, renderFunctionParams));
                           }
                        }
                     );
                  } else {
                     $this.after((renderFunction || defaultRenderFunction)(nick, renderFunctionParams));
                  }
               },
      'hide':  function() {
                  alert("hide");
               }
   };
   
   $.fn.orfonNickView = function( method ) {  
      if ( methods[method] ) {
         return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
      } else if ( typeof method === 'object' || ! method ) {
         return methods.init.apply( this, arguments );
      } else {
         $.error('Method ' +  method + ' does not exist on jQuery.orfonNickView');
      }
   };
  
})(jQuery);

