﻿// VideoGallery

var VideoGalleryArray;
var currentVideoGalleryThumbnail = 0;

function videoThumb() {
    this.tooltip = "";
    this.thumbUrl = "";
    this.navUrl = "";
}

function initVideoGallery(url) {
    $.get(url, function(data){
        var count = 0;
        VideoGalleryArray = new Array();
        $(data).find('item').each(function(){
            var $item = $(this);
            var thumb = new videoThumb();        
            var title = $item.find('title').text();
            var description = $item.find('description').text();
            thumb.tooltip = title + "\n" + description;
            thumb.thumbUrl = $item.find('enclosure').attr('url');
            thumb.navUrl = $item.find('link').text();
            VideoGalleryArray.push(thumb);
            count++;
        });

        if(VideoGalleryArray.length == 0) {
            
        } else {                 
            showMovie(VideoGalleryArray[0]);
        }
        if(VideoGalleryArray.length > 1)
            setTimeout(VideoGallerySlideShow, 6000);      
    });
}
        
function VideoGallerySlideShow() {
    // TODO: transizione
    VideoGalleryShowNext();
}

function VideoGalleryShowNext() {
    currentVideoGalleryThumbnail = (currentVideoGalleryThumbnail + 1)%VideoGalleryArray.length;
    showMovie(VideoGalleryArray[currentVideoGalleryThumbnail]);
    setTimeout(VideoGallerySlideShow, 11000);
}

function showMovie(thumb) {
    var cssStyle = "position: absolute; left: 4px; top: 4px; width: 152px; height: 102px; margin: 0; padding: 0; ";
    $('#videoGalleryThumb').html("<a href='" + thumb.navUrl +
        "' title='" + thumb.tooltip +
        "' style='display:block; " + cssStyle + " z-index: 10' target='_blank'>" +
        "<img src='graphics/spacer.gif' width='152' height='102' alt='' border='0' /></a>");
    $('#videoGalleryThumb').append("<div id='videoGaleryMovie' style='" + cssStyle + "; background-color: #05374e ; z-index: 1' />");
    $('#videoGaleryMovie').html(swfCode(thumb.thumbUrl, "video"));   
}
        
function swfCode(swfUrl, swfId) {
    var swfWidth = 152;
    var swfHeight = 102;

    return  "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'" +
            "    codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'" +
            "    width='" + swfWidth + "' height='" + swfHeight + "' id='" + swfId + "' align='middle'> " +
            "    <param name='allowScriptAccess' value='sameDomain' /> " +
            "    <param name='allowFullScreen' value='false' /> " +
            "    <param name='movie' value='" + swfUrl + "' /><param name='quality' value='high' /> " +
            "    <param name='wmode' value='transparent'> " +
            "    <embed src='" + swfUrl + "' quality='high' width='" + swfWidth + "' height='" + swfHeight + "' name='" + swfId + "' align='middle'" +
            "    allowScriptAccess='sameDomain' allowFullScreen='false' type='application/x-shockwave-flash' wmode='transparent' " +
            "    pluginspage='http://www.macromedia.com/go/getflashplayer' /> " +
	        "</object>";
}

// PhotoGallery

var PhotoGalleryThumbnails = 0;
var currentPhotoGalleryThumbnail = 0;
   
function initPhotoGallery(url) {
    $.get(url, function(data){
        var count = 0;
        $(data).find('item').each(function(){
            var $item = $(this);
            var title = $item.find('title').text();
            var description = $item.find('description').text();
            var tooltip = title + "\n" + description;
            var thumbUrl = $item.find('enclosure').attr('url');
            var navUrl = $item.find('link').text();
            $('#photoGallery').append("<a class='galleryThumb' id='thumb"+count+"' href='"+navUrl+"' title='" + tooltip + "' target='_blank'></a>");
            var myImg = $('#photoGallery').find('#thumb'+count).image(thumbUrl, function() { PhotoGalleryResize(this); });
            count++;
        });
        PhotoGalleryThumbnails = count;

        if(PhotoGalleryThumbnails == 0) {

        } else if(PhotoGalleryThumbnails == 1) {
            $('#photoGallery').find('#thumb0').css('left',4);
        } else {
            $('#photoGallery').find('#thumb0').css('left',4);
            for(var i=1; i<PhotoGalleryThumbnails; i++) {
                $('#photoGallery').find('#thumb'+i).css('left',204);
            }
            setTimeout(PhotoGallerySlideShow, 5000);
        }
    });
}

$.fn.image = function(src, f){
    return this.each(function(){
        $("<img>").appendTo(this).attr('src', src).load( f );
    });
};

function PhotoGalleryResize(img) {
    var sWidth = 152;
    var sHeight = 102;
    var iWidth = $(img).width();
    var iHeight = $(img).height();
    var scale = sWidth/iWidth;
    if(iHeight*scale > sHeight)
        scale = sHeight/iHeight;
    $(img).width(iWidth*scale);
    $(img).height(iHeight*scale);
}

function PhotoGallerySlideShow() {
    var current = currentPhotoGalleryThumbnail; 
    var next = (current +1)%PhotoGalleryThumbnails;
    PhotoGalleryResize($('#photoGallery').find('#thumb' + next).find('img'));
    $('#photoGallery').find('#thumb' + current).animate({"left": "-=200px"}, "slow", function() { $(this).css('left', 204); } );
    $('#photoGallery').find('#thumb' + next).animate({"left": "-=200px"}, "slow" );
    currentPhotoGalleryThumbnail = next;
    setTimeout(PhotoGallerySlideShow, 10000);
}