﻿/// <reference path="../jquery-1.3.2-vsdoc.js" />

/*
	Updates to this file must be run through the compressor at:
	http://javascriptcompressor.com/
	then the dsbo.shareit.min.js file must be updated
*/

/*global jQuery, document */

(function($) {
	$.shareit = function() {
		$.shareit.loading();
	};

	/*
	* Public, $.shareit methods
	*/

	$.extend($.shareit, {
		loading: function() {
			init();
		},

		reveal: function() {
			$(document).trigger('reveal.shareit');
		},

		close: function() {
			$(document).trigger('close.shareit');
			return false;
		}
	});

	/*
	* Public, $.fn methods
	*/

	$.fn.shareit = function() {
		init();

		function clickHandler() {
			$('div[id="shareit-box"]').hide();

			//get the height, top and calculate the left value for the sharebox
			var height = parseInt($(this).height(), 10);
			var top = parseInt($(this).position().top, 10);

			var position = ($(this).attr('rel') === 'shareit-top') ? "below" : "above";
			if (position === "below") {
				top = top + height;
			}
			else {
				top = top - (height * 2);
			}

			//get the left and find the center value
			var padding = parseInt($(this).css('padding-left').replace(/[\D]./, ""), 10) + parseInt($(this).css('padding-right').replace(/[\D]./, ""), 10);
			var left = parseInt($(this).position().left + ($(this).width() / 2) - ($('#shareit-box').width() / 2) + padding, 10);

			if (position === "above" || $.browser.msie) {
				left = ($(this).position().left - (($(this).width() + padding) / 2));
			}

			//grab the href value and explode the bar symbol to grab the url and title
			//the content should be in this format url|title

			//assign the value to variables and encode it to url friendly
			var url = $(this).attr('url');
			var title = $(this).attr('title');
			var field = $(this).attr('href');

			//assign the height for the header, so that the link is cover
			$('#shareit-header').height(height);
			$('#shareit-footer').height(height);

			//display the box
			var shareBox = null;
			if (position === "below") {
				shareBox = $('.shareit-below').css({ 'top': top, 'left': left }).show();
			}
			else {
				shareBox = $('.shareit-above').css({ 'top': top, 'left': left }).show();
			}

			//assign the url to the textfield
			shareBox.find('.shareit-field').val(url);
			shareBox.find('.shareit-field').focus();
			shareBox.find('.shareit-field').click(function(e) {
				// show close the box
				e.stopPropagation();
				e.preventDefault();
				return false;
			});

			return false;
		}

		return this.click(clickHandler);
	};

	/*
	* Private methods
	*/

	// called one time to setup shareit on this page
	function init() {
	};

	//onmouse out hide the shareit box
	$(document).click(function(e) {
		$('div[id="shareit-box"]').hide();
	});

	//hightlight the textfield on click event
	$('.shareit-field').focus(function(e) {
		$(this).select();
	});
})(jQuery);
