var lightId = '';

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;
}

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 showMyLightbox(id, x, y) {
    //if (lightId == id) return;
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById(id);

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	objLightbox.style.display = 'block';

	var ol = $('#' + id);
	
	var lightboxLeft = arrayPageSize[2] / 2 - ol.width() / 2;
	var lightboxTop = arrayPageScroll[1] + arrayPageSize[3] / 2 - ol.height() / 2;
	if (x) lightboxLeft = x;
	if (y) lightboxTop = arrayPageScroll[1]+y;
	
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

	
	lightId = id;
}

function hideMyLightbox() {
	if(lightId == '') return;
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById(lightId);

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	lightId = '';
}

var intervalID;
function OpenLightbox(id, id2, x, y, width, height, responseText, issecond) {
    var curWidth = 10;
    var curHeight = 10;
    var obj = document.getElementById(id);
    var obj2 = document.getElementById(id2);
    obj.style.display = 'block';
    var x1 = x + width / 2;
    var y1 = y + height / 2;
    obj.style.left = x1 + "px";
    obj.style.top = y1 + "px";
    obj2.style.width = curWidth + "px";
    obj2.style.height = curHeight + "px";
    var dx = 15;
    var dy = 15;
    intervalID = setInterval(function() {
        curWidth += dx;
        x1 -= dx / 2;
        if (curWidth >= width) {
            curWidth = width;
            x1 += dx / 2;
        }
        curHeight += dy;
        y1 -= dy / 2;
        if (curHeight >= height) {
            curHeight = height;
            y1 += dy / 2;
        }
        if (curWidth >= width && curHeight >= height) {
            clearInterval(intervalID);
            if (responseText && issecond) SetOuterHTML(id2, responseText);
            if (responseText && !issecond) SetOuterHTML(id, responseText);
            obj = document.getElementById(id);
            obj.style.display = 'block';
            obj.style.left = x + "px";
            obj.style.top = y + "px";
        }
        obj2.style.width = curWidth + "px";
        obj2.style.height = curHeight + "px";
        obj.style.left = x1 + "px";
        obj.style.top = y1 + "px";
    }, 3);
}

var moveelement = null;
var lastx = 0;
var lasty = 0;
function move(event) {
    if (!moveelement) return;
    if (lastx == 0) lastx = event.screenX;
    if (lasty == 0) lasty = event.screenY;
    var target = document.getElementById(moveelement);
    var pageScroll = getPageScroll();
    var x = target.Left();
    var y = target.Top();
    var dx = lastx - event.screenX;
    var dy = lasty - event.screenY;
    if (x - dx >= 0 && y - dy >= 0) {
        target.style.left = x - dx + "px";
        target.style.top = y - dy + "px";
    }
    lastx = event.screenX;
    lasty = event.screenY;
}

function StartDrag(id) {
    moveelement = id;
    lastx = 0;
    lasty = 0;
}
