﻿function ASize(w,h)
{
    this.width = w;
    this.height = h;
}

var AAllPhotoNews = null;
function APhotoNews(divContainer, jsonPhotos)
{
    if(AAllPhotoNews == null)
        AAllPhotoNews = new Array();
    AAllPhotoNews.push(this);
    this._iIndexInAllPhotos = AAllPhotoNews.length - 1;
    
    this._divContainer = divContainer;
    this._jsonPhotos = jsonPhotos;
    this._iCurrentIndex = -1;
    this._iViewDuration = 10; // second
    
    var w = this._divContainer.offsetWidth - 2;
    var h = this._divContainer.offsetHeight - 2;
    this._sizeImage = new ASize(w,h);
    
    this._imgNews = null;
    this._divCaption = null;
    
    this.next = function()
    {
        var jsonPhoto = this._jsonPhotos.Photos[this._iCurrentIndex];
        this._iCurrentIndex = (this._iCurrentIndex + 1) % this._jsonPhotos.Photos.length;
        return jsonPhoto;
    };
    
    this.previous = function()
    {
        var jsonPhoto = this._jsonPhotos.Photos[this._iCurrentIndex];
        this._iCurrentIndex = (this._iCurrentIndex - 1) % this._jsonPhotos.Photos.length;
        return jsonPhoto;
    };
    
    this.drawViewer = function()
    {
        this._imgNews = document.createElement("img");
        
        var THIS = this;
        this._imgNews.onload = function(ev)
        {
            THIS._divCaption.innerHTML = this.caption;
        };
        
        var jsonPhoto = this.next();
        this._imgNews.style.width = "100%";
        this._imgNews.style.height = "80%";
        //this._imgNews.style.borderBottom = "1px solid black";
        this._divContainer.appendChild(this._imgNews);
        
        this._divCaption = document.createElement("div");
        this._divCaption.style.width = "100%";
        this._divCaption.style.height = "20%";
        this._divContainer.appendChild(this._divCaption);
        
        this.showNextImage();
        
        setInterval("AAllPhotoNews["+this._iIndexInAllPhotos+"].showNextImage();", this._iViewDuration * 1000);
    };
    
    this.showNextImage = function()
    {
        var jsonPhoto = this.next();
        this._imgNews.caption = jsonPhoto.caption;
        this._imgNews.setAttribute("src", jsonPhoto.url);
        //this._divCaption.innerHTML = jsonPhoto.caption;
    };
    
    this.drawViewer();
}

