$(function(){
	
	//hide all reply and retweet buttons on mylaglee

	
	//flickr mouseover
	$('#flickr_pic a').hover(function(e){
		var offsetY = 20;
		var offsetX = 10;
		var a = $(this).parent().get(0);
		var b = $(a).find('#mlink').html();
		$('<img id="largeImage" src="'+ b + '" alt="image" />')
		.css('top', e.pageY + offsetY)
		.css('left', e.pageX + offsetX)
		.appendTo('body').hide().fadeIn("normal");	
		//console.log(b);
		
		$('#flickr_pic a').mousemove(function(e){
			$('#largeImage')
			.css('top', e.pageY + offsetY)
			.css('left', e.pageX + offsetX);
		});
		
	}, function(){
		//mouse off
		$('#largeImage').remove();
	});
	
	$('#navi a').live('click', function(){
		var a = $(this).html();
		var c = a.replace(" ", "");
		var urls = $(this).attr('href');
		var content = [];
		var url_content = [];
		var b = c.replace("'", "");
		if(b.charAt(0) == "#"){
			temp = b.substr(1);
			b = temp;
		}
		document.title = a + " - Laglee.net";
		//$('#load').fadeIn('normal');
		$('#wrapper div').hide();
		$('#wrapper .'+b +' div').show();
		$('#wrapper .'+b).load(urls+" #tcontent");
		$('#wrapper .'+b).show();

		//$(a).addClass('selected');
		//$('#load').fadeOut('normal');
		//console.log(a);
		return false;
	});
	
	$('.tags a').live('click', function(){
		var a = $(this).html();
		var b = $('#navi li');
		var c = $('#navi').find('#' + a);
		var d = a.replace(" ", "");
		if(d.charAt(0) == "#"){
			temp = d.substr(1);
			d = temp;
		}
		
		document.title = a + " - Laglee.net";
		
		var location = null;
		var url_location = null;
		//$('#tweets').hide().html("");
		var urls = $(this).attr('href');
		var content = [];
		var url_content = [];
		
		if(c.length < 1){
			if(b.length > 4){
				var toRemove = $('#navi :first');
				var e = $(toRemove).html();
				$(toRemove).remove();
				$('#wrapper div:first').remove();
			}	
			$('#wrapper div').hide();
			b.removeClass('selected');
			$("<li id="+a+"><a href=\"/tsearch?q="+a+"\">"+a+"</a></li>").appendTo('#navi').addClass('selected');
			
			$("<div class="+a+"><div id=\"tweetUrl\" class=\"grid_6 alpha\"><span id=\"tpages\"><p>Popular Web Pages</p></span></div><div id=\"tweets\" class=\"grid_6 omega\"><span id=\"ttweets\"><p>Latest Tweets</p></span></div></div>").appendTo('#wrapper').show();
			$('#load').fadeIn('normal');
			
			$.get(urls, function(data){
				$(data).find('#tweets').find('> div').each(function(){
					content.push('<div id=tweet>'+$(this).html()+'</div>');
				});

				$(data).find('#tweetUrl').find('> div').each(function(){
					url_content.push('<div id=url>'+$(this).html()+'</div>');
				});
				
				location = $('.'+d+ ' div:eq(1)');
				for(var i = 0; i < content.length; i++){
					$(content[i]).appendTo(location);
				}
				
				url_location = $('.'+d+ ' div:eq(0)');
				for(var j = 0; j < url_content.length; j++){
					$(url_content[j]).appendTo(url_location);
				}

			});
			
			$('#load').fadeOut('normal');
			
		}else{
			//$('#load').fadeIn('normal');
			$('#wrapper div').hide();
			$('#wrapper .'+d +' div').show();
			$('#wrapper .'+d).show();
			
			//b.removeClass('selected');
			//$(c).addClass('selected');
			//$('#load').fadeOut('normal');
		}
		
		return false;
	});
	
	function loadCloud(){
		$.get("/tcs.do?meth=cache", function(data){
			$('.tags').stop().animate({"opacity" : 1}).html(data);
			$('.tags2').stop().animate({"opacity" : 1}).html(data);
			$('#header').stop().animate({"opacity" : 1});
		}); 
	}

	setInterval(function(){
		$('#header').stop().animate({"opacity" : .5});
		$('.tags').stop().animate({"opacity" : .5}, loadCloud());
	}, (15 * 1000));
	
	$('.tags').hover(function(){
		$(this).stop().animate({"opacity" : 1});
	});

});

function loadData(){
	var urlList = [];
	var content = [];
	var url_content = [];
	var location = null;
	var url_location = null;
	var name = [];
	$('#navi').find('li a').each(function(){     
		var a = $(this).html(); 
		var urls = $(this).attr('href');
		var c = a.replace(" ", "");
		var b = c.replace("'", "");
		if(b.charAt(0) == "#"){
			temp = b.substr(1);
			b = temp;
		}
		
		$("<div class="+b+">"+b+"<div id=\"tweetUrl\" class=\"grid_6 alpha\"><span id=\"tpages\"><p>Popular Web Pages</p></span></div><div id=\"tweets\" class=\"grid_6 omega\"><span id=\"ttweets\"><p>Latest Tweets</p></span></div></div>").appendTo('#wrapper');
		urlList.push(urls);
		name.push(b);
		
	});	
	$('#navi li:first a').click();
};

function update(url, d){
	var urls = url;
	var content = [];
	var url_content = [];
	
	$.get(urls, function(data){
		$(data).find('#tweets').find('> div').each(function(){
			content.push('<div id=tweet>'+$(this).html()+'</div>');
		});

		$(data).find('#tweetUrl').find('> div').each(function(){
			url_content.push('<div id=url>'+$(this).html()+'</div>');
		});
		
		location = $('.'+b+ ' div:eq(1)');
		for(var i = 0; i < content.length; i++){
			$(content[i]).appendTo(location);
		}
		
		url_location = $('.'+b+ ' div:eq(0)');
		for(var i = 0; i < url_content.length; i++){
			$(url_content[i]).appendTo(url_location);
		}

	});
	
}

function loadUpdates(){
	$('#content').load("/TwitServ #content");
}

function checkLogin(){
	var login = $('.login').html();
	if(login == null){
		$('#state').html("<div class=\"grid_6 alpha omega\"><a href=/tlogin>Sign in with twitter to view your followers' updates</a></div>");
	}else{
		loadUpdates();
		setInterval("loadUpdates()", 180000);
	}
}

function limitChars(textid, limit, infodiv){
	var text = $('#twitbox .'+textid).val();	
    var textlength = text.length;
    if (textlength > limit) {
        $('#twitbox .' + infodiv).html('You cannot write more then ' + limit + ' characters! <span id=close><a href="#">close</a></span>');
        $('#twitbox .' + textid).val(text.substr(0, limit));
        return false;
    }
    else {
        $('#twitbox .' + infodiv).html('You have ' + (limit - textlength) + ' characters left. <span id=close><a href="#">close</a></span>');
        return true;
    }
};

function twitApiFunction(){
	var para = null;
	var status_id = null;
	$('.tbuttons a').live('click', function(e){
		$('#twitbox .text').keyup(function(){
	        limitChars('text', 144, 'charlimitinfo');
	    });
		
		var id = $(this).attr('id');
		var text1 = $(this).parent().parent().find('#text').html();
		var user = $(this).parent().parent().find('#user').html();
		status_id = $(this).parent().parent().find('#status_id').html();
		$('#twitbox #data').html(user);
		//$('#twitbox .text').val("@"+user);
		//console.log(status_id);
		$('.title').html(id);
		if(id == "update"){
			$('.title').html("What are you doing?");
			$('#twitbox .text').val("");
			para = "tweet";
		}
		if(id == "retweet"){
			$('#twitbox .text').val("RT @"+user +" "+text1);
			para = "tweet";
		}
		if(id == "reply"){
			$('#twitbox .text').val("@"+user);
			para = "reply";
		}
		
		//Get the screen height and width  
		var maskHeight = $(document).height();  
		var maskWidth = $(window).width();  
		var mouse = e.pageY;
		//console.log(mouse);
		$('#backgroundPopup').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect       
		$('#backgroundPopup').fadeIn('fast');      
		$('#backgroundPopup').fadeTo("slow",0.8); 
		
		//Get the window height and width  
		var winH = $(window).height();  
		var winW = $(window).width();  
		                 
		//Set the popup window to center
		//winH/2-$('#twitbox').height()/2 changed to mouse
		$('#twitbox').css('top',  mouse - 20);  
		$('#twitbox').css('left', winW/2-$('#twitbox').width()/2);  
		      
		//transition effect  
		$('#twitbox').fadeIn('normal'); 
		return false;
	});
	
	$('#twitbox #submit').click(function(){
		var data = $('#twitbox .text').val();
		$('#twitbox #data').html("para="+para+"&text="+data);
		dataString = "para="+para+"&text="+escape(data)+"&status_id="+status_id;
		$.ajax({
			type: "GET",
			url: "tas.do",
			data: dataString,
			success: function(){
				$('#twitbox #data').html("success");
				$('#twitbox').fadeOut('normal');
				$('#backgroundPopup').fadeOut('normal');
				$('#twitbox #data').html("");
			}
		});
		return false;
	});	
	
	$('#close a').live('click', function(){
		$('#twitbox .text').val("");
		$('#twitbox').fadeOut('normal');
		$('#backgroundPopup').fadeOut('normal');
		return false;
	});
	
	function statusRefresh(){
		$('#statusUp').load("/tas?para=status");
	}
}




