$(function() {
	init_global_triggers();	
});
function init_global_triggers() {
	init_placeholders();
	init_good_buttons();
	init_follow_buttons();
	
	$('.hide-if-js').each(function() {
		$(this).hide();
	});

	
	$('.InfoMessage, .ErrorMessage').append("<div class='close'>X</div><div style='clear: both;'></div>");

	$(".InfoMessage .close, .ErrorMessage .close").click(function() {
		$(this).parent().fadeOut();
	});
	
	$('.json').click(function() {
		$.getJSON($(this).attr('rel'), {}, function(json) {
			
		});
		
		return false;
	});

	$('.dislike-button').click(function() {
		$(this).addClass('selected').html('Reported');
		$.getJSON($(this).attr('rel'), {}, function(json) {

		});
		
		return false;
	});	
}
function share_facebook(details) {
	FB.ui({
		method: 'feed',
		display: 'popup',
		name: details.name,
		picture: details.picture,
		link: details.link,
		description: details.status_update
	},
	function(response) {
		// success
		if (response && response.post_id) {
			$.getJSON('/json/json.save_share.php', {
				post_id: response.post_id,
				url: details.link,
				title: details.name
			}, function (json) {
				
			});
		} else {

		}
	});
}
function share_twitter(details) {
	window.open('http://twitter.com/share?url=' + encodeURIComponent(details.link) + '&text=' + encodeURIComponent(details.status_update));
}
function share_email(details) {
	document.location = 'mailto:?subject=' + details.status_update + '&body=' + details.email_body;
}
function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}
function init_good_buttons() 
{
	$('.good-button').unbind('click');
	$('.good-button').click(function() {
		try {
			var button = $(this);
			$.getJSON('/json/json.upvote.php', {
				method: 'vote',
				vote: 1,
				id: $(this).attr('rel')
			}, function(json) {
				if(json.success) {
					if(button.hasClass('small')) {
						if(!$('#like-count-' + json.id).hasClass('upvoted')) {
							$('#like-count-' + json.id).html(number_format(parseInt($('#like-count-' + json.id).html().replace(/,/g,'')) + 1));
							$('#like-count-' + json.id).addClass('upvoted');
						}					
					} else {
						if(!$('#good-button-' + json.id).hasClass('upvoted')) {
							$('#score-box-' + json.id).effect('highlight', {}, 3000);
							$('#score-box-' + json.id).html(number_format(parseInt($('#score-box-' + json.id).html().replace(/,/g,'')) + 1));
							$('#good-button-' + json.id + ' .like').html('');
							$('#good-button-' + json.id + ' .green').show();
							$('#good-button-' + json.id + ' img.white').hide();					
							$('#good-button-' + json.id).addClass('upvoted');					
						}
					}
				} else {
				
				}
			});
		} catch (e) {
			
		}
	});	
}
function init_follow_buttons() 
{
	$('.follow-button').unbind('hover');
	$('.follow-button').hover(function() {
		var button = $(this);
		if(button.hasClass('following')) {
			button.addClass('changed_text');
			button.find('span.text').html('Unfollow');			
		}
	}, function() {
		var button = $(this);
		if(button.hasClass('changed_text')) {

			button.removeClass('changed_text');

			if(button.hasClass('following')) {
				button.find('span.text').html('Following');
			} else {
				button.find('span.text').html('Follow');				
			}
		}
	});
	
	$('.follow-button').unbind('click');
	$('.follow-button').click(function() {
		//button.find('.waiting').show();
		try {
			var button = $(this);
			$.getJSON('/json/json.follow.php', {
				state: button.find('span.text').html(),
				method: $(this).attr('rel').split('-')[1],
				id: $(this).attr('rel').split('-')[0]
			}, function(json) {
				
				if(json.lite) {
					document.location = '/login/?return=' + document.location;
				}
				
				//button.find('.waiting').hide();
				if(json.success) {
					if(json.type == 'unfollow') {
						button.removeClass('following');
						button.find('.green').hide();
						button.find('span.text').html('Follow');						
					} else {
						button.addClass('following');
						button.find('.green').show();
						button.find('span.text').html('Following');						
					}
				} else {
				
				}
			});
		} catch (e) {
			
		}
	});	
}
function set_tag(tag) {
	$('div.tag').removeClass('selected yellow');
	$('div[rel="'+ tag +'"]').addClass('selected yellow');
}
function number_format(x) {
    return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}

function init_placeholders() {

	
	$('.placeholder').each(function() {
		$(this).focus(function() {
			if(!$(this).hasClass('empty')) {
				$(this).val('');
				$(this).addClass('empty');
			}
		});
	});
	
	$('textarea.mini').each(function() {
		$(this).focus(function() {
			if(!$(this).hasClass('large')) {
				$(this).css('height', "4em");
				$(this).addClass('large');
			}
		});
	});		
}
