var mydata=new Array();
var mychat=new Array();
var path='http://club.khmer.org';
var total=0;
var totalmp3=0;
var totalmp4=0;
var index=0;
var mp3index=0;
var mp4index=0;
var currentpage=0;
var longitude=0;
var latitude=0;
var person=[];
var platform=window.navigator.platform;

person.init=function() {
	if(window.localStorage.getItem('cuserid')==null) {
		window.location.href='/index.html';	
	}
	person.getprofile();
	person.chat();
	person.getchat();
}


person.getprofile=function() {
var userid=window.localStorage.getItem('cuserid');
if(window.localStorage.getItem('rec')==null) {
	$.getJSON(path+'/json/userprofilejson.php',{userid:userid},function(data,status) {
		if(status=='success') {
			mydata=data;
			window.localStorage.setItem('rec',JSON.stringify(data));
			person.info();
		}
	});
 } else {
	 	mydata=JSON.parse(window.localStorage.getItem('rec'));
		person.info();
 }
}

person.info=function() {
	var info=mydata.profile;
	var str='';
	str+='<fieldset class="shadow"><legend>'+info._gender +'&nbsp;'+info._uid+'</legend>';
	str+='<img src="'+info._photo+'" class="photo"/>'
	str+='<div>'+info._city+'</div>';
	str+='<div id="sss">'+info._state+'</div>';
	str+='<div>'+info._country+'</div>';
	str+='<div>'+info._yearborn+' yrs</div>';
	str+='<div>'+info._hit+' hit</div>';
	str+='</fieldset>';
	str+='<div id="sidemenu"></div>';
	$('#info').html(str);
	person.sidemenu();
	if(info._state==null) {
		$('#sss').html('');	
	}
	if((info._follow==1)&&(window.localStorage.getItem('gps')==null)) {
		window.navigator.geolocation.getCurrentPosition(showmyMap,errorCB);		
		window.localStorage.setItem('gps',1);
	}
	if(info._class!=null) {
		$('body').removeClass();
		var myclass=info._class;
		if(myclass.length > 30) {
			$('body').attr('style',info._class);
		} else {
			$('body').addClass(info._class);
		}
	}
}

person.note=function() {
	var mynote=mydata.note;
	var info=mydata.profile;
	var str='';
	if(mynote.length > 1 ) {
		str+='<button id="notebtn">Edit note</button>';
	} else {
		str+='<button id="notebtn">Add note</button>';	
	}
	str+='<div id="pp">';
	for(var i=0; i < mynote.length; i++) {
		if(mynote[i]._noticeid > 0) {
		str+='<fieldset><legend>'+mynote[i]._title+'</legend>';
		str+='<p>'+mynote[i]._message+'</p>';
		str+='</fieldset>';
		}
	}
	str+='</div>';
	$('#note').html(str);
	$('#pp').addClass('pop in');
	
	$('#notebtn').click(function() {
		window.location.href='note.html';							 
	});
}



person.friends=function(page) {
	currentpage=page;
	var friend=mydata.friend;
	var i=page * 20;
	var k=i+1;
	var count=0;
	var totalfriend=Math.round(friend.length / 20);
	var str='';
	str+='<div id="pp">';
	while((i < friend.length)&&(count < 20)) {
		str+='<div class="box100">';
		str+='<div class="moveright">';
		str+='<img src="/image/x.png" onclick="person.deletefriend('+i+');"/>';
		str+='</div>';
		str+='<a href="/users/'+friend[i]._uid+'/">';
		str+='<img src="'+friend[i]._photo+'" class="photo50"/></a>';
		str+='<div>'+friend[i]._uid+'</div>';
		str+='</div>';
		i++;
		count++;
		k++;
	}
	str+='</div>';
	if(totalfriend > 0 && friend.length > 20) {
			
		var mypage=page + 1;
		str+='<center><table border="0" cellpadding="10">';
		str+='<tr><td><img src="image/blackarrowleft.png" class="leftfriend"/></td>';
		str+='<td>Page '+mypage + ' of ' + totalfriend+'</td>';
		str+='<td><img src="image/blackarrowright.png" class="rightfriend"/></td></tr>';
		str+='</table></center>';
	}
	$('#note').html(str);
	$('#pp').addClass('pop in');
	$('img.rightfriend').click(function() {
		page++;
		if(page < totalfriend) {
			person.friends(page);	
		} else {
			page=0;	
			person.friends(page);	
		}
	});
	
	$('img.leftfriend').click(function() {
		page--;
		if(page >=0) {
			person.friends(page);	
		} else {
			page=totalfriend;	
			person.friends(page);	
		}
	});
}

person.deletefriend=function(i)
{
	var myfriends=mydata.friend;
	var userid=window.localStorage.getItem('cuserid');
	var friendid=myfriends[i]._friendid;	
	$.ajax({url:path+'/json/approve.php',
				   type: 'POST',
				   data:'todo=2&friend_id='+friendid+'&userid='+userid+'&id='+Number(new Date), 
				   success:function (data) {
					//alert(data);   
					}
	 });
	var k=0;
	var temp=new Array();
	for(var j=0; j < myfriends.length; j++) {
		if(j==i) {
			//do nothing	
		} else {
			temp[k]=myfriends[j];
			k++;
		}
	}
	mydata.friend=temp;
	window.localStorage.removeItem('rec');
	window.localStorage.setItem('rec',JSON.stringify(mydata));
	person.friends(currentpage);
}


person.sidemenu=function() {
	var follow=mydata.profile._follow;
	var str='';
	str+='<div class="menu" id="inbox">'+label.inbox+'</div>';
	str+='<div class="menu" id="manage">'+label.managefriend+'</div>';
	str+='<div class="menu" onclick="person.note();">'+label.note+'</div>';
	str+='<div class="menu" id="account">'+label.account+'</div>';
	str+='<div class="menu" id="design">'+label.design+'</div>';
	if(follow==1) {
		str+='<div class="menu" id="followme">'+label.notfollow+'</div>';
	} else {
		str+='<div class="menu" id="followme">'+label.follow+'</div>';	
	}
	str+='<div class="menu" id="exports">'+label.exports+'</div>';
	$('#sidemenu').html(str);
	$('#account').click(function() {
		person.accountmenu();							 
	});
	$('#manage').click(function() {
		person.managefriend();
	});
	
	$('#inbox').click(function() {
		window.location.href='inbox.html';
	});
	$('#exports').click(function() {
		window.location.href='/mobile/';							 
	});
	$('#followme').click(function() {
		person.follow();							  
	});
	$('#design').click(function() {
		window.location.href='background.html';							
	});
}


person.chat=function(page) {
	var str='';
	str+='<form action="#" onsubmit="return false">';
	str+='<p>What do you have in mind today?</p>';
	str+='<p><textarea class="write" id="write" maxlength="250" wrap="soft"></textarea></p>';
	str+='<div><button class="button" id="submits">Submit</button>';
	str+='<div class="greybox" onclick="gourl(\'chat.html\');">MSG</div>';
	str+='<div class="greybox" onclick="gourl(\'../chat/index.php\');">Chat</div>';
	str+='<div class="greybox" onclick="gourl(\'event.html\');">Event</div>';
	str+='<div class="greybox" onclick="gourl(\'forum.html\');">Forum</div></div>';
	str+='<div id="error" class="error"></div>';
	str+='</form>';
	str+='<div id="mytext"></div>';
	str+='<div id="myresult"></div>';
	$('#note').html(str);
	$('#submits').click(function() {
		var text=$('#write').val();
	if(text=='')
	{
		$('#error').html('Enter your message');
	} else {
		$('#error').html('');
		var userid=window.localStorage.getItem('cuserid');
		$.ajax({url:path+'/json/clubchatresult.php',
				 type:'post',
				 data:'userid='+userid+'&message='+text+' '+platform+'&todo=Add',
				 success:function(data) {
					   $('#error').html(data);
					   $('#write').val('');
					   person.getchat();
					   	var st='';
						st+='<div class="border">';
						st+='<div class="block70">';
						st+='<img src="'+mydata.profile._photo+'" class="photo50"/></div>';
						st+='<div class="block410">'+text+'</div>';
						st+='</div>';
					   $('#mytext').prepend(st);
				}
		});
	}
	});						  
	
}

person.getchat=function() {
	var userid=window.localStorage.getItem('cuserid');
	$.getJSON(path+'/json/mychat.php',{userid:userid,id:Number(new Date)}, function(data,status) {
		if(status=='success') {
			mychat=data;
			person.showchat();	
		}
	});
}

person.showchat=function() {
	var str='';
	var count=0;
	var i=0;
	while((i < mychat.length)&&(count < 10)) {	
		str+='<div class="border"><div class="block70"><a href="/users/'+mychat[i]._uid+'/">';
		str+='<img src="'+mychat[i]._photo+'" class="photo50"/></a></div>';
		str+='<div class="block410">'+mychat[i]._message+'<br>';
		str+='<font class="small">'+mychat[i]._date+'</font></div></div>';
		i++;
		count++;
	}
	if(i < mychat.length) {
	str+='<section id="sec">';
	while(i < mychat.length) {	
		str+='<div class="border"><div class="block70"><a href="/users/'+mychat[i]._uid+'/">';
		str+='<img src="'+mychat[i]._photo+'" class="photo50"/></a></div>';
		str+='<div class="block410">'+mychat[i]._message+'<br>';
		str+='<font class="small">'+mychat[i]._date+'</font></div></div>';
		i++;
		count++;
	}
	str+='</section>';
	str+='<div class="more" id="more">More</div>';
	}
	$('#myresult').html(str);
	$('#sec').hide();
	var flag=true;
	$('#more').click(function() {
		if(flag) {
			$('#sec').show('slow');
			flag=false;
			$('#more').html('<a href="/chat.html">Public chat</a>');
		} else {
			$('#sec').hide('slow');
			flag=true;
			$('#more').html('More');
		}
	});
}

function gourl(url) {
	window.location.href=url;	
}

person.follow=function() {
	mydata=JSON.parse(window.localStorage.getItem('rec'));
	var follow=mydata.profile._follow;
	var str='';
	if(follow==1) {
		str+='<p>If you do not want to turn on your Geolocation, You can click on the button below to turn off your Geolocation.</p>';	
	} else {
	str+='<p>Are you sure that you want your friends to follow you?</p>';
	str+='<p>This will turn on your <b>GeoLocation</b> that will point exactly your location.</p>';
	}
	str+='<p>If you do not want a certain people <b>not</b> to follow you, ';
	str+='you can remove him or her from your friend list. Just click on Friends menu above.</p>';
	if(follow==1) {
		str+='<button id="notfollowit">'+label.notfollow+'</button>';
	} else {
		str+='<button id="followit">'+label.follow+'</button>';
	}
	str+='<div id="error" class="error"></div>';
	$('#note').html(str);
	$('#followit').click(function() {
		window.navigator.geolocation.getCurrentPosition(showMap,errorCB);							
	});
	var userid=window.localStorage.getItem('cuserid');
	$('#notfollowit').click(function() {
		var rec=JSON.parse(window.localStorage.getItem('rec'));
		rec.profile._follow=0;
		window.localStorage.removeItem('rec');
		window.localStorage.setItem('rec',JSON.stringify(rec));
		$.ajax({url:path+'/json/savelongitude.php',
			   type:'post',
			   data:'follow=0&userid='+userid,
			   success:function(data,status) {	
			   	   person.follow();
				  $('#error').html(data);
			   }
		});
	});
	
	$('#followit').click(function() {
		window.navigator.geolocation.getCurrentPosition(showMap,errorCB);
	});
}

function errorCB(e) {
	alert('error : ' + e.message);	
}

function showMap(position) {
      latitude=position.coords.latitude;
	  longitude=position.coords.longitude;
	  window.localStorage.removeItem('longitude');
	  window.localStorage.removeItem('latitude');
	  window.localStorage.setItem('longitude',longitude);
	  window.localStorage.setItem('latitude',latitude);
	  person.updatelocation();
}

person.updatelocation=function() {
		var rec=JSON.parse(window.localStorage.getItem('rec'));
		rec.profile._follow=1;
		rec.profile._latitude=latitude;
		rec.profile._longitude=longitude;
		window.localStorage.removeItem('rec');
		window.localStorage.setItem('rec',JSON.stringify(rec));
		var userid=window.localStorage.getItem('cuserid');
		$.ajax({url:path+'/json/savelongitude.php',
			   type:'post',
			   data:'follow=1&userid='+userid+'&long='+longitude+'&lat='+latitude,
			   success:function(data,status) {
				  person.follow();
				  $('#error').html(data);
			   }
		});	
	
}

function showmyMap(position) {
      latitude=position.coords.latitude;
	  longitude=position.coords.longitude;
	  window.localStorage.removeItem('longitude');
	  window.localStorage.removeItem('latitude');
	  window.localStorage.setItem('longitude',longitude);
	  window.localStorage.setItem('latitude',latitude);
	  person.updatemylocation();
}

person.updatemylocation=function() {
		var rec=JSON.parse(window.localStorage.getItem('rec'));
		rec.profile._follow=1;
		rec.profile._latitude=latitude;
		rec.profile._longitude=longitude;
		window.localStorage.removeItem('rec');
		window.localStorage.setItem('rec',JSON.stringify(rec));
		var userid=window.localStorage.getItem('cuserid');
		$.ajax({url:path+'/json/savelongitude.php',
			   type:'post',
			   data:'follow=1&userid='+userid+'&long='+longitude+'&lat='+latitude,
			   success:function(data,status) {
				  $('#error').html(data);
			   }
		});	
	
}
