/* Moxbit QuickMarker Technology 2.0 */

/* Core */
/*
 * 	Ex Flex Fixed 0.1.2 - jQuery plugin
 *	written by Cyokodog	
 *
 *	Copyright (c) 2011 Cyokodog (http://d.hatena.ne.jp/cyokodog/)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
(function($){
	$.ex = $.ex || {};

	$.ex.flexFixed = function(idx , targets , option){
		var o = this,
		c = o.config = $.extend({} , $.ex.flexFixed.defaults , option);
		c.targets = targets;
		c.target = c.targets.eq(idx);
		c.index = idx;
		c.target.css('position','fixed');
		c._win = $(window);
		c._baseTop = c.target.offset().top - c._win.scrollTop();
		c._margin = {
			top : parseInt(c.target.css('margin-top')) || 0,
			bottom : parseInt(c.target.css('margin-bottom')) || 0
		}
		c._targetHeight = c.target.outerHeight();
		c._cont = c.container ? $(c.container) : o._getContainer();
		c._contBottom = c._cont.offset().top + c._cont.outerHeight();
		c._baseLeft = (c.target.offset().left - parseInt(c.target.css('margin-left')))- c._cont.offset().left - c._win.scrollLeft();
		o._setEvent();
	}
	$.extend($.ex.flexFixed.prototype, {
		_getContainer : function(){
			var o = this, c = o.config;
			var cont;
			var parents = c.target.parents()
			parents.each(function(idx){
				if (parents.eq(idx).outerHeight() > c._targetHeight + c._margin.top + c._margin.bottom) {
					cont = parents.eq(idx);
					return false;
				}
			});
			return cont ? cont : $('body');
		},
		_setEvent : function(){
			var o = this, c = o.config;
			var nextTop = prevTop = 0;
			var adjustPosition = function(){
				var scrollTop = c._win.scrollTop();
				var viewDff = 0;
				if (c._win.height() < c._targetHeight){
					viewDff = c._targetHeight - c._win.height() + c._margin.top + c._margin.bottom;
				}
				var downTop = (c._baseTop - c._margin.top) - scrollTop;
				if (downTop + viewDff >= 0) {
					nextTop = downTop;
				}
				else {
					var bottomTop = c._contBottom - (scrollTop + c._targetHeight + c._margin.top + c._margin.bottom);
					if (bottomTop + viewDff <= 0) {
						nextTop = bottomTop;
					}
					else {
						nextTop = - viewDff;
					}
				}
				if (prevTop != nextTop) {
					c.target.css('top',nextTop);
				}
					c.target.css('top',nextTop);
				prevTop = nextTop;
				o._setLeft();
			}
			adjustPosition();
			c._win.
				scroll(function(){
					adjustPosition();
				}).
				resize(function(){
					o._setLeft();
				});
		},
		_setLeft : function(){
			var o = this, c = o.config;
			c.target.css('left',c._cont.offset().left + c._baseLeft - c._win.scrollLeft());
		}

	});
	$.ex.flexFixed.defaults = {
		container : null
	}
	$.fn.exFlexFixed = function(option){
		var targets = this;
		
		targets.each(function(idx) {
			var target = targets.eq(idx);
			var obj = target.data('ex-flex-fixed') || new $.ex.flexFixed( idx , targets , option);
			target.data('ex-flex-fixed',obj);
		});
		return targets;
	}

})(jQuery);
/* Core */

/* Back to Top */
function backToTop() {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;
	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;
	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
	if (x > 0 || y > 0) {
		window.setTimeout("backToTop()", 25);
	}
}
/* Back to Top */

/* ToolTip */
$(function() {
	$("#right-first ul li *").each(function(){
		var self = $(this), title = self.attr("title");
		var wrapper = $("#wrapper").offset();
		var offset = $(this).offset();
		
		self.hover(
			function(){
				$(".tooltip").remove();
				self.attr("title","");
				$("#wrapper").append("<div class='tooltip'>"+title+"</div>");
				$(".tooltip").css({
					position: "absolute",
					opacity: 0,
					top: offset.top-wrapper.top,
					right: self.width() + 40
				}).hide().stop(true, false).animate({opacity: 1, width:'toggle'}, 250);
			},
			function(){
				self.attr("title",title);
				$(".tooltip").stop(true, false).animate({opacity: 0, width:'toggle'}, 420, function() {$(this).remove();} );
			}
		);
	});
});
/* ToolTip */

/* ToolTip-QuickMarker */
$(function() {
	$("#quickmarker-contents ul li *").each(function(){
		var self = $(this), title = self.attr("title");
		
		self.hover(
			function(){
				$(".tooltip-quickmarker").remove();
				self.attr("title","");
				$("#wrapper").append("<div class='tooltip-quickmarker'>"+title+"</div>");
				
				var ypoint = $(document).scrollTop();
				var wrapper = $("#wrapper").offset();
				var offset = $(this).offset();

					$(".tooltip-quickmarker").css({
						position: "absolute",
						opacity: 0,
						top: offset.top-wrapper.top,
						right: self.width() + 40
					}).hide().stop(true, false).animate({opacity: 1, width:'toggle'}, 250);

			},
			function(){
				self.attr("title",title);
				$(".tooltip-quickmarker").stop(true, false).animate({opacity: 0, width:'toggle'}, 420, function() {$(this).remove();} );
			}
		);
	});
});
/* ToolTip-QuickMarker */

/* ToolTip-End */
$(function(){
	$(window).scroll(function () {
		$(".tooltip-quickmarker").stop(true, false).animate({opacity: 0}, 420, function() {$(this).remove();} );
		$(".tooltip").stop(true, false).animate({opacity: 0}, 420, function() {$(this).remove();} );
		
	});
});
/* ToolTip-End */
