﻿var api_key = "";   // to replace with the api_key of PMO.
var photosetID = ""; 
var lastphoto;

function loadFlickr()
{
    var hfKey = document.getElementById("hfFlickrAPIKey");
    if (hfKey != null)
        api_key = hfKey.value;
    
    if (api_key == "")
    {
        showDefault();
        return;
    }
    
    var hfPS = document.getElementById("hfPhotoSetID");    
    if (hfPS != null)
        photosetID = hfPS.value;
  
    if (photosetID != null && photosetID != "")
    {
        var startUpJS = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=" + api_key + "&photoset_id=" + photosetID + "&format=json&jsoncallback=getPhotoFromSet";    
        
        var f = document.createElement("script");
            f.type = "text/javascript";
            f.src = startUpJS;
        
        document.getElementsByTagName('body')[0].appendChild(f);    
    }
    else
        showDefault();
}

function getPhotoFromSet(data) {
    if (data.stat == "ok")      //if stat is not ok then don't do antything.
    {
        var phset = data.photoset;
        var photos = phset.photo || [];
      
        for (var i = 0; i < photos.length; i++) {
            var ph = photos[i];
            var photoId = ph.id;
        
            var strJS = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=" + api_key + "&photo_id=" + photoId + "&format=json&jsoncallback=getPhotoURL";
            var g = document.createElement("script");
                g.type="text/javascript";
                g.src=strJS;
            document.getElementsByTagName('body')[0].appendChild(g);
            
            var strJS2 = "http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=" + api_key + "&photo_id=" + photoId + "&format=json&jsoncallback=getPhotoInfo";                
            var h = document.createElement("script");
                h.type="text/javascript";
                h.src=strJS2;
            document.getElementsByTagName('body')[0].appendChild(h);
            
            if (i==(photos.length-1))
            {
                lastphoto = photoId;
            }                
        }
    }
    else
    {
        showDefault();
    }
}

function getPhotoURL(data) {
    var sizes = data.sizes;
    var s = sizes.size || [];
    
    for (var i = 0; i < s.length; i++) {
        if (s[i].label == "Small") {
            var url = s[i].source;
            
            document.getElementById("hfImgURL").value += url + "*\n";                
            break;
        }
    }  // end for
}

function getPhotoInfo(data) {    
    var p = data.photo;
    var desc = p.description._content;
    var pDate = new Date(getDateFromFormat(p.dates.taken, "yyyy-MM-dd HH:mm:ss"));
    var strDate = formatDate(pDate, "dd MMM yyyy");
            
    document.getElementById("hfImgDesc").value += desc + "*\n";
    document.getElementById("hfImgDate").value += strDate + "*\n";
    
    //the slideshow will run when the last photo's info is loaded.
    if (p.id==lastphoto)
    {           
        slideshow();
    }
}

var step = 0;
var slideDelay = 3000;      // the delay in photo slide show in milliseconds.
function slideit(strURLs,strDescs,strDates){ 
    if (!document.images)
        return;

    if (strURLs[step] == "")
        return;

    var img = document.getElementById("ImgRoll"); 
    var imgDate = document.getElementById("FlickrImageDate");
    var imgDesc = document.getElementById("FlickrImageDesc");

    img.src = strURLs[step];
    imgDate.innerHTML = strDates[step];
    imgDesc.innerHTML = strDescs[step];
    img.alt = strDescs[step];
    img.title = strDescs[step];
    
    if (step < strURLs.length -2)   // use -2 because strURLs[length-1] is a blank string.
        step++;
    else
        step = 0;
        
    setTimeout(function() { slideit(strURLs,strDescs,strDates); } , slideDelay); 
}

function slideshow()
{
    var str = document.getElementById("hfImgURL").value;
    var strURLs = new Array();
    strURLs = str.split('*');
            
    str = document.getElementById("hfImgDesc").value;
    var strDescs = new Array();
    strDescs = str.split('*');

    str = document.getElementById("hfImgDate").value;
    var strDates = new Array();
    strDates = str.split('*');
            
    slideit(strURLs,strDescs,strDates);
}

function showDefault()
{
    var img = document.getElementById("ImgRoll"); 
    var imgDesc = document.getElementById("FlickrImageDesc");
    
    img.src = "/NR/rdonlyres/B157CBE8-37AC-4A8F-8D17-C1AB4459CD75/0/img_default_slideshow.jpg";
    imgDesc.innerHTML = "<a href=\"http://www.flickr.com/photos/pmosingapore\" target=\"_blank\">Click here to view Prime Minister's Office's photostream on flickr</a>";
}