$(document).ready(function() {
 
	$('#dataTables-example').DataTable({
		responsive: true
	});
	 
	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+"about_page/call_div",
			success: function(msg) {
				$('#content_div').append(msg);
				$('#header_h4').text('Add New');
				modal_div.style.display = "block";
			}
		});		
	}
	var info_div = document.getElementById('about_info_div');
	window.onclick = function(event){
		if(event.target == modal_div){
			$('#content_div').html('');
			modal_div.style.display = "none";
		}
		if(event.target == info_div){			
			info_div.style.display = "none"; 
		}
	} 
	$(document).on('click','.info_close',function(){	
		
		info_div.style.display = "none"; 
	});
	
	
	 
		
});

 



$(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', '#user_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<'135' || web_width>'145' || web_height<'135' || web_height>'145'){		 
				alert('Please image upload 140*140');
				$('#user_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 formData = new FormData();
		formData.append('user_file', $('#user_file')[0].files[0]);
		formData.append('id',$('#update_id').val());
		formData.append('name',$('#name').val());	
		formData.append('designation',$('#designation').val());
		formData.append('content',$('#content').val());
		 
		
		$.ajax({
			url: HTTP_PATH+"about_page/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+'about_page';
					},2000);
				}else{
					document.getElementById("meta_submitBtn").disabled = false;
					if(msg.img){
						$('#message_div').html(msg.img);
					}else{
						$('#message_div').html(msg.text);
					}
				}
			}
		});
	}
});

 
function update_team_member(txt){
	var modal_div = document.getElementById('meta_content_popup_div');
	var name_=$('#row_'+txt).find("td").eq(0).html();
	var designation=$('#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();
	 
	
	$('#header_h4').text('Edit');
	$.ajax({
		url: HTTP_PATH+"about_page/call_div",
		success: function(msg) {
			$('#content_div').append(msg);
			modal_div.style.display = "block";
			$('#update_id').val(txt);
			$('#name').val(name_);	
			$('#designation').val(designation);
			$('#content').val(content);	
			$('#show_banner_img').html(image_);			
			$('#show_banner_img img').css({width:'80px'});		 
				
			 
		}
	});		
}

function delete_team_member(txt){
	$.ajax({
		url: HTTP_PATH+"about_page/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+'about_page';
				},2000);
			}else{
				alert(msg.text);				
			}
		}
	});
	
}


function show_info_model(div,slug){
	$('#update_slug').val(slug);
	var txt=findAndReplace(slug, "-", " ");
	$('#header_info_h4').text(txt);
	$('#content_text').val($('#'+div+'_div').html().replace(/\s+/, ""));
	$('#about_info_div').css({display:'block'});
	 
	
}
function findAndReplace(string, target, replacement) { 
	var i = 0, length = string.length; 
	for (i; i < length; i++) { 
	string = string.replace(target, replacement); 
	} 
	return string; 
}

$(document).on('click', '#info_submitBtn', function () {
	var error=false;
	$('#about_info_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("info_submitBtn").disabled = true;
		$('#info_message_div').html(http_loader);
		var formData = new FormData();
		formData.append('slug',$('#update_slug').val());
		formData.append('content',$('#content_text').val()); 
		
		$.ajax({
			url: HTTP_PATH+"about_page/about_info_save",
			data: formData,
			type: "POST",
			dataType : "JSON",
			processData: false,  // tell jQuery not to process the data
			contentType: false, 
			success: function(msg) {
				if(msg.success==true){
					$('#info_message_div').html(msg.text);
					setTimeout(function(){					
						document.location.href=HTTP_PATH+'about_page';
					},1000);
				}else{
					document.getElementById("info_submitBtn").disabled = false;
					if(msg.img){
						$('#info_message_div').html(msg.img);
					}else{
						$('#info_message_div').html(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()
*/