function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {el.style.display = 'block';
	document.getElementById('body').style.overflow="hidden";}
	else {el.style.display = 'none';
	document.getElementById('body').style.overflow="visible";}
}

function recalculeaza(popUpDivVar){
    if (typeof window.innerWidth != 'undefined') {
		var viewportheight = window.innerHeight;
	} else {
		var viewportheight = document.documentElement.clientHeight;
	}
    
    if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById('popUpDiv');
	//var popUpDiv = document.getElementById(popUpDivVar);
	var width = popUpDiv.width.slice(0,-2);
	//alert(width);
	var height = popUpDiv.height.slice(0,-2);
	//alert(popUpDiv)
	var scroll = getScrollXY();
	
	window_width=window_width/2-width/2 + scroll[0];//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
    //alert(viewportheight);	
    //alert(viewportwidth);
    
//    var blanket = document.getElementById('blanket');

//    blanket.style.height = '100%';
//    blanket.style.width = '100%';
    
    
    //alert(height);
    var popUpDiv_height = (viewportheight/2)-height/2 + scroll[1];
    //alert(popUpDiv_height);
    popUpDiv.style.top = popUpDiv_height + 'px';
//    blanket.style.top = scroll[1] + 'px';
}
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
function blanket_size(popUpDivVar, width, height){
    if (typeof window.innerWidth != 'undefined') {
		var viewportheight = window.innerHeight;
	} else {
		var viewportheight = document.documentElement.clientHeight;
	}
    
    if (typeof window.innerWidth != 'undefined') {
		var viewportwidth = window.innerWidth;
	} else {
		var viewportwidth = document.documentElement.clientWidth;
	}
    //alert(viewportheight);	
    //alert(viewportwidth);
    
    var blanket = document.getElementById('blanket');

    blanket.style.height = '100%';
    blanket.style.width = '100%';
    
    var scroll = getScrollXY();
    var popUpDiv = document.getElementById(popUpDivVar);
    //alert(height);
    var popUpDiv_height = (viewportheight/2)-height/2 + scroll[1];
    //alert(popUpDiv_height);
    popUpDiv.style.top = popUpDiv_height + 'px';
    popUpDiv.height = height + 'px';
//    alert(popUpDiv.height);
    blanket.style.top = scroll[1] + 'px';
    	
}
function window_pos(popUpDivVar, width, height) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	
	var scroll = getScrollXY();
	var blanket = document.getElementById('blanket');	
	
	window_width=window_width/2-width/2 + scroll[0];//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
	popUpDiv.width = width + 'px';
	blanket.style.left = scroll[0] + 'px';
}
function popup(windowname, width, height, url) {

    var el = document.getElementById(windowname);
    if (el.style.display != "block"){
	
	blanket_size(windowname, width, height);
	window_pos(windowname, width, height);
	document.getElementById('body').onresize =function(){recalculeaza('popUpDiv')};
	document.getElementById('body').setAttribute('onresize', 'recalculeaza("popUpDiv");');
	toggle('blanket');
	toggle(windowname);
	addHTML(url, width, height);	
    }
    else{
	toggle('blanket', url);
	toggle(windowname, url);
	removeHTML();
    }
	
	
	var blanket = document.getElementById('blanket');

	blanket.style.height = '100%';
	blanket.style.width = '100%';
	
	//alert(window.innerHeight);
	//alert(document.documentElement.clientHeight);
}


function removeHTML(){
	var body = document.getElementById('body');
	var blanket = document.getElementById('blanket');
	var popupdiv = document.getElementById('popUpDiv');
	var buton = document.getElementById('buton');
	var butonDiv = document.getElementById('butonDiv')
	var iframe = document.getElementById('iframe');
	
	//alert("Am intrat in remove");
	butonDiv.removeChild(buton);
	popupdiv.removeChild(butonDiv);
	popupdiv.removeChild(iframe);

	//body.removeChild(popupdiv);
	//body.removeChild(blanket);
}

function addHTML(url, width, height){
	//alert(out);
	var iframe = document.createElement('iframe');
	iframe.id = 'iframe';
	iframe.name = 'iframe';
	iframe.width = '0';
	iframe.frameBorder = '0';
	iframe.height = '0';
	iframe.style.visibility = 'visible';
	//iframe.onload = function(){return extract(width, height);}
	
	var butonDiv = document.createElement('div');
	butonDiv.id = 'butonDiv';
	
	var buton = document.createElement('button');
	//buton.type = 'button';
	buton.id = 'buton';
	buton.innerHTML = 'Inchide';
	buton.onclick = function(){popup('popUpDiv', 0, 0, url);}
	
	
	
	var selectPop = document.getElementById('popUpDiv');
	
	selectPop.appendChild(iframe);
	
	butonDiv.appendChild(buton);
	selectPop.appendChild(butonDiv);
	
		
	iframe.setAttribute('src', url);
	//iframe.onload = function(){extract(width, height, out);}
	//var onload = 'extract(\'' + width + '\',\''+ height + '\',\'' + out + '\');';
	//alert(onload);
	//iframe.onload = function(){eval(onload);}
	//iframe.setAttribute('onload', 'extract(\''+ width +'\',\''+ height +'\',\''+ out +'\')');
}


function draw(width, height, url) {
	//alert(out);
	if (!document.getElementById('popUpDiv')){
	    var body = document.getElementById('body');
	
	    var blanket = document.createElement('div');
	    blanket.id = 'blanket';
	    blanket.style.display = 'none';
	
	    var popupdiv = document.createElement('div');
	    popupdiv.id = 'popUpDiv';
	    popupdiv.style.display = 'none';
	    //popupdiv.style.align = 'central';
	    
	    body.appendChild(blanket);
	    body.appendChild(popupdiv);
	    //alert('Desenez...');
	}

	document.getElementById('popUpDiv').style.width= width + 'px'; 
	document.getElementById('popUpDiv').style.height= (height + 30) + 'px';
	 
	popup('popUpDiv', width, height, url);
	
	document.getElementById('butonDiv').setAttribute('style', 'width:' + width + 'px;');
	document.getElementById('buton').setAttribute('style', 'float:right;');
	
	document.getElementById('iframe').style.width= width + 'px'; 
	document.getElementById('iframe').style.height= height + 'px';
	//document.getElementById('iframe').style.overflow= 'hidden';
	//var frame = document.getElementById('iframe').contentDocument;
	//alert(frame.getElementsByTagName("div")[2].innerHTML);
	//frame.getElementsByTagName("div")[2].style.width = (width - 35) + "px";
	
	
	//alert(content);
	//document.getElementById('content_popup').innerHTLM= '<font size="50" color="red">' + content + '</font>';
	//alert(document.getElementById('content_popup').innerHTLM);
	//transfer();
}
