Ext.ns('MW.widget');

Ext.onReady(
	function()
	{
		var widget = Ext.get('inlineLoginWidget');
		//var frame = Ext.get('inlineLoginFrame');
		var loves = Ext.query('a.love');
		for (var i = 0; i < loves.length; i++)
		{
			var love = loves[i];
			var href = love.href;
			var el = Ext.get(love);
			el.on(
				'click',
				function(e,el,o)
				{
					e.stopEvent();
					// get position of the link, position iframe, set src, and show
					var href = this.dom.href;
					// pass along the current base href
					href = (href.indexOf('?') > 0) 
						? href + '&base='+location.hostname+'&nocache='+Math.random() 
						: href + '?base='+location.hostname+'&nocache='+Math.random();
					widget.show();
					var bodyBox = Ext.getBody().getBox();
					var box = widget.getBox();
					if ((box.width + e.getPageX()) > bodyBox.width)
					{
						widget.setY(e.getPageY());
						widget.setX(bodyBox.right - box.width - 36)
					}
					else
					{
						widget.setXY(e.getXY());
					}
					
					 // clear the frameset if anything was loaded
					window.frames['inlineLoginFrame'].location.replace('about:blank');
					window.frames['inlineLoginFrame'].location.replace(href);
				}
			);
		}
		
		var closers = Ext.query('#inlineLoginWidget a.close');
		for(var i = 0; i < closers.length; i++)
		{
			var close = Ext.get(closers[i]);
			close.on(
				'click',
				function(e,el,o)
				{
					e.stopEvent();
					window.frames['inlineLoginFrame'].location.replace('about:blank');
					widget.hide();
				}
			);
		}
	}
);
	
MW.widget.InlineLogin = {};

/**
 * Resize the login widget iframe to the given height
 */
MW.widget.InlineLogin.resize = function(height)
{
	var frame = Ext.get('inlineLoginFrame');
	frame.setHeight(parseInt(height),true);
}

/**
 * Call the helper iframe with a new iframe height and additional parameters if provided
 */
MW.widget.InlineLogin.push = function(base)
{
	var height = document.body.offsetHeight;
	try
	{
		var src = '//'+base+'/main/js/widget/InlineLogin/frameHelper.html?height='+height+'&nocache='+Math.random();
		if (MW.params)
		{
			for(var p in MW.params)
			{
				src += '&'+p+'='+MW.params[p];
			}
			// clear MW.params following a push
			MW.params = null;
		}
		var push = document.getElementById('helpframe');
		push.src = src;
	}
	catch(e)
	{
		// dang.
		if (console)
		{
			console.log('push error');
			console.log(e);
		}
	}
}

/**
 * Extract a query parameter from the url by name
 */
MW.widget.InlineLogin.getParameterByName = function(name)
{
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if(results == null)
	{
		return "";
	}
	else
	{
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}
}

/**
 * 
 */
MW.widget.InlineLogin.updateLove = function(businessId)
{
	var id = parseInt(businessId);
	if (id < 1) { return; }
	var counts = Ext.query('a.love[data-business-id='+id+'] span.count');
	for(var i = 0; i < counts.length; i++)
	{
		var count = counts[i];
		var increment = parseInt(count.innerHTML);
		count.innerHTML = ++increment;
	}
}
