// JavaScript Document
$(document).ready(function() {
  $.base_url = 'http://localhost:8888/smadmin/'; // Your website name
  $.report_url = 'http://localhost:8888/';
  new materialTouch('.md-ripple');
  var $window = $(window),
    $BackToOriginal = $('.sidebar');
  $(window).on('resize', function() {
    if ($window.width() > 755) {
      $(".sidebar").removeClass("active");
      $('.sidebar').animate({
        left: "0px"
      }, 200);
    }
  });
  $(function() {
    $('#testDiv, #recentSlim').slimscroll({
      height: '300px'
    });
  });
  $("a.timeago").livequery(function() {
    $(this).timeago();
  });
  $("span.timeago").livequery(function() {
    $(this).timeago();
  });
  $(".stcommenttime").livequery(function() {
    $(this).timeago();
  });
  $(".widget, .update_wrap").livequery(function() {
    $(this).emoticons();
  });
  /*Create Advertisement*/
  $('body').on('change', '#bigprofileimageform', function() {
    $("#bigprofileimageform").ajaxForm({
      target: '#ad_image',
      beforeSubmit: function() {

      },
      success: function() {

      },
      error: function() {}
    }).submit();

  });

  $("#ads_title").keyup(function() {
    $("#ads_title_prev").html($(this).val());
  });

  $("#ads_durl").keyup(function() {
    $("#ads_durl_prev").html($(this).val());
  });
  $("#description").keyup(function() {
    $("#ads_description_prev").html($(this).val());
  });
  $("#reklam_web").keyup(function() {
    var x = $(this).val()
    $("#ads_nurl").html(x);
    $("#reklam_basligi").attr("href", x);
  });

  $("#adSave").click(function() {

    var ads_title = $("#ads_title").val();
    var ads_desc = $("#description").val();
    var ads_url = $("#ads_nurl").val();
	var ads_durl = $("#ads_durl").val();
    var ads_img = $(".ad_image").attr('rel');

    var check_url = /^(http|https|ftp):\/\/(www+\.)?[a-zA-Z0-9]+\.([a-zA-Z]{2,4})\/?/;

    var dataString = 'ads_title=' + ads_title + '&ads_description=' + ads_desc + '&ads_url=' + ads_url + '&ads_durl=' + ads_durl + '&ads_img=' + ads_img;

    if (ads_title.length > 0 && ads_desc.length > 0 && check_url.test(ads_url) && ads_img.length > 0 && ads_durl.length > 0) {
      $.ajax({
        type: "POST",
        url: 'ajaxAdvertisment.php',
        data: dataString,
        cache: false,
        beforeSend: function() {},
        success: function(html) {
          $("#ads_title").val('');
          $("#ads_nurl").val('');
          $("#ads_durl").val('');
		  $("#description").val('');
          $("#exampleInputFile").val('');
          $(".ad_image").val('');
          $("#ad_image").html('');
          $("#ads_title_prev").html('Advertisement title');
          $("#ads_description_prev").html('Advertisement Description');
          $("#ads_durl_prev").html('Text URL');

          $("#AdsBlock").prepend(html);

        }
      });
    } else {
      alert("Give valid data. ");
    }

  });
  /* Delete Advertisments */
$("body").on('click', '.adsDelete', function() {
  var ID = $(this).attr("id");
  var dataString = 'ads_id=' + ID;

  jConfirm("Sure you want to delete this adverstisement? There is NO undo!", '',
    function(r) {
      if (r == true) {
        $.ajax({
          type: "POST",
          url: 'ajaxAdDelete.php',
          data: dataString,
          cache: false,
          beforeSend: function() {},
          success: function(html) {
            $("#adBlock" + ID).fadeOut("slow");
          }
        });

      }
    });
  return false;
});
  /*Left Menu Show Hide*/
  $(".icon-side").on('click', function() {
    var state = $(this).data("state");
    if (state === "close") {
      $(this).data("state", 'open');
      $(".sidebar").addClass("active");
      $('.sidebar').animate({
        left: "0px"
      }, 200);
    } else {
      $(this).data("state", 'close');
      $(".sidebar").removeClass("active");
      $('.sidebar').animate({
        left: "-220px"
      }, 200);

    }
  });
  //Anons
  $("#update").focus();

  $("#update_button").click(function() {
    var updateval = $("#annoncement").val();
    var dataString = 'annoncement=' + updateval;
    if ($.trim(updateval).length == 0) {
      swal({
        title: "Please write an article for the announcement.",
        text: "",
        timer: 5000
      });
    } else {
      $.ajax({
        type: "POST",
        url: $.base_url + "sendAnnouncement.php",
        data: dataString,
        cache: false,
        success: function(html) {
          $('#annoncement').val('');
        }
      });
    }
    return false;
  });
  /* updateDelete Users */
  $(".updateDelete").live("click", function() {
    var ID = $(this).attr("id");
    var dataString = 'msg_id=' + ID;

    jConfirm("Sure you want to delete this update? There is NO undo!", '',
      function(r) {
        if (r == true) {
          $.ajax({
            type: "POST",
            url: $.base_url + 'UpdateDelete.php',
            data: dataString,
            cache: false,
            beforeSend: function() {},
            success: function(html) {
              $("#updates" + ID).fadeOut("slow");
            }
          });

        }
      });
  });
  /* Report updateDelete Users */
  $(".ReportupdateDelete").live("click", function() {
    var ID = $(this).attr("id");
    var dataString = 'msg_id=' + ID;

    jConfirm("Are you sure you want to delete send Reporter Tagged ?", '',
      function(r) {
        if (r == true) {
          $.ajax({
            type: "POST",
            url: $.base_url + 'UpdateDelete.php',
            data: dataString,
            cache: false,
            beforeSend: function() {},
            success: function(html) {
              $("#updates" + ID).fadeOut("slow");
            }
          });

        }
      });
  });
  /* Verified and Unverified Users */
  $(".verified").click(function() {
    var X = $(this);
    var ID = $(this).attr("id");
    var REL = $(this).attr("rel")
    var msg;
    var dataString = 'uid=' + ID + '&type=' + REL;

    if (REL.length > 0) {
      msg = "Sure you want to unverify this User?";
    } else {
      msg = "Sure you want to approve as a verified User?";
    }

    jConfirm(msg, '',
      function(r) {
        if (r == true) {
          $.ajax({
            type: "POST",
            url: $.base_url + 'verifyUser.php',
            data: dataString,
            cache: false,
            beforeSend: function() {},
            success: function(html) {
              if (REL.length > 0) {
                $("#users" + ID).fadeOut("slow");
                $("#verify" + ID).fadeIn("slow");
              } else {
                X.fadeOut();
                $("#verified" + ID).html('<span class="label label-blue">Verified</span>');
              }
            }
          });

        }
      });
  });
  // Show status from right side
  $(".ShowStatus").live("click", function(event) {
    $(this).css('left', ($(window).width() - $(this).outerWidth()) / 2 + 'px');
    $(this).css('top', ($(window).height() - $(this).outerHeight()) / 2 + 'px');
    $('body').css('overflow', 'hidden');
    event.preventDefault();
    var id = $(this).data("id");
    $.ajax({
      url: $.base_url + "ShowStatus.php?msg_id=" + id,
      type: 'get',
      beforeSend: function() {
        $("#loader").fadeIn(100);
      },
    }).done(function(data) {
      $("#loader").fadeOut(100);
      $(".reported_sidebar").fadeIn().find(".reported_sidebar-content").animate({
        "right": 0
      }, 200).html(data);
      $(window).load();
    });

  });
  $(".reported_sidebar, .icon-status-close").click(function(e) {
    var preventClick = $(".reported_sidebar-content");

    if (!preventClick.is(e.target) && preventClick.has(e.target).length === 0) {
      $(".reported_sidebar-content").animate({
        "right": "0%"
      }, 200, function() {
        $(".reported_sidebar").fadeOut();
        $('body').css('overflow', 'auto');
      });
    };
  });
  //Active Link
  $(".menu-item a").filter(function() {
    return this.href == location.href.replace(/#.*/, "");
  }).addClass("pactive");

  //Change Script Name
  $("#ChangescriptName").click(function() {
    var updateval = $("#scriptName").val();
    var dataString = 'scriptName=' + updateval;
    if ($.trim(updateval).length == 0) {
      swal({
        title: "Please write your script name.",
        text: "",
        timer: 5000
      });
    } else {
      $.ajax({
        type: "POST",
        url: $.base_url + "changescriptname.php",
        data: dataString,
        cache: false,
        success: function(html) {
          $('#scriptName').val('');
        }
      });
    }
    return false;
  });
  //Change Script Title
  $("#ChangescriptTitle").click(function() {
    var updateval = $("#scriptTitle").val();
    var dataString = 'scriptTitle=' + updateval;
    if ($.trim(updateval).length == 0) {
      swal({
        title: "Please write your script Title.",
        text: "",
        timer: 5000
      });
    } else {
      $.ajax({
        type: "POST",
        url: $.base_url + "changescripttitle.php",
        data: dataString,
        cache: false,
        success: function(html) {
          $('#scriptTitle').val('');
        }
      });
    }
    return false;
  });
  // Upload and Change ScriptLogo
  /* Uploading timelineProfile  Image */
  $('body').on('change', '#scriptLogo', function() {
    $("#LogoForm").ajaxForm({
      target: '#timelineProfilePic',
      beforeSubmit: function() {},
      success: function() {
        if (html.trim() === "1") {
          swal({
            title: "Ayarlarınız kaydedildi!",
            text: ":)",
            timer: 5000
          });
        } else {
          swal({
            title: "Gizlilik ayarınız geçersiz ifade içeriyor!",
            text: ":o",
            timer: 5000
          });
        }
      },
      error: function() {

      }
    }).submit();
  });
  /* Block and Unblock Users */
  $(".block").click(function() {
    var ID = $(this).attr("id");
    var REL = $(this).attr("rel");
    var msg;
    var dataString = 'uid=' + ID + '&type=' + REL;

    if (REL.length > 0) {
      msg = "Sure you want to unblock this User?";
    } else {
      msg = "Sure you want to block this User?";
    }

    jConfirm(msg, '',
      function(r) {
        if (r == true) {
          $.ajax({
            type: "POST",
            url: 'ajaxUserBlock.php',
            data: dataString,
            cache: false,
            beforeSend: function() {},
            success: function(html) {
              $("#users" + ID).fadeOut("slow");
            }
          });

        }
      });
  });
});