
var newLightBox;

var LightBox = Class.create();
LightBox.prototype = {
  
  initialize: function() {
    
    // creating links
    var lightbox_links = $$('a.lightbox');
    for( var i=0; i<lightbox_links.length; i++) {
      var elm = lightbox_links[i];
      var url = elm.href;
      Element.observe(elm,'click',this.create.bindAsEventListener(this,url,true));
    }
    
    // creating base elements
    this.create_base_elements();
        
  },
  
  create_base_elements: function() {
    
    var lightbox_close_button = new Element('span',{ 'class': 'close', 'id': 'close_lightbox' }).update('close');
    Element.observe(lightbox_close_button,'click',this.hide_lightbox.bindAsEventListener(this));
        
    var lightbox_content = new Element('div',{ 'id': 'lightbox_content' });
    lightbox_content.insert(lightbox_close_button);
    lightbox_content.insert(new Element('div',{ 'id': 'lightbox_html' }).update('<p>some text</p>') );
    
    var lightbox_container = new Element('div',{ 'id': 'lightbox_container' }).update(lightbox_content);
    lightbox_container.hide();
    
    var lightbox_outer = new Element('div',{ 'id': 'lightbox_overlay' }).update(' ');
    lightbox_outer.setStyle({ opacity: 0.7 });
    lightbox_outer.hide();
    Element.observe(lightbox_outer,'click',this.hide_lightbox.bindAsEventListener(this));
    
    var body = $$('body')[0];
    body.insert(lightbox_outer);
    body.insert(lightbox_container);
    
  },
  
  center_contents: function() {
    
    var contents = $('lightbox_container');
    
    var page_dimensions = this.getPageSize();
    var viewport_dimensions = document.viewport.getDimensions();
    var contents_dimensions = contents.getDimensions();
    var page_scroll = document.viewport.getScrollOffsets();

    $('lightbox_overlay').setStyle({ 'height': page_dimensions[1]+'px', 'width': page_dimensions[0]+'px' });
    
    var top = page_scroll[1] + (viewport_dimensions.height / 10);
    var left = page_scroll[0];

    contents.setStyle({ 'top': top+'px', 'left': left+'px' });
    
  },
  
  show_lightbox: function(e) {
    this.select_sort(true);
    new Effect.Appear('lightbox_overlay',{ 'duration': 0.2, 'to': 0.7 });
    new Effect.Grow('lightbox_container',{ 'duration': 0.2 });
  },
  
  hide_lightbox: function(e) {
    this.select_sort(false);
    $('lightbox_container').hide();
    new Effect.Fade('lightbox_overlay',{ 'duration': 0.2 });
  },
  
  create: function(e,url,show_close_button) {
    
    var close_button = $('close_lightbox');
    
    var exp = /^(.+)\?(.+)$/;
    var url_items = exp.exec(url);
    
    var params = url_items ? url_items[2].toQueryParams() : {};
    var contents = url_items ? url_items[1] : url;
    
    params['lightbox'] = 1;
    
    new Ajax.Request(contents,{
      method: 'post',
      parameters: params,
      onComplete: function(t) {
        
        $('lightbox_html').update(t.responseText);
        
        if( close_button ) {
          if( show_close_button ) { close_button.show(); }
          else { close_button.hide(); }
        }
        
        this.center_contents();
        this.show_lightbox();
        
      
      }.bind(this),
      onException: function(t,e) {
        alert('error: '+e);
      }
    });
    
    if( e ) Event.stop(e);    
    
  },
  
  getPageSize: function() {

	  var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	},
	
	select_sort: function(disable) {
	  
	  var selects = $$('body').first().select('select');
	  
	  var browser = navigator.userAgent;
	  
	  if( /MSIE 6/.test(browser) ) {
  	  if( disable ) {
	    
  	    selects.each(function(elm){
  	      elm.setStyle({ 'visibility': 'hidden' });
  	    });
	    
  	  } else {
	      var delay_rate = this.delay_rate;
	      
  	    selects.each(function(elm){
  	      elm.setStyle({ 'visibility': 'visible' });
  	    });
	    
  	  }
	  }
	  
	}
  
};

function initLightBox() { newLightBox = new LightBox(); }
document.observe('dom:loaded',initLightBox);