/*
// 2007-09-14
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
*/

if( !window.Map ){
	var Map = { hoArea: [] };
}

try{ document.execCommand("BackgroundImageCache", false, true); }catch(e){};

Map.Object = function( oParams ){
	this.aoChild = [];
	this.create( oParams );
	return this;
}

Map.Child = function( eArea, oParent, oTop ){
	this.create( eArea, oParent, oTop );
	return this;
}

Map.Object.prototype.create = function( oParams ){
	var oThis = this;
	oThis.oParams = oParams;

	//oThis.oDrag = new Overflow.Drag.Object( oParams.eThis );

	oThis.oSelected = new Map.Selected( oParams.eSelected, oParams.sSelected_input_name );

	try{
		oParams.eMap = document.createElement( '<map name="map_' + oParams.sArea_ID + '"></map>' );
	}catch(e){
		oParams.eMap = document.createElement( 'map' );
		oParams.eMap.name = 'map_' + oParams.sArea_ID;
	}

	//Common.Dom.setStyle( oParams.eThis, 'float: left; position: relative; left: 50%; margin-left: -' + Map.hoArea[oParams.sArea_ID].iWidth / 2 + 'px;' );

	oThis.iSelected = 0;
	oThis.aoChild = []
	for( var i = 0, bSelected = false ; i < Map.hoArea[oParams.sArea_ID].aoChild.length ; i++ ){
		oThis.aoChild[oThis.aoChild.length] = new Map.Child( Map.hoArea[oParams.sArea_ID].aoChild[i], oThis );
		if( oThis.aoChild[oThis.aoChild.length - 1].bSelected ){ bSelected = true; }
	}
	if( !bSelected ){
		oThis.toggle( 0 );
	}
	if( !oThis.oSelected.iCount ){
		Common.Class.add( oParams.eSelected.parentNode, 'empty' );
		Common.Class.add( oParams.eSelected.parentNode.parentNode, 'empty' );
	}

	oParams.eThis.appendChild( oParams.eMap );

	try{
		oThis.eImage = document.createElement( '<img src="' + oParams.sTransparent_URL + '" usemap="#' + oParams.eMap.name + '" />' );
	}catch(e){
		oThis.eImage = document.createElement( 'img' );
		oThis.eImage.src = oParams.sTransparent_URL;
		oThis.eImage.setAttribute( 'usemap', '#' + oParams.eMap.name );
	}
	oParams.eThis.appendChild( oThis.eImage );
	Common.Dom.setStyle( oThis.eImage, 'position: absolute; z-index: 3; left: 0; top: 0; width: ' + Map.hoArea[oParams.sArea_ID].iWidth + 'px; height: ' + Map.hoArea[oParams.sArea_ID].iHeight + 'px;' );
	if( oThis.oDrag && oThis.oDrag.can() ){
		oThis.eImage.style.cursor = 'move';
	}
}

Map.Object.prototype.toggle = function( iSelected ){
	var iNew = this.iSelected + iSelected;
	if( iNew < 2 && this.iSelected < 2 ){
		var sDisplay = iNew ? 'none' : 'block';
		for( var i = 0 ; i < this.aoChild.length ; i++ ){
			this.aoChild[i].eThis.style.display = sDisplay;
		}
	}
	this.iSelected = iNew;
}

Map.Child.prototype.create = function( oArea, oParent, oTop ){
	var oThis = this;
	oThis.oParent = oParent;
	oThis.oTop = oTop ? oTop : oParent;
	oThis.oData = oArea;
	oThis.bSelected = false;

	var oCoords = Map.get_coords( oArea.sCoords );
	if( oCoords ){
		oThis.eArea = document.createElement( 'area' );
		oThis.eArea.setAttribute( 'shape', oArea.sCoords.match( /^\s*\d+\s*\,\s*\d+\s*\,\s*\d+\s*\,\s*\d+\s*$/ ) ? 'rect' : 'poly' );
		oThis.eArea.setAttribute( 'href', '#' );
		oThis.eArea.setAttribute( 'coords', oArea.sCoords );
		oThis.eArea.title = oArea.sName ? oArea.sName.replace( /<[^>]+>/g, '') : '';
		oThis.oTop.oParams.eMap.appendChild( oThis.eArea );
		Common.Event.add( oThis.eArea, 'mouseover', function(){ oThis.oTop.eImage.style.cursor = 'hand'; });
		Common.Event.add( oThis.eArea, 'mouseout', function(){ oThis.oTop.eImage.style.cursor = oThis.oTop.oDrag && oThis.oTop.oDrag.can() ? 'move' : 'default'; });

		oCoords.iWidth++;
		oCoords.iHeight++;
		oThis.eThis = Map.append_area( oThis.oTop.oParams.eThis, oThis.oData.sName, oCoords, 2, oThis.oTop.oParams.sParts_URL, oThis.oData.sBackground_position );
		oThis.eThis.style.display = 'none';

		Common.Event.add( oThis.eArea, 'click', function( oEvent ){
			oThis.pre_toggle();
			Common.Event.cancel( oEvent );
		} );
		if( oThis.oTop.oParams.asSelected.indexOf( oThis.oData.sID ) >= 0 ){
			oThis.pre_toggle();
		}
	}


}

Map.Child.prototype.pre_toggle = function(){
	this.toggle();
}

Map.Child.prototype.toggle = function(){
	this.bSelected = !this.bSelected;
	if( this.bSelected ){
		this.oTop.toggle( 1 );
		this.eThis.style.display = 'block';
		this.oTop.oSelected.append( this );
	}else{
		this.eThis.style.display = 'none';
		this.oTop.oSelected.remove( this );
		this.oTop.toggle( -1 );
	}
}

Map.Selected = function( eThis, sInput_name ){
	this.create( eThis, sInput_name );
	return this;
}

Map.append_area = function( eTo, sTitle, oCoords, iZ, sBackground_URL, sBackground_position ){
	var eChild = document.createElement( 'ins' );
	eTo.appendChild( eChild );
	eChild.title = sTitle;
	Common.Dom.setStyle( eChild, 'position: absolute; z-index: ' + iZ + '; overflow: hidden; left: '
	+ oCoords.iLeft + 'px; top: '
	+ oCoords.iTop + 'px; width: '
	+ oCoords.iWidth + 'px; height: '
	+ oCoords.iHeight + 'px; background: url('
	+ sBackground_URL + ') '
	+ sBackground_position + '; cursor: pointer; cursor: hand;' );
	return eChild;
}

Map.get_coords = function( sCoords ){
	var aCoords = sCoords.split( ',' ), i = 0, oCoords = { iLeft: 10000, iTop: 10000, iRight: 0, iBottom: 0 };
	if( aCoords.length ){
		while( i < aCoords.length ){
			if( aCoords[i] * 1 < oCoords.iLeft ){ oCoords.iLeft = aCoords[i]; }
			if( aCoords[i] * 1 > oCoords.iRight ){ oCoords.iRight = aCoords[i]; }
			i++;
			if( aCoords[i] * 1 < oCoords.iTop ){ oCoords.iTop = aCoords[i]; }
			if( aCoords[i] * 1 > oCoords.iBottom ){ oCoords.iBottom = aCoords[i]; }
			i++;
		}
		oCoords.iWidth = oCoords.iRight - oCoords.iLeft;
		oCoords.iHeight = oCoords.iBottom - oCoords.iTop;
		return oCoords;
	}else{
		return false;
	}
}


Map.Selected.prototype.create = function( eThis, sInput_name ){
	var oThis = this;
	oThis.eThis = eThis;
	oThis.sInput_name = sInput_name;

	oThis.iCount = 0;
	oThis.hoChild = [];

	if( eThis.tagName.toLowerCase() == 'ul' ){
		oThis.sTag_name =  'li';
	}else{
		oThis.sTag_name =  'span';
	}
}

Map.Selected.prototype.append = function( oThis ){
	if( !this.hoChild[oThis.oData.sID] && oThis.oData.sName ){
		var eLabel = document.createElement( this.sTag_name );
		if( this.sTag_name != 'li' && this.iCount ){
			this.eThis.appendChild( document.createTextNode( ' ' ) );
		}
		this.eThis.appendChild( eLabel );
		eLabel.innerHTML =  oThis.oData.sName + '<ins class="pseudo-link"> </ins>';
		if( window.need_fix_hover && need_fix_hover ){
			fix_hover( eLabel.lastChild, 'pseudo-link' );
		}
		if( oThis.oData.sColor ){
			eLabel.style.color = oThis.oData.sColor;
		}else if( oThis.oParent.oData && oThis.oParent.oData.sColor ){
			eLabel.style.color = oThis.oParent.oData.sColor;
		}
		Common.Event.add( eLabel.lastChild, 'click', function(){
			oThis.pre_toggle();
		} );


		if( oThis.oData && oThis.oData.oWidget ){
			var oWidget = oThis.oData.oWidget;
		}else{
			try{
				var eInput = document.createElement('<input type="checkbox" name="' + this.sInput_name + '" checked="true" value="' + oThis.oData.sID + '"  />');
				if( window.opera ){
					throw( new Exception() );
				}
			}catch( oException ){

				var eInput = document.createElement('input');
				eInput.type = 'checkbox';
				eInput.name = this.sInput_name;
				eInput.value = oThis.oData.sID;
				eInput.checked = true;

			}
			this.eThis.parentNode.appendChild( eInput );
			if( oThis.oTop.oParams.xForm ){
				if( !oThis.oTop.hParentWidget){
					oThis.oTop.hParentWidget = [];
				}
				if(!oThis.oTop.hParentWidget[this.sInput_name]) {
					oThis.oTop.hParentWidget[this.sInput_name] = oThis.oTop.oParams.xForm.addChild( new CheckBoxGroup( this.eThis ) );
				}
				var oWidget = oThis.oTop.hParentWidget[this.sInput_name].addChild( new StateInput( eInput ) );
				oWidget.processEvents( true );
			}
		}

		this.hoChild[oThis.oData.sID] = { eLabel: eLabel, eInput: eInput, oWidget : ( oThis.oTop.oParams.xForm ? oWidget : null ) };
		this.iCount++;
		Common.Class.remove( this.eThis.parentNode, 'empty' );
		Common.Class.remove( this.eThis.parentNode.parentNode, 'empty' );
	}else if( this.hoChild[oThis.oData.sID].oWidget ){
		this.hoChild[oThis.oData.sID].oWidget.check();
		this.hoChild[oThis.oData.sID].oWidget.processEvents( true );
	}
}

Map.Selected.prototype.remove = function( oThis ){
	if( this.hoChild[oThis.oData.sID] ){
		if( this.hoChild[oThis.oData.sID].eLabel.previousSibling && this.hoChild[oThis.oData.sID].eLabel.previousSibling.nodeValue == ' ' ){
			this.eThis.removeChild( this.hoChild[oThis.oData.sID].eLabel.previousSibling );
		}else if( this.hoChild[oThis.oData.sID].eLabel.nextSibling && this.hoChild[oThis.oData.sID].eLabel.nextSibling.nodeValue == ' ' ){
			this.eThis.removeChild( this.hoChild[oThis.oData.sID].eLabel.nextSibling );
		}

		this.eThis.removeChild( this.hoChild[oThis.oData.sID].eLabel );

		if( oThis.oTop.oParams.xForm ){
			this.hoChild[oThis.oData.sID].oWidget.uncheck();
			this.hoChild[oThis.oData.sID].oWidget.oParent.processEvents(true);
		}else if( this.hoChild[oThis.oData.sID].eInput ){
			this.eThis.parentNode.removeChild( this.hoChild[oThis.oData.sID].eInput );
		}

		this.hoChild[oThis.oData.sID] = false;
		this.iCount--;
		if( !this.iCount ){
			Common.Class.add( this.eThis.parentNode, 'empty' );
			Common.Class.add( this.eThis.parentNode.parentNode, 'empty' );
		}
	}
}