﻿$(function() {
    // add images to slideshow
    function onBefore(curr, next, opts) {
        if (opts.addSlide) // <-- important!
            while(stack.length)
                opts.addSlide(stack.pop()); 
//        $(this).find("img").css({left: '50%', marginLeft: -$(this).width()/2});
//        $(this).find("img").css({top: '50%', marginTop: -$(this).height()/2});                 
    };  
    
 var stack = [];
function parseXml(xml)
  {
  //find every Tutorial and print the author
//  $(xml).find("Tutorial").each(function()
//  {
//    $("#output").append($(this).attr("author") + "<br />");
//  });

  // Output:
  // The Reddest
  // The Hairiest
  // The Tallest
  // The Fattest
  var $slide = $('#slideshowCycle');
  var w = $slide.outerWidth();
  var h = $slide.outerHeight();

 
  
  $(xml).find("image").each(function()
  {
    //$("#slideshowCycle").append($(this).attr("img") + "<br />");
    var imageSource = $(this).attr("img");
    var img = new Image();
//    var maxWidth = w;
//    img.height=h;
//    img.style.width="auto";

    img.src = imageSource;
        if (img.complete)
            stack.push($("<div></div>").append(img));
        else 
            $(img).bind('load', function() {
//                while(img.width>maxWidth){
//                  img.height--;
//                }
                //this.height = h;
                stack.push($("<div></div>").append(this));
            });    
  });  
   

//    // preload images into an array
//    for (var i = 3; i < 9; i++) {
//        var img = new Image(200,200);
//        
//        img.src = "_assets/images/wedding/canberra/scaled-1067.jpeg"; //'images/beach' + i + '.jpg';
//        if (img.complete)
//            stack.push(img);
//        else 
//            $(img).bind('load', function() {
//                stack.push(this);
//            });
//    } 


    // start slideshow
    $('#slideshowCycle').cycle({
        timeout:  3000,
        before:   onBefore
    });
    
    //end parse xml
 }



  $.ajax({
    type: "GET",
    url: pamphoto.page.SlideShowUrl,
    dataType: "xml",
    success: parseXml
  });
  
  
  
 
});

