﻿

$(document).ready(function(){
    //Read in the XML
    $(function() {
        $.ajax({
        type: "GET",
        url: "XML/HomePageImages.xml",
        dataType: "xml",
        success: function(xml) {
        
                //get elements      
                var images = $("image", xml)
                                
                //create a random number within the range of our image count
                var now=new Date();
                var num=((now.getSeconds()) % images.length);
                
                
                
                //grab all the data we need.
                var imageUrl = images.eq(num).attr('url')
                var imageTitle = images.eq(num).attr('title')
                var imageID = images.eq(num).attr('id')
                
                //set up the photo
                $("#randomPhoto").attr({
                    src: imageUrl,
                    alt: imageTitle                    
                }).fadeIn("normal")
                
                //add the caption
                $("#randomPhotoCaption a").attr({
                    href: "Photos/Detail.aspx?pid=" + imageID,
                    title: imageTitle
                   }).text("");
                    
                //get the individual keywords
                var keywords = images.eq(num).attr('keywords')
                var myArray = new Array();
                myArray = keywords.split(",");    
                
                //create the keywords
                for (i=0;i<myArray.length;i++)
                {
                    $("#randomPhotoCaption").append('<a href="Photos/Search.aspx?q=' + myArray[i] + '" title="' + myArray[i] + '">' + myArray[i] + '</a>')
                    if  (i < (myArray.length-1)) {
                        $("#randomPhotoCaption").append(", ")
                    }
                }   
            }
       });
   });
});


       