var container;
$(document).ready(function() {
	container = $(document.body);
	runAll();
});

function runAll() {
	initThickbox();
	initRating();
}

/*--- initThickbox ---*/
function initThickbox() {
	if(typeof(tb_init) == 'function') {
		var _list = container.find('a.thickbox');
		if(_list.length > 0) tb_init(_list);
	}
}

/*--- initRating ---*/
function initRating() {
	var _list = container.find('input.star');
	if(_list.length > 0) $(_list).rating({required: 'hide'});
}

/*--- ajaxSpdyLink (home page quick link) ---*/
function ajaxSpdyLink()	{
	if ($("#url").val() == "" || $("#url").val() == "Enter URL")	{
		$("#url").val("Enter URL");
	} else {
		$.post("/ajax/ajax.shortenlink.php", { url: $("#url").val() },
			function(data)	{
				$("#quickresult").html("Your original link: " + $("#url").val().substr(0,60) + "<BR /><p><strong><a href='#' onClick=\"copyToClipboard('spdylink')\"><span id='spdylink'>" + data + "</span></a></strong></p><br /><small><a href='#' onClick='doAnotherLink();'>Shorten Another URL?</a></small>");
				$("#quickresult").show();
				$("#quickadd").hide();
			}
		);
	}
}

function doAnotherLink()	{
	$("#url").val("");
	$("#quickresult").hide();
	$("#quickadd").show();
}

/*--- get bookmark rating ---*/
/*
	flag = true - #rating_t_NNN
	flag = false - #rating_i_NNN
*/
function getBookmarkRating(id,min)	{
	var _index = 't';
	if(min == false)  _index='i';
	if($("#rating_"+_index+"_" + id).hasClass("star-rating-applied"))
	{
		$.post("/ajax/ajax.getrating.php", { bid : id },
			function(data)	{
				$("#rating_"+_index+"_" + id).rating("select", data);
				$("#rating_"+_index+"_" + id).rating("readOnly", true);
			}
		);
	}
	else
		setTimeout('getBookmarkRating('+id+','+min+')',2000);
}

/*--- load thumbnail image ---*/
function loadThumbnail(loc_url, loc_bid)	{
	$.post("/ajax/ajax.stw.php", { url : loc_url },
		function(data)	{
			$("#thumb_" + loc_bid).html(data);
		}
	);
}

/*--- load bookmark info (div) ---*/
function loadBookmark(id)	{
	var bmData = document.getElementById("bm_" + id + "_max");
	
	$.post("/ajax/ajax.bookmark.php", { bid : id },
		function(data)	{
			$("#bm_"+id+"_max").html(data);
			container = $("#bm_"+id+"_max");
			runAll();
		}
	);
}

/*--- sets the twitter status for account ---*/
function setTwitterStatus()	{
	var twitter_status = document.getElementById("twitter_status");
	
	if (twitter_status.value != "")	{
		$.post("/accounts/twitter/setStatus.php", { status: twitter_status.value },
			function(data)	{
				// update the tweets area
				getTwitterTweets(0);
				twitter_status.value = "";
			}
		);
	}
	return false;
}

/*--- shows the 20 most recent twitter status updates ---*/
function getTwitterTweets(auto_update)	{
	var twitterTweets = document.getElementById("twitter_tweets");
	
	$.post("/accounts/twitter/getTweets.php", { id: UserID },
		function(data)	{
			twitterTweets.innerHTML = data;
		}
	);
	
	if (auto_update != 0)	{
		// update once per 2-minutes
		setTimeout('getTwitterTweets(UserID)', 120000);
	}
}

/*--- display most popular links ---*/
function popularLinks()	{
	var popLinks = document.getElementById("popularLinks");
	
	$.post("/ajax/ajax.popularlinks.php", {}, 
		function(data)	{
			popLinks.innerHTML = data;
		}
	);
	
	// update once every 2 mins
	setTimeout('popularLinks()', 120000);
}

/*--- copyToClipboard ---*/
function copyToClipboard(objId)	{
//	var textArea = document.getElementById(objId);
//	textArea.focus();
//	textArea.select();
	if (window.clipboardData)	{
		var r = clipboardData.setData('Text', $("#" + objId).html());
		alert("Spdy.me URL is now on your clipboard.\nYou may now paste this URL wherever you would like.");
		return 1;
	} else {
		return 0;
	}
}

/*--- ajaxEmailNoDupe (signup page) ---*/
function ajaxEmailNoDupe()	{
	var email = document.getElementById("email");
	
	$.post("/ajax/ajax.emailcheck.php", { email: email.value },
		function(data)	{
			if (data == "true")	{
				email.style.background = "#FFB3B3";
				email.value = "Duplicate Email Exists";
			}
		}
	);
}
