function like() {
	var url = $(this).attr('href');
	
	$.ajax({
		type: "POST",
		url: url,
		success: function(response) {
			if(parseInt(response)) {
				$("#pagetools-like").html("Liked");
				$("#pagetools-like").addClass("isliked");
				$("#pagetools-like").unbind("click");
				$("#pagetools-like").click(unlike);
			} else {
				$("#pagetools-like").html("Already liked");
			}
		}
	});
	return false;
}

function unlike() {
    var url = $(this).attr('href');
    url = url.replace("/like/", "/unlike/");
    
    $.ajax({
        type : "POST",
        url : url,
		success: function(response) {
			if(parseInt(response)) {
				$(".isliked").unbind('click');
				$(".isliked").click(like);			
				$(".isliked").removeClass("isliked");
				$("#pagetools-like").html("Like this");
			} else {
				$("#pagetools-like").html("An error occured");
			}
		}
    });
    
    return false;
}

function favourite() {
	var url = $(this).attr('href');
	
	$.ajax({
		type: "POST",
		url: url,
		success: function(response) {
			if(parseInt(response)) {
				$(".notfav").attr("class", "save isfav");
				$(".isfav").unbind('click');
				$(".isfav").click(unfavourite);		//rebind isfav
				$("#pagetools-save").html("Unfavourite");
			} else {
				$("#pagetools-save").html("You can't favourite this page");
			}
		}
	});
	return false;
}

function unfavourite() {
	var url = $(this).attr('href');
	url = url.replace("/favourite/", "/unfavourite/");	//because in the controller we cant know whether user wants to favourite or unfavourite

	$.ajax({
		type: "POST",
		url: url,
		success: function(response) {
			if(parseInt(response)) {
				$(".isfav").attr("class", "save notfav");
				$(".notfav").unbind('click');
				$(".notfav").click(favourite);		//rebinds notfav
				$("#pagetools-save").html("Save to favourites");
			} else {
				$("#pagetools-save").html("Unable to remove from favourites");
			}
		}
	});
	return false;
}

function favouriteTeachingTool() {
	var url = $(this).attr('href');
	var el = $(this);
	
	$.ajax({
		type: "POST",
		url: url,
		success: function(response) {
			if(parseInt(response)) {
				el.attr("class", "add-teachingtool isfav-teachingtool");
				el.unbind('click');
				el.click(unfavouriteTeachingTool);
				el.html("Unfavourite this Teaching Tool");
			} else {
				el.html("Oops, something went wrong");
			}
		}
	});
	return false;
}

function unfavouriteTeachingTool() {
	var url = $(this).attr('href');
	var el = $(this);
	url = url.replace("/favourite/", "/unfavourite/");	//because in the controller we cant know whether user wants to favourite or unfavourite

	$.ajax({
		type: "POST",
		url: url,
		success: function(response) {
			if(parseInt(response)) {
				el.attr("class", "add-teachingtool notfav-teachingtool");
				el.unbind('click');
				el.click(favouriteTeachingTool);
				el.html("Add this Teaching Tool");
			} else {
				el.html("Unable to remove from favourites");
			}
		}
	});
	return false;
}

//method to add bookmarks
function addBookmark(title, url) {
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");		//Firefox
	} else if(document.all) {
		window.external.AddFavorite( url, title);	//IE
	} else if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {	//Chrome
		$(".bookmark").html("Bookmark: Press Ctrl/Cmd + D ");
	} else if(navigator.userAgent.toLowerCase().indexOf('safari') > -1) {	//Safari
		$(".bookmark").html("Bookmark: Press Ctrl/Cmd + D ");
	} else if(navigator.userAgent.toLowerCase().indexOf('opera') > -1) {	//Opera
		$(".bookmark").html("Bookmark: Press Ctrl/Cmd + D ");
	} else if( window.opera && window.print ) {
		return true;
	}
}

//logs a message when the user shares somewhere
function eventHandler(evt) { 
    if(evt.type == "addthis.menu.share") {
        var url = $(".share").attr('id') + "/social-network/" + evt.data.service;
    	$.ajax({
    		type: "POST",
    		url: url
    	});
    }
}

// Listen to various events
addthis.addEventListener('addthis.menu.open', eventHandler);
addthis.addEventListener('addthis.menu.close', eventHandler);
addthis.addEventListener('addthis.menu.share', eventHandler);

var addthis_config =
{
    ui_click : true
}

$(function() {
	$(".notliked:not(.nag)").click(like);
	$(".isliked:not(.nag)").click(unlike);
	$(".notfav:not(.nag)").click(favourite);
	$(".isfav:not(.nag)").click(unfavourite);

    $(".notfav-teachingtool:not(.nag)").click(favouriteTeachingTool);
    $(".isfav-teachingtool:not(.nag)").click(unfavouriteTeachingTool);
    
	$(".bookmark").click(function() {
		addBookmark(document.title, window.location);
		return false;
	});
	
	$(".print").click(function() {
		window.print();
		return false;
	});
});
