
//var isIE = (document.all) ? true : false;

//var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);


function Each(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = function(destination, source) {
  for (var property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Function.prototype.bind = function(object) {
  var __method = this, args = Array.apply(null, arguments); args.shift();
  return function() {
    return __method.apply(object, args);
  }
}

var OverLay = Class.create();
OverLay.prototype = {
  initialize: function(overlay, options) {
	this.Lay = $(overlay);
	
	
	this._size =  function(){};
	
	this.SetOptions(options);
	
	this.Color = this.options.Color;
	this.Opacity = parseInt(this.options.Opacity);
	this.zIndex = parseInt(this.options.zIndex);
	
	this.Set();
  },
  
  SetOptions: function(options) {
    this.options = {
		Color:		"#fff",
		Opacity:	50,
		zIndex:		1000
    };
    Object.extend(this.options, options || {});
  },
 
  Set: function() {
	this.Lay.style.display = "none";
	this.Lay.style.zIndex = this.zIndex;
	this.Lay.style.left = this.Lay.style.top = 0;
	
	if(Browser.ie == "6.0"){
		this.Lay.style.position = "absolute";
		this._size = function(){
			this.Lay.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px";
			this.Lay.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px";
		}.bind(this);
		
		this.Lay.innerHTML = '<iframe style="position:absolute;top:0;left:0;width:100%;height:100%;filter:alpha(opacity=0);"></iframe>'
	} else {
		this.Lay.style.position = "fixed";
		this.Lay.style.width = this.Lay.style.height = "100%";
	}
  },
  
  Show: function() {
	
	box.Fixed = true;
	box.OverLay.Color = "#000";
	box.Over = true;
	this.Lay.style.backgroundColor = this.Color;
	
	if(Browser.ie){
		this.Lay.style.filter = "alpha(opacity:" + this.Opacity + ")";
	} else {
		this.Lay.style.opacity = this.Opacity / 100;
	}
	
	if(Browser.ie == "6.0"){ this._size(); window.attachEvent("onresize", this._size); }
	
	this.Lay.style.display = "block";
  },

  Close: function() {
	this.Lay.style.display = "none";
	if(Browser.ie == "6.0"){ window.detachEvent("onresize", this._size); }
  }
};
var LightBox = Class.create();
LightBox.prototype = {
  initialize: function(box, overlay, options) {
	
	this.Box = $(box);
	
	this.OverLay = new OverLay(overlay, options);
	this.SetOptions(options);
	
	this.Fixed = !!this.options.Fixed;
	this.Over = !!this.options.Over;
	this.Center = !!this.options.Center;
	this.onShow = this.options.onShow;
	
	this.Box.style.zIndex = this.OverLay.zIndex + 1;
	this.Box.style.display = "none";
	
	
	if(Browser.ie == "6.0"){ this._top = this._left = 0; this._select = []; this._fixed = this.SetFix.bind(this); }
  },
  
  SetOptions: function(options) {
    this.options = {
		Fixed:		false,
		Over:		true,
		Center:		false,
		onShow:		function(){}
	};
    Object.extend(this.options, options || {});
  },
  
  SetFix: function(){
	var iTop =  document.documentElement.scrollTop - this._top + this.Box.offsetTop, iLeft = document.documentElement.scrollLeft - this._left + this.Box.offsetLeft;
	
	if(this.Center){ iTop += this.Box.offsetHeight / 2; iLeft += this.Box.offsetWidth / 2; }
	
	this.Box.style.top = iTop + "px"; this.Box.style.left = iLeft + "px";
	
	this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft;
  },
 
  Show: function(options) {
	
	if(this.Fixed){
		if(Browser.ie == "6.0"){
			
			this.Box.style.position = "absolute";
			this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft;
			window.attachEvent("onscroll", this._fixed);
		} else {
			this.Box.style.position = "fixed";
		}
	} else {
		this.Box.style.position = "absolute";
	}
	
	if(this.Over){
		
		this.OverLay.Show();
	} else {

		if(Browser.ie == "6.0"){
			this._select = [];
			var oThis = this;
			Each(document.getElementsByTagName("select"), function(o){
				if(oThis.Box.contains ? !oThis.Box.contains(o) : !(oThis.Box.compareDocumentPosition(o) & 16)){
					o.style.visibility = "hidden"; oThis._select.push(o);
				}
			})
		}
	}
	
	this.Box.style.display = "block";
	

	if(this.Center){
		 this.Box.style.top =this.Box.style.left = "50%";
	
		var iTop = - this.Box.offsetHeight / 2, iLeft = - this.Box.offsetWidth / 2;

		if(!this.Fixed || Browser.ie == "6.0"){ iTop += document.documentElement.scrollTop; iLeft += document.documentElement.scrollLeft; }
		this.Box.style.marginTop =  iTop + "px"; this.Box.style.marginLeft = iLeft + "px";
	}
	
	this.onShow();
  },

  Close: function() {
	this.Box.style.display = "none";
	this.OverLay.Close();
	if(Browser.ie == "6.0"){ window.detachEvent("onscroll", this._fixed); Each(this._select, function(o){ o.style.visibility = "visible"; }); 
	}
  }
};

if(!Browser.ie){
	HTMLElement.prototype.__defineGetter__("currentStyle", function () {
		return this.ownerDocument.defaultView.getComputedStyle(this, null);
	});
}

