/**
 * Site specific javascript code.
 *
 * requires jquery to be already loaded
 */

/* ============================ image rotator ============================ */
var Rotator = new Object;

Rotator.operating = false;  // set to true if rotator is 'on'
Rotator.delay = 1500;       // time to transition from one image to the next
Rotator.duration = 4000;    // time a single image stays on view
Rotator.current = 0;
Rotator.nextSlide = -1;
Rotator.slides = new Array();
Rotator.timeoutId = 0;

Rotator.initiate = function() {

    Rotator.slides = $('#image_rotator .underlay');
	if (Rotator.slides.length <= 1) {
	  // not enough slides to rotate ... abort
	  return;
	}
	
	// hide all but the initial slide
	for (var i=1; i < Rotator.slides.length; i++) {
	  $(Rotator.slides[i]).hide();
	}

    // set onClick handler to stop/start rotation
	$('#image_rotator').click( Rotator.toggle );	

	// start rotating
	Rotator.start();	
}

Rotator.start = function() {
    if (Rotator.timeoutId || Rotator.slides.length <= 1) return;
	
	Rotator.operating = true;
    Rotator.timeoutId = setTimeout(Rotator.transition, Rotator.duration);
}
Rotator.stop = function() {
    clearTimeout(Rotator.timeoutId);
	Rotator.timeoutId = 0;
	Rotator.operating = false;
}

Rotator.transition = function() {
    Rotator.timeoutId = 0;

    Rotator.nextSlide = Rotator.current+1;
    if (Rotator.nextSlide >= Rotator.slides.length) {
	    Rotator.nextSlide = 0;
    }

    $(Rotator.slides[Rotator.nextSlide]).show();
    $(Rotator.slides[Rotator.current]).fadeOut(Rotator.delay, function() {
        $(Rotator.slides[Rotator.nextSlide]).css('position','relative');
        $(Rotator.slides[Rotator.current]).css('position','static');
	  
        Rotator.current = Rotator.nextSlide;
        if (Rotator.operating) {
		  Rotator.start();
		}
	});
}
Rotator.toggle = function() {
  if (Rotator.operating) {
    Rotator.stop();
  } else {
    Rotator.start();
  }
}
/* ============================ tour control ============================ */
var Tour = new Object;

Tour.initiate = function() {
	$('#page1').addClass('active_page');
	$('#prev').hide();
	
	$('#btn-take-a-tour').click(function(){
		$('.tour-popup').show();
	}); 
	
	$('#close-tour-popup').click(function(){
		$('.tour-popup').hide();
	})
	
	$('#prev').click(function(){
		Tour.changePage(false);
	});
	$('#next').click(function(){
		Tour.changePage(true);
	});
};

Tour.changePage = function(forward){
	var id = parseInt($('.active_page').attr('id').substring(4));
	var jump_to = forward ? id+1 : id-1;
	
	$('#page' + id.toString()).hide();
	$('.pages').removeClass('active_page');

	$('#page' + jump_to.toString()).show();
	$('#page' + jump_to.toString()).addClass('active_page');

	if (jump_to == $('ul .pages').length) {
		$('#next').hide();
	} else {
		$('#next').show();
	}
	
	if (jump_to == 1) {
		$('#prev').hide();
	} else {
		$('#prev').show();
	}
	$('#counter').html($('#page' + jump_to).attr('name') + '/12');
}		
/* =========================== formlet control ============================ */

/* ============================ document ready ============================ */
$(document).ready( function() {
	// set up new window/tab for rel=external links
	$('a[rel~=external]').attr('target','_blank');
	
	// set up event handlers for login formlets
	$('.formlet input.text').
	  each ( function(i) { 
					  this.label = $(this).parent().children('label').get(0);
					  if (this.value) {
					    $(this.label).hide();
					  }
	  }).
	  focus( function(e) { $(this.label).hide(); }).
	  blur( function(e) { if (!this.value) $(this.label).show(); });
	
	$('.formlet label').
	  click( function(e) { $(this).hide(); $('#'+this.htmlFor).focus(); });

    $('#login .toggle a').
	  click( function(e) { $('#forgot').slideDown('slow'); e.preventDefault(); });

    $('#forgot .toggle a').
	  click( function(e) { $('#forgot').slideUp('slow'); e.preventDefault(); });

    // set up image rotator (home page only)
	if ($('#image_rotator').length) {
	  $.getJSON('/image_rotator.ajax.php', function(json) {
	      if (json.html) $('#image_rotator').append(json.html);
          Rotator.initiate();
      });
	}

    // set up tour (pages with 'take a tour button' and corresponding container
	if ($('#take_a_tour_container').length) {
	  $.getJSON('/take_a_tour.ajax.php', function(json) {
	      if (json.html) $('#take_a_tour_container').append(json.html);
          Tour.initiate();
      });
	}
});
