// - - - - - - - - - - - - - - - - - - GRID - - - - - - - - - - - - - - - - - - - - - - 
var isIE = (navigator.appName.indexOf("Microsoft") != -1);

// - - - - - - - - - - - - - - - - - - Open POPUP Window - - - - - - - - - - - - - - - - - - - - 

function openPopUpWindow(url, w, h, attr)
{
	if (attr = null)
		attr = 'resizable=1,directories=0,toolbar=1,scrollbars=0,menubar=1,top=0,left=0';

	var sSizeAndPosition;
	sSizeAndPosition = 'width=' + w + ', height=' + h + ', top=' + (screen.height - parseInt(h)) / 2 + ', left=' + (screen.width - parseInt(w)) / 2;
	// try to close old window
	try { window.PopUpWindow.close(); } catch (e) { }

	window.PopUpWindow = window.open(url, 'edit', sSizeAndPosition + ', ' + attr);
}

function popupFindBGColor(elem)
{
	var color, img, x = 0, y = 0;
	var emptyTest = /(rgba\([,0\s]+\))|(transparent)/i;
	elem = jQuery(elem).parent();
	elem.parents()
		.each(function ()
		{
			var e = jQuery(this);
			color = e.css('background-color');
//			img = e.css('background-image');

//			if (img != 'none') {
//				var m = img.match(/url\(['"]?([^'"\)]+)['"]?\)/i);
//				if (m) {
//					img = m[1]; // get url
//					var pos = e.css('background-position');
//					if (typeof pos != 'undefined') {
//						m = pos.match(/(\d+)px (\d+)px/i);
//						if (m) {
//							x = m[1]; y = m[2];
//						}
//					}
//					var elemPos = elem.offset();
//					var bgPos = e.offset();
//					x = elemPos.left - bgPos.left - x;
//					y = elemPos.top - bgPos.top - y;
//				}
//				else
//					img = 'none'; // continue iteration
//			}
			if (!emptyTest.test(color)) //|| img != 'none')
				return false;
		});

	if (!emptyTest.test(color))
		return 'bgcolor=' + (encodeURIComponent || encodeURI)(color);
	else
		return 'bgimg=' + (encodeURIComponent || encodeURI)(img) + '&bgpos=' + ([x, y, elem.width(), elem.height()].join(','));
}

// - - - - - - - - - - - - - - - - - - PAGE UNLOAD handler - - - - - - - - - - - - - - - - - - - - 
function Page_Unload()
{
	try {
		if (window.PopUpWindow != null)
			window.PopUpWindow.close();
	} catch (e) { }
}

window.onbeforeunload = Page_Unload;
