

$(function(){
$('[data-toggle="tooltip"]').tooltip({html:true});
$('[data-toggle="popover"]').popover({html:true});
});

$(document).ready(function(){
var order = $(".media-items").attr('id');
$(".media-items").html('<div class="spinner"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>');
$.get("ajax.php?case=ajax_media_items", { "page": 1, "order": order }, function(result){
$(window).lazyLoadXT();
$('.media-items').html(result);
});	
});

$(document).ready(function(){
var id = $('.category-media-items').attr('id');
$('#category-media-number').html('<i class="fa fa-spinner fa-spin"></i>');
$.get("ajax.php?case=ajax_category_media_items_count", { "id": id }, function(number){
$('#category-media-number i').remove();
$('#category-media-number').html(number);
});	
$(".category-media-items").html('<div class="spinner"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>');
$.get("ajax.php?case=ajax_category_media_items", { "page": 1, "id": id }, function(result){
$(window).lazyLoadXT();
$('.category-media-items').html(result);
});	
});

$(document).ready(function(){
var word = $('.search-media-items').attr('id');
if (word != '') {
$('#search-media-number').html('<i class="fa fa-spinner fa-spin"></i>');
$.get("ajax.php?case=ajax_search_media_items_count", { "word": word }, function(number){
$('#search-media-number i').remove();
$('#search-media-number').html(number);
});	
}
$(".search-media-items").html('<div class="spinner"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>');
$.get("ajax.php?case=ajax_search_media_items", { "page": 1, "word": word }, function(result){
$(window).lazyLoadXT();
$('.search-media-items').html(result);
});	
});

function media_lookup(inputString) {
				if(inputString.length < 3) {
						$('#search-instant-results').hide();
				} else {
						$.post("ajax.php?case=ajax_instant_search", {queryString: ""+inputString+""}, function(data){
							if(data.length > 0) {
							$('#search-instant-results').show();
							$('#search-instant-results').html(data);
						}
						});
				}
			} 

$(document).ready(function() {
if (language_direction == 'ltr') {
	var owl_nav_lang = ["<i class='fa fa-chevron-left'></i>","<i class='fa fa-chevron-right'></i>"];
} else {
	var owl_nav_lang = ["<i class='fa fa-chevron-right'></i>","<i class='fa fa-chevron-left'></i>"]
}

      $("#featured-media").owlCarousel({
        items : 4,
        lazyLoad : true,
		pagination : false,
        navigation : true,
		autoPlay: true,
		stopOnHover: true,
		touchDrag: true,
		responsive: true,
		navigationText : owl_nav_lang
      });
	  
	   $("#featured-lists").owlCarousel({
        items : 4,
        lazyLoad : true,
		pagination : false,
        navigation : true,
		autoPlay: true,
		stopOnHover: true,
		touchDrag: true,
		responsive: true,
		navigationText : owl_nav_lang
      });
});


function ConfirmLogOut() {
	if(confirm(logout_confirm))
		{
		$.ajax({
		type: "POST",
		url: 'ajax.php?case=logout',
		data: 'logout=1',
		success: function(result) {
			if (result == 1) {
				document.location.href = './';
			} else {
				alert(error_happened);
			}
		}
		});
	} else {
		confirm.close();
	}
}

$(function() {
  $("#login_btn").click(function() {
		var username = $("input#login-username").val();
		var password = $("input#login-password").val();
		var remember = $("input#remember").val();
		var currentpage = $("input#currentpage").val();
		
	var dataString = 'username='+ username + '&password=' + password +'&remember='+remember;
	$("#login_btn").append(' <span class="fa fa-spinner fa-spin"></span>');
	$.ajax({
      type: "POST",
      url: 'ajax.php?case=login',
      data: dataString,
      success: function(result) {
	  if (result == 1) {
		  setTimeout(
		  function() 
		  {
		document.location.href = './';
		}, 2000);
	  } else {
		$("#login_btn span").remove();
	    $("div#login-message").html(result);
	  }
      }
     });
    return false;
	});
});


$(function() {
  $("#import_btn").click(function() {
	var video_site = $("input[name=video_site]:checked").val();
	var category = $("select#category option:selected").val();
	var link = $("input#link").val();
	$("#import_btn").append(' <span class="fa fa-spinner fa-spin"></span>');
	var dataString = 'video_site='+ video_site + '&category=' + category +'&link='+link;
	$.ajax({
      type: "POST",
      url: 'ajax.php?case=fetch_single_video',
      data: dataString,
      success: function(result) {
	  if (result == 1) {
	    $("#import_btn span").remove();
		$("#upload-message").html('<div class="alert alert-warning">'+all_required_fileds+'</div>');
	  } else if (result == 2) {
	    $("#import_btn span").remove();
		$("#upload-message").html('<div class="alert alert-warning">'+link_not_related+'</div>');
	  } else if (result == 3) {
	    $("#import_btn span").remove();
		$("#upload-message").html('<div class="alert alert-warning">'+invalid_video_link+'</div>');
	  } else if (result == 4) {
	    $("#import_btn span").remove();
		$("#upload-message").html('<div class="alert alert-warning">'+video_imported_before+'</div>');
	  } else if (result == 5) {
	    $("#import_btn span").remove();
		$("#upload-message").html('<div class="alert alert-danger">'+error_happened+'</div>');
	  } else {
		$("#import_btn span").remove();
	    $("#upload-message").html('<div class="alert alert-success">'+video_imported_successfully+'</div>');
	  }
	  }
     });
	});
});


$(function() {
  $(".media-vote-up").click(function() {
	var media_id = $(".media-vote-up").attr('scope');
	$(".media-vote-up i").removeClass('fa-thumbs-up');
	$(".media-vote-up i").addClass('fa-spinner fa-spin');
	$.post("ajax.php?case=ajax_vote_up", {media_id: media_id}, function(data){
		if(data == 1) {
		setTimeout(
		function() 
		{
			$(".media-vote-up i").removeClass('fa-spinner fa-spin');
			$(".media-vote-up i").addClass('fa-thumbs-up');
			$(".media-vote-up").remove();
			$("#rate-options").prepend('<a href="javascript:void();" class="btn btn-default"><i class="fa fa-thumbs-up"></i></a>');
			$.get("ajax.php?case=ajax_reload_votes", { "media_id": media_id }, function(result){
				$('#rate-item-progress').html(result);
			});
		}, 2000);
		} else {
			$(".media-vote-up i").removeClass('fa-spinner fa-spin');
			$(".media-vote-up i").addClass('fa-thumbs-up');
		}
	});
	});
});

$(function() {
  $(".media-vote-down").click(function() {
	var media_id = $(".media-vote-down").attr('scope');
	$(".media-vote-down i").removeClass('fa-thumbs-down');
	$(".media-vote-down i").addClass('fa-spinner fa-spin');
	$.post("ajax.php?case=ajax_vote_down", {media_id: media_id}, function(data){
		if(data == 1) {
		setTimeout(
		function() 
		{
			$(".media-vote-down i").removeClass('fa-spinner fa-spin');
			$(".media-vote-down i").addClass('fa-thumbs-down');
			$(".media-vote-down").remove();
			$("#rate-options").append('<a href="javascript:void();" class="btn btn-default"><i class="fa fa-thumbs-down"></i> <span class="hidden-xs">'+dislike+'</span></a>');
			$.get("ajax.php?case=ajax_reload_votes", { "media_id": media_id }, function(result){
				$('#rate-item-progress').html(result);
			});
		}, 2000);
		} else {
			$(".media-vote-down i").removeClass('fa-spinner fa-spin');
			$(".media-vote-down i").addClass('fa-thumbs-down');
		}
	});
	});
});