
var x = {
	archive : {},
	
	layer : function(id) {
		if (typeof this.archive[id] == "undefined") {
			this.archive[id] = new this.fc(id);
		}
		return this.archive[id];
	},
	
	
	z : 100,
	
	zchange : function(id) {
		if (x.archive[id].zindex) return;
		
		x.z = x.z + 3;
		
		o1 = x.gebi('overlay_' + id);
		o2 = x.gebi('hideselect_' + id);
		o3 = x.gebi(id);
		
		if (o1 && o2) {
			o1.style.zIndex = x.z - 1;
			o2.style.zIndex = x.z - 2;
		}
		o3.style.zIndex = x.z;
	},
	
	debug : function(e) {
		alert(e);
	},
	
	gebi : function(n) {
		if(!n) return false;
		if(!document.getElementById(n)) return false;
		return document.getElementById(n)
	},
	
	bubbling : function (e) {
		e = this.bubbling_ie(e); 
		if (!e.cancelBubble) e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		return false;
	},
	
	bubbling_ie : function (e) {
		if (!e) var e = window.event;
		return e;
	},
	
	pagesize : function(){
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		
		result = new Array(w,h) 
		return result;
	},
	
	pagescroll : function (){
		var t;
		if (self.pageYOffset) {
			t = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			t = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			t = document.body.scrollTop;
		}
		
		result = new Array('',t) 
		return result;
	},
	
	bounds : function(el, sp) {
		var left = el.offsetLeft;
		var top = el.offsetTop;

		for (var parent = el.offsetParent; parent; parent = parent.offsetParent) {
			if (sp && parent.className.indexOf('offsetparent') != -1) break;
			left += parent.offsetLeft;
			top += parent.offsetTop;
		}
		return { left:left, top:top, width:el.offsetWidth, height:el.offsetHeight };
	},
	
	changePosition : function(id) {
		var o = this.gebi(id);
		var page = this.pagesize();
		var pagey = this.pagescroll();

		switch(x.archive[id].pos) {
			case 'center':
				o.style.left = ((page[0] - this.bounds(o).width) / 2) + 'px';
				o.style.top = (pagey[1] + ((page[1] - this.bounds(o).height) / 2)) + 'px';
				break;
				
			case 'centertop': /* for iframe block */
				o.style.left = ((page[0] - this.bounds(o).width) / 2) + 'px';
				o.style.top = 70 + 'px';
				break;
				
			case 'opener':
				o.style.left = x.archive[id].opener.offsetLeft + 'px';
				o.style.top = x.archive[id].opener.offsetTop + 'px';
				break;
				
			case 'openerend':
				o.style.left = x.archive[id].opener.offsetLeft + x.bounds(x.archive[id].opener).width + 'px';
				o.style.top = x.archive[id].opener.offsetTop + 'px';
				break;
				
			case 'gallery':
				var parent = x.archive[id].view ? 0 : 1;
				o.style.left = this.bounds(x.archive[id].opener, parent).left + 'px';
				o.style.top = (this.bounds(x.archive[id].opener, parent).top - (x.gebi('fsUploadProgress1') ? x.gebi('fsUploadProgress1').scrollTop : 0)) + 'px';
				break;
			
			default :
				o.className = o.className.replace(' ' + x.archive[id].offset, '');
				if (page[0] < x.archive[id].size) {
					o.className = o.className + ' ' + x.archive[id].offset;
				}
		}
		
		//this.overlay(id);
	},
	
	overlay : function(id) {
		if (window.innerHeight && window.scrollMaxY) {	
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			yScroll = document.body.scrollHeight;
		} else {
			yScroll = document.body.offsetHeight;
		}
		$('#overlay_' + id).css('height',yScroll + 'px');
		$('#hidehelect_' + id).css('height',yScroll + 'px');
	},
	
	load : function(id, url, wid) {
		$.ajax({
			type: 'get',
			url: url,
			success: function(html) {
				$('#'+id).html(html);
				if (wid) x.changePosition(wid);
			}
		});
	}
};

/* constructor */
x.fc = function(id) {
	this.opener = null;
	this.id = id;
	this.parentid = null;
	this.eff = null;
	this.prev = false;
	this.offset = 'right';
	this.docclick = true;
	this.size = 0;
	this.view = null;
	this.width = null;
	this.overlay = null;
	this.overlayclick = true;
	this.pos = null;
	this.zindex = null;
	this.style = null;
	this.group = '';
	this.parent = null;
	this.child = {};
}

/* methods */
x.fc.prototype = {
	init : function(hash) {
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
	},
	
	childadd : function(el) {
		for (var parent = el.offsetParent; parent; parent = parent.offsetParent) {
			if (parent.className.indexOf('x') != -1 && parent.id){
				if (typeof x.archive[parent.id].child[this.id] == "undefined") x.archive[parent.id].child[this.id] = this.id;
				this.parent = parent.id;
				break;
			}
		}
	},
	
	childremove : function(id) {
		if (this.parent) delete x.archive[this.parent].child[id];
	},
	
	position: function() {
		if (!x.gebi(this.id)) return;
		
		x.gebi(this.id).className = x.gebi(this.id).className.replace(' ' + this.offset, '');
		this.size = x.gebi(this.id).offsetLeft + x.bounds(x.gebi(this.id)).width;
		
		x.changePosition(this.id);
		$(window).resize(function() {
			try {
				for (var i in x.archive) {
					if (i != "undefined") {
						x.changePosition(x.archive[i].id);
					}
				}
			} catch(e) {
				//return alert(e); 
			}
		});
		$(window).scroll(function() {
			try {
				for (var i in x.archive) {
					if (i != "undefined") {
						x.changePosition(x.archive[i].id);
					}
				}
			} catch(e) {
				//return alert(e); 
			}
		});
	},
	
	overlayAppend : function(id) {
		if (!x.gebi('hideselect_' + id)) {
			$('body').append('<iframe id="hideselect_' + id + '" class="x-hideselect"' + (this.zindex ? ' style="z-index:' + this.zindex + '"' : '') + '"></iframe><div id="overlay_' + id + '" class="x-overlay"' + (this.zindex ? ' style="z-index:' + this.zindex + '"' : '') + '></div>');
			
			$('#overlay_' + id).click(function(e){
				x.bubbling(e);
				if (x.archive[id].overlayclick) x.archive[id].winremove();
			});
		}
		
		//x.overlay(this.id);
		scroll_stop();
	},
	
	show : function() {
		var id = this.id;
		var po = x.gebi(this.parentid);
		
		this.opener.className = this.opener.className + ' click-active';
				
		switch (this.eff) {
			case 'slide':
				$('#'+this.id).slideDown('fast');
				break;    
			case 'fade':
				$('#'+this.id).fadeIn('fast');
				break; 
			default:
				$('#'+this.id).css({display:''});
				
		}
		
		if (po) po.className = po.className + ' b-active';

		$('#' + id).click(function(e) {
			x.bubbling(e);
			$('div.fld-select').css({display:'none'}); //hide select box
			if (x.gebi('fc')) x.gebi('fc').style.display = 'none'; //hide calendar
		});
		
		if (this.docclick) {
			$(document).unbind('click');
			$(document).click(function() { x.archive[id].hide(); });
		}
	},
	
	hide : function() {
		var po = x.gebi(this.parentid);
		
		switch (this.eff) {
			case 'slide':
				$('#'+this.id).slideUp('fast');
				break; 
			case 'fade':
				$('#'+this.id).fadeOut('fast');
				break; 
			default:
				$('#'+this.id).css({display:'none'});
		}
	
		if (this.opener) this.opener.className = this.opener.className.replace(' click-active', '');
		if (po) po.className = po.className.replace('b-active', '');
		
		delete x.archive[this.id];
	},
		
	toggle : function(e, _this) {
		x.bubbling(e);
		
		$('div.fld-select').css({display:'none'}); //hide select box
		if (x.gebi('fc')) x.gebi('fc').style.display = 'none'; //hide calendar
		
		if (!x.gebi(this.id)) return;
		try {
			if (!this.prev) {
				for (var i in x.archive) {
					if (i != 'undefined' && x.archive[i].id != this.id && (!x.archive[i].group || x.archive[i].group == this.group)) {
						if (x.archive[i].view) x.archive[i].winremove();
							else x.archive[i].hide();
					}
				}
			}

			if (x.gebi(this.id).style.display == 'none') {
				this.opener = _this;
				this.show();
				if (this.offset) this.position();
			} else if (x.gebi(this.id).style.display == '' && this.opener != _this) {
				this.opener = _this;
				if (this.offset) this.position();
			} else {
				this.opener = _this;
				this.hide();
			}
		} catch(e) {
			x.debug(e);
		}
		
		_this.blur();
	},
	
	create : function() {
		var c = this.style ? ' ' + this.style : '';
		var s = ' style="' + (this.width ? 'width:' + this.width : '') + '"';

		switch(this.view) {
			case 1:
				$('body').append('<div id="' + this.id + '" class="x' + c + ' offsetparent"' + (this.zindex ? ' style="z-index:' + this.zindex + '"' : '') + '><table class="x1"><tr><td class="x1-11 png"><div></div></td><td class="x1-12 pngscale"><div></div></td><td class="x1-13 png"><div></div></td></tr><tr><td class="x1-21 pngscale"><div></div></td><td class="x1-22"><div class="x-close"><a href="#close" onclick="x.layer(\'' + this.id + '\').winremove(); return false;"></a></div><div id="_' + this.id + '"' + s + '><div class="x-loading"></div></div></td><td class="x1-23 pngscale"><div></div></td></tr><tr><td class="x1-31 png"><div></div></td><td class="x1-32 pngscale"><div></div></td><td class="x1-33 png"><div></div></td></tr></table><div class="x-spacer png"></div></div>');
				break;
		}
	},
	
	win : function(e, _this, url) {
		x.bubbling(e);
		$('div.fld-select').css({display:'none'}); //hide select box
		
		var id = this.id;
		
		if (this.opener == _this) {
			this.winremove();
		} else {
			this.winclear();
		
			this.opener = _this;
			this.view = this.view ? this.view : 1;
			this.childadd(this.opener);
			
			$(_this).addClass('click-active');

			if (!this.prev) {
				try {
					for (var i in x.archive) {
						if (i != 'undefined' && x.archive[i].id != this.id && (!x.archive[i].group || x.archive[i].group == this.group)) {
							if (x.archive[i].view) x.archive[i].winremove();
								else x.archive[i].hide();
						}
					}
				} catch(e) {
					x.debug(e);
				}
			}

			try {		
				if (this.overlay) this.overlayAppend(this.id);
				this.create();
				this.position();
				
				$('#' + this.id).click(function(e) { x.bubbling(e); });
				if (!this.prev) {
					$(document).unbind('click');
					$(document).click(function() { if (x.archive[id] && x.archive[id] != 'undefined') x.archive[id].winremove(); });
				}
				
				x.zchange(this.id);

				if (url && x.gebi('_'+this.id)) x.load('_'+this.id, url, this.id);
			} catch(e) {
				x.debug(e);
			}
		}
		
		_this.blur();
	},
	
	winclear : function() {
		if (this.opener) $(this.opener).removeClass('click-active');
		if (x.gebi(this.id)) $('#' + this.id).remove();
	},
	
	winremove : function() {
		for (var i in this.child) {
			if (i != 'undefined') {
				x.archive[this.child[i]].winremove();
			}
		}
	
		if (!this.opener) return;
		
		this.childremove(this.id);

		$(this.opener).removeClass('click-active');		
		x.gebi(this.id).style.display = 'none';

		$('#' + this.id).remove();	//???
		
		$('#overlay_' + this.id + ',#hideselect_' + this.id).remove();
		
		if(!$('div.x-overlay').length) scroll_offers(1, 0);
		delete x.archive[this.id];
	}
}

