function init()
{
    generateMap();
    fix();
	$('.nav div a').hover( 
		function() { 
			p = $(this).parent();
			n = p.attr('className');
			p.removeClass( n );
			p.addClass( 's'+n );
		}, 
		function() { 			
			p = $(this).parent();
			n = p.attr('className');
			p.removeClass( n );
			p.addClass( n.substr(1) );
			}
		);
	$('.search input').focus( function() { $(this).val(''); } );
	$('.mi .c a').hover( 
		function() { $(this).parent().parent().toggleClass('sel') },
		function() { $(this).parent().parent().toggleClass('sel') }
	);
	$('.ni').hover( function() { $(this).toggleClass('sel') }, function() { $(this).toggleClass('sel') } );
	$('.si').hover(	function() { $(this).toggleClass('sel') }, function() { $(this).toggleClass('sel') } );
	$('.zoom').click( zoom );

    $('#i_shapes').change( function() { 
	o = $(this).attr('checked') ? 1 : 0; 
	$('.shapes').fadeTo('normal',o); 
	( !o ? $('.cities .clegend').hide() : $('.cities .clegend').show() );
	} 
    );
    $('#i_zones').change( function() { 
	o = $(this).attr('checked') ? 1 : 0; 
	( !o ? $('.cities .zlegend').hide() : $('.cities .zlegend').show() );
	} 
    );    
    $('#i_rivers').change( function() { o = $(this).attr('checked') ? 1 : 0; $('.rivers').fadeTo('normal',o); } );
    $('#i_roads').change( function() { o = $(this).attr('checked') ? 1 : 0; $('.roads').fadeTo('normal',o); } );
    $('.mpzu a').each( function(i,c) 
        {
        $('.banner',$(c).parent()).click( function() { window.location = c.href; } );
        }
    );
    $('#up').click( function() { $(window).scrollTop(0); } );
    $(window).resize( fix );
    $('.watchform .add').click( addWatchFormValidate );	
    $('.patrol .add').click( function() { window.location = "/watch/?a=add"; } );
}

function fix( e )
{
    if( $('.mc').outerWidth() <= 960 && $('body').width() <= 960 )
	$('.mc').width( 960 );
    else
	$('.mc').css( 'width', '100%' );
    if( $('.lftc').outerWidth() < 215 )
	$('.lftc').width(215);
    else
	$('.lftc').css( 'width', '22%' );
    $('#whbg').width( $('.mc').width() );
    $('#whbg').height( $('.mc').height() );
    $('#map').css('left',Math.floor( ($('.mc').width()-$('#map').width())/2) );    
}

function zoom()
{
    $('body').append( '<div id="whbg"></div>' );
    $(window).scroll( function() 
	{ 
	$('.cpanel').clearQueue();
	$('.cpanel').animate( { top: Math.floor($(window).scrollTop()+($(window).height()-$('.cpanel').height())/2) }, 1000 ); 
	} 
    );
    $('#arr').click( function() { 
	o = $('.cpanel').position(); 
	if( o.left )
    	    {
	    $(this).removeClass('rt');
	    $(this).addClass('lf');
	    $('.legends').removeClass('lft');
	    $('.cpanel').animate( { left: 0 }, 'fast' ); 
	    }
	else
	    {
	    $(this).removeClass('lf');
	    $(this).addClass('rt');
	    $('.legends').addClass('lft');
	    $('.cpanel').animate( { left: 620 }, 'fast' ); 
	    }
	} 
    );
    $('#whbg').css('width',$('.mc').width() );
    $('#whbg').css('height',$('.mc').height() );
    $('#map').css('left',Math.floor( ($(window).width()-$('#map').width())/2) );
    $('#close').click( closeMap );
    $('#whbg').click( closeMap );
    $('#whbg').fadeTo('normal',0.9);
    $('#map').show(); 
    


    alignMap();	
}

function closeMap()
{
	$('.mc').css('overflow-y','');
	$('.mc').css('height','');	
	$('#map').hide();
	$('#whbg').fadeTo('normal',0,function() { $('#whbg').remove(); $(window).unbind('scroll'); } );
}

eprts = Array( 'ua', 'dn', 'greenparty', '@' );
function putEmail( what )
{
    s = '';
    for( i=eprts.length; i>0; s += eprts[--i] + (i==eprts.length-1?'':'.') );
    s = what+s.substr(0,s.length-1);
    document.write( '<a href="mailto:'+s+'">'+s+'<\/a>' );
}

cityMap = null;
function generateMap()
{
    if( !cityMap || !cityMap.length )
	return;
	
    cp = $('#map .cities');
    rmap = $('.region .map' );
    for( i=0; i<cityMap.length; i++ )
	{
	s = '<div class="clegend cname '+cityMap[i].cl+'" id="city_' + cityMap[i].id + '"><div></div></div>';
	cp.prepend( s );
	o = $('#city_'+cityMap[i].id,cp);
	$('div',o).html( '<a href="/watch/?a=add&cid='+cityMap[i].id+'">'+cityMap[i].name+'</a>' );
	o.css('top',cityMap[i].top);
	o.css('left',cityMap[i].left);
	
	if( cityMap[i].mess )
	    {
	    idx = 1;
	    idx = cityMap[i].mess >= 3 ? 2 : idx;
	    idx = cityMap[i].mess >= 25 ? 3 : idx;
	    idx = cityMap[i].mess >= 50 ? 4 : idx;	    
	    s = '<div class="z'+idx+'" id="z'+cityMap[i].id+'"><a href="/watch/?a=fltr&cid=' + cityMap[i].id+'">'+cityMap[i].mess + '</a></div>';
	    rmap.prepend( s );
	    o = $('#z'+cityMap[i].id,rmap);
	    o.css( 'top', Math.floor(cityMap[i].top/3-o.height()/2) );
	    o.css( 'left', Math.floor(cityMap[i].left/3-o.width()/2)+15 );
	    s = '<div class="zlegend z'+idx+'" id="zl'+cityMap[i].id+'"><a href="/watch/?a=fltr&cid=' + cityMap[i].id+'">'+cityMap[i].mess + '</a></div>';
	    cp.prepend( s );
	    o = $('#zl'+cityMap[i].id,cp);
	    o.css( 'top', Math.floor(cityMap[i].top-o.height()/2) );
	    o.css( 'left', Math.floor(cityMap[i].left-o.width()/2) );
	    }
	}
}

mapAligned = false;
function alignMap()
{
    if( !cityMap || !cityMap.length || mapAligned )
	return;
    for( i=0; i<cityMap.length; i++ )
	{
	o = $('#city_'+cityMap[i].id);
	p = o.position();
	if( cityMap[i].align == 'r' )
	    {
	    o.css( 'top', Math.floor(p.top-o.height()/2) );
	    o.css( 'left', p.left+5 );
	    }
	if( cityMap[i].align == 'l' )
	    {
	    o.css( 'top', Math.floor(p.top-o.height()/2) );
	    o.css( 'left', p.left-o.width()-5 );
	    }	    
	if( cityMap[i].align == 'u' )
	    {
	    o.css( 'top', (p.top-o.height()) );
	    o.css( 'left', Math.floor(p.left-o.width()/2) );	    
	    }	    
	if( cityMap[i].align == '' )
	    {
	    o.css( 'left', Math.floor(p.left-o.width()/2) );
	    }
	o = $('#zl'+cityMap[i].id);
	if( o.html() )
	    {
	    p = o.position();
	    o.css('left',Math.floor(p.left-o.width()/2) );
	    o.css('top',Math.floor(p.top-o.height()/2) );	    
	    }
	}
    mapAligned = true;
}

function addWatchFormValidate( e )
{
    frm = $('form.addwatch');
    city = $('#city');
    if( !(1*city.val()) )
	{
	city.focus();
	e.stopPropagation();
	return;
	}
    nm = $('input.fname');
    if( !nm.val() )
	{
	nm.focus();
	e.stopPropagation();
	return;	
	}
    title = $('#short');
    if( !title.val() )
	{
	title.focus();
	e.stopPropagation();
	return;	
	}    
    full = $('#message');
    if( !full.val() )
	{
	full.focus();
	e.stopPropagation();
	return;	
	}    
    frm.submit();
}
