/* HOMEVIEW */
/* Layout-related Functions
----------------------------------------------------------------------------- */


	var $j = jQuery.noConflict();
	var ie = (navigator.userAgent.match('MSIE') ? true : false);

	$j(document).ready(function() {
		$j('#region ul.tabs').each(center);
		$j('ul.actions.center').each(center);
		$j('#region div.form div').each(center);
		$j('#region ul.tabs li').each(function() { $j(this).fadeIn(1500); });
		$j('#drawer div.form').append('<a class="close" href="#">Close</a>');
		$j('#drawer div.form').append('<a id="draweropenbutton" class="open" href="#">Open</a>');

		$j('#drawer:not(.open)').children('div.form').each(function() {
	
			var height = parseInt($j(this).css('height'));
			var height = height + 100;

			//$j('#drawer div.form label, #drawer ul.actions').hide();

			if ($j(this).children('p').length > 0) {
				closeDrawer(height, true);
				$j('#drawer div.form a.open').show().click(function() { $j('#region a.drawer').click(); });
				$j('#drawer div.form a.close').hide().click(function() { closeDrawer(height, true, true); return false; });
			} else {
				closeDrawer(height, false);
				$j('#drawer div.form a.close').hide().click(function() { closeDrawer(height, false, true); return false; });
			}

			openDrawer(height);
				
		});


	});

	$j(window).load(function() {
	
		if (navigator.userAgent.match('WebKit')) { $j('body').addClass('webkit') }

		$j('#region ul.tabs').each(center);
		$j('#region div.form div').each(center);

		$j('#region a.search').click(function() {
		
			$j('#region').parent().submit();
			return false;
		
		});

/* DEVELOPMENT TAB
----------------------------------------------------------------------------- */


	$j('#role').toggle(function() {
		$j('#role').animate({ 'width': '250px' }, "fast", function() { $j('#role h4').fadeIn(); $j('#role ul').fadeIn(); } );

	}, function() {
		$j('#role').animate({ 'width': '10px' });
    $j('#role h4').hide();
	  $j('#role ul').hide();
	});


/* HEADER ROTATING PROMOS
----------------------------------------------------------------------------- */


	if ($j('#header ul.promos').length > 0) {
		$j('#header ul.promos').cycle({
	
			fx: 'blindY'

		});
	}


/* GOOGLE MAP
----------------------------------------------------------------------------- */


	if ($j('#google').length > 0) {
 
    mapopen = false;
    beenopen = false;
		map = new GMap2(document.getElementById("google"));
		mapControls = new GLargeMapControl3D();
		mapType = new GMenuMapTypeControl();
		mapOptions = new GMapUIOptions();

		mapOptions.controls = {};
		mapOptions.controls.smallzoomcontrol3d = false;
		mapOptions.controls.menumaptypecontrol = false;
		mapOptions.maptypes = {};
		mapOptions.maptypes.normal = true;
		mapOptions.maptypes.hybrid = true;
		mapOptions.maptypes.satellite = true;
		mapOptions.zoom = {};
		mapOptions.zoom.doubleclick = true;
		map.setUI(mapOptions);

		if ($j('#region input.prompt').length > 0) {
	
			map.setCenter(new GLatLng(41.6000000,-95.0000000), 5);

		} else {
	
			map.setCenter(new GLatLng(40.7388319,-73.9815337), 11);
	
		}
				

		$j('<a id="enlarger" class="enlarge" href="#">Enlarge Map</a>').insertBefore('#google');
		$j('<a class="close" href="#">Close Map</a>').insertBefore('#google').hide();
		$j('<div class="fade">&nbsp;</div>').insertBefore('#google');

    addmarkers(map);
    openMapIfNationwide();

		function openMap() {

		  mapopen = true;

			$j('#map').children('#google').unbind('click');		
			$j('#map').animate({ height: "400px" }, 200).addClass('enlarged');
			$j('#google').animate({ height: "400px" }, 200);

			$j('#map a.enlarge').hide();
				
			$j('div.fade').animate({ opacity: "0", height: "400px" }, 250, function() {
			
				$j(this).hide();

				setTimeout(function() {

					map.checkResize();


          var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));
          var topLeftLower = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,40));
					map.addControl(mapControls, topLeftLower);
					map.addControl(mapType, topLeft);


					map.enableScrollWheelZoom();
	
					$j('#map a.close').show();

				if (beenopen == false) {
           map.panTo(bounds.getCenter());
           beenopen = true;
          }

				}, 300);
							
			});

			return false;
		
		}

		$j('#map div.fade').click(openMap);

		$j('#map a.enlarge').click(function() {
		
			openMap();
			return false;
		
		});
		
		$j('#map a.close').click(function() {

      mapopen = false;  
 
			map.removeControl(mapControls);
			map.removeControl(mapType);
			map.disableScrollWheelZoom();
		
			$j('#map').animate({ height: "120px" }, 500);
			$j('#google').animate({ height: "120px" }, 500);
			$j('#map a.close').hide();

			$j('#map div.fade').animate({ height: "120px" }, 500, function() {

				$j('#map').removeClass('enlarged');			
			
			});

			$j('div.fade').show().animate({ opacity: "0.2" }, 500);
			$j('#map').children('#google').click(openMap);
	
			return false;		
		
		});

	}


/* TABBED PANELS
----------------------------------------------------------------------------- */


//	$j('div.panel').tabs();


// End Load Function

	});
	

/* HEADER DRAWER
----------------------------------------------------------------------------- */


	function closeDrawer(height, summary, animate) {

		if (summary == true) {
     
    if (false) {  //would be ie
			$j('#advanced_search_items').fadeOut(function() {
			
				$j('#drawer p').fadeIn();

			});

     } else {
			$j('#advanced_search_items').slideUp(function() {
			
				$j('#drawer p').fadeIn();

			});



    }

		} else if (false) {  //would be ie
		
			$j('#drawer div.form').hide();

		} else if (animate == true) {

			$j('#drawer div.form').animate({ 'marginTop': '-' + height + 'px'}, 500);

		} else {

			$j('#drawer div.form').css('marginTop', '-' + height + 'px');

		}

		$j('#draweropenbutton').fadeIn();
		$j('#drawer div.form a.close').fadeOut();
		$j('#drawer div.form a.open').fadeIn();

	}

	function openDrawer(height) {

		$j('#draweropenbutton').click(function() {
		
			if (false) {  //would be ie
			
				$j('#drawer p').hide();
				$j('#drawer div.form').show();
				$j('#drawer a.close, #advanced_search_items').fadeIn();
				
			} else {

				if ($j('#drawer p').length > 0) {
					$j('#drawer p').hide();
	
						$j('#drawer a.close,  #advanced_search_items').slideDown();
						$j('#drawer div.form').show().animate({ 'marginTop': '-30px'}, 500);			
					
				

				} else {

					$j('#drawer a.close, #drawer ul.actions').show();
					$j('#drawer div.form').show().animate({ 'marginTop': '-30px'}, 500);			
				}

			}

			$j(this).fadeOut();
		  $j('#drawer div.form a.open').fadeOut();
			return false;

		});
	
	}
