var keyuser = localStorage.getItem('keyuser');

var id = getGET().id;


if(!localStorage.getItem('imguse')){
  	$('.imguserdf').attr('src', "images/icon-user-default.png");
  	imageinme = "images/icon-user-default.png";
}else{
  	$('.imguserdf').attr('src', localStorage.getItem('imguse'));
  	imageinme = localStorage.getItem('imguse');
}
        	
function pagefunctions(){
	$.ajax({
  		type: "POST",
  		crossDomain: true,
  		url: "http://m2s.es/app/api/groupinfo.php",
  		data: {"id": id, "key": keyuser},
  		cache: false,
  		dataType: 'json',
  		success: function(data) {
  			var id = data.id,
            name = data.groupname,
            imagein = data.imagein,
            private = data.private,
            admininfo = data.admininfo,
            official = data.official,
            local = data.local,
            decription = data.description,
            peoplejoin = data.peoplejoined,
            mutegroup = data.mute;
            $('.userinfcj span.loading').remove();
        	$('.userinfcj').append('<h1>'+name+'</h1><span>'+peoplejoin+' '+Language.usersjoined+'</span>');
			$('.userprofl').attr('href','profilegr.html?id='+id);
			
			chat(id);
			
			/*
			var chatrefresh = setInterval(function() {
				updatechats(id);
			}, 14000);
			*/
  		}
	});
    
    $('.share-more').click(function(){
   		$('.share-moreapp').show();
	})

	$('.modal-share span.remove').click(function(){
		$('.share-moreapp').hide();
	})

	$('.optlink#stickerss').click(function(){
   		var slec = $('.optlink.sel').attr('id');
   		$('.optlink.sel').removeClass('sel');
   		$('.optlink#stickerss').addClass('sel');
   		$('.'+slec).hide();
   		$('.stickerss').show();
	})

	$('.optlink#imagest').click(function(){
   		var slec = $('.optlink.sel').attr('id');
   		$('.optlink.sel').removeClass('sel');
   		$('.optlink#imagest').addClass('sel');
  		$('.'+slec).hide();
   		$('.imagest').show();
	})

	$('.optlink#locastdfg').click(function(){
   		var slec = $('.optlink.sel').attr('id');
   		$('.optlink.sel').removeClass('sel');
   		$('.optlink#locastdfg').addClass('sel');
   		$('.'+slec).hide();
   		$('.locastdfg').show();
	})
	
	$('.bargroup-emo span').click(function(){
  		name = $(this).attr('class');
  		$('.icon-group').hide();
  		$('.icon-group#'+name).show();
	})
	
	$('#file-input').change(function(e) {
		var file = e.target.files[0],
		imageType = /image.*/;

		if (!file.type.match(imageType))
			return;

		var reader = new FileReader();
		reader.onload = fileOnload;
		reader.readAsDataURL(file);
	});

	$('#upload-image').click(function() {
    	var canvas = document.getElementById("canvas");
		var url = canvas.toDataURL("image/png", 1.0);
		url = url.replace('data:image/png;base64,', '');
		console.log(url);
		$.ajax({
			url: 'https://api.imgur.com/3/image',
			method: 'POST',
			headers: {
        		Authorization: 'Client-ID def4c03828b22c2',
				Accept: 'application/json'
      		},
	  		data: {
        		image: url,
				type: 'base64'
      		},
	  		success: function(result) {
        		var link = result.data.link;
				$('input.send').val($('input.send').val() + ' ' + link);
				$('#share-mre').show();
				$('.selct-shareop').show();
				$('.sections-share').show();
				$('.imageuplo').hide();
				$('.share-moreapp').hide();
				$('#upload-imge').hide();
      		}
    	});
	});	

	$('#cancel-upl-image').click(function() {
		$('#upload-imge').hide();
		$('#share-mre').show();
		$('.selct-shareop').show();
		$('.sections-share').show();
		$('.imageuplo').hide();
		$('#upload-imge').hide();
	})
	
	$('#btn-location').click(function() {
    	if ($('#btn-location.btn-info').length != '0') {
			if(navigator.geolocation){
				navigator.geolocation.getCurrentPosition(function(position) {
					console.log('Loading location...');
					var latitud = position.coords.latitude;
					var longitud = position.coords.longitude;
					var latlong = latitud + ' ' + longitud;
					console.log('Location:' + latlong);
					$('footer').append(
						$(document.createElement("input")).attr("value", latlong).attr('name', 'map').attr('type', 'hidden').attr('class', 'map')
					);
					var mapht = '<iframe marginheight="0" marginwidth="0" src="http://maps.google.com/maps?client=safari&ll=' + latitud + ',' + longitud + '&z=14&output=embed" frameborder="0" scrolling="no" style="height: 50%;margin-top: 10px;max-height: 200px;max-width: 700px;width: 100%;" id="map-html"></iframe>';
					$('#location').append(mapht);
					$('#btn-location').removeAttr("disabled");
					$('#btn-location').html(Language.removelocation);
					$('#btn-location').removeClass('btn-info');
					$('#btn-location').addClass('btn-danger');
        		}, function(error) {
                	alert('Error occurred. Error code: ' + error.code);         
            	},{timeout:5000});
      		}else{
	  			alert("Your browser doesn't the location API");
      		}
	  		$('#btn-location').attr('disabled', 'disabled');
	  		$('#btn-location').html(Language.locationloading);
    	} else {
			$('#btn-location').html(Language.sharelocation);
			$('#btn-location').removeClass('btn-danger');
			$('#btn-location').addClass('btn-info');
			$('#map-html').remove();
			$('input.map').remove();
    	}
	});
}


function fileOnload(e) {
  var $img = $('<img>', {
    src: e.target.result
  });
  var canvas = $('#canvas')[0];
  $('#file-input').val('');
  $img.load(function() {
    canvas.width = this.width;
    canvas.height = this.height;
    var context = canvas.getContext('2d');
    context.clearRect(0, 0, canvas.width, canvas.height);
    context.drawImage(this, 0, 0, this.width, this.height);
  });
  $('#upload-imge').show();
  $('#share-mre').hide();
  $('.selct-shareop').hide();
  $('.sections-share').hide();
  $('.imageuplo').show();
}

function updatechats(id) {
  console.log('Checking new messages...');
  $.ajax({
    type: "POST",
    crossDomain: true,
    url: "http://m2s.es/app/api/chat-gr.php",
    data: {"id": id, "key": keyuser},
    cache: false,
    dataType: 'json',
    success: function(data) {
      for (var i = 0; i < data.messages.length; i++) {
        if ($('.nochat').lenght != '0') {
          $('.nochat').remove();
        }
        idc = data.messages[i].id;
        username = data.messages[i].username;
        iduser = data.messages[i].iduser;
        imgr = data.messages[i].imgr;
        textmsm = data.messages[i].textmsm;
        locat = data.messages[i].locat;
        admin = data.messages[i].admin;
        fecha = data.messages[i].fecha;
        me = data.messages[i].me;
        stick = data.messages[i].stick;
        tableid = data.messages[i].tableid;
        if(imgr == null){
          imgr = 'images/icon-user-default.png';
        }
        if ($('#'+idc).length == '0') {
          var localid = "cgr"+tableid;
          var messages = new Object();
          messages.id = idc;
          messages.username = username;
          messages.iduser = iduser;
          messages.textmsm = textmsm;
          messages.imgr = imgr;
          messages.locat = locat;
          messages.fecha = fecha;
          messages.me = me;
          messages.stick = stick;
          messages.tableid = tableid;
          messages.admin = admin;
          if (!localStorage.getItem(localid)) {
            localStorage.setItem(localid, "[" + JSON.stringify(messages) + "]");
          }else{
            var antes = JSON.parse(localStorage.getItem(localid));
            antes.push(messages);
            localStorage.setItem(localid, JSON.stringify(antes));
          }
          datosmem = crearmsmd(idc, username, iduser, imgr, textmsm, locat, admin, fecha, me, stick, tableid);
          $('.' + 'msmdiv').append(datosmem);
          $('body').append('<audio src="sounds/tap.ogg"/>');
          notifications();
          
          $$("#"+idc).swipeLeft(function() {
		        $('blockquote', this).css('position', 'relative');
				$('blockquote', this).css('left', '-70px');
				if($(this).hasClass('me')){
					$('.imgp', this).css('margin-right', '80px');
	       		}
                $('.trash', this).css('display', 'block');
                console.log('swipe!'+id);
          });
            
          $$("#"+idc).swipeRight(function() {
	            $("blockquote", this).css('position', 'inherit');
	            if($(this).hasClass('me')){
					$('.imgp', this).css('margin-right', '10px');
					$('.sms.me blockquote::before').css('right', '55px');
	       		}
                $('.trash', this).css('display', 'none');
          })
            
          if(stick != '1'){
              localStorage.setItem('lastmsmcgr-'+tableid, textmsm);
          }else{
              localStorage.setItem('lastmsmcgr-'+tableid, '[stick]');
          }
          localStorage.setItem('lastmsmcgrd-'+tableid, fecha);
          var d = $('.msmdiv');
          d.scrollTop(d.prop("scrollHeight"));
          }
        }
        if($('.msmdiv').html() == ''){
          $('.' + 'msmdiv').html('<div class="center nochat"><h3><span class="icon comments"></span>'+Language.nomessageschat+'</h3></div>'); 
        }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      console.error(textStatus + ' ' + XMLHttpRequest.status);
      var divno = document.createElement('div');
      divno.innerHTML = '<div id="list-notifications" style="text-align:center;cursor:pointer"></div>';
      divno.className = 'background-dark';
      var modsv = document.getElementById('modsv');
      modsv.appendChild(divno);
      $('#list-notifications').append(Language.errorpetitionm2s);
      $('#list-notifications').click(function() {
        window.location.reload();
      })
    }
  });
}

function crearmsmd(idt, username, iduser, imgr, textmsm, locat, admin, fecha, me, stick, tableid) {
  if (admin == 'yes') {
    msm = '<div class="sms admin" id="' + idt + '">';
    if (me == 'yes') {
      msm += '<a href="javascript:deletemessage(' + idt + ',' + tableid + ',1)" class="trash" style="text-decoration:none;color:#fff;font-size: 24px;text-align: center;position: absolute;right: 14px;background-color: rgb(198, 40, 40);padding: 13px 17px;border-radius: 25px;display:none;margin-top:5px"><span class="icon trash"></span></a>';
    } else {
      msm += '<a href="javascript:deletemessage(' + idt + ',' + tableid + ',0)" class="trash" style="text-decoration:none;color:#fff;font-size: 24px;text-align: center;position: absolute;right: 14px;background-color: rgb(198, 40, 40);padding: 13px 17px;border-radius: 25px;display:none;margin-top:5px"><span class="icon trash"></span></a>';
    };
    msm += '<blockquote>';
    if (imgr != 'no') {
      msm += '<img src="' + imgr + '" width="55px" height="55px" style="float:left;margin-left:-5px;margin-top:3px;border-radius:27px"/><div style="padding-left:60px;margin-top:3px">'
    }
    msm += '<span class="user" style="font-size:16px;">Admin:</span>';
    if (stick == 'yes') {
      msm += '<div class="' + textmsm + '"></div>';
    } else {
      msm += '<p class="selecton">' + linkscom(textmsm) + '</p>';
    }
    if (locat.latitude != null) {
      longitud = locat.longitud;
      longitud = longitud.substring(0, longitud.length - 2);
      msm += '<iframe marginheight="0" marginwidth="0" src="http://maps.google.com/maps?client=safari&ll=' + locat.latitude + ',' + longitud + '&z=14&output=embed" frameborder="0" scrolling="no" style="height: 24%;max-height: 200px;max-width: 700px;width: 100%;"></iframe>';
    }
    msm += '<div class="foot">';
    msm += convert(fecha);
    msm += '</div>';
    if (imgr != 'no') {
      msm += '</div>';
    };
    msm += '</div></blockquote>';
  } else {
  if (me == 'yes') { 
    var contind = '';
    if($(".msmdiv .sms:last-child").hasClass("me")){
      if($(".msmdiv .sms:last-child").attr('date') == fecha){
        contind += 'continu';
        $(".msmdiv .sms:last-child .foot").hide();
      }
    }
    msm = '<div class="sms me '+contind+'" id="' + idt + '" date="'+fecha+'">';
    msm += '<a href="javascript:deletemessage(' + idt + ',' + tableid + ',1)" class="trash" style="text-decoration:none;color:#fff;font-size: 24px;text-align: center;position: absolute;right: 14px;background-color: rgb(198, 40, 40);padding: 13px 17px;border-radius: 25px;display:none;margin-top:5px"><span class="icon trash"></span></a>';
    msm += '<img src="' + imageinme + '" class="imgp" style="right:0px;"/>';
    msm += '<blockquote>'
    if (stick == '1') {
      msm += '<div class="' + textmsm + '"></div>';
    } else {
      msm += '<p class="selecton">' + linkscom(textmsm) + '</p>';
    }
    if (locat.latitude != null) {
      longitud = locat.longitud;
      longitud = longitud.substring(0, longitud.length - 2);
      msm += '<iframe marginheight="0" marginwidth="0" src="http://maps.google.com/maps?client=safari&ll=' + locat.latitude + ',' + longitud + '&z=14&output=embed" frameborder="0" scrolling="no" style="height: 24%;max-height: 200px;max-width: 700px;width: 100%;"></iframe>';
    }
    msm += '<div class="foot">';
    msm += convert(fecha);
    msm += '</div></div></blockquote>'
  } else {
    var contind = '';
    if(!$(".msmdiv .sms:last-child").hasClass("me")){
      if($(".msmdiv .sms:last-child .nameusrf").attr('username') == username){
        if($(".msmdiv .sms:last-child").attr('date') == fecha){
          contind += 'continu';
          $(".msmdiv .sms:last-child .foot").hide();
        }
      }
    }
    msm = '<div id="' + idt + '" class="sms '+contind+'" date="'+fecha+'" username="'+username+'">';
    msm += '<a href="javascript:deletemessage(' + idt + ',' + tableid + ',0)" class="trash" style="text-decoration:none;color:#fff;font-size: 24px;text-align: center;position: absolute;right: 14px;background-color: rgb(198, 40, 40);padding: 13px 17px;border-radius: 25px;display:none;margin-top:5px"><span class="icon trash"></span></a>';
    msm += '<a href="profile.html?id='+iduser+'">';
    msm += '<img src="' + imgr + '" class="imgp"/></a>';
    msm += '<blockquote>';
    msm += '<div class="nameusrf">'+username+':</div>';
    if (stick == '1') {
      msm += '<div class="' + textmsm + '"></div>';
    } else {
      msm += '<p class="selecton">' + linkscom(textmsm) + '</p>';
    }
    if (locat.latitude != null) {
      longitud = locat.longitud;
      longitud = longitud.substring(0, longitud.length - 2);
      msm += '<iframe marginheight="0" marginwidth="0" src="http://maps.google.com/maps?client=safari&ll=' + locat.latitude + ',' + longitud + '&z=14&output=embed" frameborder="0" scrolling="no" style="height: 24%;max-height: 200px;max-width: 700px;width: 100%;"></iframe>';
    }
    msm += '<div class="foot">';
    msm += convert(fecha);
    msm += '</div></div></blockquote>'
  }
  }
  return msm;
  var d = $('.msmdiv');
  d.scrollTop(d.prop("scrollHeight"));
}

function loadchat(id, type) {
  $('.' + 'msmdiv').html('');
  if (localidsf != '') {
    var localidsf = JSON.parse(localStorage.getItem('cgr' + id));
  }
  if (localidsf) {
    for (var i = 0; i < localidsf.length; i++) {
      idc = localidsf[i].id;
      usernamec = localidsf[i].username;
      iduserc = localidsf[i].iduser;
      imgrc = localidsf[i].imgr;
      textmsmc = localidsf[i].textmsm;
      locatc = localidsf[i].locat;
      fechac = localidsf[i].fecha;
      mec = localidsf[i].me;
      stickc = localidsf[i].stick;
      tableidc = localidsf[i].tableid;
      adminc = localidsf[i].admin;
      datosmem = crearmsmd(idc, usernamec, iduserc, imgrc, textmsmc, locatc, adminc, fechac, mec, stickc, tableidc);
      $('.' + 'msmdiv').append(datosmem);
      
      $$("#"+idc).swipeLeft(function() {
	       $('blockquote', this).css('position', 'relative');
	       $('blockquote', this).css('left', '-70px');
	       if($(this).hasClass('me')){
		       console.log('me');
		       $('.imgp', this).css('margin-right', '80px');
	       }
           $('.trash', this).css('display', 'block');
           console.log('swipe!'+id);
      });
            
      $$("#"+idc).swipeRight(function() {
           $('.trash', this).css('display', 'none');
	       $('blockquote', this).css('position', 'inherit');
	       if($(this).hasClass('me')){
		       $('.imgp', this).css('margin-right', '10px');
		       $('.sms.me blockquote::before').css('right', '55px');
	       }
      })
    }
  }
  updatechats(id);
  var d = $('.msmdiv');
  d.scrollTop(d.prop("scrollHeight"));
  if (!localStorage.getItem('cgr' + id)) {
    $('.' + 'msmdiv').html('<div class="center nochat"><h3><span class="icon comments"></span>'+Language.nomessageschat+'</h3></div>');
  }else{
    if($('.msmdiv').html() == ''){
      localStorage.removeItem('cgr' + id);
      $('.' + 'msmdiv').html('<div class="center nochat"><h3><span class="icon comments"></span>'+Language.nomessageschat+'</h3></div>');
    }
  }
}

function chat(id) {
  id = id.toString();
  if ($('.input-chat').length) {
    $('.input-chat').remove();
  }
  $("input[name='txt']").keypress(function(event) {
    if (event.keyCode == 13) {
      if (!event.shiftKey) sendmsm()
    }
  });
  loadchat(id, 'pr');
}

function deletemessage(id, tableid, med) {
  id = id.toString();
  tableid = tableid.toString();
  tabledf = JSON.parse(localStorage.getItem('cgr' + tableid));
  Array.prototype.removeValue = function(name, value) {
    var array = $.map(this, function(v, i) {
      return v[name] === value ? null : v;
    });
    this.length = 0; //clear original array
    this.push.apply(this, array); //push all elements except the one we want to delete
  }
  tabledf.removeValue('id', id);
  localStorage.setItem('cgr' + tableid, JSON.stringify(tabledf));
  if (med == 1) {
    $.ajax({
      type: "POST",
      crossDomain: true,
      url: "http://m2s.es/app/api/connect/delete-message-gr.php",
      data: {"id": id, "key": keyuser},
      cache: false,
      dataType: 'json',
      success: function(data) {
        if (data.mensaje == 'ok') {
          console.log('Message deleted succesfully');
          $('#' + id).fadeOut(1000, function() {
            var dategd = $('#' + id).attr('date');
            if($('.sms.me[date="'+dategd+'"]').length > '1'){
               if($('#'+id+'.continu').length != '0'){
                  if($('.sms.me[date="'+dategd+'"]').length == '2'){
                    $('#' + id).remove();
                    $('.sms.me[date="'+dategd+'"] .foot').show();
                  }else{
                    $('#' + id).remove();
                    var lastdis = $('.sms.me[date="'+dategd+'"]').length - 1;
                    $('.sms.me[date="'+dategd+'"]:eq('+lastdis+') .foot').show();
                  }
               }else{
                  $('#' + id).remove();
                  $('.sms.me[date="'+dategd+'"]:eq(0)').removeClass('continu');
                  $('.sms.me[date="'+dategd+'"] .foot')[0].show();
               }
            }else{
              $('#' + id).remove();
            }
          })
        }
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.error(textStatus + ' ' + XMLHttpRequest.status);
        errormod(Language.errorpetitionm2s);
      }
    });
  } else {
    console.log('Message deleted succesfully');
    $('#' + id).fadeOut(1000, function() {
        if($('#'+id).hasClass('admin')){
          $('#' + id).remove();
        }else{
          var dategd = $('#' + id).attr('date');
          var usergd = $('#' + id).attr('username');
          if($('.sms:not(.me)[date="'+dategd+'"][username="'+usergd+'"]').length > '1'){
            if($('#'+id+'.continu').length != '0'){
                if($('.sms:not(.me)[date="'+dategd+'"][username="'+usergd+'"]').length == '2'){
                  $('#' + id).remove();
                  $('.sms:not(.me)[date="'+dategd+'"][username="'+usergd+'"] .foot').show();
                }else{
                  $('#' + id).remove();
                  var lastdis = $('.sms:not(.me)[date="'+dategd+'"][username="'+usergd+'"]').length - 1;
                  $('.sms:not(.me)[date="'+dategd+'"][username="'+usergd+'"]:eq('+lastdis+') .foot').show();
                }
            }else{
                $('#' + id).remove();
                $('.sms:not(.me)[date="'+dategd+'"][username="'+usergd+'"]:eq(0)').removeClass('continu');
                $('.sms:not(.me)[date="'+dategd+'"][username="'+usergd+'"] .foot')[0].show();
            }
        }else{
            $('#' + id).remove();
        }
      }
    })
  }
  if(localStorage.getItem('cgr' + tableid) == '[]'){
    localStorage.removeItem('cgr' + tableid);
    localStorage.removeItem('lastmsmcgr-' + tableid);
    localStorage.removeItem('lastmsmcgrd-' + tableid);
    $('.' + 'msmdiv').html('<div class="center nochat"><h3><i class="mdi-communication-forum"></i>'+Language.nomessageschat+'</h3></div>');
  }else{
    var json = JSON.parse(localStorage.getItem('c' + tableid));
    var lend = json.length;
    var lastKey = json[lend-1];
    if(lastKey.stick != '1'){
        localStorage.setItem('lastmsmcgr-'+tableid, lastKey.textmsm);
    }else{
        localStorage.setItem('lastmsmcgr-'+tableid, '[stick]');
    }
    localStorage.setItem('lastmsmcgrd-'+tableid, lastKey.fecha);
  }
}

function poststick(text) {
  $('.share-moreapp').hide();
  sendchats(text, '');
}

function sendmsm() {
  var txtvalue = document.getElementsByName('txt')[0].value;
  if (document.getElementsByName('map')[0]) {
    var mapv = document.getElementsByName('map')[0].value;
  } else {
    var mapv = "";
  }
  sendchats(txtvalue, mapv);
}

function sendchats(text, map) {
    $.ajax({
      type: "POST",
      crossDomain: true,
      url: "http://m2s.es/app/api/connect/chat-gr.php",
      data: {"txt": text, "map": map, "id": id, "key": keyuser},
      cache: false,
      dataType: 'json',
      beforeSend: function() {
        console.log('Connecting...');
        $('input[name="txt"]').css('opacity','0.7');
        $('input[name="txt"]').val(Language.sending);
        $('input[name="txt"]').attr('disabled', 'disabled');
        $('.sendz').attr('onclick', 'javascript:');
        $('.sendz').html('...');
      },
      complete: function() {
        console.log('Completed');
        document.getElementsByName('txt')[0].value = '';
        $('input[name="txt"]').css('opacity','1');
        $('input[name="txt"]').removeAttr('disabled');
        $('.sendz').attr('onclick', 'javascript:sendmsm()');
        $('.sendz').html('➤');
        updatechats(id, 'prs');
      },
      success: function(result) {
        console.log(result.mensaje);
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.error(textStatus + ' ' + XMLHttpRequest.status);
        errormod("Error");
      }
    })
};

$('.botchat').click(function(){
	var d = $('.msmdiv');
    d.scrollTop(d.prop("scrollHeight"));
});