$(document).ready(function(){
	
	// >> following fix added by tjs 20101103 11:20am
	// small fix for old safari versions compatibility

	// this bug occures 
	// on iPhone OS since 3.2 version (iPad)
	// and on iOS since 4.0 version

	// as of 1.4.2 the mobile safari reports wrong values on offset()
	// http://dev.jquery.com/ticket/6446
	// remove once it's fixed
	
	if(/webkit.*mobile/i.test(navigator.userAgent)
	&& "getBoundingClientRect" in document.documentElement
	&& parseFloat(/OS (.*) like/ig.exec(navigator.userAgent)[1].replace('_', '.')) < 4.1) {
		(function ($) {
			$.fn.offsetOld = $.fn.offset;
			$.fn.offset = function () {
				var result = this.offsetOld();
				result.top -= window.scrollY;
				result.left -= window.scrollX;

				return result;
			};
		})(jQuery);
	}
	// >> END of fix
	
	///////////// THNK SAY CONTROLS ///////////////
	// TODO: DRY these handlers. They could share functions.
	// article titles link to whole article via ajax
	$('#think-say h3 a, #think-say .thumb-img a, #think-say .readmore a').live('click', function(){
		var link = $(this).attr('href') ;
		var link_parts = link.split('/');
		var rel = link_parts.slice(link_parts.length-3, link_parts.length).join('/');
		link_parts.splice(link_parts.length-1, 0, 'article');
		link = link_parts.join('/');
		$.get(link, {}, function(data){
			if($.support.leadingWhitespace){
				// webkit, ff, opera etc.
				$('#think-say > section').replaceWith(data);
			}else{
				// if IE6-8 (9?)
				$('#think-say > section').replaceWith(innerShiv(data, false));
			}
			
			$('#think-say .prev-next').remove();
			$('#think-say h2').after("<p class='prev-next' style='float:left; margin-top: 10px; padding-bottom: 14px;'>&larr; <a href='/' class='back g-book'>back to think/say</a></p>");
			$('#think-say > section').after("<p class='prev-next' style='float:left;'> &larr; <a href='/' class='back g-book'>back to think/say</a></p>");
			$('#think-say h2').hide();
			// tjs: move to position with nav bar at top of page.
			$('html, body').animate({ scrollTop: $("#nav_top").offset().top-15 }, 500);

		});
		return false;
	});
	
	// return to full article list
	$('#think-say .back').live('click', function(){
		var link = '/think_say/article_list';
		$.get(link, {}, function(data){
			if($.support.leadingWhitespace){
				// webkit, ff, opera etc.
				$('#think-say > section').replaceWith(data);
			}else{
				// if IE6-8 (9?)
				$('#think-say > section').replaceWith(innerShiv(data, false));
			}
			$('#think-say .prev-next').remove();
			$('#think-say h2').show();
			$('html, body').animate({ scrollTop: $("#nav_top").offset().top-15 }, 500);
		});
		return false;
	});

});

var GT = {
	substituteFonts: function(selector){

	},
	styleFirstLine: function(paragraph, wrapperTag){

	}
};

////////////////////////////////////////////////////////////////
////////////////////// UTILITY FUNCTIONS ///////////////////////
////////////////////////////////////////////////////////////////

function updateSlideCount(scrollable){
	var counter = $('#main #slide-count');
	var index = scrollable.getIndex();
	// counter.text(( index + 1) + '/' + scrollable.getSize());
	counter.text(( index + 1) + ' of ' + scrollable.getSize());
	if(scrollable.getConf()['slides'].length > 0){
		counter.css('color', scrollable.getConf()['slides'][index]['color']);
	}
}

// disable a scrollable element given a selector.
// hide the prev and next elements
function disableScrollable(selector){
	var scrollable = $(selector);
	scrollable.find('.prev').hide();
	scrollable.find('.next').hide();
}


///////////////////////////////////////////////////////////////
///////////// OVERRIDE SCROLLABLE FUNCTIONALITY ///////////////
///////////////////////////////////////////////////////////////

// inject 'slides' configuration parameter
$.tools.scrollable['conf']['slides'] = [];



