/* USING WITH WINDOW RESIZE EVENT */
var waitForFinalEvent = (function () {
  var timers = {};
  return function (callback, ms, uniqueId) {
    if (timers[uniqueId]) {
      clearTimeout (timers[uniqueId]);
    }
    timers[uniqueId] = setTimeout(callback, ms);
  };
})();

/* SPINNER PLUGIN */
$.fn.spin = function(opts) {
  this.each(function() {
    var $this = $(this),
        data = $this.data();

    if (data.spinner) {
      data.spinner.stop();
      delete data.spinner;
    }
    if (opts !== false) {
      data.spinner = new Spinner($.extend({color: $this.css('color')}, opts)).spin(this);
    }
  });
  return this;
};

/* MODAL POETRY */
$.fn.modal_poetry = function() {
	$('#poem_container').append($poem_exposer);
	$( "#poem.modal" ).dialog({
		closeOnEscape: true,
		autoOpen: false,
		modal: true,
		width: 775,
		height: $(window).height()-60,
		position: [$('#photo').offset().left,60]
	}).jScrollPane(
		{
			autoReinitialise: 	true,
			contentWidth: 	760
		});

	
}


/* MAIN */
$(document).ready(function() {
	
	/* NAVIGATION */
	$('#nav ul ul ul').closest('li').addClass('disabled');
	$('.disabled>a').live('click',function(e){
		return false;
	})
	
	
	
	/* MODAL POETRY */
	$('#poem').attr('title', $('#poem h3').text());
	$('#poem h3').hide();
	$poem_exposer = $('<span class="exposer">Read Poem</span>');
	$('.exposer').live('click',function()
	{
		$( "#poem.modal" ).dialog("open");
	});
	$(window).modal_poetry();
	$(window).resize(function () 
	{
	    waitForFinalEvent(function()
		{
			$(window).modal_poetry();
	    }, 300, "window_resize_modal_poetry");
	});
		
	
	/* AJAXY GALLERY NAV */
	$('#thumbs a').live('click',function(e)
	{
		var opts = {
		  lines: 12, // The number of lines to draw
		  length: 7, // The length of each line
		  width: 5, // The line thickness
		  radius: 10, // The radius of the inner circle
		  color: '#fff', // #rbg or #rrggbb
		  speed: 1, // Rounds per second
		  trail: 100, // Afterglow percentage
		  shadow: true // Whether to render a shadow
		};
		$spinner = $('#photo').spin(opts);
		$img = 	$('<img src="" alt="" />').hide();
		$('#photo.textbox').empty().removeClass('textbox').append($img);
		$('#thumbs img').removeClass('current');
		e.preventDefault();
		$this = $(this);
		url = $this.attr('href');
		$.getJSON(url, function(data, textStatus, jqXHR)
		{		 
			$('#worktitle .text').html(data[0].title);
			$('#ui-dialog-title-poem').html(data[0].poem_title);
			$('#poem_text').html(data[0].poem.replace(/\n/g, '<br />'));
			$('.exposer').show();
			$('#photo img').attr(
				{
				 src : data[0].images[0].large_image,
				 alt : data[0].title	
				}).filter(":hidden").show();
			$this.children('img').addClass('current');
			data = $spinner.data();
		    if (data.spinner) {
		      data.spinner.stop();
		      delete data.spinner;
		    }
		});
	});
	
	
	/* AJAXY ARTIST'S STATEMENT */
	$('#notes h2').click(function(){
		$this = $(this);
		$('#photo').html($('#statement').html()).prepend('<h2>'+$this.text()+'</h2>').addClass('textbox');
		$('#thumbs img').removeClass('current');
		$('#poem_container .exposer').hide();
	});
	
	
    
	
});
