jQuery(document).ready(function() {
jQuery('#seeMore a').click(function() { getPosts(this); return false; } );
jQuery('#intro+a').click(function() { 
								jQuery(this).addClass('hidden'); 
								jQuery('#about').removeClass('hidden'); 
								return false;
							});
setUpScroller();
setUpMap();
setUpContactLink();
});

document.scrolling = false;

function getWindowTop()
{
	var top = document.documentElement.scrollTop;
	if(!top)
		top = window.pageYOffset;
	
	return top;
}

function setUpScroller()
{
	jQuery(window).scroll(function() {
		var top = getWindowTop();
		if((top+document.documentElement.clientHeight+200) >= document.body.clientHeight)
			jQuery('#seeMore a').click();
	});
}

function clearScrollVar()
{
	document.scrolling = false;
}

function getPosts(link)
{
	if(!document.block)
	{
		jQuery(link).addClass('loading');
		
		document.block = true;
		var posts = '/ajax-handler/';
		var pageNum = jQuery(link).attr('href').replace(/[^0-9]/g,'');
		jQuery.get(posts, { page: pageNum, action: 'getPosts'}, function(data) { 
																	jQuery('#items').append(data); 
																	if(parseInt(pageNum)+1 <= document.maxPages)
																		jQuery(link).removeClass('loading').attr('href','./?page='+(parseInt(pageNum)+1));
																	else
																	{
																		jQuery(link).replaceWith('<a href="#top" title="Back to Top">Back to Top</a>');
																		jQuery('#seeMore a').click(function() {
																				var top = getWindowTop();
																				if(top && !document.scrolling)
																				{
																					document.scrolling = true;
																					jQuery.scrollTo(0,1000,{onAfter: function() {document.scrolling = false;}}); 
																				}
																				return false;
																			});
																		jQuery(window).unbind('scroll');
																	}
																	var pos = jQuery('#items .content:last').prev().attr('offsetTop');
																	//jQuery.scrollTo(pos,500, {onAfterFirst: 'setUpScroller'});
																	document.block = false;
																	//window.location = '#'+jQuery('#items h2:last').attr('id');
																});
	}
}

function setUpMap()
{
	 var latlng = new google.maps.LatLng(54.982585,-1.656508);
	    var myOptions = {
	      zoom: 15,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP,
	      mapTypeControl: false
	    };
	    var map = new google.maps.Map(document.getElementById("googleMap"),
	        myOptions);
	    var image = '/wp-content/themes/theroundhouse-holding/images/rhPin.png';
	    var marker = new google.maps.Marker({
	    	icon: image,
	        position: latlng, 
	        map: map, 
	        title:"The Roundhouse"
	    });
}

function setUpContactLink() 
{
	jQuery('a[title="Contact Us"]').click(function() {
		jQuery('#intro').toggleClass('hidden');
		jQuery('#contact').toggleClass('hidden');
		if(jQuery('#contact').hasClass('hidden'))
			jQuery(this).html('Contact');
		else
			jQuery(this).html('Show Intro');
		setUpMap();
		var top = getWindowTop();
		if(top && !document.scrolling)
		{
			document.scrolling = true;
			jQuery.scrollTo(0,1000,{onAfter: function() {document.scrolling = false;}}); 
		}
		return false;
	});
	
	jQuery('#showIntro').click(function() {
		jQuery('#intro').removeClass('hidden');
		jQuery('#contact').addClass('hidden');
		jQuery('a[title="Contact Us"]').html('Contact');
		var top = getWindowTop();
		if(top && !document.scrolling)
		{
			document.scrolling = true;
			jQuery.scrollTo(0,1000,{onAfter: function() {document.scrolling = false;}});  
		}
		return false;
	});
}
