// JavaScript Document
var topic;
var timestamp = 0;
var localtime = 0;
var i = 1;
var id = 1;

$(document).ready(function() {
						   
	$("textarea#description").focus();
	var desc = $("textarea#description").val('');	
	
//	$("#descPop").click(function() {
//		$('#popDescBlanket').show();
//		var closePop = $("#cl").val();
//		var description = $("#descText").val();
//		$('#popDes').append(closePop);
//		$('#popDes').append(description);					
//	});
		
		
	topic = $("#topic").val();
	getTopTwitters();
	getTopDiscussions();
	updatePostBox();
	updatePostBoxLocal();
	$('.notice').hide();
	$(".submitbtn").click(function() {
		$('.notice').hide();
		
		
		var alertPost = $("#user").val();
		if(alertPost == 'notLogin'){
			 alert('You must login first before you can post.');
			 $("#message").val('');
			 //$("#message").focus();
			 $('.notice').html(' ');
		}	
		
		var message = $("#message").val();
		var twitter = $("#twittercb").val();
		var local = $("#localcb").val();
		var repto = $("#replyto").val();
		var dataString = 'message=' + message;
		
		if($('#replyto').val != "" ) {
			dataString += '&replyto=' + repto;
		}
		
		if($("#twittercb:checked").val() != null)
			dataString += '&twitter=' + twitter;
			
		if($("#localcb:checked").val() != null)
			dataString += '&local=' + local;
			

				
		
		if(message=='') {
			
			$("#message").focus();   
			$('.notice').fadeOut(200).hide();
					
		}else if(($("#twittercb:checked").val() != null) || $("#localcb:checked").val() != null) {			
			
			
			$.ajax({
				type: "POST",
				url: "lib/post.php",
				data: dataString,
				success: function(){
					$('.notice').fadeIn(200).show();
					document.getElementById('message').value = '';
				}
			});
		}
	return false;
	});
});

function replytomsg(id, author) {
	$('#replyto').val(id);
	$('#message').val('@'+author+' ');
	$('#message').focus();
}

function text2xml(s) {
	var x, ie = /msie/i.test(navigator.userAgent);
	try{
		var p = ie? new ActiveXObject("Microsoft.XMLDOM") : new DOMParser();
		p.async = false;
	}catch(e){
		throw new Error("XML Parser could not be instantiated")
	};
	try{
		if(ie) x = p.loadXML(s)? p : false;
		else x = p.parseFromString(s, "text/xml");
	}catch(e){
		throw new Error("Error parsing XML string")
	};
	return x;
}

function getTopTwitters() {
	$.post('lib/getTopTwitters.php',{ }, function(xml) {
		setTopTwitters(xml);
	});
}

function setTopTwitters(xml) {
	xml = text2xml(xml);
	arr = jQuery.makeArray($("user",xml));
	$(arr).each( function(id) {
		entry = $(arr).get(id);
		i = (i+1)%2;
		$("#topUsers_bdy").prepend("<div class='twitter"+i+" new'>"+
								"<table>"+
									"<tr>"+
										"<td class='tImage' rowspan='2'><img height='75%' width='75%' src='"+$("profile_image_url",entry).text()+"'></td>"+
										"<td class='tAuthor'><a href='userprofile.php?user="+$("screen_name",entry).text()+"'>"+$("screen_name",entry).text()+"</a></td>"+
									"</tr>"+
									"<tr>"+
										"<td class='tDate'>"+$("followers",entry).text()+" followers</td>"+
									"</tr>"+
								"</table>"+
							  "</div>");
		$("#topUsers_bdy > div.new").slideDown("slow", function() {$(this).removeClass("new")} ).show();
	});
}

function getTopDiscussions() {
	$.post('lib/getTopDiscs.php',{ }, function(xml) {
		setTopDiscussions(xml);
	});
}

function setTopDiscussions(xml) {
	xml = text2xml(xml);
	arr = jQuery.makeArray($("topic",xml));
	arr.reverse();
	$(arr).each( function(id) {
		entry = $(arr).get(id);
		i = (i+1)%2;
		$("#topDiscs_bdy").prepend("<div class='twitter"+i+" new'>"+
								"<table>"+
									"<tr>"+
										"<td class='tDate'>"+$("rank",entry).text()+" </td>"+
										"<td class='tAuthor'><a href='viewbytopic.php?topic="+$("disc_id",entry).text()+"'>"+$("disc_title",entry).text()+"</a></td>"+
									"</tr>"+
								"</table>"+
							  "</div>");
		$("#topDiscs_bdy > div.new").slideDown("slow", function() {$(this).removeClass("new")} ).show();
	});
}

function addMessages(xml) {
	xml = text2xml(xml);
	arr = jQuery.makeArray($("entry",xml));
	arr.reverse();
	$(arr).each( function(id) {
		entry = $(arr).get(id);
		temp = $("id",entry).text();
		temp = temp.substring(28);
		if(timestamp < temp) timestamp = temp;
		i = (i+1)%2;
		id++;
		
		$("#twits").prepend("<div class='twit"+i+" new'>"+
								"<table width='565'>"+
									"<tr>"+
										"<td rowspan='3' class='tImage'><img width='48' height='48' src='"+$("link[rel='image']",entry).attr("href")+"'></td>"+
										"<td class='tAuthor'><a href='userprofile.php?user="+$("author",entry).find("uri").text().substring(19)+"'>"+$("author",entry).find("uri").text().substring(19)+"</a></td>"+
										"<td class='tReply' ><a href='javascript:void(0);' onclick='replytomsg(&quot;"+$("id",entry).text().substring(28)+"&quot;,&quot;"+$("author",entry).find("uri").text().substring(19)+"&quot;)'><img src='images/repBtn.gif' border='0'>&nbsp;Reply</a></td>"+
									"</tr>"+
									"<tr>"+
										"<td class='tMessage' colspan='2'>"+$("content",entry).text()+"</td>"+
									"</tr>"+
									"<tr>"+
										"<td class='tDate' colspan='2'>"+$("published",entry).text()+" from <strong>Twitter</strong></td>"+
									"</tr>"+
								"</table>"+
							  "</div>");
		$("#twits > div.new").slideDown("slow", function() {$(this).removeClass("new")} ).show();
	});
	setTimeout('updatePostBox()',6000);
}

function addLocalMessages(xml) {
	xml = text2xml(xml);
	if($("time",xml).text() != '')
		localtime = $("time",xml).text();
	arr = jQuery.makeArray($("entry",xml));
	arr.reverse();
	$(arr).each( function(id) {
		entry = $(arr).get(id);
		i = (i+1)%2;
		$("#twits").prepend("<div class='twit"+i+" new'>"+
								"<table width='565'>"+
									"<tr>"+
										"<td rowspan='3' class='tImage'><img width='48' height='48' src='"+$("profile_image_url",entry).text()+"'></td>"+
										"<td class='tAuthor'><a href='userprofile.php?user="+$("author",entry).text()+"'>"+$("author",entry).text()+"</a></td>"+
										"<td class='tReply' ><a href='javascript:void(0);' onclick='replytomsg(&quot;  &quot;,&quot;"+$("author",entry).text()+"&quot;)'><img src='images/repBtn.gif' border='0'>&nbsp;Reply</a></td>"+
									"</tr>"+
									"<tr>"+
										"<td class='tMessage' colspan='2'>"+$("content",entry).text()+"</td>"+
									"</tr>"+
									"<tr>"+
										"<td class='tDate' colspan='2'>"+$("published",entry).text()+" from <strong>PinoyTwits</strong></td>"+
									"</tr>"+
								"</table>"+
							  "</div>");
		$("#twits > div.new").slideDown("slow", function() {$(this).removeClass("new")} ).show();
	});
	setTimeout('updatePostBoxLocal()',6000);
}