$(document).ready(function(){
	$('.addMore').bind('click', function(){
		url = $(this).attr('url');
		var addMoreButton = $(this);
		$(".addMoreText").toggle();
		$(".addMoreImage").toggle();
		if($(this).attr('offset'))
		{
			url = url + "?offset=" + $(this).attr('offset');			
		}
		$.get(url, function(data){
			if(data.length > 0)
			{
				$('#jokes').append(data);				
				addMoreButton.attr('offset', parseInt(addMoreButton.attr('addToOffset')) + parseInt(addMoreButton.attr('offset')));
			}
			$(".addMoreText").toggle();
			$(".addMoreImage").toggle();
		});
	});
});

/* Submit A form via Ajax
 * form -> actual form to submit
 * reloadContainer -> if we get error, we have to fill data in that container
 * reloadUrl -> in case of success, we redirect to that URL
 */
function submitForm(form, reloadContainer, reloadUrl)
{
	form.ajaxSubmit(function(data){
		if($.trim(data.length) > 0)
		{
			reloadContainer.html(data);
		}
		else
		{
			window.location = reloadUrl;
		}
	});
}

/* Bind click on menu items ajax */
$('.menuItemsAjax').live('click', function(){
	var item = $(this);
	$('#dialog-modal').load($(this).attr('href'), function(data){
		$('#dialog-modal').attr('title', item.attr('title'));
		$('#dialog-modal').dialog('open');
	});	
});


/* Bind click on menu items */
$('.menuItems').live('click', function(){
	var item = $(this);
	window.location = item.attr('href');
});

/* Bind click on comment link */
$('.jokeCommentLink').live('click', function(){
	var el = $(this);
	if($('#'+el.attr('ref')).is(':visible'))
	{
		$('#'+el.attr('ref')).hide();
	}
	else
	{
		$('#'+$(this).attr('ref')).load($(this).attr('href'), function(data){
			if(data.length > 0)
			{
				$('#'+el.attr('ref')).show();
			}
					
		});
	}
	return false;
});

/* Bind click on new comment */
$('.new-comment-link').live('click', function(){
	var item = $(this);
	$.get($(this).attr('href'), function(data){
		var dataObj = $.parseJSON(data);
		$('#dialog-modal').html(dataObj.commentForm);		
		$('#dialog-modal').attr('title', item.attr('title'));
		$('#dialog-modal').dialog('open');
	});	
	return false;
});

$(document).ready(function(){
		
	$( "#dialog-modal" ).dialog({
		height: 200, 
		modal: true,
		autoOpen: false
	});
});



$(function(){
    // find all the input elements with title attributes
  $('input[title!=""]').hint('blurHint');
});

/*
 * like a joke (ajax request)
 * url -> ajax url
 * aTagObject -> current clicked tag
 */
function like(url, aTagObject)
{
	$('#' + aTagObject.attr('likeId')).load(url);
}

function submit_comment(comment_form, comment_container)
{
	comment_form.ajaxSubmit(function(data){
		var dataObj = $.parseJSON(data);
		if(dataObj.commentForm != undefined)
		{
			$('#dialog-modal').html(dataObj.commentForm);
		}
		else
		{
			comment_container.html(dataObj.comments);
			comment_container.show();
			$('#nbComments_'+comment_form.attr('jokeid')).html(dataObj.nb_comments);
			$('#dialog-modal').dialog('close');
		}
	});
}
function fbs_click(text) 
{
	u=location.href;
	t=text;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}
