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

function pagefunctions(){
	$.ajax({
    type: "POST",
    crossDomain: true,
    url: "http://m2s.es/app/api/profileinfo.php",
    data: {"id": id, "key": keyuser},
    cache: false,
    dataType: 'json',
    success: function (result) {
        var username = result.username,
            imagein = result.imagein,
            id = result.id,
            state = result.state,
            timeago = result.timeago,
            genre = result.genre,
            telf = result.telf,
            birth = result.birt,
            email = result.email;
        if(imagein == null){
            imagein = 'images/icon-user-default.png';
        }
        $('#loading-user').hide();

        var modaluser = '';

        $('header h1').append('<img src="' + imagein + '" class="profile-badge"/>');

        $('header h1').append('<h5>'+username+'</h5>');
        
        $('header h1').append('<div class="infomodf"></div>');
        
        $('header h1').append('<div class="linksoptions"></div>');
        
        if(timeago.indexOf("con")>=0){
  			timeago = Language.connected;
  		}else{
  			if(timeago.indexOf("m")>=0){
  				timeago = timeago.replace('m','');
  				timeago = Language.minago+' '+timeago+' '+Language.minagoc;
	  		}
	  	
	  		if(timeago.indexOf("d")>=0){
	  			timeago = timeago.replace('d','');
	  			timeago = Language.daysago+' '+timeago+' '+Language.daysagoc;
			}
		
			if(timeago.indexOf("h")>=0){
				timeago = timeago.replace('h','');
				timeago = Language.hoursago+' '+timeago+' '+Language.hoursagoc;
			}
  		}
  			
        if (state == '1' || state == '4'){
           $('.infomodf').append('<p>'+timeago+'</p>');

           if (state != '4') {
              $('.linksoptions').append('<a id="chatbutton" href="chat.html?id='+id+'">'+Language.chat+'</a>');          
              $('.linksoptions').append('<a id="blockfriendbutton">'+Language.block+'</a>');
              $('.linksoptions').append('<a id="deletefriendbutton">'+Language.deletefriend+'</a>');
           }else{
              $('.linksoptions').append('<a href="settings.html">'+Language.settings+'</a>');
           }
            if (result.states != '') {
                for (var i = 0; i < result.states.length; i++) {
                    idt = result.states[i].id;
                    text = result.states[i].text;
                    date = result.states[i].date;
                    locationgg = result.states[i].location;
                    if (state != '4') {
                        mens = '';
                    }else{
                        mens = 'me';
                    }
                    if (locationgg.latitude != '') {
                        longitud = locationgg.longitud;
                        locatisn = '<iframe marginheight="0" marginwidth="0" src="http://maps.google.com/maps?client=safari&ll=' + locationgg.latitude + ',' + longitud + '&z=14&output=embed" frameborder="0" scrolling="no" style="height: 24%;max-height: 200px;max-width: 700px;width: 100%;"></iframe>';
                    }else{
                        locatisn = '';
                    }
					
					
                    $('.steats').append("<li class='li-chats noimgsd' id='"+idt+"'><div class='right-info'></div><div class='right-p'><div class='stateci'><span class='name'></span><p class='state-text'>"+linkscom(text)+"</p>"+ locatisn + " <font class='date'>"+convert(date)+"</font></div></div></li>");

                    if(mens == 'me'){
	                    heigcv = $('#'+idt).height();
                        $('#'+idt).prepend('<a href="javascript:deletestate(' + idt + ')" class="deletered trash" style="display:none;height:'+heigcv+'"><span class="icon trash"></span></a>');
                    }

                    if(mens == 'me'){
                        $$('#'+idt).swipeLeft(function() {
	                      	if($(".trash").is(":visible")){
		                      	$(".trash").css('display', 'none');
		                      	$(".li-chats").removeAttr('style');
	                      	}
                                $(".trash", this).css('display', 'block');
                                $("span.trash", this).css('line-height', heigcv+'px');
                                $(this).css('left', '-30px');
                                $(this).css('position', 'relative');
                                console.log('swipe!'+id);
						});
						
						$$('#'+idt).swipeRight(function() {
                                $(".trash", this).css('display', 'none');
                                $(this).removeAttr('style');
                      	});
                    }
                }

            }
        }
        if (result.state == '23') {
            $('.linksoptions').append('<font>'+Language.waitaccept+'</font>');
        }

        if (result.state == '5') {
            $('.linksoptions').append('<a id="addfriendbutton">'+Language.addfriend+'</a>');
        }

        $('#addfriendbutton').click(function () {
            $.ajax({
                type: "POST",
                crossDomain: true,
                url: "http://m2s.es/app/api/connect/addfriend.php",
                data: {"id": id, "key": keyuser},
                cache: false,
                dataType: 'json',
                beforeSend: function () {
                    console.log('Connecting to send petition of friend...');
                    $('#addfriendbutton').attr('disabled', 'disabled');
                    $('#addfriendbutton').html(Language.sending);
                },
                success: function (result) {
                    if (result.mensaje == 'ok') {
                          $('#addfriendbutton').remove();
                          $('.linksoptions').append('<font>'+Language.waitaccept+'</font>');
                    }else{
                          errormod(Language.errorpetitionm2s);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(textStatus + ' ' + XMLHttpRequest.status);
                    errormod(Language.errorpetitionm2s);
                }
            })
        });

        $('#blockfriendbutton').click(function () {
	         confirmod(Language.blockfriendsu, function(){blockuser(id);});
        })

        $('#deletefriendbutton').click(function(){
            confirmod(Language.removefriend, function(){deletefriend(id);});
        })
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.error(textStatus + ' ' + XMLHttpRequest.status);
        errormod(Language.errorpetitionm2s);
    }
})
}

function deletestate(idf){
   $.ajax({
       type: "POST",
       crossDomain: true,
       url: "http://m2s.es/app/api/connect/delete-state.php",
       data: {"id": idf, "key": keyuser},
       cache: false,
       dataType: 'json',
       success: function(data) {
         if(data.mensaje == 'ok'){
            $('#'+idf+' .trash').remove();
            $('#' + idf).fadeOut(1000, function() {
              $('#' + idf).remove();
            })
         }else{
          console.log('Error: '+data.mensaje);
         }
       },
       error: function(XMLHttpRequest, textStatus, errorThrown) {
         console.error(textStatus + ' ' + XMLHttpRequest.status);
       }
   });
}