﻿/// <reference path="../jquery-1.3.2-vsdoc.js" />
/// <reference path="../dsbo.js" />
/// <reference path="../dsbo/dsbo.ajax-extensions.js" />
/// <reference path="../dsbo/dsbo.string-extensions.js" />

/*
	Updates to this file must be run through the compressor at:
	http://javascriptcompressor.com/
	save output to article.min.js
*/

/*global $, jQuery */

jQuery.Namespace.Register('jQuery.DSBO.Content');

jQuery.DSBO.Content.LikeCookieName = "like-article-{0}";

jQuery.DSBO.Content.LikeArticle = function(articleID) {
	var articleName = $('#Article_Slug').val();
	var categoryName = $('#Category_Slug').val();

	var data = $.getAntiForgeryToken();
	var url = $.format("/{0}/{1}/like/{2}", categoryName, articleName, articleID);
	$.postJSON(url, data, function(count) {
		if (count > -1) {
			var cookieName = $.format($.DSBO.Content.LikeCookieName, articleID);
			$.DSBO.CreateCookie(cookieName, true, 365);
			// update count
			$('.like-it').find('.checked').text($.format("You and {0} others liked this.", count));
			// show liked link
			$('.like-it').find('.unchecked').animate({ opacity: 0.0 }, 100, "linear", function() {
				$('.like-it').find('.unchecked').hide();
				$('.like-it').find('.checked').fadeIn('fast');
			});
		}
		else {
			alert("There was a problem executing the action. Please try again later.");
		}
	});
};

jQuery.DSBO.Content.EmailArticle = function() {
	var articleName = $('#Article_Slug').val();
	var categoryName = $('#Category_Slug').val();
	$.DSBO.LoadPopupPage('popup', $.format('/{0}/{1}/email', categoryName, articleName), $('#spinner'));

	// submit button
	$('#facebox').find('.footer').empty();
	$('#facebox').find('.footer').append(
		$('<input>').attr('type', 'button').val('Submit').click(function() {
			// email
			var values = "";
			$('#emailForm').find('input,textarea').each(function(i, el) {
				values += $.format("&{0}={1}", $(el).attr('name'), $(el).val());
			});
			var data = $.format('{0}{1}', $.getAntiForgeryToken(), values);
			var url = $.format('/{0}/{1}/email', categoryName, articleName);
			$.postJSON(url, data, function(result) {
				if (result) {
					// close
					$(document).trigger('close.facebox');
				}
				else {
					alert("There was a problem emailing the article");
				}
			});
		})
	);

	// close button
	$('#facebox').find('.footer').append(
		$('<input>').attr('type', 'button').val('Close').click(function() {
			$(document).trigger('close.facebox');
		})
	);

	return false;
};

(function($) {
	$(document).ready(function(e) {
		var articleID = $('#Article_ID').val();

		// share
		$('a[rel^="shareit"]').each(function(i, el) {
			$(el).shareit();
		});

		// like
		var cookieName = $.format($.DSBO.Content.LikeCookieName, articleID);
		var liked = $.DSBO.ReadCookie(cookieName);
		if (liked) {
			$('.like-it').find('.unchecked').hide();
			$('.like-it').find('.checked').show().click(function(e) {
				return false;
			});

		}
		else {
			$('.like-it').find('.checked').hide();
			$('.like-it').find('.unchecked').show().click(function(e) {
				$.DSBO.Content.LikeArticle(articleID);
				return false;
			});
		}

		// email
		$('.email').attr({ 'rel': 'facebox', 'href': '#popup' });
		$('.email').facebox();
		$('.email').click(function(e) {
			$.DSBO.Content.EmailArticle();
			return false;
		});
	});
})(jQuery);
