/**
 * ITBrix.js
 * 
 * Bootstraps and extensions needed for the system.
 *
 * @author Ivan Zhekov (ITBrix LLC) <http://wordframe.net>
 */
(function (window, document, $, undefined) {


	// jQuery is a must!
	if (!$) return;


	/**
	 * Creates namespace from the specified identifier
	 * Derived from namespacedotjs <http://code.google.com/p/namespacedotjs/>
	 *
	 * @param {String} identifier The namespace string
	 */
	function Namespace(identifier) {
		var cursor = window;

		if (identifier) {
			var spaces = identifier.split(".");
			for (var i = 0; i < spaces.length; i++) {
				if (!cursor[spaces[i]]) cursor[spaces[i]] = {};
				cursor = cursor[spaces[i]];
			}
		}
	}
	window.Namespace = Namespace;




/*----------------------------------------------------------------------------*/




	/**
	 */
	var Prototype = Prototype || {};
	Prototype.create = function() {}


	/**
	 * extend
	 *
	 * @param {Object} destination
	 * @param {Object} source
	 *
	 * @returns {Object} destination
	 */
	function extend(destination, source) {
		for (var property in source)
			destination[property] = source[property];
		return destination;
	}


	/**
	 * @param {String} tag
	 * @param {Object} options
	 * @param {Array} children
	 */
	function $new(tag, options, children) {
		var element = document.createElement(tag);
		options = options || {};
		children = children || [];

		extend(element, options);
	
		for (var i = 0, l = children.length; i < l; i++) {
			element.appendChild($new.apply(this, children[i]));
		}
	
		return element;
	}




/*----------------------------------------------------------------------------*/




$(window).load(function() {

	$(".tweetmeme-holder").each(function() {

		var post_url = $(this).find(".post-metadata .url").html();
		var post_title = $(this).find(".post-metadata .title").html();
		var post_source = $(this).find(".post-metadata .source").html();
		$(this).find(".post-metadata").remove();


		if ($(this).find(".pointer_tweetmeme")[0]) {
			$(this).find(".pointer_tweetmeme").remove();
			$(this).append('<iframe src="http://api.tweetmeme.com/widget.js?url=' + post_url + '&amp;style=normal&amp;source=' + post_source + '" width="50" height="61" frameborder="0" scrolling="no"></iframe>');
		}

		if ($(this).find(".pointer_sharewidget")[0]) {
			$(this).find(".pointer_sharewidget").remove();
			var GUID = ShareWidget.GUID++;
			$(this).append(""
				+ "<div id=\"share-widget-pointer-" + GUID + "\" class=\"share-widget-pointer\">"
				+ "<a href=\"#\">Share</a>"
				+ "</div>"
			);
			$(this).delegate(".share-widget-pointer a", "click", function(event) {
				event.preventDefault();
				ShareWidget.toggleCompact(GUID);
			});
			$(document.body).append(""
				+ "<div class=\"share-widget-holder\" id=\"share-widget-" + GUID + "\">"
				+ ShareWidget.build(GUID, post_url, post_title, "compact")
				+ ShareWidget.build(GUID, post_url, post_title, "full")
				+ "</div>"				
			);
			$("#share-widget-" + GUID).delegate(".share-list-more a", "click", function(event) {
				event.preventDefault();
				ShareWidget.toggleFull(GUID);
			});
		}

	});


	$("body").delegate(".hide-sub .toggle-sub", "click", function(event) {
		event.preventDefault();
		$(this).parent(".hide-sub").removeClass("hide-sub").addClass("show-sub");
	});
	$("body").delegate(".show-sub .toggle-sub", "click", function(event) {
		event.preventDefault();
		$(this).parent(".show-sub").removeClass("show-sub").addClass("hide-sub");
	});

})




/*----------------------------------------------------------------------------*/




var ShareWidget = {

	GUID: 0,

	providers: {
		compact: {
			favourites: {
				name: "Favorites",
				urlTemplate: "#\" onclick=\"return false;"
			},
			print: {
				name: "Print",
				urlTemplate: "#\" onclick=\"window.print(); return false;"
			},
			digg: {
				name: "Digg",
				urlTemplate: "http://digg.com/submit?url=RE_URL&amp;title=RE_TITLE"
			},
			delicious: {
				name: "Delicious",
				urlTemplate: "http://delicious.com/post?url=RE_URL&amp;title=RE_TITLE"
			},
			google: {
				name: "Google",
				urlTemplate: "http://google.com/bookmarks/mark?op=add&amp;bkmk=RE_URL&amp;title=RE_TITLE"
			},
			live: {
				name: "Live",
				urlTemplate: "https://favorites.live.com/quickadd.aspx?url=RE_URL&amp;title=RE_TITLE"
			},
			twitter: {
				name: "Twitter",
				urlTemplate: "http://twitter.com/timeline/home?status=RE_URL"
			},
			facebook: {
				name: "Facebook",
				urlTemplate: "http://facebook.com/sharer.php?u=RE_URL&amp;t=RE_TITLE"
			},
			stumbleupon: {
				name: "StumbleUpon",
				urlTemplate: "http://stumbleupon.com/submit?url=RE_URL&amp;title=RE_TITLE"
			},
			more: {
				name: "More",
				urlTemplate: ""
			}
		},
		full: {
			aim: {
				name: "AIM",
				urlTemplate: "http://share.aim.com/share/?url=RE_URL&amp;title=RE_TITLE"
			},
			ask: {
				name: "Ask",
				urlTemplate: "http://myjeeves.ask.com/mysearch/BookmarkIt?url=RE_URL&amp;title=RE_TITLE&amp;t=webpages"
			},
			bebo: {
				name: "Bebo",
				urlTemplate: "http://bebo.com/c/share?Url=RE_URL&amp;Title=RE_TITLE"
			},
			blogmarks: {
				name: "Blogmarks",
				urlTemplate: "http://blogmarks.net/my/new.php?mini=1&amp;url=RE_URL&amp;title=RE_TITLE"
			},
			buzz: {
				name: "Buzz",
				urlTemplate: "http://buzz.yahoo.com/submit?submitUrl=RE_URL&amp;submitHeadline=RE_TITLE"
			},
			delicious: {
				name: "Delicious",
				urlTemplate: "http://delicious.com/post?url=RE_URL&amp;title=RE_TITLE"
			},
			digg: {
				name: "Digg",
				urlTemplate: "http://digg.com/submit?url=RE_URL&amp;title=RE_TITLE"
			},
			facebook: {
				name: "Facebook",
				urlTemplate: "http://facebook.com/sharer.php?u=RE_URL&amp;t=RE_TITLE"
			},
			favourites: {
				name: "Favorites",
				urlTemplate: ""
			},
			friendfeed: {
				name: "FriendFeed",
				urlTemplate: "http://friendfeed.com/share?url=RE_URL&amp;title=RE_TITLE"
			},
			google: {
				name: "Google",
				urlTemplate: "http://google.com/bookmarks/mark?op=add&amp;bkmk=RE_URL&amp;title=RE_TITLE"
			},
			linkedin: {
				name: "LinkedIn",
				urlTemplate: "http://www.linkedin.com/shareArticle?url=RE_URL&amp;title=RE_TITLE&amp;mini=true"
			},
			live: {
				name: "Live",
				urlTemplate: "https://favorites.live.com/quickadd.aspx?url=RE_URL&amp;title=RE_TITLE"
			},
			mixx: {
				name: "Mixx",
				urlTemplate: "http://mixx.com/submit?page_url=RE_URL"
			},
			multiply: {
				name: "Multiply",
				urlTemplate: "http://multiply.com/gus/journal/compose/?url=RE_URL&amp;subject=RE_TITLE"
			},
			myaol: {
				name: "myAOL",
				urlTemplate: "http://favorites.my.aol.com/ffclient/AddBookmark?url=RE_URL&amp;title=RE_TITLE"
			},
			myspace: {
				name: "MySpace",
				urlTemplate: "http://myspace.com/Modules/PostTo/Pages/?u=RE_URL&amp;t=RE_TITLE"
			},
			netvibes: {
				name: "Netvibes",
				urlTemplate: "http://netvibes.com/share?url=RE_URL&amp;title=RE_TITLE"
			},
			newsvine: {
				name: "Newsvine",
				urlTemplate: "http://newsvine.com/_tools/seed&amp;save?u=RE_URL&amp;h=RE_TITLE"
			},
			print: {
				name: "Print",
				urlTemplate: "#\" onclick=\"window.print(); return false;"
			},
			reddit: {
				name: "Reddit",
				urlTemplate: "http://reddit.com/submit?url=RE_URL&amp;title=RE_TITLE"
			},
			slashdot: {
				name: "Slashdot",
				urlTemplate: "http://slashdot.org/bookmark.pl?url=RE_URL"
			},
			spurl: {
				name: "Spurl",
				urlTemplate: "http://spurl.net/spurl.php?url=RE_URL&amp;title=RE_TITLE"
			},
			stumbleupon: {
				name: "StumbleUpon",
				urlTemplate: "http://stumbleupon.com/submit?url=RE_URL&amp;title=RE_TITLE"
			},
			tailrank: {
				name: "Tailrank",
				urlTemplate: "http://tailrank.com/share/?link_href=RE_URL&amp;title=RE_TITLE"
			},
			technorati: {
				name: "Technorati",
				urlTemplate: "http://technorati.com/faves/?add=RE_URL"
			},
			twitter: {
				name: "Twitter",
				urlTemplate: "http://twitter.com/timeline/home?status=RE_URL"
			},
			ybookmarks: {
				name: "Y!Bookmarks",
				urlTemplate: "http://bookmarks.yahoo.com/toolbar/savebm?u=RE_URL&amp;t=RE_TITLE"
			}
		}
	},


	toggleCompact: function ShareWidget_toggleCompact(fromID) {
		var holder = $("#share-widget-" + fromID);

		holder[0].style.top = $("#share-widget-pointer-" + fromID).offset().top + 20 + "px";
		holder[0].style.left = $("#share-widget-pointer-" + fromID).offset().left + 50 + "px";

		if (!holder.hasClass("show-full"))
			holder.toggleClass("show-compact");
		else
			holder.removeClass("show-full");
	},


	toggleFull: function ShareWidget_toggleFull(fromID) {
		var holder = $("#share-widget-" + fromID);

		holder[0].style.top = $("#share-widget-pointer-" + fromID).offset().top + 20 + "px";
		holder[0].style.left = $("#share-widget-pointer-" + fromID).offset().left + 50 + "px";

		holder.removeClass("show-compact");
		holder.addClass("show-full");
	},


	build: function ShareWidget_build(id, url, title, mode) {

		var url = url || "",
			title = title || "",
			mode = mode || "full"
			templateString = "";

		templateString += ""
			+ "<div class=\"share-list list-" + mode + "\">"
			+ "<div class=\"widget-head\">Bookmarks and share</div>"
			+ "<div class=\"widget-body\">"
			+ "<ul>";
		$.each(ShareWidget.providers[mode], function(key, value) {
			if (key != "more") {
				templateString += ""
					+ "<li>"
					+ "<a href=\"" + value["urlTemplate"] + "\" target=\"_blank\">"
					+ "<span class=\"share-icon icon-" + key + "\">"
					+ value["name"]
					+ "</span>"
					+ "</a>"
					+ "</li>";
			} else {
			templateString += ""
				+ "<li class=\"share-list-more\">"
				+ "<a href=\"#\">"
				+ "<span class=\"share-icon icon-" + key + "\">"
				+ value["name"]
				+ "</span>"
				+ "</a>"
				+ "</li>";
			}
		});
		templateString += ""
			+ "</ul>"
			+ "</div>"
			+ "</div>";

		templateString = templateString.replace(/RE_URL/g, url).replace(/RE_TITLE/g, title);

		return templateString;
		
	}

}




/*----------------------------------------------------------------------------*/




/*Namespace("ITBrix.JS.Utils", (function(){

	var overflowFix = (function() {

		var options = {
			selector: ".overflow-fix"
		}

		function $$(selector) {
			//if document.querySelector
		}

		function getStyle(element, style) {
			if (style == "float") style = "cssFloat";
			var value = element.style[style];
			if (!value || value == "auto") {
				var css = document.defaultView.getComputedStyle(element, null);
				value = css ? css[style] : null;
			}
			if (style == "opacity") return value ? parseFloat(value) : 1.0;
			return value == "auto" ? null : value;
		}

		function doFix(element) {
			var parent = element.parentNode,
				guide = document.createElement("div");

			guide.className = element.className + "";
			guide.style = element.style;

			//
			parent.inserBefore(guide, element);

			guide = parent.removeChild(guide);
			guide = parent = "";
			delete guide; delete parent;
		}

		return {
			options: options,
			doFix: doFix
		}

	})();

	return {
		overflowFix: overflowFix
	}

})());*/




/*----------------------------------------------------------------------------*/




})(window, document, jQuery)
