

/* {{{ ipopup code  */

if (typeof Function.bind == 'undefined')
{
    Function.prototype.bind = function (object)
    {
        var method = this;
        return function() { return method.apply(object, arguments) }
    }
}

ipopup = function ()
{
    this.ipopup = document.createElement('div');
    this.ipopup.className = 'ipopup';

    this.ipopup_iframe = document.createElement('iframe');
    this.ipopup.appendChild(this.ipopup_iframe);

    this.ipopup_main = document.createElement('div');
    this.ipopup_main.className = 'ipopup_main';
    this.ipopup_main.title = 'Ùåëêíèòå, ÷òîáû çàêðûòü';
    this.ipopup_main.onclick = function ()
    {
        this.ipopup.style.display = 'none';
    }.bind(this);
    this.ipopup.appendChild(this.ipopup_main);

    this.ipopup_main_title = document.createElement('div');
    this.ipopup_main_title.className = 'ipopup_main_title';
    this.ipopup_main.appendChild(this.ipopup_main_title);

    this.ipopup_main_title_div = document.createElement('div');
    this.ipopup_main_title.appendChild(this.ipopup_main_title_div);

    this.ipopup_main_title_div_text = null;

    this.ipopup_main_image = document.createElement('div');
    this.ipopup_main_image.className = 'ipopup_main_image';
    this.ipopup_main.appendChild(this.ipopup_main_image);

    this.ipopup_main_image_img = null;

    this.ipopup_main_progress = document.createElement('div');
    this.ipopup_main_progress.className = 'ipopup_main_progress';
    this.ipopup_main.appendChild(this.ipopup_main_progress);

    this.esc_catcher = function (e)
    {
        var key_code = window.event ? window.event.keyCode : e.keyCode;
        if (key_code != 27) {
            return true;
        }

        this.ipopup.style.display = 'none';

        if (document.removeEventListener) {
            document.removeEventListener('keypress', this.esc_catcher, false);
        } else if (document.detachEvent) {
            document.detachEvent('onkeypress', this.esc_catcher);
        }

        return false;
    }.bind(this);

    document.body.appendChild(this.ipopup);
}

ipopup.prototype.show =
function (src, width, height, top, left, title)
{
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.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;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);

var viewport_height = (self.innerHeight) ? self.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : (document.body) ? document.body.clientHeight : 0;
var windowWidth = document.documentElement.clientWidth;
var a=windowWidth/2-width/2;
var b=lightboxTop;

if(viewport_height-150<height){
	var a=windowWidth/2-width/2;
	var b=lightboxTop;
}
else{
	var a=windowWidth/2-width/2;
	var b=lightboxTop+(height+50)/8;
}
	


    this.ipopup.style.top = b + 'px';
    this.ipopup.style.left = a + 'px';

    if (this.ipopup_main_title_div_text != null) {
        this.ipopup_main_title_div.removeChild(this.ipopup_main_title_div_text);
        this.ipopup_main_title_div_text = null;
    }

    this.ipopup_main_title.style.width = width + 'px';
    this.ipopup_main_title_div_text = document.createTextNode(title);
    this.ipopup_main_title_div.appendChild(this.ipopup_main_title_div_text);

    if (this.ipopup_main_image_img != null) {
        this.ipopup_main_image.removeChild(this.ipopup_main_image_img);
        this.ipopup_main_image_img = null;
    }

    this.ipopup_main_progress.style.display = 'block';
    this.ipopup_main_progress.style.width = width + 'px';
    this.ipopup_main_progress.style.height = height + 'px';

    this.ipopup_main_image_img = document.createElement('img');
    this.ipopup_main_image_img.onload = function ()
    {
        this.ipopup_main_progress.style.display = 'none';
    }.bind(this);
    this.ipopup_main_image_img.src = src;
    this.ipopup_main_image_img.style.width = width + 'px';
    this.ipopup_main_image_img.style.height = height + 'px';
    this.ipopup_main_image.appendChild(this.ipopup_main_image_img);

    this.ipopup.style.display = 'block';

    if (document.addEventListener) {
        document.addEventListener('keypress', this.esc_catcher, false);
    } else if (document.attachEvent) {
        document.attachEvent('onkeypress', this.esc_catcher);
    }

    this.ipopup_iframe.style.width = this.ipopup_main.offsetWidth + 'px';
    this.ipopup_iframe.style.height = this.ipopup_main.offsetHeight + 'px';

    return false;
}

/* }}} */
