/***
* @copyright (c) 2007 - 2008 www.gosudar.com.ru
* @copyright (c) 2005 - 2006 Flexion.Org
*            (c) 2004 - 2005 Christian Fecteau (http://portfolio.christianfecteau.com/) 
*            (c) 2004, Markus (http://www.phpmix.com) SXBB - Select Expand BBcodes MOD v1.0.2
*            (c) 2004 - 2005 Project Minerva
*            (c) 2001 - 2006 phpBB Group
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
***/

function SXBB_IsIEMac()
{
	// Any better way to detect IEMac?
	var ua = String(navigator.userAgent).toLowerCase();
	if( document.all && ua.indexOf("mac") >= 0 )
		return true;
	return false;
}
function SXBB_IsOverflowAble()
{
	// Reliable overflow usage seems to require correct DOM support with some exceptions:
	// - Opera5/6 renders 'auto' as if it was 'visible'. Bang!
	// - IEMac seems to be buggy in some circumstances:
	//   http://sonofhans.net/mac_ie5_bug/
	//
	if( document.getElementById && document.childNodes && !SXBB_IsIEMac() )
		return true;
	return false;
}

function _SXBB(id)
{
	this.id = id;
	this.size = this.min = 45;	// --- Adjust MINIMUM_BLOCK_HEIGHT here :-)
	this.extra = 5;				// --- This is added to height when expanded
	this.margin = 20;			// --- Hide [expand] command if just a few pixels height when expanded.
	this.T = [];
}
_SXBB.prototype.genCmd = function(cmd, txt)
{	
    return '&nbsp;&#0139;&nbsp;<a class="gensmall" style="text-decoration:none;" href="javascript:void(0)" onclick="SXBB[\''+this.id+'\'].'+cmd+'(\''+txt+'\');" onfocus="this.blur();"><b>'+txt+'</b></a>&nbsp;&#0155;';	
}
_SXBB.prototype.writeCmd = function()
{
	var s='';
	if( (document.selection && !SXBB_IsIEMac()) || (document.createRange && (document.getSelection || window.getSelection)) )
	{
		s += this.genCmd('select', this.T['select']);
	}
	if( SXBB_IsOverflowAble() )
	{
		// Actually, the [expand] link will be placed by the onload event (if necessary) :-)
		s += '<span id="'+this.id+'x"></span>';
	}
	
	document.write(s); // ‹Select› ‹Expand›
}
_SXBB.prototype.writeDiv = function()
{
	// Use 'overflow:auto;height:#px' only if 'overflow:auto' is supported...
	// Works on IE4+, Mozilla, Opera7, Safari
	// Fails on IEMac, NS4, Opera6-
	//
	var s = ( SXBB_IsOverflowAble() ? 'style="overflow:auto;height:'+this.min+'px;"' : '' );
	document.write('<div id="'+this.id+'" '+s+'>');
}
_SXBB.prototype.getObj = function(id)
{
	return ( document.getElementById ? document.getElementById(id) : null );
}
_SXBB.prototype.select = function()
{
	var o = this.getObj(this.id);
	if( !o ) return;
	var r, s;
	if( document.selection && !SXBB_IsIEMac() )
	{
		// Works on: IE5+
		// To be confirmed: IE4? / IEMac fails?
		r = document.body.createTextRange();
		r.moveToElementText(o);
		r.select();
	}
	else if( document.createRange && (document.getSelection || window.getSelection) )
	{
		// Works on: Netscape/Mozilla/Konqueror/Safari
		// To be confirmed: Konqueror/Safari use window.getSelection ?
		r = document.createRange();
		r.selectNodeContents(o);
		s = window.getSelection ? window.getSelection() : document.getSelection();
		s.removeAllRanges();
		s.addRange(r);
	}
}
_SXBB.prototype.resize = function(cmd)
{
	var o = this.getObj(this.id);
	if( !o ) return;
	var x = this.getObj(this.id+'x');
	if( !x ) return;

	// First, deal with requested command...
	if( cmd == 'onload' || cmd == 'onresize' )
	{
		//if( o.scrollHeight <= this.min ) { x.innerHTML = ''; return; }
		if( o.scrollHeight <= this.min ) { x.innerHTML = ''; o.style.height = 'auto'; return; }
		if( x.innerHTML != '' ) return;
		if( cmd == 'onload' )
		{
			x.innerHTML = this.genCmd('resize', this.T['expand']);
			return;
		}
		cmd = this.T['contract'];
	}
	if( cmd == this.T['expand'] )
	{
		this.size = o.scrollHeight + this.extra;
				
		//Original code
		o.style.height = 'auto';
		o.style.overflow = 'visible';
		
		//Enhanced functionality
		/*
		if( (o.scrollHeight + this.extra) > screen.height-300 ) 
		{ 
			o.style.height = screen.height-300; 
		}	 
		else 
		{ 
			o.style.height = 'auto'; 
		} 
		o.style.overflow = 'auto';										
		*/
		// Check to conditionally hide the [expand] link, if once expanded, adding [contract] is not really useful.
		x.innerHTML = ( (o.scrollHeight-this.margin) > this.min ? this.genCmd('resize', this.T['contract']) : '' );
	}
	else
	{
		this.size = this.min;
		o.style.height = this.size + 'px';
		o.style.overflow = 'auto';
		x.innerHTML = this.genCmd('resize', this.T['expand']);
	}
	if( cmd != 'onresize' )
	{
		// If necessary, adjust height of outer blocks...
		if( o.parentNode ) for( o = o.parentNode; o.parentNode; o = o.parentNode )
		{
			if( o.tagName && o.tagName == 'DIV' && o.id && o.id.indexOf('SXBB') == 0 )
			{
				if( !document.all ) SXBB[o.id].resize(this.T['contract']);
				SXBB[o.id].resize(this.T['expand']);
			}
		}
	}
	return false;
}

// --------------------------------------------------------------------------------
// Actually, this script is being included for every instance of a boxed BBCode,
// so... we MUST do something to ensure we only do the job just once!
//
if( typeof(SXBB) == 'undefined' ) {
// --------------------------------------------------------------------------------

var SXBB = [];

// --------------------------------------------------------------------------------
if( SXBB_IsOverflowAble() ) {	// The following is Not necessary on some browsers!
// --------------------------------------------------------------------------------

var SXBB_oldOnLoad = null;
var SXBB_oldOnResize = null;

function SXBB_onLoad()
{
	if( SXBB_oldOnLoad ) { SXBB_oldOnLoad(); SXBB_oldOnLoad = null; }
	SXBB_evalSize('onload');
}
function SXBB_onResize()
{
	if( SXBB_oldOnResize ) { SXBB_oldOnResize(); SXBB_oldOnResize = null; }
	SXBB_evalSize('onresize');
}
function SXBB_evalSize(cmd)
{
	for( var id in SXBB ) SXBB[id].resize(cmd);
}

if( window.addEventListener )
{
	// The DOM method
	window.addEventListener('load', SXBB_onLoad, false);
	window.addEventListener('resize', SXBB_onResize, false);
}
else if( window.attachEvent )
{
	// The IE Method
	window.attachEvent('onload', SXBB_onLoad);
	window.attachEvent('onresize', SXBB_onResize);
}
else
{
	// The 'legacy' method
	SXBB_oldOnLoad = window.onload;
	SXBB_oldOnResize = window.onresize;
	window.onload = SXBB_onLoad;
	window.onresize = SXBB_onResize;
}

// --------------------------------------------------------------------------------
}	// if( SXBB_IsOverflowAble() )
// --------------------------------------------------------------------------------
}	// if( typeof(SXBB) == 'undefined' )
// --------------------------------------------------------------------------------

//---------------------------------------------------------------------------------
// Resize Image
//---------------------------------------------------------------------------------
var do_linked_resize = parseInt( "1" ); 
var resize_percent = parseInt( "40" ); 

add_onload_event(fix_linked_image_sizes); 

function fix_linked_image_sizes() { 
if (do_linked_resize!=1) { 
return true; 
} 
var images=document.getElementsByTagName('IMG'); 
var _padding=2; 
var _count=0; 
var _img='<img src="templates/common/sub/img-resized.png" style="vertical-align:middle" border="0" alt="" />'; 
var _sw=screen.width*(parseInt(resize_percent)/100); 
for (var i=0;i<images.length;i++) { 
if (images[i].className=='linked-image') { 
_count++; 
if (images[i].width>_sw) { 
var _width=images[i].width; 
var _height=images[i].height; 
var _percent=0; 
images[i].width=_sw; 
if (images[i].width<_width&&_width>0&&images[i].width>0) { 
_percent=Math.ceil(parseInt(images[i].width/_width*100)); 
} 
images[i].id='--ipb-img-resizer-'+_count; 
images[i]._resized=1; 
images[i]._width=_width; 
var div=document.createElement('div'); 
div.innerHTML=_img+'&nbsp;'+'Уменьшено: '+_percent+'% от оригинала [ '+_width+' x '+_height+' ] - Нажмите для просмотра полного изображения'; 
div.style.width=images[i].width-(_padding*2)+'px'; 
div.className='resized-linked-image'; 
div.style.textAlign='left'; 
div.style.fontWeight='normal'; 
div.style.paddingTop=_padding+"px"; 
div.style.paddingBottom=_padding+"px"; 
div.style.paddingLeft=_padding+"px"; 
div.style.paddingRight=_padding+"px"; 
div._is_div=1;div._resize_id=_count; 
div.onclick=fix_linked_images_onclick; 
div.onmouseover=fix_linked_images_mouseover; 
div.title='Нажмите для просмотра полного изображения'; 
div._src=images[i].src; 
images[i].parentNode.insertBefore(div,images[i]); 
} 
} 
} 
} 

function fix_linked_images_onclick(e) { 
PopUp(this._src,'popup',screen.width,screen.height,1,1,1); 
//e=ipsclass.cancel_bubble_all(e); 
return false; 
} 

function fix_attach_images_mouseover(e) { 
try { 
this.style.cursor='pointer'; 
} 
catch (acold) {} 
} 

function fix_linked_images_mouseover(e) { 
try { 
this.style.cursor='pointer'; 
} 
catch (acold) {} 
} 

function PopUp (url, name, width, height, center, resize, scroll, posleft, postop) { 
showx=""; 
showy=""; 
if (posleft!=0) { 
X=posleft; 
} 
if (postop!=0) { 
Y=postop; 
} 
if (!scroll) { 
scroll=1; 
} 
if (!resize) { 
resize=1; 
} 
if ((parseInt(navigator.appVersion)>=4)&&(center)) { 
X=(screen.width-width)/2; 
Y=(screen.height-height)/2; 
} 
if (X>0) { 
showx=',left='+X; 
} 
if (Y>0) { 
showy=',top='+Y; 
} 
if (scroll!=0) { 
scroll=1; 
} 
window.location = url; 
//var Win=window.open(url,name,'width='+width+',height='+height+showx+showy+',resizable='+resize+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no'); 
} 

function add_onload_event(func) { 
var oldonload=window.onload; 
if (typeof window.onload!='function') { 
window.onload=func; 
} else { 
window.onload=function() { 
if (oldonload) { 
oldonload(); 
}; 
func(); 
}; 
} 
}










//*******************************************************************
//** SMR_RESIZE_MOD: CONFIGURATION **********************************
//*******************************************************************
SMR_confMaxDim = 350; // pixels

//*******************************************************************
//** SMR_RESIZE_MOD: START CODE *************************************
//*******************************************************************
function SMR_resize(obj) {

   thisWidth = obj.width;
   thisHeight = obj.height;
   
   if(thisWidth > thisHeight) thisMaxDim = thisWidth;
   else thisMaxDim = thisHeight;
   
   if(thisMaxDim > SMR_confMaxDim) {
      thisMinDim = Math.round((((thisWidth > thisHeight)?thisHeight:thisWidth) * SMR_confMaxDim) / thisMaxDim); 
      
      if(thisWidth > thisHeight) {
         thisWidth = SMR_confMaxDim;
         thisHeight = thisMinDim;
      } else {
         thisHeight = SMR_confMaxDim;
         thisWidth = thisMinDim;
      }
   } // if(thisMaxDim > SMR_confMaxDim)

   obj.height = thisHeight;
   obj.width = thisWidth;
}
function SMR_setLink(obj) {
   thisInnerHtml = obj.innerHTML;
   tmpArray = thisInnerHtml.split(' src=\"');
   tmpArray = tmpArray[1].split('"');
   obj.href = tmpArray[0];
}
//*******************************************************************
//** SMR_RESIZE_MOD: END ********************************************
//*******************************************************************


