//Ajax is part of the naijaborn platform
//this file contain ajax core functionality of the site, and if should be change,
// the CTO should be notified about it.

/**
 * Description of network_controller
 *
 * @package naijaborn.com
 * @subpackage controllers
 * @author ebot [dot] tabi [at] gmail [dot] com
 * @license www.afrovisiongroup.com
 * @copyright 2009 - present
 */
$(document).ready(function(){

    //this function will handle the ajax commenting for the network status updates
    $('#submitb').click(function(e){
         $('input[type=submit]', this).attr('disabled', 'disabled');
         $("textarea.text-input").attr("disabled", "disabled").css('color','grey');
         $('#submitb').attr({disable:true, value:'adding.....'});
        e.preventDefault();
        var commentF=$('#comment').val();
         if(commentF.length==0){
				var message ='you are required to fill the form';
                $('#errorInfo').html(message);
				$('#wallerror').slideDown('slow', function(){
					setTimeout(function() {
						$('#wallerror').slideUp('slow');
                        $('input[type=submit]', this).attr({disable:false});
                        $("textarea.text-input").removeAttr("disabled", "disabled");
                        $('#comment').val(' ');
                        $('#submitb').attr({disable:false, value:'post comment'});
					}, 2500);
				});

			}else{
          var id=$('#idc').val();
         $.ajax({
            type: 'POST',
            url:base_url+'network/status/'+id,
            dataType: 'json',
            data: $('#comment'),
            success: function(response){

             if(response.msg == 'success')

			  {
				$('#wallsuc').slideDown('slow', function(){
					$(response.view).appendTo('#successInfo');
				});
                $('#heading').empty().append(response.cmt);
                $('input[type=submit]', this).attr({disable:false});
                $("textarea.text-input").removeAttr("disabled", "disabled");
                $('#comment').val(' ');
                $('#submitb').attr({disable:false, value:'post comment'});


			  }
            else if(response.msg == 'error')
			{

				$('#errorInfo').html(response.msgInfo);
				$('#wallerror').slideDown('slow', function(){
					setTimeout(function() {
						$('#wallerror').slideUp('slow');
                        $('input[type=submit]', this).attr({disable:false});
                        $("textarea.text-input").removeAttr("disabled", "disabled");
                        $('#comment').val(' ');
                        $('#submitb').attr({disable:false, value:'post comment'});
					}, 2500);
				});
			}
          }


       });

            }
    });


            //this function will handle the ajax commenting for the picture section of naijaborn
            $('#submitpb').click(function(e){

               e.preventDefault();
               $('input[type=submit]', this).attr('disabled', 'disabled');
               $("#comment").attr("disabled", "disabled");
               $('#submitpb').attr({disable:true, value:'adding.....'});

               var commentF=$('#comment').val();
               if(commentF.length==0 ){

                   var message ='you are required to fill the form';
                $('#errorInfo').html(message);
				$('#wallerror').slideDown('slow', function(){
					setTimeout(function() {
						$('#wallerror').slideUp('slow');
                        $('input[type=submit]', this).attr({disable:false});
                        $("#comment").removeAttr("disabled", "disabled");
                        $('#comment').val(' ');
                        $('#submitpb').attr({disable:false, value:'post comment'});
					}, 2500);
				});

               }else{

                         var id=$('#pid').val();
                         $.ajax({
                            type: 'POST',
                            url:base_url+'profiles/showPicture/'+id,
                            dataType: 'json',
                            data: $('#comment'),
                            success: function(response){

                              if(response.msg == 'success')
                              {
                                $('#wallsuc').slideDown('slow', function(){
                                    $(response.view).appendTo('#successInfo');
                                });
                                $('#heading').empty().append(response.cmt);
                                $('input[type=submit]', this).attr({disable:false});
                                $("#comment").removeAttr("disabled", "disabled");
                                $('#comment').val(' ');
                                $('#submitpb').attr({disable:false, value:'post comment'});


                              }
                            else if(response.msg == 'error')
                            {

                                $('#errorInfo').html(response.msgInfo);
                                $('#wallerror').slideDown('slow', function(){
                                    setTimeout(function() {
                                        $('#wallerror').slideUp('slow');
                                        $('input[type=submit]', this).attr({disable:false});
                                        $("#comment").removeAttr("disabled", "disabled");
                                        $('#comment').val(' ');
                                        $('#submitpb').attr({disable:false, value:'post comment'});
                                    }, 2500);
                                });
                            }
                          }


                       });

               }


            });


            //this function will handle the ajax commenting for the music.

            $('#submitcm').click(function(e){

               e.preventDefault();
               $('input[type=submit]', this).attr('disabled', 'disabled');
               $("#comment").attr("disabled", "disabled");
               $('#submitcm').attr({disable:true, value:'adding.....'});

               var commentF=$('#comment').val();
               if(commentF.length==0 ){

                   var message ='you are required to fill the form';
                $('#errorInfo').html(message);
				$('#wallerror').slideDown('slow', function(){
					setTimeout(function() {
						$('#wallerror').slideUp('slow');
                        $('input[type=submit]', this).attr({disable:false});
                        $("#comment").removeAttr("disabled", "disabled");
                        $('#comment').val(' ');
                        $('#submitcm').attr({disable:false, value:'post comment'});
					}, 2500);
				});

               }else{

                         var id=$('#mid').val();
                         $.ajax({
                            type: 'POST',
                            url:base_url+'music/playSong/'+id,
                            dataType: 'json',
                            data: $('#comment'),
                            success: function(response){

                              if(response.msg == 'success')
                              {
                                $('#wallsuc').slideDown('slow', function(){
                                    $(response.view).appendTo('#successInfo');
                                });
                                $('#heading').empty().append(response.cmt);
                                $('input[type=submit]', this).attr({disable:false});
                                $("#comment").removeAttr("disabled", "disabled");
                                $('#comment').val(' ');
                                $('#submitcm').attr({disable:false, value:'post comment'});


                              }
                            else if(response.msg == 'error')
                            {

                                $('#errorInfo').html(response.msgInfo);
                                $('#wallerror').slideDown('slow', function(){
                                    setTimeout(function() {
                                        $('#wallerror').slideUp('slow');
                                        $('input[type=submit]', this).attr({disable:false});
                                        $("#comment").removeAttr("disabled", "disabled");
                                        $('#comment').val(' ');
                                        $('#submitcm').attr({disable:false, value:'post comment'});
                                    }, 2500);
                                });
                            }
                          }


                       });

               }


            });


            //this function will handle the ajax commenting on art works
            $('#submitart').click(function(e){

               e.preventDefault();
               $('input[type=submit]', this).attr('disabled', 'disabled');
               $("#comment").attr("disabled", "disabled");
               $('#loading').html('<img src="'+base_url+'images/ajax-loader.gif" width="18" height="16"/>');
               $('#submitart').attr({disable:true, value:'adding.....'});

               var commentF=$('#comment').val();
               if(commentF.length==0 ){

                   var message ='you are required to fill the form';
                $('#errorInfo').html(message);
				$('#wallerror').slideDown('slow', function(){
					setTimeout(function() {
						$('#wallerror').slideUp('slow');
                        $('input[type=submit]', this).attr({disable:false});
                        $("#comment").removeAttr("disabled", "disabled");
                        $('#comment').val(' ');
                        $('#loading').empty();
                        $('#submitart').attr({disable:false, value:'post comment'});
					}, 2500);
				});

               }else{

                         var id=$('#aid').val();
                         $.ajax({
                            type: 'POST',
                            url:base_url+'art/showArt/'+id,
                            dataType: 'json',
                            data: $('#comment'),
                            success: function(response){

                              if(response.msg == 'success')
                              {
                                $('#wallsuc').slideDown('slow', function(){
                                    $(response.view).appendTo('#successInfo');
                                });
                                $('#heading').empty().append(response.cmt);
                                $('input[type=submit]', this).attr({disable:false});
                                $("#comment").removeAttr("disabled", "disabled");
                                $('#comment').val(' ');
                                $('#loading').empty();
                                $('#submitart').attr({disable:false, value:'post comment'});


                              }
                            else if(response.msg == 'error')
                            {

                                $('#errorInfo').html(response.msgInfo);
                                $('#wallerror').slideDown('slow', function(){
                                    setTimeout(function() {
                                        $('#wallerror').slideUp('slow');
                                        $('input[type=submit]', this).attr({disable:false});
                                        $("#comment").removeAttr("disabled", "disabled");
                                        $('#comment').val(' ');
                                        $('#loading').empty();
                                        $('#submitart').attr({disable:false, value:'post comment'});
                                    }, 2500);
                                });
                            }
                          }


                       });

               }


            });


            //this function will handle the ajax commenting on post item
             $('#submitpsb').click(function(e){

               e.preventDefault();
               $('input[type=submit]', this).attr('disabled', 'disabled');
               $("#comment").attr("disabled", "disabled");
               $('#submitpsb').attr({disable:true, value:'adding.....'});

               var commentF=$('#comment').val();
               if(commentF.length==0 ){

                   var message ='you are required to fill the form';
                $('#errorInfo').html(message);
				$('#wallerror').slideDown('slow', function(){
					setTimeout(function() {
						$('#wallerror').slideUp('slow');
                        $('input[type=submit]', this).attr({disable:false});
                        $("#comment").removeAttr("disabled", "disabled");
                        $('#comment').val(' ');
                        $('#submitpsb').attr({disable:false, value:'post comment'});
					}, 2500);
				});

               }else{

                         var id=$('#posid').val();
                         $.ajax({
                            type: 'POST',
                            url:base_url+'post/showPost/'+id,
                            dataType: 'json',
                            data: $('#comment'),
                            success: function(response){

                              if(response.msg == 'success')
                              {
                                $('#wallsuc').slideDown('slow', function(){
                                    $(response.view).appendTo('#successInfo');
                                });
                                $('#heading').empty().append(response.cmt);
                                $('input[type=submit]', this).attr({disable:false});
                                $("#comment").removeAttr("disabled", "disabled");
                                $('#comment').val(' ');
                                $('#submitpsb').attr({disable:false, value:'post comment'});


                              }
                            else if(response.msg == 'error')
                            {

                                $('#errorInfo').html(response.msgInfo);
                                $('#wallerror').slideDown('slow', function(){
                                    setTimeout(function() {
                                        $('#wallerror').slideUp('slow');
                                        $('input[type=submit]', this).attr({disable:false});
                                        $("#comment").removeAttr("disabled", "disabled");
                                        $('#comment').val(' ');
                                        $('#submitpsb').attr({disable:false, value:'post comment'});
                                    }, 2500);
                                });
                            }
                          }


                       });

               }


            });

            //this function will handle the message reply.

             $('#send').click(function(e){

               e.preventDefault();
               $('input[type=submit]', this).attr('disabled', 'disabled');
               $("#message").attr("disabled", "disabled");
               $('#send').attr({disable:true, value:'sending.....'});

               var messag=$('#message').val();
               if(messag.length==0 ){

                   var message ='Please your message is empty';
                $('#errorInfo').html(message);
				$('#wallerror').slideDown('slow', function(){
					setTimeout(function() {
						$('#wallerror').slideUp('slow');
                        $('input[type=submit]', this).attr({disable:false});
                        $("#message").removeAttr("disabled", "disabled");
                        $('#message').val(' ');
                        $('#send').attr({disable:false, value:'send'});
					}, 2500);
				});

               }else{

                         var id=$('#meid').val();
                         $.ajax({
                            type: 'POST',
                            url:base_url+'message/reply/'+id,
                            dataType: 'json',
                            data: $('#message'),
                            success: function(response){

                             if(response.msg == 'success')
                              {
                                $('#wallsuc').slideDown('slow', function(){
                                    $(response.view).appendTo('#successInfo');
                                });
                                $('input[type=submit]', this).attr({disable:false});
                                $("#message").removeAttr("disabled", "disabled");
                                $('#message').val(' ');
                                $('#send').attr({disable:false, value:'send'});


                              }
                            else if(response.msg == 'error')
                            {

                                $('#errorInfo').html(response.msgInfo);
                                $('#wallerror').slideDown('slow', function(){
                                    setTimeout(function() {
                                        $('#wallerror').slideUp('slow');
                                        $('input[type=submit]', this).attr({disable:false});
                                        $("#message").removeAttr("disabled", "disabled");
                                        $('#message').val(' ');
                                        $('#send').attr({disable:false, value:'send'});
                                    }, 2500);
                                });
                            }
                          }


                       });

               }


            });

        

    $('.remove').click(function(e) {
    	 e.preventDefault();
    	 var id=$('#remove_id').val();
               $.fn.jmodal({
                    title: 'Naijaborn.com',
                    content: 'Are you sure you want to remove this post?',
                    buttonText: { ok: 'Remove', cancel: 'Cancel' },
                    okEvent: function(obj, args) {
                    	$.ajax({
                            type: 'POST',
                            url:base_url+'post/remove_article/'+id,
                            dataType: 'json',
                            data: '',
                            success: function(response){

                             if(response.msg == 'success')
                              {
                               $('#post-div_' +id).slideUp('slow', function() {
									$('#post-div' +id).remove();
								  });
								  args.complete();

                              }
                            else if(response.msg == 'error')
                            {

                                $('#errorInfo').html(response.msgInfo);
                                $('#wallerror').slideDown('slow', function(){
                                    setTimeout(function() {
                                        $('#wallerror').slideUp('slow');
                                        $('input[type=submit]', this).attr({disable:false});
                                        $("#message").removeAttr("disabled", "disabled");
                                        $('#message').val(' ');
                                        $('#send').attr({disable:false, value:'send'});
                                    }, 2500);
                                });
                                args.complete();
                            }
                          }


                       });
                        
                    }
                });

            });


    });


function remove_art() {
    	 
    	 var id=$('#remove_id').val();
               $.fn.jmodal({
                    title: 'Naijaborn.com',
                    content: 'Are you sure you want to remove this post?',
                    buttonText: { ok: 'Remove', cancel: 'Cancel' },
                    okEvent: function(obj, args) {
                    	$.ajax({
                            type: 'POST',
                            url:base_url+'post/remove_article/'+id,
                            dataType: 'json',
                            data: '',
                            success: function(response){

                             if(response.msg == 'success')
                              {
                               $('#post-div_' +id).slideUp('slow', function() {
									$('#post-div' +id).remove();
								  });
								  args.complete();

                              }
                            else if(response.msg == 'error')
                            {

                                $('#errorInfo').html(response.msgInfo);
                                $('#wallerror').slideDown('slow', function(){
                                    setTimeout(function() {
                                        $('#wallerror').slideUp('slow');
                                        $('input[type=submit]', this).attr({disable:false});
                                        $("#message").removeAttr("disabled", "disabled");
                                        $('#message').val(' ');
                                        $('#send').attr({disable:false, value:'send'});
                                    }, 2500);
                                });
                                args.complete();
                            }
                          }


                       });
                        
                    }
                });

            }



