var gallery = {
	gallery_root: "nabazgallery",
	overlay:"gallery_overlay",
	img_width:"640",
	img_height:"425",

	init:function(){
		if (this.all) {

		}else{
			var images = [];
			var imagepath = [];
			var imagetitle = [];

			var allimgs = $A($(this.gallery_root).select("img")).each(function(s) {
				var bigimagesrc = s.src.replace("small/","").replace("mid/","");
				imagepath.push(bigimagesrc);
				imagetitle.push(s.alt);
				if (document.images)
				{
				   var im = new Image();
				   im.src = bigimagesrc;
				   im.width = gallery.img_width;
				   im.height = gallery.img_height;
				   im.title = s.alt;
				   images.push(im);
				}
			});
			this.images = images;
			this.alltitle = imagetitle;
			this.allimgs = imagepath;
			this.all = this.allimgs.length;
		}
	},

	show:function(id){
		Video.hide();
		this.current = id;
		//this.init();
		this.overlayShow();
		this.createGallery();
		$(gallery.overlay).observe('click', function(){ gallery.close('nabaz_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 = $('nabaz').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(){
	    var info_height = 40;
	    var padding = 0;
		var width = 640 + (2*padding);
	    var height = 425 + + (2*padding) + info_height;
	    var topscroll = this.getScrollTop();
		var id = "nabaz_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.img_width+"\" height=\""+this.img_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>" + butprev + "  "+ butnext +  "</div>\
		<div class=\"space3\"></div>\
	    <div id=\"gimage\">" + content + "</div>\
	      <div class=\"space3\"></div>\
	      <div class=\"gallery_info\">\
			 <div class=\"left\"><span class=\"gallery_title\" id=\"gallery_title\">" + this.alltitle[this.current] + "</span><br /><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>\
		  <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();
		}
	},

	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++;
		}
		$('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--;
		}
		$('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();
	}
}

var Video = {
	show:function(videopath){
		$('product_image').hide();
		var s1 = new SWFObject('./mediaplayer/player.swf','player','475','315','9');
		s1.addParam('allowfullscreen','true');
		s1.addParam('allowscriptaccess','always');
		s1.addParam('autostart','true');
		s1.addParam('flashvars','file='+videopath+'');
		s1.write('product_video');
		$('product_video').show();
	},

	hide:function(){
		if ($('product_video')) {
			Video.opened = "close";
			$('product_image').show();
			$('product_video').hide();
			$('product_video').update("");
		}
	}

};

var LoginForm = {

    toggle:function(){
   		if ($('loginform').getStyle("display") == "none") {
   			LoginForm.show();
   		} else {
		    LoginForm.hide();
		}
   	},

   show:function(){
   		$('loginform').show();
   		$('nabaz').observe('dblclick', function(){ LoginForm.toggle();});
   },

   hide:function(){
   		$('loginform').hide();
   		Event.stopObserving('nabaz', 'dblclick');
   }
}

var ProductLinkOnHome = {
   show:function(id){
   		$('imgrgb'+id).show();
   		$('imggray'+id).hide();
   		$('prodonhomenew'+id).hide();
   		$('prodonhomename'+id).show();

   },
   hide:function(id){
   		$('imgrgb'+id).hide();
   		$('imggray'+id).show();
   		$('prodonhomenew'+id).show();
   		$('prodonhomename'+id).hide();
   },
   go:function(loc){
   window.location.href = loc;
   }
}


function GALLERYINIT(){
	if (document.getElementById('nabazgallery')) {
		gallery.init();
	}
}


document.observe("dom:loaded", function() {GALLERYINIT();});

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);

