var num_photos = 34;
var current_photo_index = 27;
var PHOTO_DIMMED_OPACITY = 0.88; // 0.72
var PHOTO_INOUT_DURATION = 0.8;
var CONTENT_INOUT_DURATION = 0.2;
var GALLERY_CONTROLS_INOUT_DURATION = 0.2;

window.onload = function () {
	$('feature_img').onload = function () {
   		$('feature_img').appear({duration: 0.8});
	};
	show_content('home');
};

function m2(user,host,label) {
     // the obfuscation with var a below is to allow this to validate with W3C
     var a = '<!-- -->';
     if (!host) host = 'urbankitchensandbaths.com';
     if (!label) label = user + '@' + host;
     document.write(a.charAt(0) + 'a href="' + 'mai' + 'lto:' + user + '@' + host + '">');
     document.write(label);
     document.write(a.charAt(0) + '\/a>');
}

function update_photo_numbering ()
{
   $('photo-numbering').innerHTML = "" + (current_photo_index+1) + " / " + num_photos
}

function next_photo ()
{
   current_photo_index = (current_photo_index + 1) % num_photos;
   $('feature_img').fade({duration: PHOTO_INOUT_DURATION,
   	afterFinish:
   	  function (e) {
   	  	$('feature_img').src='img/feature/'+(current_photo_index+1)+'.jpg';
        update_photo_numbering();
   	  }
   });
}

function prev_photo ()
{
   current_photo_index = (current_photo_index - 1 + num_photos) % num_photos;
   $('feature_img').fade({duration: PHOTO_INOUT_DURATION,
   	afterFinish:
   	  function (e) {
   	  	$('feature_img').src='img/feature/'+(current_photo_index+1)+'.jpg';
   	  	update_photo_numbering();
   	  }
   });
}


var current_content = "";

function update_menu (old_id, new_id)
{
   o = $("menu-"+old_id);
   if (o) {
   	  o.removeClassName("selected");
   }
   p = $("menu-"+new_id);
   if (p) {
   	  p.addClassName("selected");
   }
}

function show_content (id)
{
   // asking for the same content already present toggles to gallery mode
   if (current_content == id) { id = ""; }
    
   update_menu(current_content, id);

   var current_gallery_enabled = (current_content == "")
   var target_gallery_enabled = (id == "");
   var current_photo_opacity = (current_content == "" || current_content == "home") ? 1.0 : PHOTO_DIMMED_OPACITY;
   var target_photo_opacity = (id == "" || id == 'home') ? 1.0 : PHOTO_DIMMED_OPACITY;

   if (target_gallery_enabled && !current_gallery_enabled)
      show_photo_controls();
   else if (current_gallery_enabled && !target_gallery_enabled)
      hide_photo_controls();

   var old_id = current_content;
   
   var g = function (e) {
      if (old_id != "") {
         $(old_id).fade({
            duration: CONTENT_INOUT_DURATION,
         	afterFinish: function (e) {
         	   if (id != "") $(id).appear({duration: CONTENT_INOUT_DURATION});
         	}
         });
      } else {
         if (id != "") $(id).appear({duration: CONTENT_INOUT_DURATION});
      }
   };

   if (current_photo_opacity != target_photo_opacity)
          $('photo').fade({
            duration: CONTENT_INOUT_DURATION,
            from: current_photo_opacity,
            to: target_photo_opacity,
            afterFinish: g});
   else
   	  g(null);

/*
   // if changing from no content (gallery mode) to some content
   if (current_content == "" && id != "") {
   	  
   	  if (target_photo_opacity == 1.0)
	     $(id).appear({duration: CONTENT_INOUT_DURATION});
      else {
         $('photo').fade({
            duration: CONTENT_INOUT_DURATION,
            from: 1.0,
            to: target_photo_opacity,
            afterFinish: function (e) { $(id).appear({duration: CONTENT_INOUT_DURATION}); }
            });
      }
      hide_photo_controls();

   // else if changing from some content to no content (gallery mode)
   } else if (current_content != "" && id == "") {
      
      $('photo').appear({duration: CONTENT_INOUT_DURATION, to: 1.0});
      show_photo_controls();
      $(current_content).fade({duration: CONTENT_INOUT_DURATION});
      
   // else if changing from home content to another
   } else if (current_content == "home" && id != "") {
      
   	  $(current_content).fade({
   	     duration: CONTENT_INOUT_DURATION
   	     });

      $('photo').fade({
         duration: CONTENT_INOUT_DURATION,
         from: 1.0,
         to: target_photo_opacity,
         afterFinish: function (e) { $(id).appear({duration: CONTENT_INOUT_DURATION}); }
         });

   // else if changing from one content to another
   } else if (current_content != "" && id != "") {
      
   	  $(current_content).fade({
   	     duration: CONTENT_INOUT_DURATION,
   	     afterFinish: function (e) { $(id).appear({duration: CONTENT_INOUT_DURATION}); }
   	     });
   	     
   	  if (id == 'home') hide_photo_controls();

   }

*/   
   current_content = id;
}

function hide_photo_controls()
{
	$('prev-photo').fade({duration: GALLERY_CONTROLS_INOUT_DURATION});
	$('next-photo').fade({duration: GALLERY_CONTROLS_INOUT_DURATION});
	$('photo-numbering').fade({duration: GALLERY_CONTROLS_INOUT_DURATION});
}

function show_photo_controls()
{
	$('prev-photo').appear({duration: GALLERY_CONTROLS_INOUT_DURATION});
	$('next-photo').appear({duration: GALLERY_CONTROLS_INOUT_DURATION});
	update_photo_numbering();
	$('photo-numbering').appear({duration: GALLERY_CONTROLS_INOUT_DURATION});
}

function start_gallery ()
{
	if (current_content != "")
		show_content("");
}

function on_photo ()
{
	if (current_content != "")
		on_results();
	else
		next_photo();
}

var page_divs = [ 'meet-phil-1', 'meet-phil-2', 'meet-phil-3' ];
var current_page = 0;

function next_page ()
{
	$(page_divs[current_page]).fade({
		duration: 0.5,
	    afterFinish: function (e) {
	    	current_page++;
	    	$(page_divs[current_page]).appear({duration: 0.5});
	    }});
}

/*
   if ($('about').visible()) { $('about').fade({from:0.88,to:0.0,afterFinish:function (e) {$('about').hide}}); } else { $('about').appear({from:0.0,to:0.88,afterFinish:function (e) {$('about').show}});
*/
