// Код позаимствован из Интернета. Основная функция модуля - showPopup, показать всплывающее окно. 

var popup_defaults = [
	["width", 670],
	["height", 650],
	["location", "no"],
	["menubar", "no"],
	["toolbar", "no"],
	["resizable", "yes"],
	["scrollbars", "yes"],
	["status", "yes"]
]
var popup = new Array();
function pairs_string_get_value( sText, sName, sFrom, sBefore ){
	var sValue = "";
	if( sText ){
		if( !sFrom ) sFrom = "=";
		if( !sBefore ) sBefore = ";";
		sText = sText.replace( new RegExp( "(" + sBefore + ")\\s+", "g" ), "$1" );
		var iStart = sText.indexOf( sBefore + sName + sFrom );
		if( iStart >= 0 ){
			iStart += ( sBefore.length + sName.length + sFrom.length );
		}else{
			iStart = sText.indexOf( sName + sFrom );
			if( iStart == 0 ){
				iStart += ( sName.length + sFrom.length );
			}else{
				iStart = -1;
			}
		}
		if( iStart >= 0 ){
			var iEnd = sText.indexOf( sBefore, iStart );
			if( iEnd < 0 ){
				iEnd = sText.length;
			}
			sValue = sText.substring( iStart, iEnd );
		}
	}
	return sValue;
}
function pairs_string_set_value( sText, sName, sValue, sFrom, sBefore ){
	if( !sFrom ) sFrom = "=";
	if( !sBefore ) sBefore = ";";
	var iStart = -1;
	if( sText ){
		sText = sText.replace( new RegExp( "\\s+(" + sBefore + "|" + sFrom + ")\\s+", "g" ), "$1" );
		iStart = sText.indexOf( sBefore + sName + sFrom );
		if( iStart >= 0 ){
			iStart += ( sBefore.length + sName.length + sFrom.length );
		}else{
			iStart = sText.indexOf( sName + sFrom );
			if( iStart == 0 ){
				iStart += ( sName.length + sFrom.length );
			}else{
				iStart = -1;
			}
		}
		if( iStart >= 0 ){
			var iEnd = sText.indexOf( sBefore, iStart );
			if( iEnd < 0 ){
				iEnd = sText.length;
			}
			sText = sText.substring( 0, iStart ) + sValue + sText.substr( iEnd );
		}
	}
	if( iStart < 0 ){
		if( sText && sText.lastIndexOf( sBefore ) != ( sText.length - sBefore.length ) ){
			sText += sBefore;
		}
		sText += sName + sFrom + sValue + sBefore;
	}
	return sText;
}
function showPopup( sURL, sName, sFeatures, bReplace ){
	var sTarget = sName;
	if( !sURL.length ){
		if( sURL && sURL.tagName.toLowerCase() == "a" ){
			if( !sName ){ sName = ( sURL.id ) ? sURL.id : "_blank"; }
			sURL = sURL.href;
		}else{
			return false;
		}
	}else if( !sName ){
		sName = "_blank";
	}
	for( var i = 0 ; i < popup_defaults.length ; i++ ){
		if( !pairs_string_get_value( sFeatures, popup_defaults[i][0], "=", "," ) ){
			sFeatures = pairs_string_set_value( sFeatures, popup_defaults[i][0], popup_defaults[i][1], "=", "," );
		}
	}

	var iWidth = pairs_string_get_value( sFeatures, "width", "=", "," );
	var iHeight = pairs_string_get_value( sFeatures, "height", "=", "," );
	if( screen ){
		var iScreen_height = screen.height ? screen.height - 150 : 0;
		var iScreen_width = screen.width ? screen.width - 100 : 0;
		var bScroll = false;
		if( iScreen_height < iHeight ){ bScroll = true; iHeight = iScreen_height; sFeatures = pairs_string_set_value( sFeatures, "height", iHeight, "=", "," ); }
		if( iScreen_width < iWidth ){ bScroll = true; iWidth = iScreen_width; sFeatures = pairs_string_set_value( sFeatures, "width", iWidth, "=", "," ); }
		if( bScroll ) {
			sFeatures = pairs_string_set_value( sFeatures, "scrollbars", "yes", "=", "," );
		}
		sFeatures = pairs_string_set_value( sFeatures, "top", Math.round( ( iScreen_height - iHeight ) / 2 ), "=", "," );
		sFeatures = pairs_string_set_value( sFeatures, "left", Math.round( ( iScreen_width - iWidth ) / 2 ), "=", "," );
	}

	if( sURL.match(/\.(gif|jpe?g|png)(\?.*)?$/i) ){
		popup[sName] = window.open( "", sName, sFeatures );
		if( popup[sName] ){
			var sTitle = unescape( pairs_string_get_value( sFeatures, "title", "=", "," ) );
			sTitle = sTitle.replace( /<\/?\w[^>]*>/g, " " ).replace( /</g, "&lt;" ).replace( /</g, "&lt;" ).replace( /"/g, "&quot;" );
			popup[sName].document.open();
			popup[sName].document.write('<html><head><title>'
				+ sTitle
				+ '</title></head><body bgcolor="white" style="margin: 0px; padding: 0px;"><table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"><tr><td align="center"><img src="'
				+ sURL + '" alt="'
				+ sTitle + '" /></td></tr></table></body></html>');
			popup[sName].document.close();
		}
	}else{
		var bWas_open = false;
		try{ bWas_open = ( popup[sName] && popup[sName].location.href != sURL ) }catch(e){}
		if( !popup[sName] || popup[sName].closed || !bWas_open ){
			if( sTarget && sTarget.match( /^_/ ) ){
				popup[sName] = window.open( sURL, sTarget );
			}else{
				popup[sName] = window.open( sURL, sName, sFeatures, bReplace );
			}
		}
	}
	if( sName != "_blank" ){
		popup[sName].focus();
	}

	return false;
}
