function initShowSettings(where, value, parameters, groupId) {
    where = where+'-settings';
    jsonData = eval('(' + parameters + ')');
    if (document.getElementById(where+'-element') != null) {
        Element.remove($(where+'-element'));
    }
    switch(value) {
        case 'Supernik_Gadget_News':        showNewsSettings(where, jsonData, groupId); break;
        case 'Supernik_Gadget_Text':        showTextSettings(where, jsonData, groupId); break;
        case 'Supernik_Gadget_Slideshow':   showSlideshowSettings(where, jsonData, groupId); break;
        case 'Supernik_Gadget_Image':       showImageSettings(where, jsonData, groupId); break;
        case 'Supernik_Gadget_Events':      showEventsSettings(where, jsonData, groupId); break;
        case 'Supernik_Gadget_Rss':         showRssSettings(where, jsonData, groupId); break;
    }
}

function showImageSettings(where, parameters, groupId) 
{
    if(parameters == null) { comment = ''; fileId = ''; } else { comment = parameters.comment; fileId = parameters.fileId; }
    indexUrl = '/ajax/group/'+groupId+'/drawImageSettings';
    new Ajax.Updater(where, indexUrl, {
        parameters: 'comment='+comment+'&where='+where+'&fileId='+fileId
    });
}
function showTextSettings(where, parameters, groupId) 
{
    if(parameters == null) { 
        textId = ''; 
    } else { 
        textId = parameters.textId
    }

    indexUrl = '/ajax/group/'+groupId+'/drawTextSettings';
    new Ajax.Updater(where, indexUrl, {
        parameters: 'textId='+textId+'&where='+where
    });
}
function showRssSettings(where, parameters, groupId) 
{
    if(parameters == null) { 
        rssFlow = ''; 
        rssItemCount = ''; 
    } else { 
        rssFlow = parameters.rssFeedUrl
        rssItemCount = parameters.count
    }
    
    indexUrl = '/ajax/group/'+groupId+'/drawRssSettings';
    new Ajax.Updater(where, indexUrl, {
        parameters: 'rssFlow='+rssFlow+'&rssItemCount='+rssItemCount+'&where='+where
    });
}
function showEventsSettings(where, parameters, groupId) 
{
    if(parameters == null) { calendarId = ''; eventItems = ''; } else { calendarId = parameters.calendarId; eventItems = parameters.eventItems; }
    indexUrl = '/ajax/group/'+groupId+'/drawUpcomingEventsSettings';
    new Ajax.Updater(where, indexUrl, {
        parameters: 'selectedId='+calendarId+'&eventItems='+eventItems+'+&where='+where 
    });
}

function showNewsSettings(where, parameters, groupId) {
    if(parameters == null) { count = '';} else { count = parameters.count; }
    indexUrl = '/ajax/group/'+groupId+'/drawlatestnewssettings';
    new Ajax.Updater(where, indexUrl, {
        parameters: 'count='+count+'&where='+where
    });
}

function showSlideshowSettings(where, parameters, groupId) {
    if(parameters == null) { 
        tagName = ''; 
        width = 150;
        height = 250;
    } else { 
        tagName = parameters.tagName;
        width = parameters.width;
        height = parameters.height; 
    }
    indexUrl = '/ajax/group/'+groupId+'/drawSlideshowSettings';
    new Ajax.Updater(where, indexUrl, {
        parameters: 'tagName='+tagName+'&where='+where+'&height='+height+'&width='+width
    });
}

function initSlideshow(groupId, tagId, divId, width, height) {
    var url = '/ajax/group/'+groupId+'/galleryId/'+tagId+'/div/'+divId;
    new Ajax.Request(url, {
        parameters: 'width='+escape(width)+'&height='+escape(height),
        onSuccess: slideshow,
        onFailure: displayAjaxError
    });
}

function slideshow(t) {
    var serverData = eval('(' + t.responseText + ')');
    var images = serverData.images;
    var divId = serverData.divId;
    var imageArray = new Array();
    var i = 0;
    var arrayPosition = 0;
    var lastCreatedPosition = null;
    serverData.width = (serverData.width-10) 
    images.each(function(id) {
        if (serverData.height) {
            imageArray[i] = 'http://www.sportnik.com/file/show/'+id+'?width='+serverData.width+'&height='+serverData.height;
        } else {
            imageArray[i] = 'http://www.sportnik.com/file/show/'+id+'?width='+serverData.width;
        }
        i++;
    });
    
    element = Builder.node('div', {id: divId+'-'+arrayPosition, style: 'display: none; width: ' + serverData.width + 'px'}, [
        Builder.node('img', {src: imageArray[arrayPosition], width: serverData.width})
    ]);

    $(''+divId+'').appendChild(element)
    Effect.toggle(''+divId+'-'+arrayPosition+'', 'appear', { duration: 0.5 })
    lastCreatedPosition = arrayPosition;
    if (imageArray[arrayPosition+1] == null) { arrayPosition = 0; } else { arrayPosition++; }
    
    setInterval(function() {
        if(lastCreatedPosition != null) $(''+divId+'').removeChild($(''+divId+'-'+lastCreatedPosition+''));
        
        element = Builder.node('div', {id: divId+'-'+arrayPosition, style: 'display: none; width: ' + serverData.width + 'px'}, [
            Builder.node('img', {src: imageArray[arrayPosition], width: serverData.width})
        ]);
     
        
        $(''+divId+'').appendChild(element)
        Effect.toggle(''+divId+'-'+arrayPosition+'', 'appear', { duration: 0.5 })
        lastCreatedPosition = arrayPosition;
        if (imageArray[arrayPosition+1] == null) { arrayPosition = 0; } else { arrayPosition++; }
    }, 7 * 1000); 
}

