var currentSlide = 0;
var change_url = null;
var current_url = '';
var jScrollbars;
var counter = 0;

$(document).ready(function() {

	$('#info').click(function(e) {
		$('body').removeClass('index').toggleClass('info');
		jScrollbars = null;
		scrollbars();
		e.preventDefault();
	});
	
	$('#index').click(function(e) {
		$('body').removeClass('info').toggleClass('index');
		jScrollbars = null;
		scrollbars();
		e.preventDefault();
	});
	
	$('#twitter').click(function(e) {
		window.open('http://twitter.com/intent/follow?screen_name=joshsmithnyc','follow','width=500,height=500');
		e.preventDefault();
	});
	
	$('#supersized').click(function(e) {
		//$('body').removeClass('index info');
		$('#next').click();
		e.preventDefault();
	});
	
	$('#supersized, #next, #prev').click(function(e) {
		if($(this).attr('id') == 'prev') {
		  api.prevSlide();
		} else {
		  api.nextSlide();
		}
		setTimeout(update_page,10);
		e.stopPropagation();
		e.preventDefault();
	});
	$(document.documentElement).keyup(function(e) {
		setTimeout(update_page,10);
	});
	
	$('div.index a').click(function(e) {
		change_to($(this).attr('href'));
		e.stopPropagation();
		e.preventDefault();
	});
	
	scrollbars();

});

$(window).load(function() {

	setTimeout(function(){update_page(location.hash.indexOf('#/') == 0)},10);
	
	$(window).bind('hashchange', function() {
		//change_to(location.hash.substr(1));
	});
	
	$.pathchange.init({interceptLinks: false});
	
	$(window).bind({
		pathchange: function(e) {
		  if (window.location.hash && window.location.hash.substr(1) != current_url) {
		  	change_to(window.location.hash.substr(1));
		  } else if (window.location.pathname != current_url) {
		  	//change_to(window.location.pathname); //causing FF3.5 bugs
		  }
		}
	});
	
}).resize(function() {
  scrollbars();
});

function scrollbars() {
  $target = ($('div.content').is(':visible')) ? $('div.content') : $('div.index');
  
  if ($target.find('ul,article').height()+130 > $(window).height()) {
    $target.css('top','20px');
    if (!jScrollbars) {
      jscrollbar = $target.css('top','20px').jScrollPane({autoReinitialise:true});
      jScrollbars = jscrollbar.data('jsp');
    } else {
      if (jScrollbars) jScrollbars.reinitialise();
    }
  } else {
    $target.css('top','auto');
    if (jScrollbars) jScrollbars.destroy();
    jScrollbars = null;
  }
}

function start(url) {
	start_slide = find_slide(1, url);
	
	$.supersized({
				
  	// Functionality
  	slideshow       :  1,			// Slideshow on/off
  	autoplay				:  0,			// Slideshow starts playing automatically
  	start_slide     :  start_slide,			// Start slide (0 is random)
  	stop_loop				: 0,			// Pauses slideshow on last slide
  	random					:  0,			// Randomize slide order (Ignores start slide)
  	slide_interval  :  10000,		// Length between transitions
  	transition      :   6, 			// 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
  	transition_speed :	500,		// Speed of transition
  	new_window			 :	0,			// Image links open in new window/tab
  	pause_hover      :   0,			// Pause slideshow on hover
  	keyboard_nav     : 1,			// Keyboard navigation on/off
  	performance			 :	1,			// 0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
  	image_protect    :	1,			// Disables image dragging and right click with Javascript
  											   
  	// Size & Position						   
  	min_width		     :   0,			// Min width allowed (in pixels)
  	min_height		   :   0,			// Min height allowed (in pixels)
  	vertical_center  :   1,			// Vertically center background
  	horizontal_center  :   1,			// Horizontally center background
  	fit_always			  :	0,			// Image will never exceed browser width or height (Ignores min. dimensions)
  	fit_portrait      :   1,			// Portrait images will not exceed browser height
  	fit_landscape			:   0,			// Landscape images will not exceed browser width
  											   
  	// Components							
  	slide_links				:	false,	// Individual links for each slide (Options: false, 'number', 'name', 'blank')
  	thumb_links				:	1,			// Individual thumb links for each slide
  	thumbnail_navigation    :   0,			// Thumbnail navigation
  	slides 					  :  slides,
  								
  	// Theme Options			   
  	progress_bar			:	0,			// Timer for each slide							
  	mouse_scrub				:	0
  	
  });
}

function change_to(url) {
	i = find_slide(1, url);
	if (i) {
	 api.goTo(i);
	 update_page();
	}
}

function find_slide(fallback,url) {
	if (!url && location.hash.indexOf('#/') == 0 && location.hash != '#/') url = location.hash.substr(1);
	if (url) {
		for(var i in slides)
		{
			if (url == slides[i].url) {
				start_slide = 1+eval(i);
				return start_slide;
			}
		}
	}
	return fallback;
}

function update_page() {
	i = vars.current_slide;
	$(document).attr('title', $('meta[name=title]').attr('content') + ' | ' + slides[i].page_title);
	var next = (slides[i+1]) ? slides[i+1] : slides[0];
	var prev = (slides[i-1]) ? slides[i-1] : slides[slides.length-1];
	var url = slides[i].url;
	$('#info').attr('href', url);
	$('#slidecaption').html(slides[i].title);
	$('#next').attr('href', next.url);
	$('#prev').attr('href', prev.url);
	$('div.index a').removeClass('active');
	$('div.index a[href="'+url+'"]').addClass('active');
	scrollbars();
	//location.hash = url;
	current_url = slides[i].url;
	if (change_url !== false) $.pathchange.changeTo((($.pathchange.detectHistorySupport()) ? '': '#')+slides[i].url);
	change_url = null;
}
