var myevent=new Array();
var myeventlist=new Array();
var eventdate='';
var eventbox=[];
var forumid=0;
var mydata=new Array();
eventbox.init=function() {
	if(window.localStorage.getItem('rec')!=null) {
	var rec=JSON.parse(window.localStorage.getItem('rec'));
	var info=rec.profile;
	if(info._class!=null) {
		$('body').removeClass();
		var myclass=info._class;
		if(myclass.length > 30) {
			$('body').attr('style',info._class);
		} else {
			$('body').addClass(info._class);
		}
	}	
	}
	eventbox.getmyevent();
}

eventbox.getmyeventlist=function()
{
	var myid=window.localStorage.getItem('cuserid');
	$.getJSON("/json/myeventjson.php", {subcat_id:289,userid:myid,id:Number(new Date)}, function(data){
		myeventlist=data;
		eventbox.displaymyeventlist(0);
	});
}

eventbox.getmyevent=function()
{
	$.getJSON("/json/eventjson.php", {subcat_id:289,id:2}, function(data){
		myevent=data;
		eventbox.displaymyevent(0);
	});
}


eventbox.displaymyevent=function(page)
{
	$('#mysubmit').hide();
	$('#myedit').hide();
	
	var k=page * 10;
	var count=0;
	var str='';
	var total=parseInt(myevent.length / 10);
	
	str+='<div class="title">'+myevent.length+' Events</div>';
	str+='<div class="pop in">';
	while((k < myevent.length)&&(count < 10))
	{
		var uid=myevent[k]._uid;
		var photo=myevent[k]._photo;
		var comment=myevent[k]._comment;
		var bigphoto=myevent[k]._bigphoto;
		var forumid=myevent[k]._forumid;
		var date = myevent[k]._date;
		var eventdate = myevent[k]._eventdate;
		var eventday = myevent[k]._eventday;
		str+='<table border="1" cellspacing="0" cellpadding="5" width="100%">';
		str+='<tr><td><a href="/users/'+uid+'/"><img src="'+photo+'" class="photo50">'+uid+'</a></td></tr>'; 
		str+='<tr><td class="coloryellow"><b class="subtitle">Event date: '+eventday+'</b></td></tr>';
		str+='<tr><td>';
		str+='<div class="mylink">'+ comment+'</div>';
		str+='</td></tr>';
		str+='<tr><td>Posted date : ' + date+'</td></tr>';
		str+='</table>';
		str+='<br><br>';
		k++;	
		count++;
	}
	str+='</div>';
	if(total > 0 && myevent.length > 10) {
		var mypage=page + 1;
		str+='<center><table border="0" cellpadding="10">';
		str+='<tr><td><img src="image/blackarrowleft.png" class="left"/></td>';
		str+='<td>Page '+mypage + ' of ' + total+'</td>';
		str+='<td><img src="image/blackarrowright.png" class="right"/></td></tr>';
		str+='</table></center>';
	}
	$('#myevent').html(str);
	$('#myevent').show();
	
	$('img.right').click(function() {
		if(page < total) {
			page++;
			eventbox.displaymyevent(page);
		}
	});
	
	$('img.left').click(function() {
		if(page > 0) {
			page--;
			eventbox.displaymyevent(page);
		}
	});	
	
	$('div.mylink img').each(function() {
		var w=$(this).width();
		if(w > 80)
		{
			$(this).attr({width:75,height:75});
		}
	});
	
	$('div.mylink img').click(function() {
		var i=$('div.mylink img').index(this);
		var photo=$('div.mylink img').eq(i).attr('src');
		var str='';
		str+='<div id="up" class="yellow">';
		str+='<img src="'+photo+'"/>';
		str+='</div>';
		$('#submenu').html(str);
		$('#submenu').show();
		$('#up').dialog({title:'Photo',modal:true});
	});
}


eventbox.displaymyeventlist=function(page)
{
	$('#mysubmit').hide();
	$('#myevent').hide();
	
	var k=page * 10;
	var count=0;
	var str='';
	var total=parseInt(myeventlist.length/10);
	if(myeventlist.length > 0) {
	str+='<div class="title">'+myeventlist.length+' Events</div>';
	while((k < myeventlist.length)&&(count < 10))
	{
		var uid=myeventlist[k]._uid;
		var photo=myeventlist[k]._photo;
		var comment=myeventlist[k]._comment;
		var bigphoto=myeventlist[k]._bigphoto;
		var forumid=myeventlist[k]._forumid;
		var date = myeventlist[k]._date;
		var eventdate = myeventlist[k]._eventdate;
		var eventday = myeventlist[k]._eventday;
		str+='<div id="'+k+'">';
		str+='<table border="1" cellspacing="0" cellpadding="5" width="100%">';
		str+='<tr><td><a href="/users/'+uid+'/index.php"><img src="'+photo+'" class="photo50">'+uid+'</a></td></tr>'; 
		str+='<tr><td class="coloryellow"><b>Event date: '+eventday+'</b></td></tr>';
		str+='<tr><td id="forum'+k+'">';
		str+='<div class="msg">'+ comment+'</div>';
		str+='</td></tr>';
		str+='<tr><td>Posted date : ' + date+'</td></tr>';
		str+='<tr><td><button onclick="eventbox.editmyevent('+k+');">Edit</button>&nbsp;&nbsp;';
		str+='<button onclick="eventbox.deletemyevent('+k+');">Delete</button></td></tr>';
		str+='</table>';
		str+='<br>';
		str+='</div>';
		k++;	
		count++;
	}
	} else {
		str+='<p>You have no event because you did not post any event.</p>';	
	}
	if(total > 1 && myeventlist.length > 10) {
		var mypage=page + 1;
		str+='<center><table border="0" cellpadding="10">';
		str+='<tr><td><img src="image/blackarrowleft.png" class="left"/></td>';
		str+='<td>Page '+mypage + ' of ' + total+'</td>';
		str+='<td><img src="image/blackarrowright.png" class="right"/></td></tr>';
		str+='</table></center>';
	}
	
	$('#myedit').html(str);
	$('#myedit').show();
	$('img.right').click(function() {
		if(page < total) {
			page++;
			eventbox.displaymyeventlist(page);
		}
	});
	
	$('img.left').click(function() {
		if(page > 0) {
			page--;
			eventbox.displaymyeventlist(page);
		}
	});	
	
	$('div.msg img').each(function() {
		var w=$(this).width();
		if(w > 80)
		{
			$(this).attr({width:75,height:75});
		}
	});
	
	$('div.msg img').click(function() {
		var i=$('div.msg img').index(this);
		var photo=$('div.msg img').eq(i).attr('src');
		var str='';
		str+='<div id="up" class="yellow">';
		str+='<img src="'+photo+'"/>';
		str+='</div>';
		$('#submenu').html(str);
		$('#submenu').show();
		$('#up').dialog({title:'Photo',modal:true});
	});
}



eventbox.createevent=function(todo)
{
	$('#mysubmit').show();
	$('#myevent').hide();
	$('#myedit').hide();
	
	var str='';
	str+='<form onsubmit="return false">';
	str+='<div id="errormsg" class="error"></div>';
	str+='<p><span><b>Title:</b></span>&nbsp;&nbsp;<span><input type="text" size="50" maxlength="250" id="eventtitle"/></span></p>';
	str+='<p><span><b>Information</b></span><br/>';
	str+='<span><textarea nowrap id="comment" rows="15" cols="60"></textarea></span></p>';
	str+='<p><span><b>Event date:</b></span>&nbsp;<span><input type="text" size="20" maxlength="30" id="eventdate" onclick="this.blur();"></span>';
	str+='&nbsp;&nbsp;<a onclick="return false;" id="selectevent"><img src="/image/calendar.gif" border="0"></a>';
	str+='&nbsp;<span id="mydate"></span></p>';
	str+='<div id="attache"></div>';
	str+='<div><input type="file" id="Filedata"/></div>';
	str+='<p><input type="button" id="submitq" value="Submit" class="button"></p>';
	str+='</form>';
	
	if(window.localStorage.getItem('cuserid')==null)
	{
		var str2='<fieldset><legend class="title">Create new event</legend>';
	str2+='<p>You need to <a href="/loginpage.html">login in order to submit an event</a>.</p>';
		str2+='</fieldset>';
		$('#mysubmit').html(str2);
	} else {
		
		$('#mysubmit').html(str);
		var myuid=window.localStorage.getItem('uid');
		var userid=window.localStorage.getItem('cuserid');
		$('#Filedata').uploadify({
    	'uploader'  : '/upload/uploadify.swf',
    	'script'    : '/upload/uploadfile.php',
    	'cancelImg' : '/upload/cancel.png',
    	'auto'      : true,
		'scriptData' : {"uid":myuid,"user_id":userid},
		'fileExt'     : '*.jpg;*.gif;*.png;*.pdf',
  		'fileDesc'    : 'Image Files',
		'multi'       : false,
		'sizeLimit'   : 50102400,
		'buttonText'  : 'Browse pic or pdf',
		'onProgress'  : function(event,ID,fileObj,data) {
      		var bytes = Math.round(data.bytesLoaded / 1024);
      		//$('#error').html(bytes+ 'kb');
    	},
	  'onComplete':function(event, ID, fileObj, response, data) {
			mydata=JSON.parse(response);
			$('#attache').html('<img src="http://club.khmer.org'+mydata.url+'" id="pic"/>');	
    	},
		'onError':function(event,ID,fileObj,errorObj) {
			$('#error').html(errorObj.info);
		}
  	});
		
		var dateflag=false;
		
		$('#selectevent').click(function() {
			$('#mydate').css({'z-index':'10','position':'absolute'});
			$("#mydate").datepicker({
				showStatus: true,
				currentText: 'Today',
				dateFormat: 'mm-dd-yy',
				onSelect: function(dateText) {
					eventdate=dateText;
					$('#eventdate').val(dateText);
					$('#mydate').hide('slow');
					dateflag=false;
				}
			});
			if(dateflag==false)
			{
				$('#mydate').show('slow');
				dateflag=true;
			} else {
				$('#mydate').hide('slow');
				dateflag=false;
			}
		});	
	
	
		$('#submitq').click(function() {
		   var str ='<div id="bodytext">'+ $('#comment').val() +'</div><br>'+ $('#attache').html();	
			var eventtitle= $('#eventtitle').val();
			var uid=window.localStorage.getItem('uid');
			var userid=window.localStorage.getItem('cuserid');
			if((str=='')||(eventtitle=='')||(eventdate==''))
			{
				$('#errormsg').html("Please enter Title, Information, and event date.");	
			} else {	
				str='<p class="title">'+eventtitle+'</p>' + str;
				$.ajax({type:"post",
		   		url:"/json/save_event.php", 
		   		data:"subcat_id=289&comment="+str+"&todo="+todo+"&uid="+uid+"&eventdate="+eventdate+'&userid='+userid+'&forum_id='+forumid, 
		   			success: function(data){
							$('#errormsg').html(data);
							$('#eventtitle').val('');
							$('#eventdate').val('');	
							$('#comment').val('');
							$('#attache').html('');
						}
				});	
			}
		});
	}
}

eventbox.editmyevent=function(i)
{
	$('#tab').tabs({selected:2});
	var title=$('#forum'+i+' div.msg').find('p.title').text();
	var comment=$('#forum'+i+' div.msg').find('#bodytext').html();
	var photo=$('#forum'+i+' div.msg').find('img').attr('src');
	eventdate=myeventlist[i]._eventdate;
	forumid=myeventlist[i]._forumid;	
	
	eventbox.createevent('Update');
	
	$('#eventtitle').val(title);
	$('#comment').val(comment);
	$('#attache').html('<img src="'+photo+'"/>');
	$('#eventdate').val(eventdate);
	
}

eventbox.deletemyevent=function(i)
{
	var forum_id=myeventlist[i]._forumid;
	var userid=window.localStorage.getItem('cuserid');
	var uid=window.localStorage.getItem('uid');
	var photo=$('#forum'+i+' div.msg').find('img').attr('src');
	$.ajax({type:"post",
		    url:"/json/save_event.php",
			data:"todo=Delete&forum_id="+forum_id+'&userid='+userid+'&uid='+uid+'&photo='+photo,
			success:function(data) {
					$('#'+i).remove();	
			}
	});
	
}
