/**
 * @author Oleg Korovin (http://olegrorovin.spb.ru/)
 * @include "./_Lib.js"
 */


var Popups = {

 /******************************************************************************/
	place			: function(elThis,elPopup,iXd,iYd,bThisR,bThisB,bPopupR,bPopupB){
		var oThis = this;
		elThis = $(elThis);
		elPopup = $(elPopup);

		winPopup.close();

		winPopup.show(
			null,
			_layerShow,
			_layerHide
		);

		function _layerShow(){

			iXd = iXd * 1 || 0;
			iYd = iYd * 1 || 0;

			bThisR = bThisR * 1 || 0;
			bThisB = bThisB * 1 || 0;

			bPopupR = bPopupR * 1 || 0;
			bPopupB = bPopupB * 1 || 0;

			var oThis = Coordinates.element(elThis);
			oThis.w = elThis.offsetWidth;
			oThis.h = elThis.offsetHeight;
			//alert(oThis.x + ',' + oThis.y + ',' + oThis.w + ',' + oThis.h);

			elPopup.style.left = '0';
			elPopup.style.top = '0';
			Styles.show(elPopup);
			Styles.invis(elPopup);

			var oPopup = {
				w	: elPopup.offsetWidth,
				h	: elPopup.offsetHeight
			};

			oPopup.x = oThis.x;
			oPopup.y = oThis.y;

			var bInvH = 0;
			var bInvV = 0;

			if(bInvH){
				bThisR = !bThisR;
				bPopupR = !bPopupR;
			}
			oPopup.x = oThis.x + (bInvH?-1:1)*iXd + bThisR * oThis.w - bPopupR * oPopup.w;





			function _calcY(){
				if(bInvV){
					bThisB = !bThisB;
					bPopupB = !bPopupB;
				}
				oPopup.y = oThis.y + (bInvV?-1:1)*iYd + bThisB * oThis.h - bPopupB * oPopup.h;
			}
			//oPopup.x -= iXd;
			//oPopup.y += iYd;

			_calcY();

			if( (oPopup.y + oPopup.h) > (scrollTop() + document.documentElement.clientHeight) ){
				bInvV = 1;
				_calcY();
			}

			//oPopup.x += oThis.w;
			//oPopup.x -= oPopup.w;

			//oPopup.y += oThis.h;
			//oPopup.y -= oPopup.h;



			elPopup.style.left = oPopup.x + 'px';
			elPopup.style.top = oPopup.y + 'px';

			Styles.vis(elPopup);
			//alert(elPopup.style.left + ',' + elPopup.style.top);

			var mForms = $$('form',elPopup);
			if( mForms[0] )
				Styles.setClass(mForms[0].parentNode,'PopupContentEnd',0);
		};
		function _layerHide(){
			Styles.hide(elPopup);
			//Styles.invis(elPopup);
		};
	},

 /******************************************************************************/
	show			: function(sLayer){
		var oThis = this;
		sLayer = $(sLayer);

		winPopup.show(
			null,
			_layerShow,
			_layerHide
		);

		function _layerShow(){
			sLayer.style.display = '';
		};
		function _layerHide(){
			sLayer.style.display = 'none';
		};
	},

 /******************************************************************************/
	hide			: function(e){
		var oThis = this;
		winPopup.close(e);
	},


 /***************************************/
	send			: function(elForm){
		var oThis = this;

		Styles.removeClass(elForm , 'CheckError');

		if( !checkForm(elForm) ){
			Styles.addClass(elForm , 'CheckError');
		 	return false;
		}

		var
			mInp = elForm.elements,
			oValues = {};

		for(var i=0,l=mInp.length;i<l;i++){
			if(
				mInp[i].type != 'checkbox' ||
				mInp[i].checked
			){
				oValues[mInp[i].name] = mInp[i].value;
			}
		}

		sendRequest( getUrl(elForm.action) , oValues, 25, function(res,text){
			if(res && res.complete){
				Styles.setClass(elForm.parentNode,'PopupContentEnd',1);
				elForm.reset();
			}
			else{
//				winPopup.close();
			}
		});
	}
}
