var _ga_popup;

function popup_play(url, params){
	var obj = document.body;
	_ga_popup = new Object();
	_ga_popup.getContentDoc = function(){
		if(this.framer.contentDocument) return this.framer.contentDocument;
		if(this.framer.contentWindow)return this.framer.contentWindow.document;
		return this.framer.document;
	}
	_ga_popup.getContentWnd = function(){
		if(this.framer.contentWindow) return this.framer.contentWindow;
		return this.framer.window;
	}	
	_ga_popup.onresize = function (e){
			var psize = ga_sizer.getInfo();
			this.stopper.style.height = String(psize.pageHeight) + 'px';
			this.stopper.style.width = String(psize.bodyWidth) + 'px';
			var csize = ga_sizer.getInfo(this.getContentWnd(), this.getContentDoc());
			this.framer.style.height = String(csize.bodyHeight) + 'px';
			this.framer.style.width = String(csize.bodyWidth) + 'px';
			this.framer.style.left = String(Math.max((psize.clientWidth - csize.bodyWidth) / 2 + psize.scrollLeft, 0)) + 'px';
			this.framer.style.top = String(Math.max((psize.clientHeight - csize.bodyHeight) / 2 + psize.scrollTop, 0)) + 'px';
	}
		
	_ga_popup.close = function(){
			popup_remove_resize_listener(this.parent_body, this.onresize);
			this.stopper.style.display = 'none';
			this.framer.style.display = 'none';
			try{
				this.parent_body.removeChild(this.framer);
				this.parent_body.removeChild(this.stopper);
			} catch(e){}
	}
	
	_ga_popup.parent_body = obj;
	_ga_popup.parent_document = document;
	_ga_popup.parent_wnd = window;
	_ga_popup.params = params;
	_ga_popup.stopper =document.createElement('div');
	_ga_popup.framer = document.createElement('iframe');
	_ga_popup.stopper.className = 'popup_stopper';		
	_ga_popup.framer.className = 'popup_frame';
	_ga_popup.framer.setAttribute('src', url);
	_ga_popup.framer._ga_popup = _ga_popup;
	_ga_popup.framer.setAttribute('border', '0');
	_ga_popup.framer.setAttribute('frameborder', '0');
	_ga_popup.framer.setAttribute('scrolling', 'no');
	_ga_popup.framer.setAttribute('background', 'none');
	if(IS_IE)	
		_ga_popup.stopper.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(opacity=.5)';
	else 
		_ga_popup.stopper.style.opacity = '0.5';
	
	obj.appendChild(_ga_popup.stopper);
	obj.appendChild(_ga_popup.framer);
	popup_add_resize_listener(obj, _ga_popup.onresize);
	_ga_popup.stopper.style.display = 'block';
	_ga_popup.framer.style.display = 'block';
	_ga_popup.onresize('');
}

function popup_add_resize_listener(ob, ev){
	if(ob.addEventListener) ob.addEventListener('resize', ev, false);
	else if(ob.attachEvent) ob.attachEvent('resize', ev);
	else ob.onresize = ev;
}

function popup_remove_resize_listener(ob, ev){
	if(ob.removeEventListener) ob.removeEventListener('resize', ev, false);
	else if(ob.detachEvent) ob.detachEvent('resize', ev);
	else ob.onresize = null;
}

var ga_sizer = {
	_fres: function(n_win, n_docel, n_body){		
			var n_result = n_win ? n_win : 0;
			if (n_docel && (!n_result || (n_result > n_docel)))
					n_result = n_docel;
			return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	},
	_get_wnd: function(wind){
		if(wind) return wind;
		else return window;
	},
	_get_doc: function(doc){
		if(doc) return doc;
		else return document;
	},
	clientWidth: function(wnd, doc){
		wnd = ga_sizer._get_wnd(wnd);
		doc = ga_sizer._get_doc(doc);
		return ga_sizer._fres(
			wnd.innerWidth ? wnd.innerWidth : 0,
			doc.documentElement ? doc.documentElement.clientWidth : 0,
			doc.body ? doc.body.clientWidth : 0);
	},
	clientHeight: function(wnd, doc){
		wnd = ga_sizer._get_wnd(wnd);
		doc = ga_sizer._get_doc(doc);
		return ga_sizer._fres(
			wnd.innerHeight ? wnd.innerHeight : 0,
			doc.documentElement ? doc.documentElement.clientHeight : 0,
			doc.body ? doc.body.clientHeight : 0);
	},
	scrollLeft: function(wnd, doc){
		wnd = ga_sizer._get_wnd(wnd);
		doc = ga_sizer._get_doc(doc);
		return ga_sizer._fres(
			wnd.pageXOffset ? wnd.pageXOffset : 0,
			doc.documentElement ? doc.documentElement.scrollLeft : 0,
			doc.body ? doc.body.scrollLeft : 0);
	},
	getInfo:function(wnd, doc){
		var psize = ga_sizer.pageSize(wnd, doc);
		return {
			pageWidth: 		psize[0],
			pageHeight: 	psize[1],
			clientWidth: 	ga_sizer.clientWidth(wnd, doc),
			clientHeight: ga_sizer.clientHeight(wnd, doc),
			scrollLeft:		ga_sizer.scrollLeft(wnd, doc),
			scrollTop:		ga_sizer.scrollTop(wnd, doc),
			bodyHeight: 	ga_sizer.bodyHeight(wnd, doc),
			bodyWidth: 		ga_sizer.bodyWidth(wnd, doc)
		}
	},
	bodyHeight: function(wnd, doc){
		wnd = ga_sizer._get_wnd(wnd);
		doc = ga_sizer._get_doc(doc);
		return doc.body.scrollHeight;
	},
	bodyWidth: function(wnd, doc){
		wnd = ga_sizer._get_wnd(wnd);
		doc = ga_sizer._get_doc(doc);
		return doc.body.scrollWidth;
	},
	scrollTop: function(wnd, doc){
		wnd = ga_sizer._get_wnd(wnd);
		doc = ga_sizer._get_doc(doc);
		return ga_sizer._fres(
			wnd.pageYOffset ? wnd.pageYOffset : 0,
			doc.documentElement ? doc.documentElement.scrollTop : 0,
			doc.body ? doc.body.scrollTop : 0);
	},
	pageSize: function(wnd, doc){
		doc = ga_sizer._get_doc(doc);
		wnd = ga_sizer._get_wnd(wnd);
		var xScroll, yScroll;
		//Берем общую высоту документа 
		if (wnd.innerHeight && wnd.scrollMaxY) {
			xScroll = doc.body.scrollWidth;
			yScroll = wnd.innerHeight + wnd.scrollMaxY;
		} else if (doc.body.scrollHeight > doc.body.offsetHeight){ // all but Explorer Mac
			xScroll = doc.body.scrollWidth;
			yScroll = doc.body.scrollHeight;
		} else if (doc.documentElement && doc.documentElement.scrollHeight > doc.documentElement.offsetHeight){ // Explorer 6 strict mode
			xScroll = doc.documentElement.scrollWidth;
			yScroll = doc.documentElement.scrollHeight;
		} else { // Explorer Mac...would also work in Mozilla and Safari
			xScroll = doc.body.offsetWidth;
			yScroll = doc.body.offsetHeight;
		}
	
		var windowWidth, windowHeight;
		if (self.innerHeight) { // all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (doc.documentElement && doc.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = doc.documentElement.clientWidth;
			windowHeight = doc.documentElement.clientHeight;
		} else if (doc.body) { // other Explorers
			windowWidth = doc.body.clientWidth;
			windowHeight = doc.body.clientHeight;
		}
  
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight)
			pageHeight = windowHeight;
		else 
			pageHeight = yScroll;
  
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth)	
			pageWidth = windowWidth;
		else
			pageWidth = xScroll;

		return [pageWidth, pageHeight, windowWidth, windowHeight];
	}
}

var USER_DATA = {

    Browser: {
        KHTML: /Konqueror|KHTML/.test(navigator.userAgent) &&
                !/Apple/.test(navigator.userAgent),
        Safari: /KHTML/.test(navigator.userAgent) &&
                /Apple/.test(navigator.userAgent),
        Opera: !!window.opera,
        MSIE: !!(window.attachEvent && !window.opera),
        Gecko: /Gecko/.test(navigator.userAgent) &&
                !/Konqueror|KHTML/.test(navigator.userAgent)
    },

    OS: {
        Windows: navigator.platform.indexOf("Win") > -1,
        Mac: navigator.platform.indexOf("Mac") > -1,
        Linux: navigator.platform.indexOf("Linux") > -1
    }
}

var IS_IE = USER_DATA['Browser'].MSIE;
