/*
// 2005-12-08
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
*/

function cmnInformation(){
	var eHTML = document.getElementsByTagName( 'html' )[0];
	this.sUser_agent = navigator.userAgent.toLowerCase();
	if( window.opera ){
		this.bOpera = true; cmnSet_class( eHTML, 'Opera');
	}
	if( !this.bOpera && this.sUser_agent.indexOf( 'msie' ) != -1 ){
		this.bIE = true; cmnSet_class( eHTML, 'IE');
	}else if( navigator.product == 'Gecko' ){
		this.bGecko = true; this.bMozilla = true; cmnSet_class( eHTML, 'Mozilla');
	}
	this.bMAC = ( this.sUser_agent.indexOf( 'mac' ) != -1 );
	this.sLanguage = null;
	this.bHTTP = null;
	this.bHandheld = false;
	return this;
}

function cmnPlaceholder( eThis, sText, sClass_on_empty ){
	eThis.onfocus = function(){ if( eThis.value.length && eThis.value == sText ){ eThis.value = ''; } cmnRemove_class( this, sClass_on_empty ); eThis.select(); }
	eThis.onblur = function(){ if( !this.value.length ){ cmnSet_class( this, sClass_on_empty ); this.value = sText; } }
	if( !eThis.value.length ){ eThis.onblur(); }
	if( !eThis.getAttribute( 'placeholder' ) ){ eThis.setAttribute( 'placeholder', sText ); }
}

var cmn_aPopup_defaults = [
	["width", 720],
	["height", 650],
	["location", "no"],
	["menubar", "no"],
	["toolbar", "no"],
	["resizable", "yes"],
	["scrollbars", "yes"],
	["status", "yes"]
]
var cmn_aPopup = new Array();
function cmnPopup( 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 < cmn_aPopup_defaults.length ; i++ ){
		if( !cmnPairs_string_get_value( sFeatures, cmn_aPopup_defaults[i][0], "=", "," ) ){
			sFeatures = cmnPairs_string_set_value( sFeatures, cmn_aPopup_defaults[i][0], cmn_aPopup_defaults[i][1], "=", "," );
		}
	}

	var iWidth = cmnPairs_string_get_value( sFeatures, "width", "=", "," );
	var iHeight = cmnPairs_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 = cmnPairs_string_set_value( sFeatures, "height", iHeight, "=", "," ); }
		if( iScreen_width < iWidth ){ bScroll = true; iWidth = iScreen_width; sFeatures = cmnPairs_string_set_value( sFeatures, "width", iWidth, "=", "," ); }
		if( bScroll ) {
			sFeatures = cmnPairs_string_set_value( sFeatures, "scrollbars", "yes", "=", "," );
		}
		sFeatures = cmnPairs_string_set_value( sFeatures, "top", Math.round( ( iScreen_height - iHeight ) / 2 ), "=", "," );
		sFeatures = cmnPairs_string_set_value( sFeatures, "left", Math.round( ( iScreen_width - iWidth ) / 2 ), "=", "," );
	}

	if( sURL.match(/\.(gif|jpe?g|png)$/i) ){
		cmn_aPopup[sName] = window.open( "", sName, sFeatures );
		if( cmn_aPopup[sName] ){
			var sTitle = unescape( cmnPairs_string_get_value( sFeatures, "title", "=", "," ) );
			sTitle = sTitle.replace( /<\/?\w[^>]*>/g, " " ).replace( /</g, "&lt;" ).replace( /</g, "&lt;" ).replace( /"/g, "&quot;" );
			cmn_aPopup[sName].document.open();
			cmn_aPopup[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>');
			cmn_aPopup[sName].document.close();
		}
	}else{
		var bWas_open = false;
		try{ bWas_open = ( cmn_aPopup[sName] && cmn_aPopup[sName].location.href != sURL ) }catch(e){}
		if( !cmn_aPopup[sName] || cmn_aPopup[sName].closed || !bWas_open ){
			if( sTarget && sTarget.match( /^_/ ) ){
				cmn_aPopup[sName] = window.open( sURL, sTarget );
			}else{
				cmn_aPopup[sName] = window.open( sURL, sName, sFeatures, bReplace );
			}
		}
	}
	if( sName != "_blank" ){
		cmn_aPopup[sName].focus();
	}

	return false;
}

var cmn_oInformation = new cmnInformation();

function cmnInit_Information(){
	cmn_oInformation.sLanguage = ( document.body && document.body.getAttribute( "lang" ) != "" ) ? document.body.getAttribute( "lang" ) : "ru";
	cmn_oInformation.bHTTP = ( document.location.href.indexOf("http://") == 0 ) ? true : false;
}

cmnAdd_event( window, "load", cmnInit_Information );

function cmnFlash(){
	var aeFlash = new Array();
	this.build = function( vEnabled, vDisabled, vNone, iVersion  ){
		var vVersion = this.get_version( iVersion );
		if( vVersion.toString() != "false" ){
			this.enable();
			vVersion = this.get_version( iVersion );
		}
		if( vVersion ){
			if( vEnabled.nodeType )	vEnabled.style.display = "block";
			else document.write( build_object(vEnabled) );
			if( vDisabled.nodeType ) vDisabled.style.display = "none";
			if( vNone.nodeType ) vNone.style.display = "none";
		}else{
			if( vEnabled.nodeType ) vEnabled.style.display = "none";
			if( vDisabled.nodeType ) vDisabled.style.display = "block";
			else if( vDisabled ) document.write( vDisabled );
			if( parseInt(vVersion) == 0 );
				if( vNone.nodeType ) vNone.style.display = "block";
				else if( vNone ) document.write( vNone );
		}
	}
	this.get_version = function( iVersion ){
		var vVersion = cmnGet_cookie( "flash" );
		if( !vVersion ){
			vVersion = check_flash_version( iVersion );
			set_flash_cookie( vVersion );
			return vVersion;
		}else if( iVersion && iVersion > vVersion ){
			vVersion = false;
		}
		return ( vVersion == "false" ) ?  false : vVersion;
	}
	this.disable = function(){
		set_flash_disabled( true );
	}
	this.enable = function(){
		set_flash_disabled( false );
	}
	this.set_disabled = function( bDisabled ){
		set_flash_disabled( bDisabled );
	}

	return this;

	function build_object( sOptions ){
		var asOptions = sOptions.split( "," );
		for( var i = 0 ; i < asOptions.length ; i++){
			asPair = asOptions[i].split( "=" );
			asOptions[asPair[0]] = unescape( asPair[1] );
		}
		var sHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
		sHTML += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' + 'width="' + asOptions.width + '" height="' + asOptions.height + '" align="middle">' + '<param name="allowScriptAccess" value="sameDomain">' + '<param name="movie" value="' + asOptions.source + '">' + '<param name="quality" value="high">';
		if( asOptions.base ){ sHTML += '<param name="base" value="' + asOptions.base + '">'; }
		if( asOptions.background ){ sHTML += '<param name="bgcolor" value="' + asOptions.background + '">'; }
		if( asOptions.transparent ){ sHTML += '<param name="wmode" value="transparent">' }
		if( asOptions.flashvars ){ sHTML += '<param name="flashvars" value="' + asOptions.flashvars + '">'; }
		sHTML += '<embed quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"' + ' pluginspage="http://www.macromedia.com/go/getflashplayer"' + ' src="' + asOptions.source + '" width="' + asOptions.width + '" height="' + asOptions.height + '"';
		if( asOptions.base ){ sHTML += ' base="' + asOptions.base + '"'; }
		if( asOptions.background ){ sHTML += ' bgcolor="' + asOptions.background + '"'; }
		if( asOptions.transparent ){ sHTML += ' wmode="transparent"' }
		if( asOptions.flashvars ){ sHTML += ' flashvars="' + asOptions.flashvars + '"'; }
		sHTML += '></embed></object>';
		return sHTML;
	}
	function set_flash_disabled( bDisabled ){
		set_flash_cookie( ( bDisabled ) ?  "false" : "" );
	}
	function check_flash_version( iVersion ){
		if( navigator.plugins && navigator.plugins['Shockwave Flash'] ){
			var sVersion = ( !iVersion ) ? "\\d+" : "[" + iVersion + "-9]\\d*";
			sVersion = parseInt( navigator.plugins['Shockwave Flash'].description.replace( new RegExp( "^[^\\d]+(" + sVersion + ").*$"), "$1" ) );
			return ( isNaN( sVersion ) ) ? ( !iVersion ) ? false : 0 : sVersion;
		}else if( cmn_oInformation.bIE && !cmn_oInformation.bMAC ){
			var oTemp, iMax = 20;
			iVersion = ( !iVersion ) ? 3 : iVersion;
			for( var i = iVersion ; i < iMax ; i++ ){
				try{
					oTemp = new ActiveXObject( "ShockwaveFlash.ShockwaveFlash." + i );
					iVersion = i;
				}catch(e){
					return ( i > iVersion ) ? iVersion : 0;
				}
			}
			return iVersion;
		}else{
			return ( !iVersion ) ? false : 0;
		}
	}
	function set_flash_cookie( sCookie_value ){
		var dNow = new Date();
		var dExpires = new Date();
		dExpires.setTime( dNow.getTime() + 1000 * 60 * 60 * 24 * 365 );
		cmnSet_cookie( "flash", ( !sCookie_value ) ? "" : sCookie_value, dExpires );
	}
}

function cmnSet_cookie( sName, vValue, dExpires ){
   document.cookie = sName + "=" + escape( vValue )
	   + ( dExpires == null ? "" : ( "; expires=" + dExpires.toGMTString() ) )
	   + "; path=/";
}

function cmnGet_cookie( sName ){
	return cmnPairs_string_get_value( document.cookie, sName );
}

function cmnSwitch_class( eOn, sClass_name, sInstead ){
	if( cmnMatch_class( eOn, sClass_name ) ){
		cmnSet_class( eOn, sInstead, sClass_name );
	}else{
		cmnSet_class( eOn, sClass_name, sInstead );
	}
}

function cmnRemove_class( eOn, sClass_name ){
	cmnSet_class( eOn, "", sClass_name );
}

function cmnSet_class( eOn, sClass_name, sInstead ){
	if( eOn ){
		sClass_name = ( sClass_name.length ) ? sClass_name.replace( /(^\s+|\s+$)/, "" ) : "";
		if( eOn.className.length ){
			var sOld = sClass_name;
			if( sInstead && sInstead.length ){
				sInstead = sInstead.replace( /\s+(\S)/g, "|$1" );
				if( sOld ){
					sOld += "|";
				}
				sOld += sInstead;
			}
			eOn.className = eOn.className.replace( new RegExp("(^|\\s+)(" + sOld +")($|\\s+)", "g"), "$1" );
		}
		eOn.className += ( eOn.className.length && sClass_name ? " " : "" ) + sClass_name;
	}
}

function cmnMatch_class( eOn, sClass_name ){
	return ( sClass_name && eOn.className && eOn.className.length && eOn.className.match( new RegExp("(^|\\s+)(" + sClass_name +")($|\\s+)") ) );
}

function cmnPairs_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 cmnPairs_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 cmnAdd_button_events( eOn ){
	cmnAdd_hover( eOn );
	cmnAdd_event( eOn, "mousedown", function(){ cmnSet_class( eOn, "mousedown" ) } );
	cmnAdd_event( eOn, "mouseup", function(){ cmnSwitch_class( eOn, "mousedown", "mouseup" ) } );
}

function cmnAdd_hover( eOn ){
	cmnAdd_event( eOn, "mouseover", function(){ cmnSet_class( eOn,    "hover" ) } );
	cmnAdd_event( eOn, "mouseout",  function(){ cmnRemove_class( eOn, "hover" ) } );
}

function cmnAdd_events( eOn, asEvent_type, ptrFunction ){
	for( var sName in asEvent_type ){
		cmnAdd_event( eOn, asEvent_type[sName], ptrFunction );
	}
}

function cmnAdd_event( eOn, sEvent_type, ptrFunction ){
	if( eOn.addEventListener ){
		eOn.addEventListener( sEvent_type, ptrFunction, false );
	}else{
		if( !ptrFunction.$$guid ){ ptrFunction.$$guid = cmnAdd_event.guid++; }
		if( !eOn.events ){ eOn.events = {}; }
		var aHandler = eOn.events[sEvent_type];
		if( !aHandler ){
			aHandler = eOn.events[sEvent_type] = {};
			if( eOn['on' + sEvent_type] ){ aHandler[0] = eOn['on' + sEvent_type]; }
			eOn['on' + sEvent_type] = cmnHandle_event;
		}
		aHandler[ptrFunction.$$guid] = ptrFunction;
	}
}
cmnAdd_event.guid = 1;

function cmnRemove_event( eOn, sEvent_type, ptrFunction ){
	if( eOn.removeEventListener ){
		eOn.removeEventListener( sEvent_type, ptrFunction, false );
	}else if( eOn.events && eOn.events[sEvent_type] && ptrFunction.$$guid ){
		delete eOn.events[sEvent_type][ptrFunction.$$guid];
	}
}

function cmnHandle_event( event ){
	event = event || cmnFixe_event( window.event );
	var returnValue = true;
	var aHandler = this.events[event.type];
	for( var i in aHandler ){
		if( !Object.prototype[i] ){
			this.$$ptrFunction = aHandler[i];
			if( this.$$ptrFunction(event) === false ){ returnValue = false; }
		}
	}
	if( this.$$ptrFunction ){ this.$$ptrFunction = null; }

	return returnValue;
}

function cmnFixe_event( event ){
	event.preventDefault = cmnFixe_event.preventDefault;
	event.stopPropagation = cmnFixe_event.stopPropagation;
	return event;
}
cmnFixe_event.preventDefault = function(){
	this.returnValue = false;
}
cmnFixe_event.stopPropagation = function(){
	this.cancelBubble = true;
}

function cmnInit_tels(){
	var aeNOBR = document.getElementsByTagName( 'nobr' );
	for( var i = 0 ; i < aeNOBR.length ; i++)
		if( cmnMatch_class( aeNOBR[i], 'phone' ) )
			aeNOBR[i].innerHTML = '<a href="tel:' + aeNOBR[i].innerHTML.replace( /(<\/?\w[^>]*>|[\s\,\.\;\-\(\)]+)/g, '' ) + '">' + aeNOBR[i].innerHTML + '</a>';
}



/* сворачивает-разворачивает списки */

function expandList(oTarget){
	var oElem=oTarget.parentNode.parentNode;
	var oDT, oDD, oSibling;
	switch(oElem.tagName){
		case 'DT':
			if( (oSibling=CL_SearchSibling(oElem, 'DD', 'next')) ){
				oDT=oElem;
				oDD=oSibling;
			}
			break;
		case 'DD':
			if( (oSibling=CL_SearchSibling(oElem, 'DT', 'previous')) ){
				oDT=oSibling;
				oDD=oElem;
			}
			break;
	}

	if(oDD && oDT){
		if(cmnMatch_class(oDD, 'Expanded')){
			cmnRemove_class(oDD, 'Expanded');
			cmnRemove_class(oDT, 'Expanded');
		}
		else{
			cmnSet_class(oDD, 'Expanded');
			cmnSet_class(oDT, 'Expanded');
		}
	}
}


/* ищет ближайший соседний элемент и проверяет его название */

function CL_SearchSibling(oElem, sSiblingName, sWay){
	var oSibling=oElem;
	switch(sWay){
		case 'next':
			while( (oSibling = oSibling.nextSibling) )
				if(oSibling.nodeType == 1)
					return (oSibling.tagName == sSiblingName) ? oSibling : null;
			break;
		case 'previous':
			while( (oSibling = oSibling.previousSibling) )
				if(oSibling.nodeType == 1)
					return (oSibling.tagName == sSiblingName) ? oSibling : null;
			break;
	}
	
	return null;
}

