
    $(window).load(function(){
        magnificPopupGroupFn();
        
        $('.jDisplayReply').hide();
    $("#business_category").on('change', function(){
       var cat_id =$(this).val();
       window.location = mainUrl+"organization_list/?groupby=" + cat_id;
    });
    
//    $("#community_category").on('change', function(){
//       var cat_id =$(this).val();
//       window.location = mainUrl+"community/?groupby=" + cat_id;
//    });  // if (data.count != '1 Like' && data.count != '1 Likes') {
        //     currentLikeCount = Number(currentLikeCount) - 1;
        // }
        // else {
        //     currentLikeCount = Number(currentLikeCount) + 1;
        // }
        // $('#jlike_' + announcementId).removeClass();
        // if (data.classtype == '') {
        //     $('#jlike_' + announcementId).addClass('unliked jLikeFeedcommunity');
        // }  // if (data.count != '1 Like' && data.count != '1 Likes') {
        //     currentLikeCount = Number(currentLikeCount) - 1;
        // }
        // else {
        //     currentLikeCount = Number(currentLikeCount) + 1;
        // }
        // $('#jlike_' + announcementId).removeClass();
        // if (data.classtype == '') {
        //     $('#jlike_' + announcementId).addClass('unliked jLikeFeedcommunity');
        // }
        // else {
        //     $('#jlike_' + announcementId).addClass(data.classtype + ' jLikeFeedcommunity');
        // }
        // $('#jcountlike_' + announcementId).html(currentLikeCount + ' Likes');
        // else {
        //     $('#jlike_' + announcementId).addClass(data.classtype + ' jLikeFeedcommunity');
        // }
        // $('#jcountlike_' + announcementId).html(currentLikeCount + ' Likes');
//    $("#my_community_category").on('change', function(){
//       var cat_id =$(this).val();
//       window.location = mainUrl+"my-community/?groupby=" + cat_id;
//    });
//    $("#sub_community_category").on('change', function(){
//       var cat_id =$(this).val();
//       window.location = mainUrl+"my-subscribed-community/?groupby=" + cat_id;
//    });
//    $("#active_status").on('change', function(){
//       var status_id =$(this).val();
//       window.location = mainUrl+"my-subscribed-community/?groupbystatus=" + status_id;
//    });

        // Trigger click event on organization banner image file upload
        $("i#jorgcam").bind('click', function () {
            $("#jorgtimelinepic").trigger('click');
        });

        // Trigger click event on organization profile image file upload
        $("i#jOrgCamProfile").bind('click', function () {
            $("#jorgprofilepic").trigger('click');
        });

        // Initiates cover image upload flow for organization
        $('#jorgtimelinepic').bind('change', function () {
            var uploadUrl = 'user/orgtimelineimageupload/' + $("#business_id").val();
            
            validImageMaximumAndMinimumCheck('jorgtimelinepic', 2000, 452, 1140, 258, uploadUrl, 'cover-wrapper');
            //validImageCheck('jorgtimelinepic', 1140, 258, uploadUrl, 'cover-wrapper');
        });

        // Initiates profile image upload flow for organization
        $('#jorgprofilepic').bind('change', function () {
            var uploadUrl = 'index/addOrgProfilePic/' + $("#business_id").val();
            validImageCheck('jorgprofilepic', 150, 150, uploadUrl, 'timeline_left_pic');
        });


$('.jorganizationimage_feed').live('change',function(e){
//         var aid=e.target.attributes.aid.value;
//          alert(aid);
        var group_id  = $(this).attr('bid');
        var fd = new FormData(); 
        var fileid = 'jorganizationimage_feed';
        //fd.append( 'file',  $('input[type=file]')[0].files[0]);
        fd.append( 'file',  $('#'+fileid)[0].files[0]);
       // alert(fd);exit;
       // $("#loader_"+aid).show();
       
        $.ajax({
          url: mainUrl+"user/uploadorganizationfeed/"+group_id,
          data: fd,
          processData: false,
          contentType: false,
          type: 'POST',
          success: function(data){
              //alert(data);exit;
              // $("#loader_"+aid).hide();
           // $("#Image_Uploads").hide();
            $("#juploadorgimagepreview").html(data);
          }
        });
});


        $('.jLikeFeedOrganization').live('click', function () {

//            console.info('I\'m trying to like this ');

            $.blockUI({message: ''});
//            console.group('Like attr');
            var org_id = $(this).attr('cid');
//            console.log('Org id'+org_id);
            var announcement_id = $(this).attr('aid');
//            console.log('announcement id'+announcement_id);
//            var old_val = 0;

//            var old = $('#jcountlike_' + announcement_id).text().split(' ');
//            console.log('old ' + old);
//            old_val = old[0];
//            console.log('old of 0 ' + old[0]);
//            if (old_val == '') {
//                old_val = 0;
//            }
//            console.groupEnd();

            $.ajax({
                type: 'POST',
                url: mainUrl + 'user/postMyLikeOrganization',
                data: 'org_id=' + org_id + '&announcement_id=' + announcement_id,
                dataType: 'json',
                cache: false,
                success: function (data) {
//                    if (data.count != '1 Like' && data.count != '1 Likes') {
//                        old_val = old_val - 1;
//                    }
//                    else {
//                        old_val = Number(old_val) + 1;
//                    }
                    $('#jlike_' + announcement_id).removeClass();
                    if (data.classtype == '') {
                        $('#jlike_' + announcement_id).addClass('unliked jLikeFeedOrganization');
                    }
                    else {
                        $('#jlike_' + announcement_id).addClass(data.classtype + ' jLikeFeedOrganization');
                    }
                    $('#jcountlike_' + announcement_id).html(data.count);
                }
            });
            return false;
        });
    
    $('.jOrgCommentButton').live('click',function(){
        var announcemnet_id     = $(this).attr('aid');
       // alert(announcemnet_id);
       $('#jCommentBoxDisplayDiv_'+announcemnet_id).toggle();
          if($("#val_"+announcemnet_id).val()>0){
     
       $('#jCommentDisplayDiv_'+announcemnet_id).toggle();
   }
       return false;
    });
    
    $('.jorgimage_file').live('change',function(e){
//         var aid=e.target.attributes.aid.value;
//          alert(aid);
         var aid = $(this).attr('aid');
        var fd = new FormData(); 
        var fileid = 'file_'+aid;
        //fd.append( 'file',  $('input[type=file]')[0].files[0]);
        fd.append( 'file',  $('#'+fileid)[0].files[0]);
        $("#loader_"+aid).show();
        $.ajax({
          url: mainUrl+"user/uploadorganizationimagesubmit/"+aid,
          data: fd,
          processData: false,
          contentType: false,
          type: 'POST',
          success: function(data){
              //alert(data);
               $("#loader_"+aid).hide();
            $("#Image_Uploads").hide();
            $("#juploadimagepreview_"+aid).html(data);
          }
        });
});

        $('.jPostCommentButtonOrganization').live('click', function () {
           
            $.blockUI({ message: '' }); 
            var id = $('#image_id').val();
            var comment_id = $(this).attr('cmid');
            var announcement_id = $(this).attr('aid');
            var organization_id = $(this).attr('cid');
            var count = $('#reply_' + comment_id).val();
            var new_count = Number(count) + 1;

            if (comment_id > 0) {
                var comments = $.trim($("#watermark_" + announcement_id + '_' + comment_id).val());
            }
            else {
                var comments = $.trim($("#watermark_" + announcement_id).val());
            }

            if (!comment_id) {
                if (comments == '' && isNaN(id)) {
                    $("#msg_display_" + announcement_id).html('Please enter comments/images');
                    $("#msg_display_" + announcement_id).addClass('error_div');
                    return false;
                }
            } else {
                if (comments == '' && isNaN(id)) {
                    $("#msg_display_" + comment_id).html('Please enter comments/images');
                    $("#msg_display_" + comment_id).addClass('error_div');
                    return false;
                }
            }
        //    alert(comments);exit;
            $('#jCommentBoxDisplayDiv_' + announcement_id).block({ message: null });
            // $.blockUI({message: ''});
            $.ajax({
                type: 'POST',
                url: mainUrl + 'user/postorganizationfeedcomment',
                data: 'announcement_id=' + announcement_id + '&comments=' + comments + '&organization_id=' + organization_id + '&comment_id=' + comment_id + '&image_id=' + id,
                cache: false,
                success: function (data) {
                    $('#jCommentBoxDisplayDiv_' + announcement_id).unblock();
                    var html = $.parseHTML(data);
                    enableEmojiOn(html);

                    if (comment_id > 0) {
                        var comments = $('#jcountreply_' + comment_id).html().split(' ');
                        var numcomments = Number(comments[0]) + 1;
                        $('#jCommentDisplayDiv_' + announcement_id).show();
                        $('#postingReply_' + comment_id).prepend(html);
                        $("#watermark_" + announcement_id).val("");
                        $('#jcountreply_' + comment_id).html(new_count + ' Replies');
                        $('#watermark__' + comment_id).val('');
                    }
                    else {
                        var comments = $('#jcountcomment_' + announcement_id).html().split(' ');
                        var numcomments = Number(comments[0]) + 1;
                        $('#jCommentDisplayDiv_' + announcement_id).show();
                        $('#posting_' + announcement_id).prepend(html);
                        $("#watermark_" + announcement_id).val("");
                        $('#jcountcomment_' + announcement_id).html(numcomments + ' Comments');

                    }
                    $('.emoji-wysiwyg-editor').html('');
                    // parse facebook share button
                    FB.XFBML.parse();

                    $('#reply_' + comment_id).val(new_count);
                    magnificPopupSingleFn();
                    $(".register_form").remove();
                    $(".emoji-wysiwyg-editor .fa-camera").html(" ");
                    return false;
                }
            })
        });
    
    $('.jorgimagereply_file').live('change', function () {
        //alert("here");
        var cid = $(this).attr('cid');
        var fd = new FormData();
        var fileid = 'imagereply_file_' + cid;
        //fd.append( 'file',  $('input[type=file]')[0].files[0]);
        fd.append('file', $('#' + fileid)[0].files[0]);
//       var file = this.files[0];
//       var formData = new FormData();
//       formData.append('file',file);
//        var id = document.getElementById('reply_comment_id').value;
        //var announce_id = document.getElementById('announceid').value;
        // alert(id);
        $("#imagereply_loader_" + cid).show();
        $.ajax({
          url: mainUrl+"user/uploadimagereplysubmitorganization/"+cid,
          data: fd,
          processData: false,
          contentType: false,
          type: 'POST',
          success: function(data){
            $("#imagereply_loader_"+cid).hide();
            $("#Image_Uploads").hide();
            $("#jDivReplyImagePreview_"+cid).append(data);
          }
        });
    });


    $('.jShowReply, .jOrgShowReply').live('click',function(){
        var comment_id =$(this).attr('id');
        var replyexist =$('#postingReply_'+comment_id).text().length;
       if(replyexist >0){
          $('#postingReply_'+comment_id).toggle();
          return false
       }
       else{
        $.ajax({
                type    :'POST',
                url     : mainUrl+'user/showorgcommentreply',
                data    :'comment_id='+comment_id,
                cache    : false,
                success : function(data){
                    var html = $.parseHTML(data);
                    enableEmojiOn(html);
                    
                    var comments     =$('#jcountreply_'+comment_id).html().split(' ');
                    var numcomments = Number(comments[0])+1;
                    $('#postingReply_'+comment_id).prepend(html);
                    $("#watermark_"+comment_id).val("");
                }
            })
       }
        return false;
    });
    
    $('.jOrgFeedCommentLike').live('click',function(){
        var announcement_id     = $(this).attr('aid');
        var comment_id           = $(this).attr('cid');
         $.ajax({
                type    :'POST',
                url     : mainUrl+'user/postorgCommentLike',
                data    :'announcement_id='+announcement_id+'&comment_id='+comment_id,
                dataType : 'json',
                cache    : false,
                success : function(data){
                    //console.log(data);
                   //$('#jlike_'+announcement_id).html(data.type); 
                   $('#jlikeComment_'+comment_id).removeClass();
                   if(data.classtype==''){
                      $('#jlikeComment_'+comment_id).addClass('unliked jOrgFeedCommentLike'); 
                   }
                   else{
                     $('#jlikeComment_'+comment_id).addClass(data.classtype+' jOrgFeedCommentLike');  
                   }
                   
                   $('#jcountcommentlike_'+comment_id).html(data.count); 
                }
        });
      return false;  
    });
    
    $('.jOrgDeletetButton').live('click',function(){
        if(confirm("This post will be deleted. Are you sure to continue?")){
      //  var comment_id        = $(this).attr('id');
        var announcement_id     = $(this).attr('aid');
        var dataString          = 'announcement_id='+announcement_id;
        $(this).attr('disabled','disabled'); 
        $("#loading_"+announcement_id).show();
        $.ajax({
            type    : "POST",
            url     : mainUrl+"user/orgfeeddelete",
            data    : dataString,
            cache   : false,
            success : function(data){
                $("#loading_"+announcement_id).hide();
                $('#org_feed_'+announcement_id).remove();
               //$('#jdivComment_'+comment_id).remove();
             //  $('#jcountcomment_'+announcement_id).html(data+' comments');
               
             }
           
        });
        //  window.location.reload();
      //  return false;
    }
     return false;
    }) 
    
     $('.jFeedCommentDelete').live('click',function(){
        var comment_id        = $(this).attr('id');
        var announcement_id     = $(this).attr('aid');
        var dataString          = 'comment_id='+comment_id+'&announcement_id='+announcement_id;
        $(this).attr('disabled','disabled'); 
        $("#loading_"+announcement_id).show();
        $.ajax({
            type    : "POST",
            url     : mainUrl+"user/feedOrganizationCommentDelete",
            data    : dataString,
            cache   : false,
            success : function(data){
                $("#loading_"+announcement_id).hide();
                $('.jCommentDisplayDiv #posting_'+announcement_id+' #jdivComment_'+comment_id).remove();
               //$('#jdivComment_'+comment_id).remove();
               $('#jcountcomment_'+announcement_id).html(data+' comments');
               
             }
           
        });
        //  window.location.reload();
        return false;
    });
    
    $("#jFollowPage").live('click',function(){
        var business_id= $(this).attr("aid");
        var business_name= $("#search_selected_activity_buisness_name").val();
        var business_alias= $("#search_selected_activity_buisness_alias").val();
        var business_image= $("#search_selected_activity_buisness_image").val();
        //alert(business_image);
        var image_path= $("#user_image_path").val();
        //var role= $("#role").val();
        var tagg_id=''; 
       // var email= $("#email").val();
        
        if(business_id == '')
            {
                 $('#jmessage').html("Please select a Business");
                 $('#jmessage').addClass("error_div");
                  return false;
            }
            
          
        
       
            
    
        $.blockUI({ message: '' });    
        var dataString          = 'business_id='+business_id+'&tagg_id='+tagg_id;
        $.ajax({
            type    : "POST",           
            url     : mainUrl+"index/addtag",
            async   : false,
            data    : dataString,
            cache   : false,
            success : function(data){ 
              //console.log(data);
             $("#jFollowPage").html('<i class="fa fa-user-times"></i> Unfollow </a>');
             $("#jFollowPage").addClass('jUnfollowPage');
             $("#jFollowPage").attr('bid',data.replace(/^\s+|\s+$/g,''));
             $(".jUnfollowPage").attr('id','');
            
               
            }
        });
        
     });
     
  $('.jUnfollowPage').live('click',function(){
         if(confirm("Are you sure you want to unfollow this page?")){
        //alert("dfjf");exit;
        var id=$(this).attr('bid');
       
        $.blockUI({ message: '' }); 
        $.ajax({
            type    : "POST",
            url     : mainUrl+"user/deletetagged",
            data    : 'id='+id,
            cache   : false,
            success : function(data){
             $(".jUnfollowPage").html('<i class="fa fa-user-plus"></i> Follow </a>');
             $(".jUnfollowPage").attr('id','jFollowPage');
             $("#jFollowPage").removeClass('jUnfollowPage');
            }
        });
         }
     });
    
     $('body').on('click','.jFeedCommentReply',function(){
       var comment_id          = $(this).attr('cid');
        $('.jDisplayReply').hide();
        $('.jCommentDisplayDiv #jdivReply_'+comment_id).toggle();
        return false;
    });

        $('#jViewDiscussion').live('click', function () {
            var alias = $(this).attr('alias');
            $("#myprofile").hide();
            $("#jFeedSearch").hide();
            $.blockUI({message: ''});
            $.ajax({
                type: "POST",
                url: mainUrl + "organizationdiscussion",
                data: "alias=" + alias,
                cache: false,
                success: function (data) {

                    var html = $.parseHTML(data,true);
                    enableEmojiOn(html);
                    $('#jDivDisplayBusiness').html(html);

                    // parse facebook share button
                    FB.XFBML.parse();
                }
            });
        });
        
                $('.jOrgReplyDelete').live('click',function(){
        var comment_id        = $(this).attr('id');
        var announcement_id     = $(this).attr('aid');
        var dataString          = 'comment_id='+comment_id+'&announcement_id='+announcement_id;
        var x = $('#reply_'+announcement_id).val();
        x = x-1;
        //alert(announcement_id);
        //exit;
      //  alert($('#jcountreply_'+announcement_id).innerHTML);
        $(this).attr('disabled','disabled'); 
       $("#loading_"+announcement_id).show();
        $.ajax({
            type    : "POST",
            url     : mainUrl+"user/OrgFeedReplyDelete",
            data    : dataString,
            cache   : false,
            success : function(data){
                $("#loading_"+announcement_id).hide();
                $('#jcountreply_'+announcement_id).html(x+' Replies');
                $('#reply_'+announcement_id).val(x);
               // console.log ("jgj"+announcement_id);
                $('#jdivComment_'+comment_id).hide();
               // $('#jcountreply_'+announcement_id).html(" ");
               
              //  $('#jcountreply_'+announcement_id).innerHTML("6 Replies");
               //$('#jdivComment_'+comment_id).remove();
              // $('#jcountcomment_'+announcement_id).html(data+' comments');
               
             }
           
        });
         // window.location.reload();
        return false;
    })
    
     $('#jMoreOrgGallery').live('click',function(){
         $('.jtab li.active').removeClass('active');
        $('#jViewAssociatedGallery ').addClass('active');
        $( "#jViewAssociatedGallery" ).trigger('click');
    })
     $('#jMoreOrgphotos').live('click',function(){
         $('.jtab li.active').removeClass('active');
        $('#jViewAssociatedPeople ').addClass('active');
        $( "#jViewAssociatedPeople" ).trigger('click');
    })


  $('body').on('click','.jOrgFeedCommentDelete',function(){
           // alert("jdkjgd");exit;
        var comment_id        = $(this).attr('id');
        var announcement_id     = $(this).attr('aid');
        var dataString          = 'comment_id='+comment_id+'&announcement_id='+announcement_id;
        $(this).attr('disabled','disabled'); 
        $("#loading_"+announcement_id).show();
        $.ajax({
            type    : "POST",
            url     : mainUrl+"user/orgFeedCommentDelete",
            data    : dataString,
            cache   : false,
            success : function(data){
                $("#loading_"+announcement_id).show();
                $('.jCommentDisplayDiv #posting_'+announcement_id+' #jdivComment_'+comment_id).remove();
              //  $('.jCommentDisplayDiv #posting_'+announcement_id+' #jdivComment_'+comment_id).remove();
               //$('#jdivComment_'+comment_id).remove();
               $('#jcountcomment_'+announcement_id).html(data+' comments');
               
             }
           
        });
        //  window.location.reload();
        return false;
    }) 
    
    $(".jShowOrgLikeUsers").live('mouseover',function(){
    var id = $(this).attr("id");
    var news_feed_id = id.split("_");
    $("#org_like_users_"+news_feed_id[1]).toggle();
});
    
    });

    /**
     * Appends comments onto 'discussion' tab template onClick() of post button
     */
    var postOrganizationFeed = function () {
        $.blockUI({ message: '' }); 
 $("#organizationfeed_comment").focus();
        var comment = $.trim($("#organizationfeed_comment").val());
        comment = comment.replace(/(?:\r\n|\r|\n)/g, '<br />');
        var imgid = $.trim($("#image_id_org").val());
        var comm_id = $.trim($("#organization_id").val());
        var fd = new FormData();
        var file_data = $("#jorganizationimage_feed").prop("files")[0];
        if (imgid != '' || comment != '') {
            fd.append("file", file_data);
            fd.append("comment", comment);
            fd.append("id", imgid);
            fd.append("organization_id", comm_id);
            $(".jpost_comment_div").block({ message: null });

            $.ajax({
                type: 'POST',
                url: mainUrl + "user/updateorganizationfeed",
                data: fd,
                processData: false,
                contentType: false,
                cache: false,
                success: function (data) {
                $("#organizationfeed_comment").val('');
                    var html = $.parseHTML(data);
                    enableEmojiOn(html);

                    $('.jgroupfeeddisplay_div').prepend(html);
                    $(".jpost_comment_div").unblock();
                    magnificPopupSingleFn();

                    // Reset html values
                    $("#juploadorgimagepreview").html('');
                    $("#jerrordiv").html('');
                    $(".emoji-wysiwyg-editor").html('');
                    $("#image_id").val('');
                     $(".img").attr('src','');
                     $("#image_id_org").val('');
                }
            });
        }
        else {
            $("#jerrordiv").html('Please enter your comment or upload an image');
        }
       
    };

