function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		
		if(c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie()
{
	username=getCookie('username');
	if (username!=null && username!="")
	{
		alert('Welcome again '+username+'!');
	}
	else {
		username=prompt('Please enter your name:',"");
		if (username!=null && username!="")
		{
			setCookie('username',username,365);
		}
	}
}

var loadedImg = new Array();

function loadImg(src)
{
	var img=new Image();
	img.src=src;
	loadedImg.push(img);
}

var audio;
var activeModule;
var activeAction;

$(document).ready(function(){
	//$("#leftMenu").scrollFollow({killSwitch:'scrollToggleImg'});
	var $this = $(this);
	$( ".column" ).resizable({minWidth:300,maxWidth:450,handles:"e",resize: function(event, ui){
		Utility.updateContainerWidth();
	},stop: function(event, ui) {
		
	}});
	
	$(".ui-resizable-handle").mousedown(function(){
		document.onselectstart = function(){ return false; };
	}).mouseup(function(){
		document.onselectstart = function(){ return true; };
	});
	
	Utility.updateContainerWidth();
	
	$("h3").mouseenter(function(){
		$(this).find(".delete").show();
	}).mouseleave(function(){
		$(this).find(".delete").hide();
	});
	
	$(".altHoverImg").hover(function(){
		iBox = new InfoBox($(this));
		iBox.show($(this).attr("alt"),-62,32);
	},function(){
		$(".infoBox").remove();
	});
	
	var sessionId=$("#hiddenSessionId").val();
	
	activeModule=$("#hiddenActiveModule").val();
	activeAction=$("#hiddenActiveAction").val();
	
	var facebookSession=$("#hiddenFacebookSession").val();
	var twitterSession=$("#hiddenTwitterSession").val();
	
	if($('#fileInput').length==1)
	{
		$('#fileInput').uploadify({
		'uploader'  : '/uploadify.swf',
		'script'    : '/uploadify.php',
		'cancelImg' : '/img/close2.png',
		'auto'      : true,
		'folder'    : '/uploads',
		'scriptData': {'session_name': sessionId},
		'onComplete': function(event,queueID,fileObj,response,data){
			if(response==0)
				alert("There was an error while uploading the image!");
			else {
				var img=new Image();
				img.src="http://www.feedmount.com/uploads/img/thumb/"+response+".jpg";
				
				if($("#publishItems").is(":hidden"))
					$("#publishItems").show();
				
				$("#publishItems").append(img);
				Publish.addImage(response);
			}
		}
		});
	}
	
	$(".more").live('mouseover',function(){
		$(this).addClass("moreHover");
	});
	
	$(".more").live('mouseout',function(){
		$(this).removeClass("moreHover");
	});
	
	$(".action").live('mouseenter',function(){
		ActionMenu.close(true);
		ActionMenu.open(this);
	});
	
	$(".action").live('mouseleave',function(){
		ActionMenu.close();
	});
	
	
	$(".stream").live('mouseover',function(){
		var footer = $(this).find(".actions");
		if(footer.is(":hidden"))
		{
			footer.fadeIn("fast");
		}
	});

	$(".stream").live('mouseleave',function(){
		var footer = $(this).find(".actions");
		ActionMenu.close();
		footer.fadeOut("fast");
	});
	
	$("#columns h3").mousedown(function(){
		document.onselectstart = function(){ return false; };
	}).mouseup(function(){
		document.onselectstart = function(){ return true; };
	});
		
	$("#columns").sortable({tolerance: 'pointer',helper: 'original',opacity: 0.7, revert:true, handle:"h3",axis:"x",start: function(event, ui) {
		
	},stop: function(event, ui) {
		
	},update: function(event, ui) {
		Col.saveCurrentOrder();
	}});
	
	
	$(".stream .likeLink").live('click',function(){
		
		var feed=$(this).parents(".stream");
		var attr=feed.attr("id");
		var arr=attr.split("-");
		var post_id=arr[1];
		if($(this).hasClass("positive"))
			Like.add(post_id);
		else if($(this).hasClass("negative"))
			Like.remove(post_id);
		
		return false;
	});
	
	$(".stream .commentLink").live('click',function(){
		var feed=$(this).parents(".stream");
		var attr=feed.attr("id");
		var arr=attr.split("-");
		var post_id=arr[1];
		
		Comment.show(post_id);
		
		return false;
	});

	$("#feedContent").keydown(function(e){
		resetPublishTextarea(e);
	}).focus(function(){
		resetPublishTextarea({keyCode:'i'});
	}).blur(function(){
		resetPublishTextarea({keyCode:'i'});
	});
	
	
	$("#feedContent").keyup(function(e){
		var area=$("#publisherContainer textarea");
		var val=area.val();
		var l=val.length;
		var left=140-l;
			$("#charCounter").html(left);
		
		if(e.keyCode==8 || e.keyCode==86 || e.keyCode==46)
			resetPublishTextarea(e);
	});
	
	$("#publishButton").click(function(){
		Publish.post();
	});
	
	Update.first();
	
	$(document).everyTime(60000, function(i) {
		 Update.general();
	});
	
	$("body").everyTime(1000,function(){
		$(".feedTimeStamp").each(function(){
			
			var second=parseInt($(this).val());
			second+=1;
			$(this).val(second);
		});
	});
	
	$("body").everyTime(30000,function(){
		
		$(".feedTimeStamp").each(function(){
			
			var second=parseInt($(this).val());
			
			var minute=		Math.floor(second/60);
			var hour=		Math.floor(minute/60);
			var day=		Math.floor(hour/24);
			var month=		Math.floor(day/30);
			
			var text;
			
			if(day > 0 || month > 0)
				return;
			else if(hour > 0)
			{
				if(hour == 1)
					text=hour+" hour";
				else
					text=hour+" hours";
			}
			else {
				if(minute==1 || minute==0)
					text=minute+" minute";
				else
					text=minute+" minutes";
			}
			
			
			$(this).parent().find(".timeText").html(text);
		});
	});
		
	var img1=new Image();
	img1.src="http://www.feedmount.com/img/icons/facebook_32_alpha.png";
	
	var img2=new Image();
	img2.src="http://www.feedmount.com/img/icons/twitter_32_alpha.png";
	
	var img3=new Image();
	img3.src="http://www.feedmount.com/img/icons/app_22_alpha.png";
	
	var img4=new Image();
	img4.src="http://www.feedmount.com/img/icons/media_22_alpha.png";
	
	var img5=new Image();
	img5.src="http://www.feedmount.com/img/icons/status_22_alpha.png";
	
	
	loadImg("http://www.feedmount.com/img/popBg_top.png");
	loadImg("http://www.feedmount.com/img/popBg_bottom.png");
	loadImg("http://www.feedmount.com/img/popBg_left.png");
	loadImg("http://www.feedmount.com/img/popBg_right.png");
	loadImg("http://www.feedmount.com/img/popBg_rightTop.png");
	loadImg("http://www.feedmount.com/img/popBg_rightBottom.png");
	loadImg("http://www.feedmount.com/img/popBg_leftTop.png");
	loadImg("http://www.feedmount.com/img/popBg_leftBottom.png");
	loadImg("http://www.feedmount.com/img/icons/archive_32_alpha.png");
	
	$("#leftMenu ul li").each(function(i){
		if(i==0)
			return;
		$(this).hover(function(){
			var link=$(this).find("a");
			menuHover(this,$(link[0]).attr("title"));
		},function(){
			$(".infoBox").remove();
			$(".remove",this).remove();
		});
	});
	
	
	if(activeModule=="Archive")
	{
		$(".stream").each(function(){
			$(this).hover(function(){
				var button=$("<button class='grayButtonSmall seeLaterButton'>Remove</button>");
				button.click(function(){
					Archive.remove(this);
				});
				$(this).append(button);
			},function(){
				$(".seeLaterButton",this).remove();
			});
		});
	}
	
	var text=new Array();
	$(".preText").live("focus",function(){
		var val=$(this).val();
		var attr=$(this).attr('name');
		if(text[attr]==undefined || text[attr]==val)
		{
			text[attr]=val;
			$(this).val("");
		}
	});
	
	$(".preText").live("blur",function(){
		var attr=$(this).attr('name');
		if($(this).val().length==0)
			$(this).val(text[attr]);
	});
	
	$(".thumbContainer").live("mouseenter",function(){
		$(".playImg",this).animate({left:'15'},50);
	});
	$(".thumbContainer").live("mouseleave",function(){
		$(".playImg",this).animate({left:'10'},50);
	});
	
	audio = new Audio("/inc/beep.mp3");
});
