function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {elm.addEventListener(evType, fn, useCapture);return true;}
     else if (elm.attachEvent) {var r = elm.attachEvent('on' + evType, fn);return r;}
     else {elm['on' + evType] = fn;}
}
function removeEvent( elm, evType, fn ) {
	if (elm.detachEvent) {return elm.detachEvent('on' + evType, fn);}
	  else {elm.removeEventListener(evType, fn, false);return true;}
}
var popup = function (url,pr){
	var _self = this,shadow,windw,cont;
	var pr = pr||{};
	if(!window.PopUps) window.PopUps = [];

	this.open = function (url){
		_self.shadowShow();
		_self.createPopup();
		_self.redraw();

		if(cont){
			var h = $H(pr);
			new Ajax.Request(url||location.pathname, {
			  method: 'post',
			  parameters: h.update({ popup: 1, r: Math.random() }).toQueryString(),
			  onSuccess: function(transport) {
			    cont.update(transport.responseText);
				_self.redraw();
			  },
			  onComplete : function(transport) {
			  }
			});
		}
        if(window.PopUps.length){
			$$('select').each(function(s){s.addClassName('hiden');});
		}
	}
	this.shadowCreate = function(){
		shadow=new Element('DIV').addClassName('shadow').setStyle({zIndex:++window.maxZ});
		$(document.body).insert(shadow);
		if(pr || !pr.shClose){
			shadow.observe('click',function(){_self.closePopup();})
		}
	}
	this.shadowShow = function(){
		if(!shadow) _self.shadowCreate();
		shadow.show();
	}
    this.shadowHide = function(){
		if(!shadow) return;
		shadow.hide();
		shadow.remove();
	}

	this.closePopup = function(){
		windw.remove();windw=null;
		shadow.remove();shadow=null;
		cont.remove();cont=null;
		window.maxZ--;
		window.PopUps[window.PopUps.length-1] = null;
        window.PopUps=window.PopUps.compact();
		if(!window.PopUps.length){
			$$('select.hiden').each(function(s){s.removeClassName('hiden');});
		}
	}
	this.createPopup = function(){
		var uniq = 'U'+Math.random();
		windw = new Element('DIV').addClassName('popup2')
			.setStyle({zIndex:parseInt(shadow.getStyle('zIndex'))+1})
			.insert(new Element('DIV').addClassName('card_ug1').insert(new Element('DIV').update('&nbsp;')))
            .insert(new Element('DIV').addClassName('card').insert(new Element('IMG',{'src':'/i/popup_loading.gif'})))
            .insert(new Element('DIV').addClassName('card_ug2').insert(new Element('DIV').update('&nbsp;')))
			.writeAttribute('uid',uniq);
			window.PopUps[window.PopUps.length] = {'uid' : uniq, 'obj': _self};
		$(document.body).insert(windw);
		if(pr && !pr.noClose){
			$(windw).down('.card_ug1').insert(
				new Element('DIV').addClassName('close')
					.update('<img src="/i/close.gif" />')
					.observe('click',function(){_self.closePopup();})
			);
		}
		if(pr && !pr.noPrint){
			$(windw).down('.card_ug1').insert(
				new Element('DIV').addClassName('print2')
					.update('<img src="/i/print2.gif" />')
					.observe('click',function(){_self.printPopup();})
			);
		}
		cont = $(windw).down('.card');
	}
	this.printPopup = function(){
		var w = window.open('about:blank',null,'width=350,height=300');
		var h = $H(pr);
		new Ajax.Request(url||location.pathname, {
		  method: 'post',
		  parameters: h.update({ popup: 1, r: Math.random(),print:1 }).toQueryString(),
		  onSuccess: function(transport) {
			w.document.open();
			w.document.write(transport.responseText);
			w.document.close();
			//w.document.onload = function(){
				if(w){w.print();w.close();}
			//}

		  },
		  onComplete : function(transport) {
		  }
		});

		return false;
//		var r=Math.random();x = new Element('IFRAME',{id: 'print'+r, name: 'print'+r});
//		$(document.body).insert(x);
//		var frameDoc = x.contentWindow.document || x.contentDocument;
//		frameDoc.open();
//		frameDoc.write('<head><LINK href="/css/print.css" type=text/css rel=stylesheet></head><body>'+cont.innerHTML+'</body>');
//		frameDoc.close();
//		window.frames['print'+r].window.print();
	}
    this.redraw = function(){
		if(shadow){shadow.setStyle({
			width:document.body.scrollWidth+'px',
			height:((document.documentElement)?document.documentElement:document.body).scrollHeight+'px'
		});}
		if(windw){var i=document.viewport.getScrollOffsets();windw.setStyle({
			left:((document.body.scrollWidth-windw.getWidth())/2)+'px',
			top: ((i.top+windw.getHeight())<window.bodyHeight)? i.top+'px': (window.bodyHeight-windw.getHeight())+'px'
		});}
		if(cont){
			var w = cont.getWidth();
			windw.down('div.card_ug1').setStyle({width:w+'px'}).next('div.card_ug2').setStyle({width:w+'px'});
		}
	}
	addEvent(window, 'resize', this.redraw);
	this.open(url);
}
function closePopup(el){
	if(!window.PopUps) return false;
	var uid = el.up('div.popup2').readAttribute('uid');
	if(!uid) return false;
	for(var i=0;i<window.PopUps.length;i++){if(window.PopUps[i].uid==uid){window.PopUps[i].obj.closePopup();}}
}
addEvent(window, 'load',function(){
	var i=0;
	$(document.body).descendants().each(function(s){
		if(s.nodeType!=8){if(i < s.getStyle('zIndex') ){
			i = s.getStyle('zIndex');
		}}
	});
	window.maxZ = parseInt(i);
	window.bodyHeight = document.documentElement.scrollHeight;
	var x;if(null!=(x=document.location.hash.match(/id=(\d{5,})/))){
		new popup(location.pathname,{type:'card',id:x[1]});
	}
});

