var gallery = {
	gallery_root: "xlgallery",
	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 authors = [];
			var imagepath = [];
			var size = [];
			var width, height;

			   var allimgs = $A($(this.gallery_root).select("img")).each(function(s) {
				var bigimagesrc = s.src.replace("small/","").replace("mid/","");

				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].substring(5);
				   height = size[1];
				   im.width = width;
				   im.height = height;
				   im.title = s.alt;
				   images.push(im);

				   if (s.nextSibling) {
				   	authors.push(s.nextSibling.innerHTML);
				   }
				}
			});
			this.images = images;
			this.allimgs = imagepath;
			this.authors = authors;
			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('xl_galery');});
              $('gimage').observe('click', function(){ if(gallery.current < (gallery.all-1)) gallery.next(); else gallery.close('xl_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 = $('xl').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');
	},

	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) + leftpanel;
	    var height = parseInt(new_height) + (2*padding) + info_height;
	    var topscroll = this.getScrollTop();
	    var id = "xl_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>";


	    var autor = this.authors[gallery.current];

	    if (document.getElementById('infopanel_info')) {
	    	var infopanel = "<img height=\"49\" border=\"0\" width=\"84\" src=\"images/default/Xl_logo.gif\" alt=\"XL logo\"/>" + " <div class=\"space20\"></div><span class=\"gpanel_title\">"+ $('infopanel_title').innerHTML + "</span><div class=\"space20\"></div><span class=\"gpanel_info\">"+ $('infopanel_info').innerHTML + "<div id=\"infoAuthor\">"+autor+"</div></span>";
	    }else {
		var infopanel = "";
	    }

	    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=\"right\" id=\"gallerypanel\" style=\"width:"+ (leftpanel-10) +"px; padding:0 5px 0 5px;\">"+ infopanel+"</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) + leftpanel;
	    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;");
        $('xl_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]);
		$('infoAuthor').update(this.authors[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]);
		$('infoAuthor').update(this.authors[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();
	}
}

function GALLERYINIT(){
	if (document.getElementById('xlgallery')) {
		gallery.init();
	}
}

var Layout = {
	_resize:function(){
		if ($('agentura_right')) {
			$('agentura_right').setStyle('height:'+$('agentura_middle').getHeight()+'px;');
		}
	}
}



function openPopup() {
	window.open('?action=popup&class=xl_koncert&iid=<..iid.>&tset=default&tname=popup&lng=default', 'Detail', 'width=<.flash_x.>, height=<.flash_y.>, resizable=yes, scrollbars=no');
	}



function openPromo(w, h) {
	window.open("", "PROMO" ,"top=0, left=0, width="+w+", height="+h+", resizable=yes, menubar=no, scrollbars=no");
}



function openFotkaPopup(idfotka) {
	window.open("?action=popup&class=xl_fotka&iid=" + idfotka + "&tset=default&tname=popup&lng=default", 'Detail', 'width=750, height=600, scrollbars=no');
}





document.viewport = {

  getDimensions: function() {

    return { width: this.getWidth(), height: this.getHeight() };

  }

};



(function(v) {

  var element;

  function define(D) {

    element = element || (function() {

      var doc = document;

      return ('clientWidth' in doc) ? doc : // Safari < 3

       ('async' in doc) ? doc.body : doc.documentElement; // Opera < 9.5 : Others

    })();



    v['get' + D] = function() { return element['client' + D] };

    return v['get' + D]();

  }



  v.getWidth = define.curry('Width');

  v.getHeight = define.curry('Height');

})(document.viewport);



document.observe("dom:loaded", function() {
	GALLERYINIT();
	Layout._resize();
});





