function popup(text, width) {
	return '<div class="popup" style="width: ' + width + '">' + text + '</div>'; 
}


function disable_button(el, disabled_action) {
	if (el && typeof(el) != 'undefined') {
		el.oldClassName = el.className
		el.className = "button_disabled"
		el.disabled = true;
	}
}

function enable_button(el, enable_action, enable_href) {
	if (el && typeof(el) != 'undefined') {
		el.className = el.oldClassName
		el.disabled = false;
	}
}

$(document).ready(function () {
	$('.gw-tooltip .gw-hint-link').each(function () {
		var
			child = $(this).parent().parent().find('.gw-hint-content-wrapper'), 
			currentNode = this,

			default_tip, adjusted_tip, default_api, adjusted_api,

			beforeShow = function (event) {
				var
					type = this.options.mb_type,

					default_type_tooltip = default_api.elements.tooltip.hasClass('gw-tooltip-tooltip'),
					default_type_tooltip_back = default_api.elements.tooltip.hasClass('gw-tooltip-tooltip-back'),
					// default_type_tooltip_reverse = default_api.elements.tooltip.hasClass('gw-tooltip-tooltip-reverse'),
					// default_type_tooltip_reverse_back = default_api.elements.tooltip.hasClass('gw-tooltip-tooltip-reverse-back'),

					// adjusted_type_tooltip = adjusted_api.elements.tooltip.hasClass('gw-tooltip-tooltip'),
					// adjusted_type_tooltip_back = adjusted_api.elements.tooltip.hasClass('gw-tooltip-tooltip-back'),
					adjusted_type_tooltip_reverse = adjusted_api.elements.tooltip.hasClass('gw-tooltip-tooltip-reverse'),
					adjusted_type_tooltip_reverse_back = adjusted_api.elements.tooltip.hasClass('gw-tooltip-tooltip-reverse-back')
				;

				if (type == 'default' && default_type_tooltip) {
					return true;
				}
				if (type == 'adjusted' && default_type_tooltip_back && (adjusted_type_tooltip_reverse || adjusted_type_tooltip_reverse_back)) {
					return true;
				}

				return false;
			},

			onPositionUpdate = function (event) {
				var rel = this.elements.tip.attr('rel'), type = this.options.mb_type;

				this.elements.tooltip.removeClass('gw-tooltip-tooltip gw-tooltip-tooltip-back gw-tooltip-tooltip-reverse gw-tooltip-tooltip-reverse-back');
				this.elements.tip.removeClass('gw-tooltip-tip gw-tooltip-tip-back gw-tooltip-tip-reverse gw-tooltip-tip-reverse-back');
				this.elements.tip.removeClass('gw-tooltip-default gw-tooltip-adjusted');

				if (/^left/.exec(rel))   this.elements.tooltip.addClass('gw-tooltip-tooltip');
				if (/^right/.exec(rel))  this.elements.tooltip.addClass('gw-tooltip-tooltip-back');
				if (/^top/.exec(rel))    this.elements.tooltip.addClass('gw-tooltip-tooltip-reverse');
				if (/^bottom/.exec(rel)) this.elements.tooltip.addClass('gw-tooltip-tooltip-reverse-back');

				if (/^left/.exec(rel))   this.elements.tip.addClass('gw-tooltip-tip');
				if (/^right/.exec(rel))  this.elements.tip.addClass('gw-tooltip-tip-back');
				if (/^top/.exec(rel))    this.elements.tip.addClass('gw-tooltip-tip-reverse');
				if (/^bottom/.exec(rel)) this.elements.tip.addClass('gw-tooltip-tip-reverse-back');

				if (type == 'default')  this.elements.tooltip.addClass('gw-tooltip-default');
				if (type == 'adjusted') this.elements.tooltip.addClass('gw-tooltip-adjusted');

				return true;
			},

			default_options = {
				content: {
					prerender: true,
					text: child
				},
				show: {
					delay: 0,
					when: {
						event: 'mouseover'
					},
					effect: {
						type: 'grow',
						length: 0
					}
				},
				hide: {
					delay: 0,
					fixed: true,
					when: {
						event: 'mouseout'
					},
					effect: {
						type: 'grow',
						length: 0
					}
				},
				position: {
					corner: {
						target: 'rightTop',
						tooltip: 'leftTop'
					},
					adjust: {
						screen: true
					}
				},
				style: {
					tip: {
						corner: 'leftTop',
						color: '#A4A8B7',
						size: { x: 8, y: 18 }
					},
					classes: {
						target: 'gw-tooltip-link',
						tooltip: 'gw-tooltip-tooltip',
						tip: 'gw-tooltip-tip',
						title: 'gw-tooltip-title',
						button: 'gw-tooltip-button',
						content: 'gw-tooltip-content',
						active: 'gw-tooltip-active'
					}
				},
				api: {
					beforeShow: beforeShow,
					onPositionUpdate: onPositionUpdate
				},
				mb_type: 'default'
			},

			adjusted_options = jQuery.extend(true, {}, default_options)
		;

		adjusted_options.position.corner.target  = 'bottomRight';
		adjusted_options.position.corner.tooltip = 'topRight';
		adjusted_options.style.tip.corner = 'topRight';
		adjusted_options.style.tip.size = { x: 18, y: 8 };
		adjusted_options.style.classes.tip = 'gw-tooltip-tip-reverse';
		adjusted_options.mb_type = 'adjusted';

		adjusted_options.show.effect = default_options.show.effect;
		adjusted_options.hide.effect = default_options.hide.effect;

		default_tip = $(currentNode).qtip(default_options);
		default_api = default_tip.eq(0).qtip('api');

		adjusted_tip = $(currentNode).qtip(adjusted_options);
		adjusted_api = adjusted_tip.eq(0).qtip('api');

		$(currentNode).data('apis',[default_api, adjusted_api])

		setTimeout(function () {
			default_api.updatePosition();
			adjusted_api.updatePosition();

			if (child.parent().hasClass('gw-tooltip-active')) {
				default_api.show();
				adjusted_api.show();
			}
		});

		child.remove();
	});
});

function update_hint_text (target, text) {
	if (typeof(target) == 'string') target = '#'+target;
	text = '<div class="gw-hint-content-wrapper">' + text + '</div>';

	var apis = $(target).find('.gw-hint-link').eq(0).data('apis'), i, newtext = $(text).css('display','none').css('visibility','hidden');

	if ($.browser.msie) $(document.body).append(newtext);

	for (i = 0; i < apis.length; i++) {
		apis[i].updateContent(text, true);
		if ($.browser.msie) apis[i].updateWidth(newtext.width() > apis[i].options.style.width.max ? apis[i].options.style.width.max : newtext.width());
	}

	if ($.browser.msie) $(document.body).append(newtext);
}

// Current version of jquery (1.2.6) we used on GW, does not calculate correctly the window height,
// if browser is Opera. The issue is fixed into next version. So, wrap the core behaviour for this case.
(function () {
	if (jQuery().jquery == '1.2.6' && jQuery.browser.opera) {
		var old_func = jQuery.fn['height'];
		jQuery.fn['height'] = function (size) {
			if (this[0] == window) jQuery.browser.opera = false;
			var result = old_func.call(this,size);
			if (this[0] == window) jQuery.browser.opera = true;
			return result;
		};
	}
})();


