var oBrowserCheck = 
{
	'oPrefs': null,
	
	'Init': function ()
	{
		var oPrefs = this.oPrefs = Drupal.settings.mmg_browser_check;
		var sVer = this.Parse(jQuery.browser.version, 4, 3);

		if // check criteria
		(
			(jQuery.browser.mozilla && sVer < this.Parse(oPrefs.sVerGecko , 4, 3)) ||
			(jQuery.browser.safari  && sVer < this.Parse(oPrefs.sVerWebkit, 4, 3)) ||
			(jQuery.browser.msie    && sVer < this.Parse(oPrefs.sVerMsie  , 4, 3)) ||
			(jQuery.browser.opera   && sVer < this.Parse(oPrefs.sVerOpera , 4, 3))
		)
		{ this.Open(); }
	},
	
	'Open': function ()
	{
		var oPrefs = this.oPrefs;
		var jTint, jWrapper, jDialog, jLinks, iDays;
		
		$('body').append
		(
			jTint = $(document.createElement('div'))
			.attr('id', 'mmg-browser-check-tint')
			.bind('click.mmg_browser_check', function ()
			{
				if (oBrowserCheck.oPrefs.bBGClose) { oBrowserCheck.Close(true); }
			})
			.append
			(
				jWrapper = $(document.createElement('div'))
				.attr('id', 'mmg-browser-check-wrapper')
				.append
				(
					jDialog = $(document.createElement('div'))
					.attr('id', 'mmg-browser-check-dialog')
					.append
					(
						$(document.createElement('h2'))
						.attr('id', 'mmg-browser-check-title')
						.html(oPrefs.sTitle),
						
						$(document.createElement('p'))
						.attr('id', 'mmg-browser-check-prompt')
						.html(oPrefs.sPrompt),
						
						jLinks = $(document.createElement('p'))
						.attr('id', 'mmg-browser-check-links'),
						
						$(document.createElement('p'))
						.append
						(
							$(document.createElement('input'))
							.attr('id', 'mmg-browser-check-close')
							.attr('type', 'button')
							.attr('value', oPrefs.sClose)
							.bind('click.mmg_browser_check', function () { oBrowserCheck.Close(true); })
						)
					)
				)
			)
		);
		
		var aBrowsers = new Array // standards sorted
		(
			{'sn': 'Safari' , 'ln': 'Safari'},
			{'sn': 'Chrome' , 'ln': 'Chrome'},
			{'sn': 'Firefox', 'ln': 'Firefox'},
			{'sn': 'Opera'  , 'ln': 'Opera'},
			{'sn': 'Msie'   , 'ln': 'Internet Explorer'}
		);
		
		switch (oPrefs.sOrder)
		{
			case 'random'   : aBrowsers.sort(this.SortRandom); break;
			case 'alpha'    : aBrowsers.sort(this.SortAlpha); break;
			case 'standards': default: break;
		}
		
		for (var iIndex in aBrowsers)
		{
			var sShort = aBrowsers[iIndex].sn;
			var sLong = aBrowsers[iIndex].ln;
			var sUrl = oPrefs['sUrl' + sShort];
			
			jLinks.append
			(
				$(document.createElement('a'))
				.attr('href', sUrl)
				.attr('target', oPrefs.bSpawn ? '_blank' : '')
				.addClass(sShort)
				.html(sLong)
			);
		}
		
		iDays = (iDays = parseInt(oPrefs.iPersist)) ? iDays : 365; // make not zero
		
		this.Cookie('mmg_browser_check_prompted', true, iDays);
	},
	
	'Parse': function (sVer, iGroups, iPlaces)
	{
		// to clarify: 00.00.00 = 3 groups, 2 places each
		
		var sVer = sVer || '0';
		var iGroups = iGroups || 1;
		var iPlaces = iPlaces || 1;
		var aVer = sVer.split('.');
		while (aVer.length < iGroups) { aVer[aVer.length] = '0'; } // add trailing groups
		
		for (iIndex = 0; iIndex < aVer.length; iIndex++) // per group
		{
			while (aVer[iIndex].length < iPlaces) { aVer[iIndex] = '0' + aVer[iIndex]; } // add leading zeros
		}

		var sVer = aVer.join('');
		for (iIndex = 0; iIndex < sVer.length; iIndex++) // trim leading zeros
		{
			if (sVer.charAt(iIndex) != '0') { return parseInt(sVer.slice(iIndex)); } // result
		}
		
		return 0;
	},
	
	'SortRandom': function () { return Math.round(Math.random()) - .5; },
	
	'SortAlpha': function (oA, oB) { return (oA.ln[0] < oB.ln[0]) ? -1 : 1; },
	
	'Cookie': function (sName, mVal, iDays)
	{
		var iDays = iDays || -1;
		var dExp = new Date();
		
		dExp.setTime(1000 * 60 * 60 * 24 * iDays + dExp.getTime());
		
		var aCookie = new Array
		(
			sName + '=' + mVal.toString(),
			'expires=' + dExp.toUTCString(),
			'path=/'
		);
		
		document.cookie = aCookie.join(';');
	},
	
	'Close': function (bDie)
	{
		$('#mmg-browser-check-tint *').unbind('.mmg_browser_check');
		$('#mmg-browser-check-tint').remove();
		
		if (bDie) { this.Die(); }
	},
	
	'Die': function () { oBrowserCheck = null; }
};



$(function () { oBrowserCheck.Init(); }); // dom ready


