var PopupContent = Class.create({
	initialize: function() {
		
		// Get options
		this.options = Object.extend({					 
			minWidth:			0,
			maxWidth:			0,
			minHeight:			0,
			maxHeight:			0,
			topOffset:			false,
			coverColor:			'#aaaaaa',
			coverOpacity:		0.7,
			coverZIndex:		50,
			autoScroll:			false,
			autoBorder:			false,
			actionCallback:		null,
			customCallback:		null
		}, arguments[0] || {} );
		
		// Bind event functions
		this.bonClick = this.hide.bindAsEventListener(this);
		this.bselectAction = this.selectAction.bindAsEventListener(this);
		this.bcustomAction = this.customAction.bindAsEventListener(this);
		
		// Make a cover for the gallery
		this.cover = new PopupCover({color: this.options.coverColor, opacity: this.options.coverOpacity, closeCallback: this.bonClick});
		
		// Make the structure for content
		this.content = $(Builder.node('div'));
		if(this.options.autoBorder === true)
		{
			this.border = $(Builder.node('table', { className: 'popupcontent-border', style: 'display:none;position:absolute;', cellpadding: '0', cellspacing: '0' }, [
				Builder.node('tbody', [
					Builder.node('tr', [
						Builder.node('td', { className: 'popupcontent-ul' }),
						Builder.node('td', { className: 'popupcontent-upper' }),
						Builder.node('td', { className: 'popupcontent-ur' })
					]),
					Builder.node('tr', [
						Builder.node('td', { className: 'popupcontent-left' }),
						Builder.node('td', { className: 'popupcontent-center' }, [this.content]),
						Builder.node('td', { className: 'popupcontent-right' })
					]),
					Builder.node('tr', [
						Builder.node('td', { className: 'popupcontent-ll' }),
						Builder.node('td', { className: 'popupcontent-lower' }),
						Builder.node('td', { className: 'popupcontent-lr' })
					])
				])
			]));
		}
		else {
			this.border = $(Builder.node('div', { className: 'popupcontent-border', style: 'display:none;position:absolute;' }, [this.content])); }
		
		if(this.options.autoScroll === true) {
			this.content.setStyle({overflow: 'auto'}); }
		else {
			this.content.setStyle({overflow: 'hidden'}); }
		
		// Insert the structure
		$(document.body).insert(this.border);
		
		// Setup the class to control loading ajax content
		this.ajaxContent = new AjaxContent(this.content, {
			linkClass:			'popupcontent-link',
			waitImages:			true,
			loadingDiv:			null,
			loadDelay:			800,
			preCallback:		this.border.hide.bind(this.border),
			postCallback:		this.contentLoaded.bind(this)
		});
		
		//window.onscroll = function(){alert('here')};
		
		// Attach css dynamically only if the border is being used
		if(this.options.autoBorder === true) {
			Loader.css('/popupContent/popupContent.css'); }
	},
	
	selectAction: function(event) {
		event.stop();
		
		if(this.options.actionCallback !== null) {
			this.options.actionCallback(event); }
		
		this.hide();
	},
	
	customAction: function(event) {
		event.stop();
		
		if(this.options.customCallback !== null) {
			this.options.customCallback(event); }
	},
	
	attachAction: function() {
		$$('.popupcontent-border .popupcontent-action').each(function(elem) {
			elem.observe('click', this.bselectAction);
		}, this);
	},
	
	attachCustom: function() {
		$$('.popupcontent-border .popupcontent-custom').each(function(elem) {
			elem.observe('click', this.bcustomAction);
		}, this);
	},
	
	cleanAction: function() {
		$$('.popupcontent-border .popupcontent-action').each(function(elem) {
			elem.stopObserving('click', this.bselectAction);
		}, this);
	},
	
	cleanCustom: function() {
		$$('.popupcontent-border .popupcontent-custom').each(function(elem) {
			elem.stopObserving('click', this.bcustomAction);
		}, this);
	},
	
	contentLoaded: function() {
		
		// Attach events for action objects
		this.attachAction();
		
		// Attach events for custom actions
		this.attachCustom();
		
		// Set the size to auto
		this.content.setStyle({width: 'auto', height: 'auto'});
		
		// Get dimensions
		this.borderDim = this.border.getDimensions();
		
		// Check the max and min width
		if(this.options.maxWidth > 0 && this.borderDim.width > this.options.maxWidth)
		{
			this.content.setStyle({width: this.options.maxWidth + 'px'});
			this.borderDim = this.border.getDimensions();
		}
		else if(this.options.minWidth > 0 && this.borderDim.width < this.options.minWidth)
		{
			this.content.setStyle({width: this.options.minWidth + 'px'});
			this.borderDim = this.border.getDimensions();
		}
		
		// Check the max and min height
		if(this.options.maxHeight > 0 && this.borderDim.height > this.options.maxHeight)
		{
			this.content.setStyle({height: this.options.maxHeight + 'px'});
			this.borderDim = this.border.getDimensions();
		}
		else if(this.options.minHeight > 0 && this.borderDim.height < this.options.minHeight)
		{
			this.content.setStyle({height: this.options.minHeight + 'px'});
			this.borderDim = this.border.getDimensions();
		}
		
		// Get the top position of the content
		var top = 0;
		if(this.options.topOffset === false) {
			top = Math.round(this.windowDim.height/2 - this.borderDim.height/2 + this.windowPos.top); }
		else {
			top = Math.round(this.options.topOffset + this.windowPos.top); }
		
		// Position the box and show it
		this.border.setStyle({
			left: Math.round(this.windowDim.width/2  - this.borderDim.width/2  + this.windowPos.left) + 'px',
			top:  top  + 'px'
		});
		this.border.show();
	},
	
	show: function(url) {
		
		// Get the window position
		this.windowDim = document.viewport.getDimensions();
		this.windowPos = document.viewport.getScrollOffsets();
		
		// Make sure the zIndex gets updated and show the cover
		this.cover.zIndex(this.options.coverZIndex);
		this.cover.show();
		
		// Set the z-index for the content
		this.border.setStyle({zIndex: (this.options.coverZIndex + 1)});
		
		// Load the content
		this.ajaxContent.getContent(url);
	},
	
	hide: function() {
		
		// Detach events for action objects before we hide it
		this.cleanAction();
		
		// Detach events for custom action
		this.cleanCustom();
		
		this.cover.hide();
		//this.border.hide();
		Effect.toggle(this.border, 'appear', { duration: 0.25 });
	}
});