 /**
* 
* @version 0.1
* @author Andreas Stephan 
* @param {Object} options an object to overwrite the default settings
* 
* @classDescription
*  enables like funcitonality on same domain pages for sigroups verticals
*  for more documentation on the callback see function http://docs.jquery.com/Ajax/jQuery.ajax#options 
*  
*  useage: init on Dom load on a link with a selector of your choice:
*  
*  jQuery(function(){
*    jQuery('.likeLink').sgInlineLike({
*      communityUrl: 'http://www.schoener-essen.de',
*      partnercodeId: 4
*    });
*  });
* 
* 
*
*/
jQuery.fn.sgInlineLike = function(options){
  return this.each(function(){
    var likeLink = this;

    /*
     * @param settings - default configuration
     * 
     */
    var settings = jQuery.extend({
      partnercodeId: '',
      communityUrl : 'http:://stage.schoenerEssen.de',
      
      /**
       * @param beforeSend Function overwrites the standard method that is called before the ajax call is fired
       */
      beforeSend: null, //overwrides the standard BeforeSend Method
      
      /**
       * @param success Function overwrites the standard success method, 
       *    recieves one argument: a json encoded data block containing
       *   - likeCount the new like count
       *   
       */
      success: null,
      
       
      /**
       * param error Function overwrites the standard error method (alert)
       * 
       */
      error: null             
    }, options);        
    
    /**
     * stores currently pending calls to prevent they are fired twice
     */
    var callsInProgess = {
      
    } ;

    jQuery(likeLink).click(function (event) {
      var likeUrl = settings.communityUrl+'/like/likeByUrl/?url='+encodeURIComponent(likeLink.href)+'&partnercodeId='+settings.partnercodeId;
     
      //TODO: set a local cookie, so we know can store the liked iems of this user event if we dont have a server store available
                
      
      jQuery.ajax({
        url: likeUrl,
        dataType: "json",
        likeLink: likeLink,//likeLink is passed in so its accessible for all
        
        /**
         * executed before the ajax request, useful to show a loading animation or similar
         */
        beforeSend: function(XMLHttpRequest){         
          if(callsInProgess[this.url]){
            return false;
          }
          else{
            callsInProgess[this.url] = true;
            if(typeof settings.beforeSend == 'function'){
              return settings.beforeSend.apply(this, arguments);              
            }
            else{
              jQuery(likeLink).removeClass('smallHeartGray').addClass('loading');
            }        
          }
          return true;
        },
        
        success: function(data){          
          if(data.errors){
            if(typeof settings.error == 'function'){
              settings.error.call(this, data.errors);                
            }
            else{
             throw data.errors;
            }          
          }
          
          //custom success function
          else if(typeof settings.success == 'function'){
            settings.success.call(this, data);
          }
          
          //standard success funciton
          else{
            var previousContent = jQuery(likeLink).html(); 
            jQuery(likeLink).html('<span>Danke!</span>');
            setTimeout(function(){
                jQuery(likeLink).find('span').fadeOut('fast', function(){
                  jQuery(likeLink).html(previousContent);
                });
              },
              2000);                                                
          }              
        },
        
        complete: function(XMLHttpRequest, textStatus){
          callsInProgess[this.url] = true;
        },
        
        error: function(XMLHttpRequest, textStatus, errorThrown){            
          if(typeof settings.error == 'function'){
            settings.error.call(this, 'Sorry, something went wrong: '+errorThrown);
          }
          alert('Sorry, an error occurred!');              
        } 
      });
       return false;     
    });
  });
};  