$(document).ready(function(){
	
	//WINDOW RESIZE CODE
	$(window).load(resizeWindow);
	$(window).resize(resizeWindow);
	
	
	//SHOW HIDE DRAWER CODE
	$('div.content').hide();
	$('div.title, h1 span').bind('click', toggleContent);
	
	
	//START WITH RESTAURANTS WINDOW OPEN
	$('a.properties').addClass('active');	
	
	
	//SET DEFAULT MAPTYPE CODE
	$('a.simplemap').addClass('active');

	$('#results').css('right','15px');
	
	//LOADING ANIMATIONS	
	$('#topbar').fadeIn('slow', function(){
	
		$('#bottomContent').fadeIn('slow', function(){
			
			$('#information').slideDown('500', function() {
			
				$('#legend').fadeIn('slow');
				$('#results').fadeIn('slow');
				
				$('#information').animate({opacity: 1.0}, 3000, function(){

					if (window.innerWidth) { 
						var w = window.innerWidth;
					}
					else {
						var w = document.documentElement.offsetWidth;
					}
					
					w1 = w - 325;
					
					var resetPosition = w1 + 'px';	
				
					$('#information').slideUp(500);
					$('#legend').animate({ top: '38px', left: '15px' }, 500 );
					$('#results').animate({ top: '38px', left: resetPosition }, 500 );
				
				});
			
			});	
		
		});	
	
	});


	//TOGGLE INFORMATION & HELP
	$('a.information').bind('click', informationToggle);

	//SHOW PROPERTIES
	$('#results h2').dblclick(function(){
		$('div.results').toggle();
		
		//SHOW
		if ($('#showProperties').html() == 'Hide Properties')
		{
			$('#showProperties').html('Show Properties');
		}
		
		//HIDE
		else
		{
			$('#showProperties').html('Hide Properties');
		}
	});


	//LEGEND
	$('#legend ol li:last').css('margin','0');


	//RESTAURANTS
	$('a.properties').click(function(){
		$(this).toggleClass('active');
		$('#results').toggle();
	});
	
	
	//SHOW & HIDE MULTIPLE RESTAURANTS or CHAINS
	$('#results div.results ul li.parent a').click(function(){
		
		//SHOW
		if ($(this).children('span').html() == '+')
		{
			$(this).children('span').html('-');
			$(this).parent().children('ol').show();
		}
		
		//HIDE
		else
		{
			$(this).children('span').html('+');
			$(this).parent().children('ol').hide();
		}		
	});	


	//SHOW & HIDE RESTAURANTS LIST
	$('#showProperties').click(function(){
		$('div.results').slideToggle();
		
		//SHOW
		if ($(this).html() == 'Hide Properties')
		{
			$(this).html('Show Properties');
		}
		
		//HIDE
		else
		{
			$(this).html('Hide Properties');
		}		
	});
	
	
	//HIDE RESTAURANTS X BUTTON
	$('#results h2 span').click(function(){
		$('#results').hide();
		$('a.properties').removeClass('active');
	});
	

	//HIDE TOP&BOTTOM LINES
	$('#results div.results ul li:first').css('border-width','0 0 1px 0');
	$('#results div.results ul li:last').css('border-width','1px 0 0 0');
	
	$('#legend ol li:first').css('border-width','0');
	$('#legend ol li:last').css('margin','0 0 -3px 0');
		

	//HIDE DIRECTIONS BY DEFAULT 
	$('#directions').hide();
	

	//HIDE DIRECTIONS X BUTTON
	$('#directions h2 span').click(function(){
		$('#directions').hide();
	});

	$('#directions h2').dblclick(function(){
		$('div.directions').toggle();
	});
	
	
	//SIMPLEMAP BUTTON CODE
	$('a.simplemap').click(function(){
		$(this).addClass('active');
		
		$('a.satellite').removeClass('active');
		$('a.hybrid').removeClass('active');
		
		map.setMapType(G_NORMAL_MAP);
		return false;
	});
	
	
	//SATELLITE BUTTON CODE
	$('a.satellite').click(function(){
		$(this).addClass('active');
		
		$('a.simplemap').removeClass('active');
		$('a.hybrid').removeClass('active');
		
		map.setMapType(G_SATELLITE_MAP);
		return false;
	});
	
	
	//HYBRID BUTTON CODE
	$('a.hybrid').click(function(){
		$(this).addClass('active');
		
		$('a.simplemap').removeClass('active');
		$('a.satellite').removeClass('active');
		
		map.setMapType(G_HYBRID_MAP);
		return false;
	});
	
	
	//DRAG RESULTS WINDOW
	$('#results').jqDrag('h2');
	

	//DRAG LEGEND WINDOW
	$('#legend').jqDrag('h2');
	
	
	//DRAG DIRECTIONS WINDOW
	$('#directions').jqDrag('h2');


	//SLIDER CODE
	$('#zoomlevel li').hover(function(){
		$(this).children('ul').fadeIn('slow');
		$('a.zoomlevel').addClass('active');
		
		},function(){
		
		$('a.zoomlevel').removeClass('active');
		
		if (document.all) {
			$('#zoomlevel li').hoverClass('sfHover');
			$('#choosezoomlevel').hoverClass('hover');
		}
	});

	//ZOOMSLIDER CODE
	$('.slider').Slider({
		accept: '.indicator',
		fractions: 18,
		onSlide: function( cordx, cordy)
			{
				map.setZoom(Math.abs(Math.round((18/100)*cordy) - 18));
			}
	});
	
	$('.slider').SliderSetValues(
		[
			[50,50]
		]
	);
	
});


$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover(
			function() { $(this).addClass(c); },
			function() { $(this).removeClass(c); }
		);
	});	
};


function zoomIn(){ map.zoomIn(); }

function zoomOut() { map.zoomOut(); }

function setZoomSlider(newZoom) 
	{
		alert(newZoom);
		newZoomY = Math.abs(Math.round(newZoom / 18 * 100));
		alert(newZoomY);
		$('.slider3').SliderSetValues(
			[
				[50,newZoomY]
			]
		);
	
	}

//TOGGLE DRAWER
var toggleContent = function(e)
	{
		
		var targetContent = $('div.content');
		
		//SHOW
		if (targetContent.css('display') == 'none') {
			targetContent.slideDown(300);
			$('h1').addClass('active');
			
			resizeWindow2();
			
			$('select#type').focus();
			
			$('#showProperties').html('Show Properties');
			$('div.results').slideUp();			
			
		}
		
		//HIDE
		else {
			targetContent.slideUp(300);
			$('h1').removeClass('active');
			
			resizeWindow();
			
			$('#showProperties').html('Hide Properties');
			$('div.results').slideDown();			
		}
		
		return false;
	};


//TOGGLE INFORMATION
var informationToggle = function(e)
	{
	
		if ($('#information').css('display') == 'none') {
		
			if (window.innerWidth) { 
				var w = window.innerWidth;
			}
			else {
				var w = document.documentElement.offsetWidth;
			}
			
			w1 = w - 325;
			
			var resetPosition = w1 + 'px';		
							
			$('#information').slideDown(500);
			$('#legend').animate({ top: '98px', left: '15px' }, 500 );
			$('#results').animate({ top: '98px', left: resetPosition }, 500 );		
		
		}
		
		else {
		
			if (window.innerWidth) { 
				var w = window.innerWidth;
			}
			else {
				var w = document.documentElement.offsetWidth;
			}
			
			w1 = w - 325;
			
			var resetPosition = w1 + 'px';	
		
			$('#information').slideUp(500);
			$('#legend').animate({ top: '38px', left: '15px' }, 500 );
			$('#results').animate({ top: '38px', left: resetPosition }, 500 );		
		
		}
		
		return false;
		
	};


//RESIZE WINDOW FOR DRAWER CLOSED
var resizeWindow = function(e)
	{
		if (window.innerHeight) { 
			var h = window.innerHeight;
		}
		else {
			var h = document.documentElement.offsetHeight;
		}
		
		h = (h < 113) ? 0 : h - 113;
		
		var mapHeight = h + 'px';
		
		$('#map').animate({ height: mapHeight }, 500 );
	};


//RESIZE WINDOW FOR DRAWER OPEN
var resizeWindow2 = function(e)
	{
		if (window.innerHeight) { 
			var h = window.innerHeight;
		}
		else {
			var h = document.documentElement.offsetHeight;
		}
		
		h = (h < 235) ? 0 : h - 235;
			
		var mapHeight = h + 'px';
		
		$('#map').animate({ height: mapHeight }, 500 );
	};