var gallery = {
	gallery_root: "objectgallery",
	overlay:"gallery_overlay",
	use_global_size:false,
	img_width:"640",
	img_height:"425",
	leftpanel_width:120,

	init:function(){
		if (this.all) {

		}else{
			var images = [];
			var imagepath = [];
			var size = [];
			var width, height;

			var allimgs = $A($(this.gallery_root).select("img")).each(function(s) {
			var bigimagesrc = s.src.replace("small/","big/").replace("mid/","big/").replace("list/","big/");

				imagepath.push(bigimagesrc);
				if (document.images)
				{
				   var im = new Image();
				   im.src = bigimagesrc;
				   //im.width = gallery.img_width;
				   //im.height = gallery.img_height;

				   size = s.id.split("||");
				   width = size[0];
				   height = size[1];

				   im.width = width;
				   im.height = height;
				   im.title = s.alt;
				   images.push(im);
				}
			});
			this.images = images;
			this.allimgs = imagepath;
			this.all = this.allimgs.length;
		}
	},

	show:function(id,new_width, new_height){
		this.current = id;
		//this.init();
		this.overlayShow();
		this.createGallery(new_width, new_height);
		$(gallery.overlay).observe('click', function(){ gallery.close('orava_galery'); });
		$('gimage').observe('click', function(){ if(gallery.current < (gallery.all-1)) gallery.next(); else gallery.close('orava_galery'); });
		Event.observe(document, 'keydown', function(event) {
			if(event.keyCode == 39) {
				gallery.next();
			}
			else if(event.keyCode == 37){
				gallery.prev();
			};
		});
	},

	overlayShow:function(){
		var sirka = document.viewport.getWidth();
		var vyska = document.viewport.getHeight();
		var vyskapage = $('oravaweb').getHeight();
		var resultvyska = vyskapage;

		if (vyska > vyskapage) {
			resultvyska = vyska;
		}

		$(this.overlay).setStyle("width:"+sirka+"px;");
		$(this.overlay).setStyle("height:"+resultvyska+"px;");
		$(this.overlay).show();
	},

	close: function(id){
		this.overlayHide();
		$(id).remove();
		Event.stopObserving(gallery.overlay, 'click');
		Event.stopObserving(document, 'keydown');
		Event.stopObserving('gimage', 'click');

	},

	getScrollTop:function() {
		return this.f_filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	},

	f_filterResults:function (n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	},


	createGallery:function(new_width, new_height){
	    var info_height = 40;
	    var padding = 5;
	    var leftpanel = this.leftpanel_width;
		 var width = parseInt(new_width) + (2*padding);
	    var height = parseInt(new_height) + (2*padding) + info_height;
	    var topscroll = this.getScrollTop();
		var id = "orava_galery";
		var className = "GScms_win";
	    var top = (document.viewport.getHeight() / 2) - ( height / 2) + topscroll;
		var left = (document.viewport.getWidth() / 2) - ( width / 2);

		var win = new Element('div', { 'class': 'CMSwindow', 'id':''+id+'', 'style':'width:'+width+'px;height:'+height+'px; position:absolute; z-index:200; top:'+top+'px; left:'+left+'px' });

	    var content ="<img src=\""+this.images[this.current].src+"\" width=\""+this.images[this.current].width+"\" height=\""+this.images[this.current].height+"\" id=\"gallery_image\"/>";

	    var butnext ="<a class=\"gallery_next\" href=\"javascript:;\" id=\"gallery_next\" onclick=\"gallery.next();\"></a>";
	    var butprev ="<a class=\"gallery_prev\" href=\"javascript:;\" id=\"gallery_prev\" onclick=\"gallery.prev();\"></a>";


	    win.innerHTML = "<div id='"+ id +"_row1' class=\"galleryimg\">\
		<div class=\"left\" id=\"gallerymain\" style=\"width:"+ new_width +"px;\">\
		<div align=\"center\"><div style=\"width:94px;\">" + butprev + "  "+ butnext +  "</div></div>\
		<div class=\"space3\"></div>\
	    <div id=\"gimage\">" + content + "</div>\
	      <div class=\"space3\"></div>\
	      <div class=\"gallery_info\">\
			 <div class=\"left\"><span class=\"gallery_counter\" id=\"gallery_counter\">"+ (parseInt(this.current)+1) +" / "+ this.all+"</span></div>\
			 <a class='gallery_close' href='javascript:;' onclick='gallery.close(\""+ id +"\")'>ZAVRIEŤ</a>\
			 <div class=\"space3\"></div>\
		  </div>\
		  </div>\
		  <div class=\"space3\"></div>\
		  </div>\
	    ";
	    document.body.appendChild(win);
		if (gallery.current >= (gallery.all -1) ) {
			$('gallery_next').hide();
		}
		if (gallery.current <= 0) {
			$('gallery_prev').hide();
		}
	},

	resizeGallery:function(new_width,new_height){
		var info_height = 40;
		var padding = 5;
	    var leftpanel = gallery.leftpanel_width;
		var width = parseInt(new_width) + (2*padding);
	    var height = new_height + + (2*padding) + info_height;
	    var topscroll = gallery.getScrollTop();
	    var top = (document.viewport.getHeight() / 2) - ( height / 2) + topscroll;
		var left = (document.viewport.getWidth() / 2) - ( width / 2);
        //$('xl_galery').setStyle("height:"+height+"px;width:"+width+"px;top:"+top+"px;left:"+left+"px;");
        $('orava_galery').setStyle("height:"+height+"px;width:"+width+"px;left:"+left+"px;");
        $('gallerymain').setStyle("width:"+new_width+"px;");
        //$('gallerypanel').setStyle("width:"+(leftpanel-10)+"px;");
	},

	next:function(){

		if (gallery.current >= (gallery.all-2)) {
			gallery.current = (gallery.all-1);
			$('gallery_next').hide();
			if (gallery.all == 2) {
				$('gallery_prev').show();
			}
		}
		else {
			if ($('gallery_prev').getStyle('display') == "none") {
				$('gallery_prev').show();
			}
			gallery.current++;
		}

		gallery.resizeGallery(this.images[gallery.current].width,this.images[gallery.current].height);

		$('gimage').update(this.images[gallery.current]);
		gallery.updateCounter();
		//gallery.updateTitle();
	},

	prev:function(){
		if (gallery.current <= 1) {
			gallery.current = 0;
			$('gallery_prev').hide();
			if (gallery.all == 2) {
				$('gallery_next').show();
			}
		}
		else {
			if ($('gallery_next').getStyle('display') == "none") {
				$('gallery_next').show();
			}
			gallery.current--;
		}
		gallery.resizeGallery(this.images[gallery.current].width,this.images[gallery.current].height);

		$('gimage').update(this.images[gallery.current]);
		gallery.updateCounter();
		//gallery.updateTitle();
	},

	updateCounter:function(){
		var string = (parseInt(gallery.current)+1) +" / "+gallery.all;
		$('gallery_counter').update(string);
	},

	updateTitle:function(){
		$('gallery_title').update(this.alltitle[gallery.current]);
	},

	overlayHide:function(){
		$(this.overlay).hide();
	},

	activateNext:function(rowid){
		$('grow'+(rowid-1)).hide();
		$('grow'+rowid).show();
	},

	activatePrev:function(rowid){
		$('grow'+(parseInt(rowid)+1)).hide();
		$('grow'+rowid).show();
	}
}

