

//var httpHost = window.location.origin+'/'+window.location.pathname.split('/')[1]+"/members";


function login()
{
      $('#loginForm').on('submit',function(e){
         e.preventDefault();
		  httpHost = $(this).attr('action');
         $.ajax({
              type : 'POST',
              url  :  httpHost+'/core/api/auth/log/login',
              data : $(this).serialize(),
              dataType : 'json',
              beforeSend : function (response)
              {
				$('#resp').html("<img src='assets/images/typing.svg'>");
              },
              success : function(response){
				  console.log(response);
                if(response.status =="success") 
                {
                  $('#resp').html("<h3 style='color:green;'>"+response.message+"</h3>");
                  setTimeout(function(){
					location.reload();
				   },3000);
                  
                }
                else
                {
                  $('#resp').html("<h5 style='color:red;'>"+response.message+"</h5>");
                }
              }

         })
		 $(this).unbind('submit');
      })
}




function logoff(httpHost)
{
 

   $.ajax({
      type : 'POST',
      url  : httpHost+'/core/api/auth/log/signout',
      success : function(response){
          location.href = httpHost;
      }
   })

}
$(document).ready(function() {

    var readURL = function(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('.avatars').attr('src', e.target.result);
            }
    
            reader.readAsDataURL(input.files[0]);
        }
    }
    
     
    $(".file-upload").on('change', function(){
        readURL(this);
    });
});

function upstatus(id,type,httpHost){
	
	$.ajax({
		type     : 'POST',
		url      : httpHost+'/core/api/0/'+type+'/active',
		data: 'id='+id,
		success: function(result){
		if (result == "success") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "updated successfully"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'success'});
			setInterval(function () {
			location.reload();
			 }, 1000);
		}else{
			$.notify({
			title: "<strong>Failed:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "Failed to update"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'danger'});
		}
		//$("#loads").load(" #loads");
	  }
	  });
}


function Save(type,httpHost){
			var events = $("#signupForm" );
			$( "#signupForm" ).validate( {
				rules: {
					name: "required",
					appname: "required",
					status: "required",
					tv_stream: "required",
					whatsapp: {
						required: false,
						number: true
					},
				},
				submitHandler: function (form) {
				   $.ajax({
					type     : 'POST',
					url      : httpHost+'/core/api/0/'+type+'/add',
					data     : new FormData($(form)[0]),
					mimeType : "multipart/form-data",
				    success: function(result){
					//console.log(result);
					if (result == "success") {
						$.notify({
						title: "<strong>Success:</strong> ",
						icon: 'glyphicon glyphicon-user',
						message: type+" created successfully"
					
						},{animate:{
						enter: "animated fadeInUp",
						exit: "animated fadeOutDown"
						},type:'success'});
						setInterval(function () {
						location.reload();
						 }, 3000);
						events[0].reset();
					}else{
						$.notify({
						title: "<strong>Failed:</strong> ",
						icon: 'glyphicon glyphicon-user',
						message: "Failed to create "+type
					
						},{animate:{
						enter: "animated fadeInUp",
						exit: "animated fadeOutDown"
						},type:'danger'});
					}
				  },
				  cache: false,
				  contentType: false,
			      processData: false
				  
				  });
				$(this).unbind('submit');
			
				},
				messages: {
					name: "This field is required.",
					appname: "This field is required.",
					description: "This field is required.",
					tv_stream: "This field is required.",
					themename: "This field is required.",
					grad_start_color: "This field is required.",
					grad_end_color: "This field is required.",
					grad_orientation: "This field is required.",
					radio_stream: "This field is required.",
					title: "This field is required.",
					website: "This field is required.",
					facebook: "This field is required.",
					youtube: "This field is required.",
					whatsapp: {
						required: "This field is required.",
						number: "Please enter a valid number."
					},
					
				},
				errorElement: "em",
				errorPlacement: function ( error, element ) {
					// Add the `help-block` class to the error element
					error.addClass( "help-block" );

					if ( element.prop( "type" ) === "file" ) {
						error.insertAfter( element.parent( "label" ) );
					} else {
						error.insertAfter( element );
					}
				},
				highlight: function ( element, errorClass, validClass ) {
					$( element ).parents( ".colsm" ).addClass( "has-error" ).removeClass( "has-success" );
				},
				unhighlight: function (element, errorClass, validClass) {
					$( element ).parents( ".colsm" ).addClass( "has-success" ).removeClass( "has-error" );
				}
				
			} );
			
} 

/*****************************/
function del(id,type,httpHost){
	if(confirm("Are you sure to delete this ?")){
	$.ajax({
		type     : 'POST',
		url      : httpHost+'/core/api/0/'+type+'/delete',
		data: 'id='+id,
		success: function(result){
		if (result == "success") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "deleted successfully"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'success'});
			setInterval(function () {
			location.reload();
			 }, 2000);
		}else{
			$.notify({
			title: "<strong>Failed:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: "Failed to delete"
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'danger'});
		}
		//$("#loads").load(" #loads");
	  }
	  });		
	
	}
}

function update(type,httpHost)
{
  //$('#updateForms').on('submit',function(e){
  $(document).on('submit','#updateForms',function(e){
	 e.preventDefault();
	  var events = $(this);
	 $.ajax({
		type     : 'POST',
		url      : httpHost+'/core/api/0/'+type+'/update',
		data     : new FormData($(this)[0]),
		mimeType : "multipart/form-data",
		dataType : 'json',
		beforeSend: function(){
			$('#btn').attr('disabled', 'disabled');
			$('#uploadStatus').html("<center><img src="+httpHost+"/assets/img/typing.svg></center>");
		},
		success: function(result){
		$('#uploadStatus').html("");
		$('#btn').removeAttr('disabled');
		//console.log(result);
		$('.modal').modal('hide');
		if (result.status == "success") {
			$("#csscode").text('<style>iframe.contentPlayer {width: 100%;min-height: 500px;left: 0;overflow: hidden;position: fixed;bottom: 0;}</style>');
			$('#resultplayer').html(result.widget);
			$('#resultcode').text('<meta name="viewport" content="width=device-width, initial-scale=1"><iframe name="contenedorPlayer" class="contentPlayer" allow="autoplay" width="100%" frameborder=0 scrolling=no  src='+result.code+'></iframe>');
			$("#csscode").attr('style','display: block !important');
			//events[0].reset();
		}else{
		}
	  },
	  cache: false,
	  contentType: false,
	  processData: false
	  
	  });
  });
}


$(document).ready(function() {

    var readURL = function(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('.avatars').attr('src', e.target.result);
            }
    
            reader.readAsDataURL(input.files[0]);
        }
    }
    
     
    $(".file-upload").on('change', function(){
        readURL(this);
    });
});

$(document).ready(function() {
    $("body").on("click", "#cimage .dropify-clear",function(){
         $('#old_cover').val('');  
    });
});

$(document).ready(function() {
    $("body").on("click", "#bimage .dropify-clear",function(){
         $('#old_background').val('');  
    });
});
function updatepass(type,httpHost)
{
   $('#updateForms').on('submit',function(e){ 
    //alert(httpHost+"/"+$(this).attr('action'));   
        e.preventDefault();
          var events = $(this);
	 $.ajax({
		type     : 'POST',
		url      : httpHost+'core/api/0/'+type+'/update',
		data     : new FormData($(this)[0]),
		mimeType : "multipart/form-data",
		dataType : 'json',
		success: function(result){
		$('.modal').modal('hide');
		//console.log(result);
		if (result.status == "success") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: result.message
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'success'});
			setInterval(function () {
			location.reload();
			}, 3000);
			events[0].reset();
		}else if (result.status == "notmatch") {
			$.notify({
			title: "<strong>Success:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: result.message
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'warning'});
		}else{
			$.notify({
			title: "<strong>Failed:</strong> ",
			icon: 'glyphicon glyphicon-user',
			message: result.message
		
			},{animate:{
			enter: "animated fadeInUp",
			exit: "animated fadeOutDown"
			},type:'danger'});
		}
	  },
	  cache: false,
	  contentType: false,
	  processData: false
	  
	  });
        $(this).unbind('submit');
   }) 
}
function GetPlayer(type,httpHost){
			var events = $("#createplayerForm" );
			$( "#createplayerForm" ).validate( {
				rules: {
					
					stream:{
						required: true,
						url: true
					},
					logo: "required",
					cover_image: "required",
					theme: "required",
					history: "required",
					volume: "required",
					background: "required",
					textcolor: "required",
					platform: "required",
				},
				submitHandler: function (form) {
				   $.ajax({
					type     : 'POST',
					url      : httpHost+'/core/api/0/'+type+'/add',
					data     : new FormData($(form)[0]),
					mimeType : "multipart/form-data",
					dataType : 'json',
				    beforeSend: function(){
						$('#btn').attr('disabled', 'disabled');
						$('#uploadStatus').html("<center><img src="+httpHost+"/assets/img/typing.svg></center>");
					},
				    success: function(result){
			        $('#uploadStatus').html("");
			        $('#btn').removeAttr('disabled');
					//console.log(result);
					if (result.status =="success") {
					    $.notify({
            			title: "<strong>Success:</strong> ",
            			icon: 'glyphicon glyphicon-user',
            			message: "Widget Created successfully"
            		
            			},{animate:{
            			enter: "animated fadeInUp",
            			exit: "animated fadeOutDown"
            			},type:'success'});
            			setInterval(function () {
            			location.reload();
            			}, 3000);
            			events[0].reset();
					}else{
							$.notify({
            			title: "<strong>Failed:</strong> ",
            			icon: 'glyphicon glyphicon-user',
            			message: "Failed to Create widget"
            		
            			},{animate:{
            			enter: "animated fadeInUp",
            			exit: "animated fadeOutDown"
            			},type:'danger'});
					}
				  },
				  cache: false,
				  contentType: false,
			      processData: false
				  
				  });
				$(this).unbind('submit');
			
				},
				messages: {
					theme: "This field is required.",
					platform: "This field is required.",
					volume: "This field is required.",
					history: "This field is required.",
					background: "This field is required.",
					textcolor: "This field is required.",
					themename: "This field is required.",
					logo: "This field is required.",
					cover_image: "This field is required.",
					stream: {
						required: "This field is required.",
						number: "Please enter a valid url."
					},
					
				},
				errorElement: "em",
				errorPlacement: function ( error, element ) {
					// Add the `help-block` class to the error element
					error.addClass( "help-block" );

					if ( element.prop( "type" ) === "file" ) {
						error.insertAfter( element.parent( ".dropify-wrapper" ) );
					} else {
						error.insertAfter( element );
					}
				},
				highlight: function ( element, errorClass, validClass ) {
					$( element ).parents( ".colsm" ).addClass( "has-error" ).removeClass( "has-success" );
				},
				unhighlight: function (element, errorClass, validClass) {
					$( element ).parents( ".colsm" ).addClass( "has-success" ).removeClass( "has-error" );
				}
				
			} );
			
} 