$(document).ready(function() {
 
	$('#dataTables-example').DataTable({
		responsive: true,
		"order": [[ 6, 'desc' ]]
	});
	 
	var modal_div = document.getElementById('meta_content_popup_div');
	var modal_show = document.getElementById('add_btn_new');
	var modal_close = document.getElementsByClassName("close")[0];
	modal_close.onclick = function() {
		$('#content_div').html('');
		modal_div.style.display = "none";
	}
	modal_show.onclick = function() {
		$.ajax({
			url: HTTP_PATH+"testimonials/call_div",
			success: function(msg) {
				$('#content_div').append(msg);
				$('#header_h4').text('Add New');
				modal_div.style.display = "block";
			}
		});		
	}
	
	window.onclick = function(event){
		if(event.target == modal_div){
			$('#content_div').html('');
			modal_div.style.display = "none";
		}
	}
	
	$(document).on('change','.check_text',function(){
		check_type($(this).val());
	});
		
});

function check_type(type){	
	if(type==0){
		$('#type_div').html('');
	}
	
	if(type==1){
		var file='<strong>Content Image</strong><i class="span_star">*</i><span class="span_ms">(Best Size 320px*200px)</span><input id="type_file" name="type_file" style="height:35px" type="file">'
		$('#type_div').html(file);
	}
	
	if(type==2){
		var txt='<strong>Video Link</strong><i class="span_star">*</i><input id="video_link" name="video_link" class="form-control" placeholder="Video Link" type="text">';
		$('#type_div').html(txt);
	}
}



$(document).on('click', '#meta_submitBtn_cancel', function () {     
	var modal_div = document.getElementById('meta_content_popup_div');	
	$('#content_div').html('');
	modal_div.style.display = "none";
});

$(document).on('focus', 'input[type=text],textarea', function () {     
	$(this).removeClass('error_m');		
});
 
var _URL = window.URL || window.webkitURL;
$(document).on('change', '#page_file', function () {    
 
	var web_width=0; 
	var web_height=0;
	var file, img1;
	if ((file = this.files[0])) {
		img1 = new Image();
		img1.onload = function () {
			web_width=this.width;
			web_height=this.height;
			if(web_width<'100' || web_width>'110' || web_height<'100' || web_height>'110'){		 
				alert('Please image upload 100*100 px');
				$('#page_file').val(null).clone(true);
			}
		};
		img1.src = _URL.createObjectURL(file);
	}
});
var _URL1 = window.URL || window.webkitURL;
$(document).on('change', '#type_file', function () {    
 
	var web_width=0; 
	var web_height=0;
	var file, img1;
	if ((file = this.files[0])) {
		img1 = new Image();
		img1.onload = function () {
			web_width=this.width;
			web_height=this.height;
			if(web_width<'300' || web_width>'335' || web_height<'200' || web_height>'220'){		 
				alert('Please image upload 320*200 px');
				$('#type_file').val(null).clone(true);
			}
		};
		img1.src = _URL.createObjectURL(file);
	}
});

$(document).on('click', '#meta_submitBtn', function () {
	var error=false;
	$('#meta_content_popup_div').find('input[type=text],textarea').each(function () {
		$(this).removeClass('error_m');		
		if($(this).val()==""){	
			$(this).addClass('error_m');
			error=true;			
		} 
    });
	
	if(error==true){
		return false;
	}else{
		document.getElementById("meta_submitBtn").disabled = true;
		$('#message_div').html(http_loader);
		var type=$("input[type='radio'][name='check_text']:checked").val();
		
		var formData = new FormData();
		formData.append('banner_file', $('#page_file')[0].files[0]);
		formData.append('id',$('#update_id').val());
		formData.append('name',$('#name').val());	
		formData.append('other_info',$('#other_info').val());
		formData.append('content',$('#content').val());
		formData.append('content_type',type);
		if(type==1){
			formData.append('type_file', $('#type_file')[0].files[0]);
		}
		if(type==2){
			formData.append('video_link', $('#video_link').val());
		}
		
		$.ajax({
			url: HTTP_PATH+"testimonials/actions",
			data: formData,
			type: "POST",
			dataType : "JSON",
			processData: false,  // tell jQuery not to process the data
			contentType: false, 
			success: function(msg) {
				if(msg.success==true){
					$('#message_div').html(msg.text);
					setTimeout(function(){					
						document.location.href=HTTP_PATH+'testimonials';
					},2000);
				}else{
					document.getElementById("meta_submitBtn").disabled = false;
					if(msg.img){
						$('#message_div').html(msg.img);
					}else{
						$('#message_div').html(msg.text);
					}
				}
			}
		});
	}
});

var link_i='';
var lik_v='';
var link_i_h='';
var link_v_h='';
function update_testimonial_call(txt){
	var modal_div = document.getElementById('meta_content_popup_div');
	var name_=$('#row_'+txt).find("td").eq(0).html();
	var other_info=$('#row_'+txt).find("td").eq(1).html();
	var image_=$('#row_'+txt).find("td").eq(2).html();
	var content=$('#row_'+txt).find("td").eq(3).html();
	var tpye_= $('#row_'+txt).find("td").eq(4).text().replace(/\s+/, "") ;
	var type_content=$('#row_'+txt).find("td").eq(5).html();
	
	$('#header_h4').text('Edit');
	$.ajax({
		url: HTTP_PATH+"testimonials/call_div",
		success: function(msg) {
			$('#content_div').append(msg);
			modal_div.style.display = "block";
			$('#update_id').val(txt);
			$('#name').val(name_);	
			$('#other_info').val(other_info);
			$('#content').val(content);	
			$('#show_banner_img').html(image_);			
			$('#show_banner_img img').css({width:'20%'});
			 
				
			if(tpye_=="Video"){
				$("input[type='radio'][name='check_text'][value=2]").prop("checked", true);
				check_type(2);
				$('#type_content').html(type_content).css({width:'30%', float:'right', margin:'-50px 0 0 0'});
				$('#video_link').val($('#type_content iframe').attr('src'));					
				
			}else if(tpye_=="Image"){
				$("input[type='radio'][name='check_text'][value=1]").prop("checked", true);
				check_type(1);
				$('#type_content').html(type_content).css({width:'30%', float:'right'});			 
				 
			}else{
				$("input[type='radio'][name='check_text'][value=0]").prop("checked", true);
				 check_type(0);				
			}
		}
	});		
}

function delete_testimonial(txt){
	$.ajax({
		url: HTTP_PATH+"testimonials/deletes",
		data: {deleteId: txt},
		type: "POST",
		dataType : "JSON", 
		success: function(msg) {
			if(msg.success==true){
				alert(msg.text);
				setTimeout(function(){					
					document.location.href=HTTP_PATH+'testimonials';
				},2000);
			}else{
				alert(msg.text);				
			}
		}
	});
}

	 
/* 	$('#meta_content_popup_div').find('input[type=text],textarea').each(function () {
		$(this).removeClass('error_m');		
		if($(this).val()==""){	
			$(this).addClass('error_m');
			//$(this).after('<p class="error_m">data sds dsadf </p>');			 
		} 
     }); $('#row_2').find("td").eq(4).html()
*/