var CallXHR = function(url, func){
	var xhr; 
	try{ xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
	catch(e){
		try{ xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
		catch(e2){
          		try{ xhr = new XMLHttpRequest(); }
          		catch(e3){ xhr = false; }
        	}
     	}
	
	xhr.onreadystatechange  = function(){ 
         if(xhr.readyState  == 4){
              if(xhr.status  == 200){
                 func(xhr.responseText); 
              }
         }
    }; 

   xhr.open( "GET", url,  true); 
   xhr.send(null); 
}

window.addEvent('domready', function(){
	var handler = $('wtgallery_category_area');
	if(handler){
		var cats = handler.getElements('a');
		var reg=new RegExp("tx_wtgallery_pi1%5Bcategory%5D=[0-9]+","g");
		var coord = $(document.body).getCoordinates();
		var box = new Element('div', {
			'id': 'wtgallery_category_lightbox'
		});
		var overlay = new Element('div', {
			'class': 'overlay',
			'styles':{
				'opacity': 0.9
			}
		}).addEvent('click', function(){
			box.setStyle('display', 'none');
		});
		var lightbox = new Element('div');
		box.adopt([overlay,lightbox]);
		$(document.body).adopt(box);
		var showimg = function(){
			var detail = lightbox.getElement('div.detail');
			if(detail){
				$try(function(){detail.getElement('img').destroy();});
				var imgclone = new Element('img', {'src': this.retrieve('img')});
				detail.adopt(imgclone);
				box.setStyle('display', 'block');
			
				overlay.setStyles({
					'width': '100%',
					'min-height': $(document.body).getScrollSize().y,
					'height': '100%'
				});
			
				/*var diff = detail.getSize().y-imgclone.getSize().y-20;
				imgclone.setStyle('margin-top', (diff>0)?diff/2:0);*/
			}
		}
		if(cats){
			cats.each(function(cat){
				cat.addEvent('click', function(e){
					new Event(e).stop();
					var params = this.retrieve('params');
					if(!params){
						for(var i=0; i<5 && !params; i++){
							params = reg.exec(this.getProperty('href'));
						}
						this.store('params', params);
					}
					if(params){
						var update = function (result){
							lightbox.set('html', result);
							var detail = lightbox.getElement('div.detail');
							if(detail){
								var imgs = lightbox.getElements('div.thumbnails a');
								if(imgs){
									imgs.each(function(img,idx){
										img.store('img',img.getProperty('href'));
										img.setProperty('href', '#');
										if(idx==0){
											showimg.bind(img).delay(0);
										}
										img.addEvent('click', function(){
											imgs.removeClass('selected');
											showimg.bind(this).delay(0);
										});
									});
								}
								lightbox.getElements('div.close').each(function(close){
									close.addEvent('click', function(){
										box.setStyle('display', 'none');
									});
								});
								lightbox.getElements('div.next').each(function(next){
									next.addEvent('click', function(){
										var idx = detail.retrieve('current');
										if(idx>=0 && imgs && imgs[idx+1]){
											showimg.bind(imgs[idx+1]).delay(0);
											detail.store('current',idx+1);
										}
									});
								});
								lightbox.getElements('div.previous').each(function(previous){
									previous.addEvent('click', function(){
										var idx = detail.retrieve('current');
										if(idx>0 && imgs && imgs[idx-1]){
											showimg.bind(imgs[idx-1]).delay(0);
											detail.store('current',idx-1);
										}
									});
								});
							}
						}
						lightbox.empty();
						CallXHR('index.php?id=13&type=346&plugin=tx_wtgallery_pi1&no_cache=1&'+params, update);
					}
				});
			});
		}
	}
});
