/* DIRECTIONS */
var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("mapCanvas"));
		gdir = new GDirections(map, document.getElementById("directionsTable"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);

		setDirections($('#fromAddress').val(), $('#toAddress').val() );
	}
}

function setDirections(fromAddress, toAddress) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": "en_US" });
}

function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("We're sorry. That address could not be found.The address might be relatively new, or it may be incorrect. Please double check it.");
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("We're sorry. The directions could not be retrieved.");

else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("We're sorry. The directions could not be retrieved. Please double check the address you entered and try again.");

// else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong
// alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("We're sorry. The directions could not be retrieved.");

else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("We're sorry. The directions could not be retrieved.");

else alert("We're sorry. The directions could not be retrieved.");

}

function onGDirectionsLoad(){
//	$('#directionsDetails').removeClass('hidden');
}

/* END DIRECTIONS */

$(document).ready(function(){
	
	$('.photosBlock li a').click(function(i){
		var photosBlock = $(this).closest('.photosBlock');

		$('.displayed', photosBlock).removeClass('displayed');
		$('img', this).addClass('displayed');

		if ( $('.photosPhoto', photosBlock).length == 0 )
		{
			$('<div class="photosPhoto"  style="display: none;"><p class="center"><img src="' + $(this).attr('href') + '" /></p><p>' + $('img', this).attr('alt') + '</p></div>').insertBefore( $('.photosBlock .instructions:first') ).slideDown();

		 	$('.photosBlock .instructions').hide();
		 	$('.photosBlock .photosToggle').show();
		}
		else
		{
			$('.photosPhoto', photosBlock).html('<p class="center"><img src="' + $(this).attr('href') + '" /></p><p>' + $('img', this).attr('alt') + '</p>');
		}

		$('.photosBlock .photosToggle a')
		.unbind('click')
		.click(function(){
			$('.photosPhoto', photosBlock).slideUp('normal', function(){$(this).remove()});
			$('.photosBlock .instructions').show();
			$('.photosBlock .photosToggle').hide();
			$('.displayed', photosBlock).removeClass('displayed');
			return false;
		});

		return false;
	});
	
	if ( $('#shareLinks .shareBlockToggle').length == 1 )
	{
		var shareBlockLeft = parseInt($('#shareLinks .shareBlockToggle').position().left) + parseInt($('#shareLinks .shareBlockToggle').width()) - $('#shareBlock').width();
		
		$('.shareBlockToggle').click(function(){
			if ( $('#shareBlock').is(':hidden') )
			{
				$('#shareBlock')
				.css('top', $('#shareLinks .shareBlockToggle').position().top - 2 )
				.css('left', shareBlockLeft)
				.slideDown('fast');
			}
			else
			{
				$('#shareBlock').slideUp('fast');
			}
			
			return false;
		});
	}

//	var searchBlockLeft = Math.round(parseInt($('#directory .searchToggle').position().left) + (parseInt($('#directory .searchToggle').width())/2) - ( $('#searchFormWrapper').width()/2 ));

//	var searchBlockTop = parseInt($('#directory .searchToggle').position().top) + parseInt($('#directory .searchToggle').height()) + 25;

	$('.searchToggle').click(function(){
		$('#categoriesWrapper').hide();
		if ( $('#searchFormWrapper').is(':hidden') )
		{
			$('#searchFormWrapper')
			.css('top', searchBlockTop )
			.css('left', searchBlockLeft)
			.slideDown('fast');
		}
		else
		{
			$('#searchFormWrapper').slideUp('fast');
		}
		
		return false;
	});
	
//	var categoriesBlockLeft = Math.round(parseInt($('#directory .categoriesToggle').position().left) + (parseInt($('#directory .categoriesToggle').width())/2) - ( $('#categoriesWrapper').width()/2 ));

//	var categoriesBlockTop = parseInt($('#directory .categoriesToggle').position().top) + parseInt($('#directory .categoriesToggle').height()) + 25;

	$('.categoriesToggle').click(function(){
		$('#searchFormWrapper').hide();
		if ( $('#categoriesWrapper').is(':hidden') )
		{
			$('#categoriesWrapper')
			.css('top', categoriesBlockTop )
			.css('left', categoriesBlockLeft)
			.slideDown('fast');
		}
		else
		{
			$('#categoriesWrapper').slideUp('fast');
		}
		
		return false;
	});

	if (!$.browser.msie || !$.browser.version.substr(0,1)<7) {
		$('#contactBlock iframe').show();
	}
	else
	{
		$('#contactBlock img').show();
	}
	
	$('.directionsToggle')
	.click(function(){
		if ( $('.address', this).length == 1 )
		{
			//window.scrollTo(0,0);
			var toAddress = $('.address', this).val();
			if ( $('#toAddress').val() == toAddress )
			{
				$('#directions').slideToggle('fast');
			}
			else
			{
				$('#directions').slideDown('fast', function(){
					$('#toAddress').val( toAddress );
					$('p.toAddress')
					.text( toAddress )
					.css('background', '#EDB832')
					.animate({ backgroundColor: "#FFF" }, 1500);;
				});
			}
		}

		return false;
	});
	
	$('#showMore')
	.click(function(){
		if(!$('#aboutMore').hasClass('shown')){
			$('#aboutMore').slideDown('slow', function(){			
				$('#showMore').html('See less about me');
				$('#aboutMore').addClass('shown');
			});
		}else{
		
			$('#aboutMore').slideUp('slow', function(){			
				$('#showMore').html('See more about me');
				$('#aboutMore').removeClass('shown');
			});
		}
	});
	
	$('.smallImage').mouseenter(function(){
		
		$('#bigImage').attr('src', $(this).attr('src'));
		$('#itemTitle').text($(this).attr('alt'));
		$('#itemPrice').text($(this).attr('price'));
		$('#itemDesc').text($(this).attr('desc'));
		$('#bigImgSrc').attr('href', $(this).closest('a').attr('href'));
		
	});
	
	$('.directionsClose').click(function(){
		$('#directions').slideToggle('fast');

		return false;
	});

	$('#directionsForm').submit(function(){
		if ( 'block' != $('#directionsDetails').css('display') )
		{
			$('#directionsDetails').show(1, function(){
				initialize();
			});
			
		}
		else
		{
			setDirections($('#fromAddress').val(), $('#toAddress').val());
		}		
		
		return false;
	});
	
	$('#contactForm').submit(function(){
		var errorFlag = false;
		
		$('#contactForm input.error').removeClass('error');
		$('#contactForm p.error').remove();
		
		$('#contactForm dd input').each(function(){
			if ( '' == $(this).val() ) {
				$(this).addClass('error');
				errorFlag = true;
			}
		});

		if (errorFlag) {
			$('<p class="error">Please fill out all the fields.</p>').prependTo('#contactForm');
			return false;
		}
		
		$.ajax({
		type: "POST",
		url: $(this).attr('action'),
		data: $(this).serialize(),
		success: function(){

			$('#contactForm').html('<p class="successMessage">Thank you for contacting us. We\'ll get back to you as soon as possible.</p>');
		},
		error: function(){

			$('#contactForm').html('<p class="successMessage">We\'re sorry, your email could not be delivered.</p>');
		}
		});
		
		return false;
	});
	
	$('#contactButton a').click(function() {
		setTimeout(function(){
			$('#contactBlock .block:has(#contactForm)')
			.css('background', '#CFC')
			.animate({ backgroundColor: "#FFF" }, 1500);
		}, 150);
	});
	
	$('.addressMap').click(function() {
		setTimeout(function(){
			$('#contactBlock .block:has(#contactMap)')
			.css('background', '#CFC')
			.animate({ backgroundColor: "#FFF" }, 1500);
		}, 150);
	});
	
	$("div.scrollable").scrollable({
		size: 4,
		clickable: false,
		keyboard: false
	});
	$("div.scrollImage").scrollable({
		size: 1,
		clickable: false,
		keyboard: false
	});

});
