// JavaScript Document
function swapImages(_elem, _input){
    var _ids = [];
    var _imgs = [];
 	
    for(id in _input) {
        _ids.push(id);
        _imgs.push(_input[id]);
    }
    var _current = 0;
    var _max = _imgs.length;

    
    var _loadImage = function(){
        var node = document.getElementById(_elem);
        node.src = _imgs[_current];
        node.parentNode.href = ['javascript:openPopup(', _ids[_current], ');void(0);'].join('');
    };
    
    this.next = function(){
        if (_current + 1 === _max) {
            _current = -1;
        }
        ++_current;
        _loadImage();
    };
    
    this.prev = function(){
        if (_current - 1 < 0) {
            _current = _max;
        }
        --_current;
        _loadImage();
    };
    
    _loadImage();
}

function listNavigation(_objName, _max, _seiteId, _maxSeiteId){
    var _current = 0;
    var _navigations = [];
    
    this.addNav = function(id){
        _navigations.push(id);
        var str = ['<a href="javascript:', _objName, '.prev();void(0);"><img src="images/button-list-prev.png" alt=">" /></a>'].join('');
        for (var i = 0; i < _max; ++i) {
            str =[str,'<a id="',id,'-button-',i,'" href="javascript:',_objName,'.open(',i,');void(0);">',i+1,'</a>']   .join('');
        }
        document.getElementById(id).innerHTML =[str,'<a href="javascript:',_objName,'.next();void(0);"><img src="images/button-list-next.png" alt=">" /></a>'] .join('');
    };
    
    this.next = function(){
        if (_current + 1 === _max) {
            _current = -1;
        }
        ++_current;
        this.open(_current);
    };
    
    this.open = function(id){
        if (typeof(id) !== 'undefined') {
            _current = id;
        }
        
        var i, ii;
        for (i = 0; i < _max; ++i) {
            document.getElementById('list' + i).style.display = 'none';
            for (ii = 0; ii < _navigations.length; ++ii) {
                document.getElementById([_navigations[ii], '-button-', i].join('')).className = '';
            }
        }
        for (ii = 0; ii < _navigations.length; ++ii) {
            document.getElementById([_navigations[ii], '-button-', _current].join('')).className = 'a';
        }
        document.getElementById('list' + _current).style.display = 'block';
        document.getElementById(_seiteId).innerHTML = _current + 1;
    };
    
    this.prev = function(){
        if (_current - 1 < 0) {
            _current = _max;
        }
        --_current;
        
        this.open(_current);
    };
    
    this.reset = function () {
        _current = 0;
        _navigations = [];
    }

    this.setMax = function(max) {
        _max = max;
        if (document.getElementById(_maxSeiteId) != null) {
            document.getElementById(_maxSeiteId).innerHTML = max;
        }
    }
    
    this.setMax(_max);
}

function openPopup(elem, width, height,fullscreen){
    if (typeof(width) === 'undefined') {
        width = 546;
    }
    if (typeof(height) === 'undefined') {
        height = 650;
    }
    
    if (typeof(win) == 'object') {
        win.close();
    }
    
    var key = 'id';
    if (typeof(elem) !== 'number') {
        key = 'file';
        if (typeof(elem) != 'string') {
            elem = elem.src.substring(elem.src.lastIndexOf('=') + 1);
        }
    }
	
	if (fullscreen !== true) {
        win = window.open([basePath, 'popup.php?', key, '=', elem].join(''), 'test', ['width=', width, ',height=', height].join(''));
    }
	else
	{	
		width = screen.width;
		height = screen.height;	
		win = window.open([basePath, 'popup.php?', key, '=', elem].join(''), 'test', ['width=', width, ',height=', height, ',fullscreen=yes,top=0,left=0'].join(''));
	}
}

function getGallery(id, elem){
    node = document.getElementById(elem).parentNode.firstChild;
    while (node) {
        if (node.nodeName.toLowerCase() == 'a') {
            node.className = '';
        }
        node = node.nextSibling;
    }
    document.getElementById(elem).className = 'a';
    
    new Ajax.Updater('galleryHolder', 'backend/action.php?action=gallery', {
        parameters: ['?id=', id].join(''),
        evalScripts: true
    });
}
