(function($){
 var $$;

$$ = $.fn.followup = function($options) {

    // extend the options
	var $opts = $.extend($$.defaults, $options);

    for (var i in $opts) {
		if (i) {
			$.followup[i]  = $opts[i];
		}
	}

};

$$.follow = function(){
    $.ajax({
        type:'POST',
        url: '/_json_/followup?timestamp=' + Number(new Date()),
        dataType: "json",
        data: {
            action:'follow',
						leader_data:$$.defaults.oObserveData,
						observe_config:oObservedActions
        },
				beforeSend:function(){
					showGlobalLoader();
        },
				success:function(json){
		

					var aAffectedSubjects = json.affected.split(',');
					var bOverallNotificationPosted = false;
					var oNotificationTarget = {};

					$('#followup_save').attr({disabled:false});

					$.each(aAffectedSubjects, function(i,id){
						if(!bOverallNotificationPosted){
							if($('#'+id+'_notification').length==1){
								bOverallNotificationPosted = false;
								oNotificationTarget = $('#'+id+'_notification');
								aNotificationHide.push(id+'_notification');

								if(empty(rNotificationHide)){
									rNotificationHide = window.setInterval(function(){
										$('#'+aNotificationHide[iToHideIndex]).hide();
										iToHideIndex++;
										if(empty(aNotificationHide[iToHideIndex])){
											window.clearInterval(rNotificationHide);
											rNotificationHide = null;
										}
									},2500);
								}

							}	
							else{
								bOverallNotificationPosted = true;
								oNotificationTarget = $('#'+sActionInfoContainerId);	
							}
						}
					});
					oNotificationTarget.html(json.notification);


					hideFollowOptions();
					hideGlobalLoader();
				},
        error:function(){}
    });

};

$$.unfollow = function(){
   $.ajax({
       type:'POST',
       url: '/_json_/followup?timestamp=' + Number(new Date()),
       dataType: "json",
       data: {
          action:'unfollow',
          leader_data:$$.defaults.oObserveData
        },
        beforeSend:function(){
					showGlobalLoader();
        },
				success:function(json){
					$('#'+$$.defaults.aSctionInfoContainerId).innerHTMl = json.notification;
					hideGlobalLoader();
				},
        error:function(){}
    });

};

$$.getFollowOptions = function(){
   $.ajax({
       type:'POST',
       url: '/_json_/followup?timestamp=' + Number(new Date()),
       dataType: "json",
       data: {
           action:'get_follow_options',
           leader_data:$$.defaults.oObserveData
        },
        beforeSend:function(){
					showGlobalLoader();
        },
				success:function(json){
					if(json.count==1 && empty(json.options[0].actions)){
						oObservedActions = {};
						oObservedActions[json.options[0].id] = {
							single:true
						};
						$$.follow();
					}
					else if(json.count > 1 || json.count==1 && !empty(json.options[0].actions)){
						var jTemplate = {};

            $('#'+sActionsListContainerId).html('');
						$.each(json.options, function(i,section){
							
							jTemplate = $.template( $('#'+sActionsListSectionTpl).html() );

							var sCheckboxHidden = json.count>1 ? '' : 'display:none;' ;

							$('#'+sActionsListContainerId).append(jTemplate,{
								section_chkbox_display: sCheckboxHidden,
								section_name:section.name,
								section_id:section.id
              });

							$('#'+sActionsContainerId+section.id).html('');

							$.each(section.actions, function(ii,option){
    						jTemplate = $.template( $('#'+sActionsListActionTpl).html() );
								$('#'+sActionsContainerId+section.id).append(jTemplate,{
									action_id:option.ID,
									action_parent_id:section.id,
									action_name:option.DESCRIPTION
                });

								if(empty(oObservedActions[section.id])){
									oObservedActions[section.id] = {
										follow:true,								
										actions:{}
									}
								}

								oObservedActions[section.id]['actions'][option.ID] = true;

							});
						});

						$('input.follow_section_chbx').change(function(e){followSectionsAction(e.target);});
						$('input.follow_action_chbx').change(function(e){followOptionsAction(e.target);});

						jQuery.overbox.show('#'+sActionsOverboxId);

					}
					hideGlobalLoader();
				},
        error:function(){}
    });

};

$$.getObserved = function(sLeaderType,fCallback){
   $.ajax({
       type:'POST',
       url: '/_json_/followup?timestamp=' + Number(new Date()),
       dataType: "json",
       data: {
           action:'get_observed',
           leader_type:sLeaderType
        },
        beforeSend:function(){
//					showGlobalLoader();
        },
				success:function(json){
					if(!empty(json.observed)){
						fCallback(json.observed);				
					}
				},
        error:function(){}
    });

};

/**
 *
 * @name $$.defaults
 *
 * @desc Domyslne zmienne dla obiektu
 *
 *
**/

$$.defaults = {
		oObserveData:null,
    oExtraParams  : {}
};




/**
 *
 * @name jQuery.comments
 *
 * @desc The global galleria object holds four constant variables and four public methods:
 *       $.comments.activate(_src) = displays an image from _src in the galleria container.
  *
**/

$.extend({followup : {
				follow : function(){
					$$.follow();
        },
				unfollow : function(){
					$$.unfollow();
				},
				getFollowOptions:function(){
					$$.getFollowOptions();
				},
				addToDefaults:function(oConfObj){
					if(!empty(oConfObj)){
						$.each(oConfObj, function(k,item){			
							$$.defaults[k] = item;
						});
					}
				},
				getObserved:function(sLeaderType,fCallback){
					$$.getObserved(sLeaderType,fCallback);
				}
    }
});

})(jQuery);


