$( window ).load(function() {
    
    
    if($("#serachbuisness" ).length){
            $( "#serachbuisness" ).autocomplete({
            source: mainUrl+"index/searchbusiness",
            minLength:1,
            select: function( event, ui ) {
               // console.log(ui);
                 $(event.target).val(ui.item.label);
                 $("#search_selected_activity_buisness").val(ui.item.id);
                 $("#search_selected_activity_buisness_name").val(ui.item.label);
                 $("#search_selected_activity_buisness_alias").val(ui.item.value);
                 $("#search_selected_activity_buisness_image").val(ui.item.image);
                 $("#serachbuisness").val(ui.item.label);
                 
                 if(ui.item.type == 'user'){
               //  window.location.href = mainUrl+'profile/'+ui.item.value;
                 }
                 if(ui.item.type == 'business')
                 { 
                  //    window.location.href = mainUrl+'directory/'+ui.item.value;
                 }
                  if(ui.item.type == 'community')
                 { 
                   //   window.location.href = mainUrl+'community/'+ui.item.value;
                 }
                 return false;
            }
        }).data( "autocomplete" )._renderItem = function( ul, item ) {
            
        if(item.type=='user'){    
         if(item.image == null || item.image == '')
               {   
                   
                  
                   var image = 'member_noimg.jpg';
               
                  
                   
               }else{
                  var image = item.image;
               }    
       
        var inner_html = '<a><div class="list_item_container"><div class="image"><img src="' +mainUrl+'project/files/thumb/'+ image + '"></div><div class="label">' + item.label + '</div><span class="search_info">User</span></div></a>';
         
    }
        if(item.type=='business'){   
            if(item.image == null || item.image == '')
               {    var image = 'noimage.jpg';
                   
                   
               }else{
                  var image = item.image;
               }   
        var inner_html = '<a><div class="list_item_container"><div class="image"><img src="' +mainUrl+'project/files/thumb/'+ image + '"></div><div class="label">' + item.label + '</div><span class="search_info">Page</span></div></a>';
        
    }
        
         if(item.type=='community'){ 
           if(item.image === null || item.image == '')
               {    var image = 'noimage.jpg';
                   
                   
               }else{
                  var image = item.image;
               }      
         
        var inner_html = '<a><div class="list_item_container"><div class="image"><img src="' +mainUrl+'project/files/thumb/'+ image + '"></div><div class="label">' + item.label + '</div><span class="search_info">Group</span></div></a>';
       
    }
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append(inner_html)
            .appendTo( ul );
    };



    }


//   $("#role" ).autocomplete({
//            source: mainUrl+"index/searchdesignation",
//            minLength:1,
//            select: function( event, ui ) {
//                 $(event.target).val(ui.item.label);
//                
//                 $("#role").val(ui.item.label);
//              
//                 return false;
//            }
//        }).data( "autocomplete" )._renderItem = function( ul, item ) {
            
       
           
       
//        var inner_html = '<a><div class="list_item_container"><div class="label">' + item.label + '</div></div></a>';
//         
//    
//        
//        
//        
//        return $( "<li></li>" )
//            .data( "item.autocomplete", item )
//            .append(inner_html)
//            .appendTo( ul );
//    };
//










  $('#serachbuisnessform').submit(function(e){
   //  alert("kjgkdf");exit;
       // e.preventDefault();
        var business_id= $("#search_selected_activity_buisness").val();
        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= $("#tagg_id").val(); 
       // 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);exit;
               if(tagg_id>0 || data.tagg_id>0)
                   {
                      //document.location = mainUrl+"myprofile";
                       //window.location.reload();
                    // $("#tag_list_"+tagg_id).remove();
                   // $("#role_"+tagg_id).html(role);
                    $("#email_id_"+tagg_id).html(email);
                    $("#business_"+tagg_id).html(business_name);
                    $("#business_"+tagg_id).attr("href",mainUrl+"group/"+business_alias);
                    $("#image_link_"+tagg_id).attr("href",mainUrl+"group/"+business_alias);
                    $("#image_"+tagg_id).attr("src",image_path+business_image);
                   }
               
               
                        $("#search_selected_activity_buisness").val('');
                       // $("#role").val('');
                        $("#tagg_id").val('');
                        $("#serachbuisness").val('');
                        $("#email").val('');
                        
                        //$("#tagList").prepend(data);
                       var parsed = JSON.parse(data);

                    var arr = [];

                    for(var x in parsed){
                      arr.push(parsed[x]);
                    }
                    //console.log(arr);
               if(arr[2]>0){   
                $('#jmessage').html("Page Tagged Successfully!");
                $('#jmessage').addClass("success_div");
                $('#jmessage').fadeIn('fast');
                 setTimeout(function() {$('#jmessage').fadeOut('fast');
                                 }, 5000);
                // $("html, body").animate({ scrollTop: '0px' }, 1000);
                 return false;
            }
            else{
                $('#jmessage').html("You have already tagged this page!");
                $('#jmessage').addClass("error_div");
//                $('#jmessage').fadeIn('fast');
//                 setTimeout(function() {$('#jmessage').fadeOut('fast');
//                                 }, 5000);
//                 $("html, body").animate({ scrollTop: '0px' }, 1000);
                 return false; 
            }
                   
               
            }
        });
        
     });

});  
    
    
    
 
    function updateTags(tagg_id,bus_id,email)
    {
       var business = $("#business_"+tagg_id).text();
       var role = $("#role_"+tagg_id).html(); 
       var email_id = $("#email_id_"+tagg_id).html(); 
       var image = $("#image_"+tagg_id).attr('src');
       var split_img = image.split("thumb/");
       $("#search_selected_activity_buisness_name").val(business);
       $("#search_selected_activity_buisness_image").val(split_img[1]);
       $("#search_selected_activity_buisness").val(bus_id);
         $("#serachbuisness").val(business);
        $("#role").val(role);
        $("#email").val(email_id);
       $("#tagg_id").val(tagg_id); 
    $("#serachbuisness").focus();
    }
    
    function deleteTags(tagg_id,bus_id)
    {
        if(confirm("Are you sure you want to delete the business?")){
        
         
        var dataString          = 'business_id='+bus_id+'&tagg_id='+tagg_id;
        $.blockUI({ message: '' }); 
        $.ajax({
            type    : "POST",
           
           
            url     : mainUrl+"index/removetag",
            async : false,
            data    : dataString,
            cache   : false,
            success : function(data){
               
               if(tagg_id>0)
                   {
                       $("#tag_list_"+tagg_id).remove();
                   }
               
               
                       $("#search_selected_activity_buisness").val('');
                        $("#role").val('');
                        $("#tagg_id").val('');
                        $("#serachbuisness").val('');
                        $('#jmessage').html("Business deleted!");
                        $('#jmessage').addClass("success_div");
                        return false;
                       
                  
              
            }
        });
    }
       
        
        
        
          
        
    }
  
    