var LightBoxItems;


SqueezeBox.lightBox = Hash();
SqueezeBox.lightBox.groups = new Hash();


SqueezeBox.extend({
  onKey: function(e){
    switch (e.key){
      case "esc" : this.close();
      case "up": case "down": return false;
      case "right": if(this.options.handler == 'lightbox') return this.lightBoxNext();
      case "left": if(this.options.handler == 'lightbox') return this.lightBoxPrev();
        
    }
  },
  lightBoxNext: function(){
        if(this.lightBox.nextItem == null) return false;
        return this.open(this.lightBox.nextItem,{handler:'lightbox'});
  },

  lightBoxPrev: function(){        
        if(this.lightBox.prevItem == null) return false;
        return this.open(this.lightBox.prevItem,{handler:'lightbox'});  
   }
});





SqueezeBox.parsers.lightbox = SqueezeBox.parsers.image;
SqueezeBox.handlers.extend({
	lightbox: function(url) {
		var size, tmp = new Image();

              if(this.element.rel != 'lightbox') {
                 this.lightBox.prevButton.setStyle('display','block');
                 this.lightBox.nextButton.setStyle('display','block');

                 index = this.lightBox.groups[this.element.rel].indexOf(this.element);
                 
                 if(this.element !== this.lightBox.groups[this.element.rel].getLast()){
                   this.lightBox.nextItem = this.lightBox.groups[this.element.rel][index+1];
                   this.lightBox.preload.src = this.lightBox.nextItem.href;
                 } else
                   this.lightBox.nextItem = this.lightBox.groups[this.element.rel][0];
                 
                 if(index != 0) 
                   this.lightBox.prevItem = this.lightBox.groups[this.element.rel][index-1];
                 else 
                   this.lightBox.prevItem =  this.lightBox.groups[this.element.rel].getLast();
                 
              } else {
                this.lightBox.prevButton.setStyle('display','none');
                this.lightBox.nextButton.setStyle('display','none');
              }
              
              this.addEvent('onClose',function (){
                SqueezeBox.lightBox.prevButton.setStyle('display','none');
                SqueezeBox.lightBox.nextButton.setStyle('display','none');
              });


              this.asset = null;
		tmp.onload = tmp.onabort = tmp.onerror = (function() {
			tmp.onload = tmp.onabort = tmp.onerror = null;
			if (!tmp.width) {
				this.onError.delay(10, this);
				return;
			}
			var box = this.doc.getSize();
			box.x -= this.options.marginImage.x;
			box.y -= this.options.marginImage.y;
			size = {x: tmp.width, y: tmp.height};
			for (var i = 2; i--;) {
				if (size.x > box.x) {
					size.y *= box.x / size.x;
					size.x = box.x;
				} else if (size.y > box.y) {
					size.x *= box.y / size.y;
					size.y = box.y;
				}
			}
			size.x = size.x.toInt();
			size.y = size.y.toInt();
			this.asset = $(tmp);
			tmp = null;
			this.asset.width = size.x;
			this.asset.height = size.y;
			this.applyContent(this.asset, size);
		}).bind(this);
		tmp.src = url;
		if (tmp && tmp.onload && tmp.complete) tmp.onload();
              return (this.asset) ? [this.asset, size] : null;
	}
});





window.addEvent('domready', function() {
       SqueezeBox.initialize({});
       
       SqueezeBox.lightBox.prevButton = new Element('a',{id:'sbox-lb-prev',onclick:'return SqueezeBox.lightBoxPrev();',style:'display:none'});
       SqueezeBox.lightBox.nextButton = new Element('a',{id:'sbox-lb-next',onclick:'return SqueezeBox.lightBoxNext();',style:'display:none'});
       SqueezeBox.lightBox.preload = new Image();
       
       SqueezeBox.lightBox.nextButton.inject(SqueezeBox.win);
       SqueezeBox.lightBox.prevButton.inject(SqueezeBox.win);




       SqueezeBox.lightBox.Items = $$("a").filter(function(el) {return el.rel && el.rel.test(/^lightbox/i);});       
       SqueezeBox.lightBox.Items.each(function (i,k){
         if(SqueezeBox.lightBox.groups[i.rel] == null) {
           lightBoxFilter = function(el) {
             return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
           };
           SqueezeBox.lightBox.groups[i.rel] = SqueezeBox.lightBox.Items.filter(lightBoxFilter,i);
         }
       });
       SqueezeBox.assign(SqueezeBox.lightBox.Items,{handler:'lightbox'});

       //window.constructor.prototype.close = function(){alert('b')};
       SqueezeBox.assign($$('a.iframe'), { 
         handler:'iframe', 
         onOpen: function(){},
         parse:'rev' }
       );
       document.domain = 'elam.sk';

      
});