/*
// Area Map
// 2007-09-14
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
// LastEdit - Ibliaminov Albert (2010-05-20)
*/

if( !window.Map ){
	var Map = { hoArea: [], tags: [] };
}

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.Tag = function( eArea, oParent ){
	this.create( eArea, oParent );
	return this;
}

Map.Object.prototype.create = function( oParams ){
	var oThis = this;
	oThis.oParams = oParams;                                                                    

	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 = [];
    oThis.aoTags = [];

	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; }
	}

    for( var i = 0; i < Map.tags[oParams.sArea_ID].length ; i++ ){
		oThis.aoTags[oThis.aoTags.length] = new Map.Tag(Map.tags[oParams.sArea_ID][i], oThis);
	}

	/*if( !bSelected ){
		oThis.toggle( 0 );
	}*/

	if( !oThis.oSelected.iCount ){
		$(oParams.eSelected.parentNode).addClass('empty');
		$(oParams.eSelected.parentNode.parentNode).addClass('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 );
	$(oThis.eImage).css(
        {
            position : 'absolute',
            zIndex: 3,
            left: 0,
            top: 0,
            width: Map.hoArea[oParams.sArea_ID].iWidth,
            height: Map.hoArea[oParams.sArea_ID].iHeight    
        }
    );
	if( oThis.oDrag && oThis.oDrag.can() ){
		oThis.eImage.style.cursor = 'move';
	}

    oParams.oClose.button.click(
        function() {
            if (!oThis.isHidden) {
                oParams.oClose.container.addClass('hidden');
                oParams.oClose.parentButton.css({marginBottom : 0});
                oThis.isHidden = true;
                var selectedRegions = 0;
                for (var i=0; i < oThis.aoChild.length; i++) {
                    if (oThis.aoChild[i].bSelected) {
                        selectedRegions++;
                    };
                }
                var regions_label = [' микрорайоне', ' микрорайонах', ' ']
                var lastDigit = String(selectedRegions).length > 1 ? String(selectedRegions).substring(String(selectedRegions).length-1) : selectedRegions;
                var descStr = '';
                
                if(selectedRegions < 10)
                switch (parseInt(lastDigit)) {
                    case 0 : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 1 : descStr = '' + selectedRegions + regions_label[0] + ''; break;
                    case 2  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 3  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 4  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    default : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                }                
                else if(selectedRegions > 9 && selectedRegions < 21)
                switch (parseInt(lastDigit)) {
                    case 0 : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 1 : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 2  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 3  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 4  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    default : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                }           
                else 
                switch (parseInt(lastDigit)) {
                    case 0 : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 1 : descStr = '' + selectedRegions + regions_label[0] + ''; break;
                    case 2  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 3  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    case 4  : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                    default : descStr = '' + selectedRegions + regions_label[1] + ''; break;
                }

                oParams.oClose.regionDesc.html('<span>' + 'в&nbsp;'+descStr + '</span>');
            }
        }
    );

    oParams.oClose.parentButton.click(
        function() {
            if (oThis.isHidden) {
                oParams.oClose.container.removeClass('hidden');
                oParams.oClose.parentButton.css({marginBottom : '10px'});
                oThis.isHidden = false;
            }
        }
    );

}

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;
    oThis.oTags = [];

	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 );
		$(oThis.eArea).bind('mouseover', function(){ oThis.oTop.eImage.style.cursor = 'hand'; });
		$(oThis.eArea).bind('mouseover', 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';

		$(oThis.eArea).bind('click', function( oEvent ){
			oThis.pre_toggle();
			oEvent.stopPropagation();
            oEvent.preventDefault();
		} );
		if( oThis.oTop.oParams.asSelected.indexOf( oThis.oData.sID ) >= 0 ){
			oThis.pre_toggle();
		}
	}


}

Map.Child.prototype.pre_toggle = function(){
	
	debug(this);
	for(var t=0; t<this.oTags.length;t++)
	{
		var active = true;
		for(var y=0; y < this.oTags[t].aRegions.length; y++)
		{
			//Перебираем id районов внутри тега
			if(this.oTags[t].aRegions[y].bSelected == false && this.oTags[t].aRegions[y].oData.sID != this.oData.sID)
				active = false;
		}
		if(active == true)
		{
			debug("Активируется тег");
			this.oTags[t].oLink.addClass('active');
			this.oTags[t].bActive = true;
		}
	}
	
	
	this.toggle();
}

Map.Child.prototype.toggle = function(){
    this.bSelected = !this.bSelected;
    if( this.bSelected ){
        this.eThis.style.display = 'block';
        this.oTop.oSelected.append(this);
    }else{
        this.eThis.style.display = 'none';
        this.oTop.oSelected.remove(this);
        this.checkTags();
    }
}

Map.Child.prototype.checkTags = function() {
    for (var i=0; i < this.oTags.length; i++){
        if(this.oTags[i].bActive) {
            this.oTags[i].oLink.removeClass('active');
            this.oTags[i].bActive = false;
            for (var j=0; j < this.oTags[i].aRegions.length; j++) {
                if( this.oTags[i].aRegions[j].bSelected ) {
                    this.oTags[i].aRegions[j].oTop.oSelected.append( this.oTags[i].aRegions[j] );    
                }
            }
        }
    }
}

Map.Tag.prototype.create = function( oArea, oParent ){
	var oThis = this;
    
	oThis.oParent = oParent;
	oThis.oData = oArea;
    oThis.aRegions = [];
    oThis.bActive = false;

    oThis.oLink = $('<span class="' + oThis.oData.name + ' pseudo">' + oThis.oData.webName + '</span>').appendTo($(oParent.oParams.eTags));

    for (var j=0; j < oThis.oData.regions.length; j++) {
        for (var i=0; i < oParent.aoChild.length; i++) {
            if(oParent.aoChild[i].oData.sID == oThis.oData.regions[j]) {
                oThis.aRegions[oThis.aRegions.length] = oParent.aoChild[i];
                oParent.aoChild[i].oTags[oParent.aoChild[i].oTags.length] = oThis;
            }
        }
    };

    oThis.oLink.click(
        function() {
            if (!oThis.bActive) {
                for (var i=0; i < oThis.aRegions.length; i++) {
                    if (!oThis.aRegions[i].bSelected) {
                        oThis.aRegions[i].pre_toggle();
                    }
                }
                oThis.bActive = true;
                oThis.oLink.addClass('active');
            }
            else {
                for (var i=0; i < oThis.aRegions.length; i++) {
                    if (oThis.aRegions[i].bSelected) {
                        oThis.aRegions[i].pre_toggle();
                    }
                }
                oThis.bActive = false;
                oThis.oLink.removeClass('active');
            }
			//SDF1288 = oThis;
        }
    );

}

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;
	$(eChild).css(
        {
            position : 'absolute',
            zIndex : iZ,
            overflow : 'hidden',
            left : oCoords.iLeft + 'px',
            top : oCoords.iTop + 'px',
            width : oCoords.iWidth,
            height : oCoords.iHeight,
            backgroundImage : 'url(' + sBackground_URL + ')',
            backgroundPosition :  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;
        }
        $(eLabel.lastChild).bind('click', function(){
            oThis.pre_toggle();
			oThis.oParent.oParent.oParams.OnChange&&oThis.oParent.oParent.oParams.OnChange();//SDF2374 = oThis;
        } );

		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 );
			}
		}
		//.oParent.oParams.OnChange && oThis.oParent.oParams.OnChange();
		this.hoChild[oThis.oData.sID] = { eLabel: eLabel, eInput: eInput, oWidget : ( oThis.oTop.oParams.xForm ? oWidget : null ) };
		this.iCount++;
		$(this.eThis.parentNode).removeClass('empty');
		$(this.eThis.parentNode.parentNode).removeClass('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 ){
			$(this.eThis.parentNode).addClass('empty');
			$(this.eThis.parentNode.parentNode).addClass('empty');
		}
		//oThis.oParent.oParams.OnChange && oThis.oParent.oParams.OnChange();
	}
}












/*
// Metro Map
// 2007-09-14 
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
// LastEdit - Ibliaminov Albert (2010-05-20)
*/

Metro = {
	hoCity: [],
    tags : [],
    city : ""

	, Object: function( oParams ){
		Metro.city = oParams.sCity_ID;
		this.aoBranch = [];
		this.create( oParams );
		return this;
	}

	, Branch: function( oBranch, oParent, oTop ){
		this.create( oBranch, oParent, oTop );
		return this;
	}

	, Station: function( oStation, oParent, oTop ){
		this.create( oStation, oParent, oTop);
		return this;
	}

    , Tag: function( oStation, oParent, sType ){
		var oThis = this;
		setTimeout(function(){
			oThis.create( oStation, oParent, sType );
		},150);
		return this;
	}

};

Metro.Object.prototype.create = function( oParams ){
	var oThis = this;
	oThis.oParams = oParams;
    oThis.aoBranches = [];
    oThis.aoTagsStations = [];
    oThis.aoTagsBranches = [];    

	//oThis.oDrag = new Overflow.Drag.Object( oParams.eMetro );

	$(oParams.eMetro).css(
        {
            position: 'relative'    
        }
    );
	oThis.eDamp = document.createElement( 'ins' );
	oParams.eMetro.appendChild( oThis.eDamp );
	$(oThis.eDamp).css(
        {
            position: 'absolute',
            zIndex: 1,
            left: 0,
            top: '45px',
            width: Metro.hoCity[oParams.sCity_ID].iWidth + 'px',
            height: (Metro.hoCity[oParams.sCity_ID].iHeight - 45) + 'px',
            background: '#ffffff',
            opacity: 0.5
        }
    );
	oThis.eDamp.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=50)';

	try{
		oParams.eMap = document.createElement( '<map name="map_metro_' + oParams.sCity_ID + '"></map>' );
	}catch(e){
		oParams.eMap = document.createElement( 'map' );
		oParams.eMap.name = 'map_metro_' + oParams.sCity_ID;
	}

	oThis.oSelected_branches = new Map.Selected( oParams.eSelected_branches, oParams.sSelected_branches_input_name );
	oThis.oSelected_stations = new Map.Selected( oParams.eSelected_stations, oParams.sSelected_stations_input_name );

	for( var i = 0 ; i < Metro.hoCity[oParams.sCity_ID].aoBranch.length ; i++ ){
		oThis.aoBranches[oThis.aoBranches.length] = new Metro.Branch( Metro.hoCity[oParams.sCity_ID].aoBranch[i], oThis );
	}
	//oThis.toggle();

	oParams.eMetro.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.eMetro.appendChild( oThis.eImage );
	$(oThis.eImage).css(
        {
            position: 'absolute',
            zIndex: 3,
            left: 0,
            top: 0,
            width: Metro.hoCity[oParams.sCity_ID].iWidth + 'px',
            height: Metro.hoCity[oParams.sCity_ID].iHeight + 'px'
        }
    );
	if( oThis.oDrag && oThis.oDrag.can() ){
		oThis.eImage.style.cursor = 'move';
	}

    for( var i = 0; i < Metro.tags[oParams.sCity_ID].stations.length ; i++ ){
		oThis.aoTagsStations[oThis.aoTagsStations.length] = new Metro.Tag(Metro.tags[oParams.sCity_ID].stations[i], oThis, 'stations');
	}
    for( var i = 0; i < Metro.tags[oParams.sCity_ID].branches.length ; i++ ){
		oThis.aoTagsBranches[oThis.aoTagsBranches.length] = new Metro.Tag(Metro.tags[oParams.sCity_ID].branches[i], oThis, 'branches');
	}

}

Metro.Object.prototype.toggle = function(){
	this.eDamp.style.display = this.oSelected_branches.iCount || this.oSelected_stations.iCount ? 'block' : 'none';
	if( this.oSelected_branches.iCount + this.oSelected_stations.iCount == 0 ){
		$(this.oParams.eSelected_branches.parentNode.parentNode).addClass('empty');
	}else{
		$(this.oParams.eSelected_branches.parentNode.parentNode).removeClass('empty');
	}
}

Metro.Branch.prototype.create = function( oBranch, oParent, oTop ){
	var oThis = this;
	oThis.oData = oBranch;
	oThis.oParent = oParent;
	oThis.oTop = oTop ? oTop : oParent;

	oThis.bSelected = false;

	var oCoords = Map.get_coords( oBranch.sCoords );
	if( oCoords ){
		oThis.eArea = document.createElement( 'area' );
		oThis.eArea.setAttribute( 'shape', 'poly' );
		oThis.eArea.setAttribute( 'href', '#' );
		oThis.eArea.setAttribute( 'coords', oBranch.sCoords );
		oThis.eArea.title = oBranch.sName;
		oThis.oTop.oParams.eMap.appendChild( oThis.eArea );
		$(oThis.eArea).bind('mouseover', function(){ oThis.oTop.eImage.style.cursor = 'hand'; });
		$(oThis.eArea).bind('mouseout', function(){ oThis.oTop.eImage.style.cursor = oThis.oTop.oDrag && oThis.oTop.oDrag.can() ? 'move' : 'default'; });

		oCoords.iWidth++;
		oCoords.iHeight++;
		oThis.eBranch = Map.append_area( oThis.oTop.oParams.eMetro, oThis.oData.sName, oCoords, 2, oThis.oTop.oParams.sParts_URL, oThis.oData.sBackground_position );
		oThis.eBranch.style.display = 'none';

		$(oThis.eArea).bind('click', function( oEvent ){
			oThis.pre_toggle();
            oThis.oParent.oParams.OnChange&&oThis.oParent.oParams.OnChange();//SDF3608 = oThis;
			oEvent.stopPropagation();
			oEvent.preventDefault();
		} );
	}

	oCoords = Map.get_coords( oBranch.sCoords_label );
	if( oCoords ){
		oThis.eLabel = Map.append_area( oThis.oTop.oParams.eMetro, oThis.oData.sName, oCoords, 4, oThis.oTop.oParams.sTransparent_URL, '-' + oCoords.iLeft + 'px -' + oCoords.iTop + 'px' );

		$(oThis.eLabel).bind('click', function( oEvent ){
			oThis.pre_toggle();
			 oThis.oParent.oParams.OnChange&&oThis.oParent.oParams.OnChange();//SDF4620 = oThis;
			oEvent.stopPropagation();
			oEvent.preventDefault();
		} );
	}

	oThis.aoStation = [];
	oThis.iSelected = 0;
	oThis.iCount = 999;
	for( var i = 0, iCount = 0 ; i < oBranch.aoStation.length ; i++ ){
		oThis.aoStation[oThis.aoStation.length] = new Metro.Station( oBranch.aoStation[i], oThis, oThis.oTop );
		iCount++;
	}
	oThis.iCount = iCount;

	if( oThis.oTop.oParams.asSelected_branches.indexOf( oThis.oData.sID ) >= 0 ){
		oThis.pre_toggle();
	}
}

Metro.Branch.prototype.pre_toggle = function(){
	this.bSelected = !this.bSelected;
	this.iSelected = this.bSelected ? this.iCount : 0;
	this.toggle();
	for( var i = 0 ; i < this.aoStation.length ; i++ ){
		this.aoStation[i].toggle( this.bSelected );
	}
}

Metro.Branch.prototype.toggle = function(){
	this.bSelected = this.iSelected == this.iCount;
	if( this.bSelected ){
		
		debug(this);
		
		this.eBranch.style.display = 'block';
		this.eLabel.style.backgroundImage = 'url(' + this.oTop.oParams.sParts_URL + ')';
		for( var i = 0 ; i < this.aoStation.length ; i++ ){
			this.oTop.oSelected_stations.remove( this.aoStation[i] );
		}
		this.oTop.oSelected_branches.append( this );
		
		$('#id_branch_i'+this.oData.sID).parent().addClass('active');
		
	}else{
		this.eBranch.style.display = 'none';
		this.eLabel.style.backgroundImage = 'url(' + this.oTop.oParams.sTransparent_URL + ')';
		if( this.iSelected ){
			for( var i = 0 ; i < this.aoStation.length ; i++ ){
				if( this.aoStation[i].bSelected ){
					this.oTop.oSelected_stations.append( this.aoStation[i] );
				}
			}
		}
		this.oTop.oSelected_branches.remove( this );
		$('#id_branch_i'+this.oData.sID).parent().removeClass('active');
	}
}

Metro.Station.prototype.create = function( oStation, oParent, oTop, oParams ){
	var oThis = this;
	oThis.oData = oStation;
	oThis.oParams = oParams;
	oThis.oParent = oParent;
	oThis.oTop = oTop ? oTop : oParent;

	oThis.bSelected = false;

	var oCoords = Map.get_coords( oStation.sCoords );
	if( oCoords ){
		oThis.ePoint = Map.append_area( oThis.oTop.oParams.eMetro, oThis.oData.sName, oCoords, 4, oThis.oTop.oParams.sTransparent_URL, '-' + oCoords.iLeft + 'px -' + oCoords.iTop + 'px' );

		$(oThis.ePoint).bind('click', function(){
			oThis.pre_toggle();
			oThis.oParent.oParent.oParams.OnChange&&oThis.oParent.oParent.oParams.OnChange();//SDF5694 = oThis;
		} );
	}

	oCoords = Map.get_coords( oStation.sCoords_label );
	if( oCoords ){
		oThis.eLabel = Map.append_area( oThis.oTop.oParams.eMetro, oThis.oData.sName, oCoords, 4, oThis.oTop.oParams.sTransparent_URL, '-' + oCoords.iLeft + 'px -' + oCoords.iTop + 'px' );

		$(oThis.eLabel).bind('click', function(){
			oThis.pre_toggle();
			oThis.oParent.oParent.oParams.OnChange&&oThis.oParent.oParent.oParams.OnChange();//SDF6704 = oThis;
		} );
	}
	if( oThis.oTop.oParams.asSelected_stations.indexOf( oThis.oData.sID ) >= 0 ){
		oThis.pre_toggle();
	}
}

Metro.Station.prototype.pre_toggle = function(){
	var city = Metro.city;

	if(this.bSelected == true)
	{
		for(var i=0;i<Metro.tags[city].stations.length;i++){
			var active = false;
			for(var m=0;m<Metro.tags[city].stations[i].items.length;m++)
			{
				if(this.oData.sID == Metro.tags[city].stations[i].items[m])
					active = true;	
			}

			if(active)
				$('#id_tag_'+Metro.tags[city].stations[i].name).parent().removeClass('active');
		}
	}
	
	this.toggle();
}

Metro.Station.prototype.toggle = function( bSelected ){
	
	if( bSelected || bSelected === false ){
		this.bSelected = bSelected;
	}else{
		this.bSelected = !this.bSelected;
		this.oParent.iSelected = this.bSelected ? this.oParent.iSelected + 1 : this.oParent.iSelected - 1;
		if( this.bSelected ){
			this.oTop.oSelected_stations.append( this );
		}else{
			this.oTop.oSelected_stations.remove( this );
		}
		this.oParent.toggle();
	}
	this.ePoint.style.backgroundImage = 'url(' + ( this.bSelected ? this.oTop.oParams.sParts_URL : this.oTop.oParams.sTransparent_URL ) + ')';
	this.eLabel.style.backgroundImage = this.ePoint.style.backgroundImage;
	//this.oTop.toggle();
}

Metro.Tag.prototype.create = function( oArea, oParent, sType ){
	var oThis = this;

	oThis.oParent = oParent;
	oThis.oData = oArea;
    oThis.bActive = false;
    oThis.aStations = [];

    if(sType != 'branches')
    	oThis.oLink = $('<ins><span id="id_tag_'+oThis.oData.name+'" class="' + oThis.oData.name + ' pseudo">' + oThis.oData.webName + '</span></ins>').appendTo($(oParent.oParams.eTags).find('.'+sType));
    else
    	oThis.oLink = $('<ins><span id="id_branch_'+oThis.oData.id+'" class="' + oThis.oData.name + ' pseudo">' + oThis.oData.webName + '</span></ins>').appendTo($(oParent.oParams.eTags).find('.'+sType));
    	
    	
    for (var i=0; i < oThis.oData.items.length; i++) {
        for (var j=0; j < oParent.aoBranches.length; j++) {
            for (var k=0; k < oParent.aoBranches[j].aoStation.length; k++){
                if(oParent.aoBranches[j].aoStation[k].oData.sID == oThis.oData.items[i]) {
                    oThis.aStations[oThis.aStations.length] = oParent.aoBranches[j].aoStation[k];
                }
            }
        }
    };

    oThis.oLink.click(
        function() {
            if (!oThis.bActive) {
                for (var i=0; i < oThis.aStations.length; i++) {
                    if (!oThis.aStations[i].bSelected) {
                        oThis.aStations[i].pre_toggle();
                    }
                }
                oThis.bActive = true;
                oThis.oLink.addClass('active');
            }
            else {
                for (var i=0; i < oThis.aStations.length; i++) {
                    if (oThis.aStations[i].bSelected) {
                        oThis.aStations[i].pre_toggle();
                    }
                }
                oThis.bActive = false;
                oThis.oLink.removeClass('active');
            }
			oThis.oParent.oParams.OnChange&&oThis.oParent.oParams.OnChange();//SDF7796 = oThis;
        }
    );

}