function hackyScroller (id) {
    $(id).show();
    Scroller.updateAll();
    new Effect.Opacity($(id), {duration: 1.0, from: 0.0, to: 1.0});
}

function openNewsItem (id) {
    if ($(id).style.display == 'none') {
        $(id).setOpacity(0);
        new Effect.BlindDown($(id), {duration: 1.0, from: 0.0, to: 1.0, afterFinish: hackyScroller(id)});
    } else {
        $(id).hide();
        Scroller.updateAll();
    }
}

function openHolder (scroller) {
    if ($(scroller).style.display == 'none') {
        $('scroller1', 'scroller2', 'scroller3', 'scroller4').invoke('hide');
        Scroller.updateAll();
        $(scroller).show();
        
        $$('#submenu a').each(function (item) {
            item.removeClassName('selected');
        });
        
        var changeling = ['scroller1', 'scroller2', 'scroller3', 'scroller4'].indexOf(scroller);
        $$('#submenu a').each(function (item, i) {
            if (changeling == i) {
                item.addClassName('selected');
            }
        });
    }
}

var Gallery = {
	slike: null,
	previousImage: null,
    loadImage: function (img) {
        var newImage = new Element('img');
        if (img) {
            newImage.setAttribute('src', slike[img]);
			if (this.previousImage) {
				this.pimg = Number(this.previousImage + 1);
			} else {
				this.pimg = 1;
			}
			
			$$('div#numbers a').each(function(a) { a.removeClassName('selected'); });
			$(Number(img + 1) + 'a').addClassName('selected');
        } else {
            newImage.setAttribute('src', slike[0]);
			
			if (this.pimg) {
				$$('div#numbers a').each(function(a) { a.removeClassName('selected'); });
				$('1a').addClassName('selected');
			}
        }
        
        this.previousImage = img;
        $(newImage).setStyle({position: 'absolute', left: '0', top: '0', opacity: '0'});
        $('image').setStyle({position: 'relative', overflow: 'hidden'});
        $('image').insert(newImage);
        
        if (!newImage.complete) {
			this.loading = new Element('div').setStyle({position: 'absolute', left: '5px', bottom: '5px', color: '#FFF'}).update('Nalagam...');
			this.loading.addClassName('loading');
            $('image').insert(this.loading);
            this.bfx = this._onLoad.bindAsEventListener(null, this, newImage);
            Event.observe(newImage, 'load', this.bfx);
        } else {
            this._transitionImage(newImage);
        }
    },
    
    _onLoad: function (event, reference, newImage) {
        reference._transitionImage(newImage);
        Event.stopObserving(newImage, 'load', this.bfx);
    },
    
    _transitionImage: function (image) {
        $$('div.loading').each( function (el) { el.remove(); } );
        
        new Effect.Appear(image, {duration: 1.5, from: 0.0, to: 1.0});
        
        if (this.oldimage) {
            new Effect.Appear(this.oldimage, {duration: 1.5, from: 1.0, to: 0.0, afterFinish: this._removeImage});
        }
        
        this.oldimage = image;
    },
    
	_removeImage: function(object) {
		object.element.remove();
	},
	
    loadNumbers: function (num) {
		var numbers = $('numbers').select('span');
		numbers.each(function(e) {
			e.remove();
		})
		
		$('numbers').setStyle({display: 'none'});
		
		for (var i = num; i > 0; i--) {
			var span = document.createElement('span');
			span.setAttribute('id', i);
            var a = document.createElement('a');
            a.setAttribute('href', '#');
			a.setAttribute('id', i + 'a');
            a.onclick = function () { Gallery.loadImage(parseInt(this.identify()) - 1); return false; }
            a.appendChild(document.createTextNode(i));
			span.appendChild(a);
            $('numbers').appendChild(span);
        }
        
        $('1a').addClassName('selected');
		new Effect.Appear($('numbers'), {duration: 1.5, from: 0.0, to: 1.0});
    },
    
    hackyScroller: function (id) {
        $(id).show();
        Scroller.updateAll();
        new Effect.Opacity($(id), {duration: 1.0, from: 0.0, to: 1.0});
    },
	
	getNewData: function (tip, id) {
		slike = null;
		img = null;
		previousImage = null;
		pimg = null;
		var url = 'lib/jsonslike.php';
		var rand = Math.random(9999);
		var pars = 'tip=' + tip + '&id=' + id + '&rand=' + rand;
        if ($(tip + id).style.display == 'none') {
            $(tip + id).setOpacity(0);
            new Effect.BlindDown($(tip + id), {duration: 1.0, from: 0.0, to: 1.0, afterFinish: Gallery.hackyScroller(tip + id)});
            new Ajax.Request(url, {
                    method: 'get',
                    parameters: pars,
                    onSuccess: function(transport, json) {
                        this.slike = json;
                        Gallery.loadImage();
                        Gallery.loadNumbers(slike.size());
                    }
                }
            );
        } else {
            $(tip + id).hide();
            Scroller.updateAll();
        }
	}
}
