/*****************************************************************
/ dd-popup based on MOOdalBox [http://www.e-magine.ro/moodalbox]
/****************************************************************/
var ddPopup = {

	init: function (options) {

		var dOverflow = document.body.style.overflow;

		/*/ init default options
		this.options = Object.extend({
			evalScripts: 		true,
			evalResponse: 		true
		}, options || {});*/

		this.eventKeyDown = this.keyboardListener.bindWithEvent(this);
		this.eventPosition = this.position.bind(this);

		//HTML
		// the overlay (clickable to close)
		this.overlay = new Element('div').setProperty('id', 'dd_overlay').injectInside(document.body);
		// the actual page contents
		this.contents = new Element('div').setProperty('id', 'dd_contents').setStyles({width: '480px', marginLeft: '-240px', height: '0', overflow: 'hidden'}).injectInside(document.body);

		//get current document properties
		this.doc = {
			origOverflow: document.body.style.overflow
		}

		// attach the close event to the overlay
		this.overlay.onclick = this.close.bind(this);

		// init the effects
		this.fx = {
			overlay: 	this.overlay.effect('opacity', { duration: 500 }).hide(),
			contents: 	this.contents.effect('opacity', { duration: 500})
		};

		this.ajaxRequest = Class.empty;

	},

	click: function(link) {
		return this.open (link.href, link.title, link.rel);
	},

	open: function(sourceFile) {
		this.href = sourceFile;
		this.position();
		this.setup(true);
		this.top = Window.getScrollTop() + (Window.getHeight() / 15);
		this.contents.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.custom(0.7);
		return this.loadContents(sourceFile);
	},

	position: function() {
		this.overlay.setStyles({top: Window.getScrollTop()+'px', height: Window.getHeight()+'px'});
	},

	setup: function(open) {
		var elements = $A($$('object'));
		elements.extend($$(window.ActiveXObject ? 'select' : 'embed'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
	},

	loadContents: function() {

		document.body.style.overflow = 'hidden';

		this.fx.contents.hide();

		// AJAX call here
		var showContent = this.showContent.bind(this);
		var ajaxFailure = this.ajaxFailure.bind(this);
		var ajaxOptions = {
			method: 		'get',
			update: 		this.contents,
			onComplete: 	showContent,
			onFailure: 		ajaxFailure
			};
		this.ajaxRequest = new Ajax(this.href, ajaxOptions).request();

		return false;
	},

	ajaxFailure: function (){
		this.contents.setHTML('');
		this.error.clone().injectInside(this.contents);
	},


	showContent: function() {
		this.contents.setStyles({height: '', display: ''});
		this.fx.contents.custom(0,1);
		//added tracking with delay so it doesn't slow down loading
		setTimeout("pageTracker._trackPageview('"+this.href.split('?')[0]+"')",2000);
	},

	keyboardListener: function(event) {
		// close the ddPopup when the user presses CTRL + W, CTRL + X, ESC
		if ((event.control && event.key == 'w') || (event.control && event.key == 'x') || (event.key == 'esc')) {
			this.close();
			event.stop();
		}
	},

	close: function() {
		for(var f in this.fx) this.fx[f].clearTimer();
		this.fx.contents.chain(this.setup.pass(false, this)).custom(0);
		this.fx.overlay.chain(this.setup.pass(false, this)).custom(0);
		this.contents.setStyles({height: '0', display: 'none'});
		document.body.style.overflow = this.doc.origOverflow;
		//track parent page again, as we are 'returning' to it
		pageTracker._trackPageview();
		return false;
	}

};

// startup
Window.onDomReady(ddPopup.init.bind(ddPopup));