var NOFLARE = false;
var calendarOpen = false;
var photoSectionOpen = false;

function slideOutElement( element, mode ) {
	if( !mode ) {
		mode = 'vertical';
	}

	if( !NOFLARE ) {
		var thisSlide = new Fx.Slide( element, {mode: mode} );
		thisSlide.slideOut();
	}
	element.addClass( 'hidden' );
}
	 
function slideInElement( element, mode ) {
	if( !mode ) {
		mode = 'vertical';
	}

	if( !NOFLARE ) {
		var thisSlide = new Fx.Slide( element, {mode: mode} );
		thisSlide.hide();
	}
	element.removeClass( 'hidden' );
	if( !NOFLARE ) {
		thisSlide.slideIn();
	}
}

function fadeInElement( element ) {

	var fx = element.effects( { duration: 300, transition: Fx.Transitions.linear } ).set( 0 );
	fx.start( { 'opacity': [ 0, 1 ] } );
	showElement( element );
}

function fadeOutElement( element ) {

	var fx = element.effects( { duration: 300, transition: Fx.Transitions.linear } );
	fx.start( { 'opacity': [ 0 ] } );

	hideElement( element );

}

function hideElement( element ) {
	element.addClass( 'hidden' );
}

function showElement( element ) {
	element.removeClass( 'hidden' );
}

function addRollovers( element, normalColor, rollColor ) {
	var fx = new Fx.Styles( element, { duration: 200, wait: false } );

    if( normalColor == undefined )
        normalColor = '#000';

    if( rollColor == undefined )
        rollColor = '#ff0000';

	element.addEvent('mouseenter', function(){
			fx.start({
					'color': rollColor
						});
		});
	
	element.addEvent('mouseleave', function(){
			fx.start({
					'color': normalColor
						});
		});
	
}

function remoteQuery( url, requestData, func, data, format ) {
	var results = null;
	
	var req = new Ajax( url + '?' + requestData, { method: 'get' } );

	req.addEvent( 'onComplete', function( resp ) {
			results = null;

			if( format == 'json' )
				results = Json.evaluate( resp );

			else if( format == 'raw' )
				results = resp;
			

			if( func != null ) {
				func( results, data );
			}

		} ).request( );

	return req;
}

function setupMenu() {
    var szNormal = 145, szSmall  = 135, szFull = 180;

    var kwicks = $ES("li", 'theList');
    var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
    kwicks.each(function(kwick, i) {
	    kwick.addEvent("mouseenter", function(event) {
		    var o = {};
		    o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
		    kwicks.each(function(other, j) {
			    if(i != j) {
				var w = other.getStyle("width").toInt();
				if(w != szSmall) o[j] = {width: [w, szSmall]};
			    }
			});
		    fx.start(o);

		});
	});
    
    $("theList").addEvent("mouseleave", function(event) {
	    var o = {};
	    kwicks.each(function(kwick, i) {
		    o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
		});
	    fx.start(o);
	})

}

var mapMarkers = new Array(0);

function createMap() {
    var mapContainer = $( 'mapContainer' );

    if( mapContainer ) {
        var baseIcon = new GIcon(G_DEFAULT_ICON);

        /*    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
              baseIcon.iconSize = new GSize(20, 34);
              baseIcon.shadowSize = new GSize(37, 34);
              baseIcon.iconAnchor = new GPoint(9, 34);
              baseIcon.infoWindowAnchor = new GPoint(9, 2);
        */

        var map = new GMap2( mapContainer );
        map.addControl( new GLargeMapControl() );
        map.addControl( new GMapTypeControl() );
        map.setCenter( new GLatLng( 37.871935, -122.265979 ), 15);
        
        $$( '#diningLocations li' ).each( function( el, i ) {
                var letteredIcon = new GIcon( baseIcon );
                var letter = String.fromCharCode( "A".charCodeAt( 0 ) + i );
                letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

                var point = new GLatLng( el.getAttribute( "lat" ), el.getAttribute( "lng" ) );
                var marker = new GMarker( point, { icon: letteredIcon } );
                GEvent.addListener( marker, 'click', function() {
                        mapItemClicked( i );
                         } );
                mapMarkers.push( marker );
                map.addOverlay( marker );
            } );

        $$( '#diningLocations li' ).each( function( el, i ) {
                var nameDiv = el.getChildren()[ 0 ];
                var infoDiv = el.getChildren()[ 1 ];
                
                var infoLink = new Element( 'a' );
                infoLink.innerHTML = "-More Information-";
                infoLink.setAttribute( "href", "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=" + nameDiv.innerHTML + ",+berkeley, ca&sll=&ie=UTF8&z=16&iwloc=A" );

                infoDiv.appendChild( new Element( 'br' ) );
                infoDiv.appendChild( infoLink );
            } );
    }
}

function mapItemClicked( id ) {
    var children = $$( '#diningLocations li' );

    $$( '#diningLocations li' ).each( function( thisLi, i ) {
            if( i != id ) {
                thisLi.getChildren()[ 1 ].addClass( 'hidden' );
                thisLi.removeClass( 'selectedMapItem' );
            }
        } );
                    
    $ES( 'div', children[ id ] ).removeClass( 'hidden' );
    children[ id ].addClass( 'selectedMapItem' );

    mapMarkers[ id ].openInfoWindowHtml( children[ id ].innerHTML );


}

function showPhoto( photo ) {

    var section = $( 'currentPhotoSection' );

    var div = new Element( 'div' );
    var img = new Element( 'img' );
    img.setAttribute( 'src', photo );

    img.addClass( 'currentPhoto' );
    img.addEvent( 'click', function( e ) {
            new Event( e ).stop();
            hideElement( section );
        } );

    div.appendChild( img );

    section.empty();

    section.appendChild( div );

    showElement( section );
}

function enablePhotoLinks() {
    $$( 'a[class=photoLink]' ).each( function( link, i ) {
        link.addEvent( 'click', function( e ) {
                new Event( e ).stop();
                showPhoto( link.getAttribute( 'href' ) );
            } );
        } );
}


function domReady() {
    $$( '#diningLocations li' ).each( function( el ) {
            addRollovers( el, "#000", "#e3b900" ) } );

    $$( '#participantList li' ).each( function( el, i ) {
            if( i % 2 != 1 ) {
                el.addClass( 'zebraRow' );
            }
        } );

    $$( '#staffList li' ).each( function( el, i ) {
            if( i % 2 != 1 ) {
                el.addClass( 'zebraRow' );
            }
        } );

    $$( '#agendaTable tr' ).each( function( el, i ) {
            if( i % 2 != 1 ) {
                el.addClass( 'zebraRow' );
            }
        } );

    $$( '#agendaTable tr td ul li b a' ).each( function( el ) {
            el.addClass( "ToolTip" );
            el.setAttribute( 'title', "Recommended Readings from :: " + el.innerHTML );
        });

    $$( '.personnelList li span a' ).each( function( el ) {
            el.addClass( "ToolTip" );
            el.setAttribute( 'title', "Send Mail to :: " + el.innerHTML );
        });

    new Tips( $$( '.ToolTip' ), { showDelay: 200, hideDelay: 20 } );

    $$( '#attendeeList li' ).each( function( el, i ) {
            if( i % 2 != 1 ) {
                el.addClass( 'zebraRow' );
            }
        } );

    $$( '#readings ul li' ).each( function( el, i ) {
            addRollovers( el, "#000", "#e3b900" );

            if( i % 2 != 1 ) {
                el.addClass( 'zebraRow' );
            }
        } );

    /*
    $$( 'div[class=menuItem] a' ).each( function( el ) {
            addRollovers( el, "#000", "#e3b900" ) } );

    $$( '#readings ul li a' ).each( function( el, i ) {
            addRollovers( el, "#000", "#e3b900" );
        } );
    */
    $$( '#diningLocations li' ).each( function( el, i ) {
            if( i % 2 != 1 ) {
                el.addClass( 'zebraRow' );
            }
            
            el.getChildren()[0].addEvent( 'click', function( e ) {
                    new Event( e ).stop();
                    
                    mapItemClicked( i );
                } );


        } );

    $$( 'a' ).each( function( el ) {
            addRollovers( el, "#000", "#e3b900" );
        } );

    createMap();

    enablePhotoLinks();
}