function startSlideshow() {
 $('.play').css('background-image', 'url("/images/pause.png")');
 $('#slide').css('background-image', 'none');
 active = true;
 $('.play').everyTime(5000, 'controlled', function() {
  $('.nextLink').click();
 });
}
    
$(document).ready(function() {
if ($.browser.msie && $.browser.version == 6)
{

} else {
 $('#thumbs').jScrollPane();
}

 var firstSrc = $('.images').children('a:first').attr('href');
 var firstCaption = $('.images').children('a:first').attr('title');
 swapImg(firstSrc, firstCaption);
 setTimeout( "startSlideshow()", 1000);	

 var size = '/pics/size/500x305/';
 var base = '/pics/';
 //lightbox

 $("a[rel^='prettyPhoto']").lightBox();

 $('.fullscreen').click(function() {
  var thumbSrc = $('#imagewrapper>img').attr('src');

 var thumbOrigSrc = thumbSrc.replace(size, base);
 $(".images a[href='"+thumbOrigSrc+"']").click();
     $('.play').stopTime('controlled');
     $('.play').css('background-image', 'url("/images/play.png")');
     $('#slide').css('background-image','url("/images/loading.gif")');
     active = false;
 });

 //email to friend
 var contactBoxy = null;
 $('.emaillink').click(function() {

  var url = window.location.toString();
  //Remove anchor from url
  var anchor_index = url.indexOf('#');
  if (anchor_index != -1) {
   url = url.substring(0, anchor_index);
  }

  var boxy_content;
  boxy_content += "<div style=\"width:300px; height:280px\"><form id=\"sendphoto\">"; 
  boxy_content += "<p>Your Name<br /><input class=\"required\" type=\"text\" name=\"your_name\" id=\"your_name\" size=\"41\" /></p>";
  boxy_content += "<p>Your Email Address<br /><input class=\"required email\" type=\"text\" name=\"your_email\" id=\"your_email\" size=\"41\"/></p>";
  boxy_content += "<p>Recipient Email<br /><input type=\"text\" class=\"required email\" name=\"recipient\" id=\"recipient\" size=\"41\"/></p>";
  boxy_content += "<br /><input type=\"submit\" name=\"submit\" value=\"Send >>\" />"; 
  boxy_content += "</form></div>";
  
  contactBoxy = new Boxy(boxy_content, {
    title: "Send Slideshow to a Friend",
    draggable: false,
    modal: true,
    behaviours: function(c) {
      c.find('#sendphoto').submit(function() {
//        if (c.find("#sendphoto").validate().form())
// 	  {
          Boxy.get(this).setContent("<div style=\"width: 300px; height: 300px\">Sending...</div>");
          // submit form by ajax using post and send 3 values: subject, your_email, comment
            $.post("/utilities/mailphoto", {
	      name: c.find("#your_name").val(),
	      your_email: c.find("#your_email").val(),
	      recipient: c.find("#recipient").val(),
	      url: url+'#Slideshow'
	      },
            function(data){
	        /*set boxy content to data from ajax call back*/
               contactBoxy.setContent("<div style=\"width: 300px; height: 300px\">"+data+"</div>");
	      }
	    );
//	  }  
        return false;
      });
    }
  });
 });

 // show thumbnail view 

 $('.thumbnails').click(function() {
  $('.play').stopTime('controlled');
  $('.play').css('background-image', 'url("/images/play.png")');
  $('#imagewrapper').fadeOut("fast", function() {
    $('#caption').html('');
    $('#imagewrapper>img').attr('src', '');
    $('#imagewrapper').height(0);
    $('.thumbcontainer').fadeIn("fast");
    $('.thumbcontainer').css("visibility", 'visible');
    $('#imagewrapper').css('display', 'none');
  });  

 });

//slideshow

 $('.play').click(function() {
  if (!active) {
    active = !active;
    $('.nextLink').click();
    $(this).css('background-image', 'url("/images/pause.png")');
    $('#slide').css('background-image', 'none');
    $(this).everyTime(5000, 'controlled', function() {
      $('.nextLink').click();
    });
  } else {
    active = !active;
    $(this).css('background-image', 'url("/images/play.png")');
    $('#slide').css('background-image', 'url("/images/loading.gif")');
    $(this).stopTime('controlled');
  }		      
 });

 // click on thumbnail
 $('#thumbs>div>a').click(function(){
  var lgSrc = $(this).attr('href');
  var caption = $(this).attr('title');
 //  $('#thumbs').hide();
  $('.thumbcontainer').fadeOut("fast", function() {
    swapImg(lgSrc, caption);
  });

  return false;
 });

 // show caption
 $('#slide').hover(function(){
  if ($('#caption').html() != '' && $('#imagewrapper>img').css("display") != 'none')
    $('#caption').fadeIn("fast");
 },
 function(){
  if ($('#caption').html() != '')
    $('#caption').fadeOut("fast");
 });

 // large image click
 $('#imagewrapper').click(function(){
	var thumbSrc = $('#imagewrapper>img').attr('src');
	var firstSrc = $('.images').children('a:first').attr('href');
	var firstCaption = $('.images').children('a:first').attr('title');
	var lastSrc = $('.images').children('a:last').attr('href');
	// if not clicking the last image
	if (!(thumbSrc == lastSrc.replace(base, size))){

	  // find the matching image, get the src of the next image
          var thumbOrigSrc = thumbSrc.replace(size, base);
          var nextSrc = $(".images a[href='"+thumbOrigSrc+"']").next().attr('href');
          var nextCaption = $(".images a[href='"+thumbOrigSrc+"']").next().attr('title');
          swapImg(nextSrc, nextCaption);
	} else {
		// if clicking the last image
		swapImg(firstSrc, firstCaption);
	};
	return false;
});

	// next link is same as clicking image
	$('.nextLink').click(function(){

	 if ($('.thumbcontainer').css('display') != 'none' && $('.thumbcontainer').css('visibility') != 'hidden'	)
	 {
	   var firstSrc = $('.images').children('a:first').attr('href');
	   var firstCaption = $('.images').children('a:first').attr('title');
	   $('.thumbcontainer').fadeOut("fast", function(){
	     swapImg(firstSrc, firstCaption);
	   });
	 } else 
	 {
 	  $('#imagewrapper').click();
	  return false;
	 }
	});	

	// back link is another story
	$('.backLink').click(function(){
        if ($('.thumbcontainer').css('display') != 'none'      )
        {
          var lastSrc = $('.images').children('a:last').attr('href');
          var lastCaption = $('.images').children('a:last').attr('title');
	  $('.thumbcontainer').fadeOut("fast", function() {
	    swapImg(lastSrc, lastCaption);
	  });

        } else
	{
	        var thumbSrc = $('#imagewrapper>img').attr('src');
 	        var firstSrc = $('.images').children('a:first').attr('href');
	        var lastSrc = $('.images').children('a:last').attr('href');
		var lastCaption = $('.images').children('a:last').attr('title');
		// if not clicking the first image
		if (!(thumbSrc == firstSrc.replace(base, size))){


var thumbOrigSrc = thumbSrc.replace(size, base);
        var prevSrc = $(".images a[href='"+thumbOrigSrc+"']").prev().attr('href');
        var prevCaption = $(".images a[href='"+thumbOrigSrc+"']").prev().attr('title');
        swapImg(prevSrc, prevCaption);
			} else {
			        // if clicking the first image
				swapImg(lastSrc, lastCaption);
			};
		return false;
	  }
	});
}); 

function swapImg(src, caption)
{

  var photo = new Image();
  src2 = src.replace('/pics/','/pics/size/500x305/');
  photo.src = src2;
  
  $('#caption').hide();
  $('#caption').html('');
  $('#imagewrapper').fadeOut("fast", function() {
    $('#imagewrapper').height(305);
    $('#caption').width(photo.width-10);
    $('#imagewrapper').html(photo);
    $('#imagewrapper>img').attr('alt', caption)
    $('#imagewrapper>img').attr('src', src2).load(function() {
      $('#imagewrapper').fadeIn("slow");
      $('#caption').html(caption);
    });
    $('#imagewrapper').fadeIn("slow");
  });
}

