/*
 * qTips.js
 * Copyright (c) 2011 Optical Communications.
 *
 * **USAGE**
 * - HTML -
 * <link rel="stylesheet" href="css/qtips.css">
 * <a id="target" href="some.png">TIPS LINK</a>
 * OR <a id="target" href="some.html?height=780&width=240">TIPS LINK</a>
 * - JS -
 * $('#target').qTips();
 */

(function($) {
	$.fn.qTips = function() {
		var names = {
			_bg:    'qtip_popupAll',
			_tip:   'qtip_baloon',
			_close: 'qtip_baloon_close',
			_in:    'qtip_baloon_inner'
		};
		var method = function() {
			var btn = $(this);
			var o   = btn.offset();
			$('body').append(
				'<div id="' + names._bg + '"></div>'
				+ '<div id="' + names._tip + '"><div id="' + names._in + '">'
				+ get_tag(this.href) +
				'<p id="' + names._close + '">閉じる</p></div></div>'
			);
			$.get(this.href, function(){
        $('#'+names._tip).css({width: $('#qins').width() + 20});
				var baloon_css = { top: o.top + btn.height() - 5, left: o.left };
				if ($.browser.msie && $.browser.version < 7.0) {
					$('#'+names._bg).css({height: $('body').height()});
				}
				var left_right = (o.left > $('body').innerWidth() / 2) ? 'right' : 'left';
				if (left_right == 'right') {
					baloon_css.left += btn.width() - $('#'+names._tip).width();
				}
				$('#'+names._tip).css(baloon_css).addClass(names._tip + '_' + left_right);
			});
			return false;
		};
		$(this).click(method);
		
		$('#'+names._bg+', #'+names._close).live('click', function(){
			$('#'+names._bg+', #'+names._tip).fadeOut(function(){$(this).remove();});
			return false;
		});
		
		var get_tag = function(href) {
			if (href.match(/\.(gif|jpe?g|png)$/)) {
				return '<img src="' + href + '" id="qins" />';
			} else {
				var style = [];
				style.push(href.match(/height=\d+/) || '');
				style.push(href.match(/width=\d+/)  || '');
				return '<iframe frameborder="0" src="' + href + '" ' + style.join(' ') + ' id="qins"></iframe>';
			}
		};
		
		return this;
	};
})(jQuery);




