function showLoading()
{
	var windowWidth = $(window).width();
	var x = (windowWidth-$("#loadingMessage").width())/2;
	$("#loadingMessage").css("left",x+"px");
	$("#loadingMessage").show();
}

function removeLoading()
{
	$("#loadingMessage").hide();
}

var ActionMenu = {
	
	status:0,
	$menu:null,
	$button:null,
	$stream:null,
	
	open:function(obj)
	{
		this.$button = $(obj);
		this.$button.parent().find("a").css("background","url(/img/action_bg.png) center center repeat-x");
		this.$button.css("background","url(/img/action_hover_bg.png) center center repeat-x");
		this.$menu = $("<div class='feedMenu'></div>");
		
		this.$stream = this.$button.parents(".stream");
		
		var pos = $(obj).parent().position();
		this.$menu.css({"top":pos.top+23, "right":1});
		this.initMenuItems();
		
		ActionMenu.status = 1;
		
		this.$menu.mouseleave(function(){
			ActionMenu.close();
		});
		
		this.$menu.mouseenter(function(){
			ActionMenu.status = 1;
		});
		
		this.$menu.click(function(){
			ActionMenu.$button.parent().fadeOut("fast");
			ActionMenu.close(true);
		});
		
		$(obj).parents(".stream").append(this.$menu);
	},
	
	initMenuItems:function()
	{
		var toRight = 30;
		if(this.$button.hasClass("action-twitter-other"))
		{
			var m = "<ul>" +
					"<li><a href='#' onclick='Publish.reply(this);'>Reply</a></li>" +
					//"<li><a href='#'>Archive</a></li>" +
					"</ul>";
			this.$menu.append($(m));
		}
		else if(this.$button.hasClass("action-facebook-like"))
		{
			var likeCount = this.$stream.find(".likeCount").html();
			likeCount = parseInt(likeCount);
			
			var m = "<ul>" +
					"<li><a href='#' onclick='Like.add(this);'>Like</a></li>" +
					"<li><a href='#' onclick='Like.show(this);'>Show Likes ("+likeCount+")</a></li>" +
					"</ul>";
			this.$menu.append($(m));
			this.$menu.css({"right":72-toRight});
		}
		else if(this.$button.hasClass("action-facebook-comment"))
		{
			var commentCount = this.$stream.find(".commentCount").html();
			commentCount = parseInt(commentCount);
			
			var m = "<ul>" +
					"<li><a href='#' onclick='Comment.input(this);'>Comment</a></li>" +
					"<li><a href='#' onclick='Comment.show(this);'>Show Comments ("+commentCount+")</a></li>" +
					"</ul>";
			this.$menu.append($(m));
			this.$menu.css({"right":34-toRight});
		}
		else if(this.$button.hasClass("action-facebook-other"))
		{
			var m = "<ul>" +
					"<li><a href='#'>Other</a></li>" +
					"</ul>";
			this.$menu.append($(m));
		}
	},
	
	isOn:function()
	{
		return ActionMenu.status == 1;
	},
	
	isOff:function()
	{
		return ActionMenu.status == 0;
	},
	
	close:function(now){
		if(now)
		{
			ActionMenu.status=0;
			if(ActionMenu.$menu!=null)
				ActionMenu.$menu.remove();
			return;
		}
		
		ActionMenu.status=0;
		$(document).oneTime(10,function(){
			if(ActionMenu.status==0)
			{
				if(ActionMenu.$menu!=null)
				{
					ActionMenu.$menu.remove();
					ActionMenu.$menu=null;
					ActionMenu.$button.css("background","url(/img/action_bg.png) center center repeat-x");
				}
			}
		});
	}
};

var VIEW=0;

function playBeep()
{
	var beep=getCookie("beep");
	if(beep==1)
		audio.play();
}

function toggleBeep(obj)
{
	var beep=getCookie("beep");
	if(beep==1 || beep=="" || beep==undefined)
	{
		setCookie("beep",0,100);
		$(obj).attr("src","/img/icons/bell_24_alpha.png");
	}
	else {
		setCookie("beep",1,100);
		$(obj).attr("src","/img/icons/bell_24.png");
	}
}

function viewToggle()
{
	if(VIEW==1)
	{
		VIEW=0;
		$(".container").fadeTo("slow", 1);
	}
	else {
		VIEW=1;
		$(".container").fadeTo("slow", 0.6);
	}
}

function confirmMessage(message,func)
{
	if(func==undefined || func==null)
		funct="";
	Pop.show("Confirm",400);
	Pop.insert( "<p style='margin-top:20px;' class='confirm'>"+message+"</p><br/>"+
				"<p class='center'>"+
				"<button class='grayButton' onclick='Pop.close();"+func+"'>Yes</button> <button onclick='Pop.close();' class='grayButton red'>No</button>"
				+"</p>");
	
}

function showRestComments(obj)
{
	var p=$(obj).parents(".stream")[0];
	
	if($(p).find(".comment:last").is(":hidden"))
	{
		//show
		$(obj).html("show less comments.");
		$(p).find(".hiddenComments").slideDown();
	}
	else {
		//close
		$(obj).html("show more comments.");
		$(p).find(".hiddenComments").slideUp();
		$('html, body').animate({scrollTop: $(p).offset().top-20}, 500);
	}
}

function showMoreText(obj)
{
	var p=$(obj).parent().find('.hidden')[0];
	if($(p).is(":hidden"))
	{
		$(p).show();
		$(obj).html("see less.");
	}
	else {
		$(p).hide();
		$(obj).html("see more.");
	}
}

function uploadContainer()
{
	if($("#uploadContainer").is(":hidden"))
	{
		$("#imageUploadIcon").hide();
		$("#uploadContainer").show();
	}
}

function redirect(page)
{
	window.location=page;
}

function changeBg(profileBg)
{
	showTitleLoading("#h3TitleBg");
	$.post("/ajax/account/change_bg/"+profileBg,function(data) {
		removeTitleLoading("#h3TitleBg");
		redirect("/account/settings");
	},'json');
}

function simplePublish(text,publishToFacebook,publishToTwitter,id)
{
	var url="/ajax/post/post/1";

	$.post(url,{text:text,publishToFacebook:publishToFacebook,publishToTwitter:publishToTwitter},function(data) {

		if(id!=undefined && id!=null)
		{
			removeFeedAjaxLoader(id);
		}
		Update.general();
	},'json');
}

function menuHover(obj,text)
{
	iBox=new InfoBox($(obj));
	iBox.show(text,70,16);
}

function isArray(obj) {
	//returns true is it is an array
	if (obj.constructor.toString().indexOf("Array") == -1)
	return false;
	else
	return true;
}

function showFeedAjaxLoader(post_id)
{
	if($("#feed-"+post_id+" .title .feedAjaxLoader").length>0)
		return false;
	var img=new Image();
	img.src="/img/ajax1.gif";
	$(img).addClass("feedAjaxLoader");
	$("#feed-"+post_id+" .title").append($(img));
}

function removeFeedAjaxLoader(post_id)
{	
	$("#feed-"+post_id+" .title .feedAjaxLoader").remove();
}

function showTitleLoading(id)
{
	if(id==undefined || id==null)
		$("h3:first img:first").show();
	else {
		var i=$(id).find("img")[0];
		$(i).show();
	}
}

function removeTitleLoading(id)
{
	if(id==undefined || id==null)
		$("h3:first img:first").hide();
	else {
		var i=$(id).find("img")[0];
		$(i).hide();
	}
}

function retweet(id,obj)
{
	showFeedAjaxLoader(id);
	$.post("/ajax/Twitter/get_tweet/"+id,function(data){
		if(data.error==0)
		{
			$('html, body').animate({scrollTop: 0}, 500);
			if($("#publisherContainer").is(":hidden"))
			{
				$("#publisherContainer").slideToggle();
			}
			$("#feedContent").val("RT @"+data.text.u+": "+data.text.t);
			$("#feedContent").trigger("keyup").focus();
		}
		removeFeedAjaxLoader(id);
	},'json');
	
	/*
	var screenName=$("#feed-"+id).find(".screenName").html();
	var text="RT @"+screenName+" : "+$("#feed-"+id).find(".rawMessage").html();
	simplePublish(text, 1, 1,id);
	*/
}

function replaceVideo(post_id,file)
{
	var heightPercent=28/48;
	var width=300;
	
	var height=heightPercent*width;

	var so = new SWFObject("/mpw_player.swf", "swfplayer", width, height, "9", "#000000");
	so.addVariable("flv", file);
	so.addVariable("autoplay","true");
	so.addParam("allowFullScreen","true");
	//so.addParam("wmode","transparent"); 
	so.write('video_'+post_id);
}			
