// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var timerID = 0
var controlsVisible = true;


function start_slideshow_timer(photo_id, seconds) {
	clearTimeout(timerID);
	timerID = setTimeout("next_slide("+ photo_id + ")", seconds * 1000);
}

function next_slide(photo_id) {
	if(Ajax.activeRequestCount == 0){
	 	new Ajax.Request('/categories/photo/' + photo_id, { method: 'post' });
	}
}

function stop_slideshow() {
	clearTimeout(timerID);
}

function bubbledFromChild(element, event)  {
  var target = $(event).element();
  if (target === element) target = event.relatedTarget;
  return (target && target.descendantOf(element));
}

// this stuff helps deal with those terrible double clicks

 // initialize ajax state
 var ajax_in_use = false;
  
 // turn ajax calls off
 function start_ajax(ajax_in_use) {
     document.body.style.cursor = 'wait';
     return true;
 }
  
// allow ajax calls
 function end_ajax(ajax_in_use) {
     document.body.style.cursor = 'default';
     return false;
 }


// event observers go here!

document.observe('dom:loaded', function() {
	// Makes the controls appear when mouse is over the slideshow div
	$('slideshow').observe('mouseover', function(event){
		if(!bubbledFromChild(this, event)){
			if(!controlsVisible) {
				$('slideshow_navigation').appear({duration: 0.25});
				controlsVisible = true;
				return false;
			}
		}
	});
	
	$('slideshow').observe('mousemove', function(event){
		if(!bubbledFromChild(this, event)){
			if(!controlsVisible) {
				$('slideshow_navigation').appear({duration: 0.25});
				controlsVisible = true;
				return false;
			}
		}
	});
	
	// Makes the controls disappear
	$('slideshow').observe('mouseout', function(event){
		if(!bubbledFromChild(this, event)) {
			if(controlsVisible) {
			//$('myList').hide();
				$('slideshow_navigation').fade({duration: 0.25});
				controlsVisible = false;
				return false;
			}
		}
	});
	
	// Initializes controls by fading them
	$('slideshow_navigation').fade({duration: 3.0});
	
	// Starts the slideshow on page load
	var url = $('slideshow_control').down('a').readAttribute('onclick').match(/\/.*\/[0-9]*\'/)[0].replace("'","");
	new Ajax.Request(url, { method: 'post'});
	
	// Makes sure the controls will appear when image is clicked	
	$('slide_one','slide_two').each(function(s){
		s.observe('click', function(event){
			//if(!controlsVisible) {
				$('slideshow_navigation').appear({duration: 0.25});
				//controlsVisible = true;
				return false;
			//}
		});
	});
});



