/**
 * class for popup login/singup windows
 * @uses jqModal
 * @param String id
 * @param String loginPopupPage
 * @param String regPageSpecified
 * @param String regPopupPage
 * @param String regPage
 * @param undefined itSelf gap for late connecting
 */
var PopupLogin = function(id, loginPopupPage, regPageSpecified, regPopupPage, regPage, itSelf)
{
	// hack to save context
	itSelf = this;
	/**
	 * shows login popup
	 */
	this.show = function(ajaxParams)
	{
		itSelf.ajaxParams = ajaxParams;
		if ('' != loginPopupPage)
		{
			$0('#popupLoginDialog').jqm({
				ajax: loginPopupPage,
				'ajaxParams': itSelf.ajaxParams,
				onLoad: itSelf.popupLoaded,
				onHide: itSelf.cleanIt,
				target: $0('#popupLoginContainer'),
				onShow: null
			}).jqDrag('.jqDrag').jqm().jqmShow();
		} else if ('' != regPageSpecified)
		{
			location.href = regPageSpecified;
		} else {
			alert('Sorry, can\'t find registration page!');
		}
	}
	/**
	 * jqModal callback function
	 * @param Object hash
	 */
	this.showReg = function(ajaxParams)
	{
		itSelf.ajaxParams = ajaxParams;
		if ('' != regPopupPage)
		{
			$0('#popupLoginContainer').empty();
			$0('#popupLoginDialog').width(460);
			$0('#popupLoginContainer').load(regPopupPage, itSelf.ajaxParams, function()
				{
					$0('#popupLoginDialog').jqDrag('.jqDrag').jqmAddClose('.jqmClose');
					itSelf.setPopupDimsAndPosition($0('#popupLoginDialog'));
					$0("img[@src$=png], input[@src$=png], .png, .transparent", $0('#popupLoginDialog')).pngfix({"imageFixSrc":"/i/1x1.gif"});
				    $0("label.overlabel").overlabel();
					$0('.customSelect', $0('#popupLoginDialog')).selectbox({'inputClass':'custSelectbox', 'containerClass':'custSelectbox-wrapper'});
				});

		}
		else if ('' != regPage)
		{
			location.href=regPage;
		}
		else
		{
			alert('Sorry, can\'t find registration page!');
		}
		return false;
	}

	/**
	 * jqModal callback function
	 * @param Object hash
	 */
	this.showRegDirect = function(ajaxParams)
	{
		itSelf.ajaxParams = ajaxParams;
		if ('' != regPopupPage)
		{
			$0('#popupLoginDialog').width(460);
			$0('#popupLoginDialog').jqm({
				ajax: regPopupPage,
				'ajaxParams': itSelf.ajaxParams,
				onLoad: itSelf.popupLoaded,
				onHide: itSelf.cleanIt,
				target: $0('#popupLoginContainer'),
				onShow: null
			}).jqDrag('.jqDrag').jqm().jqmShow();
		} else if ('' != regPage)
		{
			location.href=regPage;
		} else {
			alert('Sorry, can\'t find registration page!');
		}
		return false;
	}

	/**
	 * jqModal callback function
	 * @param Object hash
	 */
	this.popupLoaded = function(hash)
	{
		var obj = $0('#'+hash.w.attr('id'));
		itSelf.setPopupDimsAndPosition(obj);

		$0('#popupLoginDialog').jqDrag('.jqDrag');
		$0("label.overlabel", $0('#popupLoginDialog')).overlabel();

		if (typeof(itSelf.ajaxParams) == 'undefined')
		{
			itSelf.ajaxParams = null;
		}
		$0('#switchToReg').bind('click', function(){ formArray = new Array(); itSelf.showReg(itSelf.ajaxParams)});
		$0("img[@src$=png], input[@src$=png], .transparent, .png", $0('#popupLoginDialog')).pngfix({"imageFixSrc":"/i/1x1.gif"});
		$0('.customSelect', $0('#popupLoginDialog')).selectbox({'inputClass':'custSelectbox', 'containerClass':'custSelectbox-wrapper'});

	}
	/**
	 * jqModal callback function
	 * @param Object hash
	 */
	this.cleanIt = function(hash)
	{
		hash.w.hide();
		hash.o.remove();
		$0('#popupLoginDialog').width('312px');
		$0('#popupLoginContainer').empty();
		if (typeof(formArray) == 'object') formArray = new Array();
	}

	this.setPopupDimsAndPosition = function (obj)
	{
		setWidth({w: obj});//defined in /i/ssm/js/_for_popupscript.js
		setHeight({w: obj});//defined in /i/ssm/js/_for_popupscript.js
		center({w: obj});
	}
}

/**
 * saves URL to return
 * @param String backUrl URL to return after login/reginctation (optional)
 */
saveBackUrl = function(backUrl)
{
	$0.ajax({
		url: '/',
		dataType: 'json',
		data: {
			cmd: 'SSM_Members_Command_SaveBackUrl',
			url: backUrl,
			forced: true
		}
	});
}

/**
 * jqModal callback function
 * @param Object hash
 */
var yacenter = function (hash)
{
	var x, y;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		hash.w.css('position', 'fixed');
		x = window.document.documentElement.clientWidth/2;
		y = window.document.documentElement.clientHeight/2;
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
		&& typeof document.documentElement.clientWidth !=
		'undefined' && document.documentElement.clientWidth != 0)
	{
		hash.w.css('position', 'absolute');
		x = document.documentElement.clientWidth/2 + (window.document.documentElement.scrollLeft);
		y = document.documentElement.clientHeight/2 + (window.document.documentElement.scrollTop);
	}
	// older versions of IE
	else {
		hash.w.css('position', 'absolute');
		x = document.getElementsByTagName('body')[0].clientWidth/2 + document.getElementsByTagName('body')[0].scrollLeft;
		y = document.getElementsByTagName('body')[0].clientHeight/2 + document.getElementsByTagName('body')[0].scrollTop;
	}
	hash.w.css('top', (y - hash.w.height()/2) + 'px');
	hash.w.css('left', (x - hash.w.width()/2) + 'px');
	hash.w.show();
}
