/**
 * Copyright 2008 networkteam - creative webprojects.
 *
 * NJS.IFrame based on original MooTools IFrame (by mad4milk)
 *
 **/
var NJS = NJS || {};

// Fix wrong document access for non-local src
NJS.IFrame = new Native({

	name: 'IFrame',

	generics: false,

	initialize: function(){
		var params = Array.link(arguments, {properties: Object.type, iframe: $defined});
		var props = params.properties || {};
		var iframe = $(params.iframe) || false;
		var onload = props.onload || $empty;
		delete props.onload;
		props.id = props.name = $pick(props.id, props.name, iframe.id, iframe.name, 'IFrame_' + $time());
		iframe = new Element(iframe || 'iframe', props);
		var onFrameLoad = function(){
			var host = $try(function(){
				return iframe.contentWindow.location.host;
			});
			if (host && host == window.location.host){
				var win = new Window(iframe.contentWindow);
				var doc = new Document(iframe.contentWindow.document);
				$extend(win.Element.prototype, Element.Prototype);
				onload.call(iframe.contentWindow, iframe.contentWindow.document);
			} else {
				onload.call(iframe.contentWindow);
			}
		};
		(!window.frames[props.id]) ? iframe.addListener('load', onFrameLoad) : onFrameLoad();
		return iframe;
	}

});

NJS.FrameBox = new Class({
	Implements: [Options, Events],
	options: {
		linkSelector: "a[rel='external']",
		
		iframeClass: 'external-iframe',
		
		externalStateId: 'external',
		
		duration: 'long',
		
		heightOffset: 0
	},
	// Current IFrame element
	frame: null,
	// Current external URL
	externalUrl: null,
	// Reference to label element (top of iframe position)
	labelElement: null,
	initialize: function(labelElement, options) {
		var that = this;
		this.labelElement = $(labelElement);
		this.setOptions(options);

		this.onResize = (function() {
			if(this.frame) {
				this.frame.setStyle('height', this.getIFrameHeight());
			}
		}).bind(this);

		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, function(e) {
			if(e.value == '/') {
				that.removeFrame();
			} else if(e.value == '/' + that.options.externalStateId) {
				if(that.externalUrl) {
					that.showFrame(that.externalUrl);
				}
			}
		});
		
		$$(that.options.linkSelector).addEvent('click', function(event) {
			$(window).scrollTo(0, 0);
			that.externalUrl = this.href;
			SWFAddress.setValue(that.options.externalStateId);
			event.stop();
			return false;
		});
	},
	addCloseLink: function(element) {
		var that = this;
		$(element).addEvent('click', function(event) {
			SWFAddress.setValue('');
			event.stop();
			return false;
		});
	},
	showFrame: function(url) {
		var that = this;
		if(this.frame) {
			this.frame.dispose();
		}
		
		that.fireEvent('open', this);

		// Don't have a scroll bar for the document
		$(document.body).setStyle('overflow', 'hidden');
		this.frame = new NJS.IFrame({
			'src': url,
			'class': this.options.iframeClass,
			'styles': {
				'margin': '0',
				'padding': '0',
				'border': 'none',
				'border-top': '1px solid #000000',
				'overflow': 'auto',
				'position': 'absolute',
				'bottom': '0px',
				'left': '0px',
				'width': '100%',
				'height': '0px',
				'z-index': '9999'
			},
			'frameborder': '0',
			'events': {
				'load': function(event) {
					var fx = new Fx.Morph(that.frame, {duration: that.options.duration,
						transition: Fx.Transitions.Quad.easeOut});
					fx.onComplete = function() {
						that.frame.removeEvents('load');
					};
					fx.start({
						'opacity': [0, 1],
						'height': [0, that.getIFrameHeight()]
					});
					that.fireEvent('load');
				}
			}
		});
		this.frame.set('opacity', 0);
		$(document.body).adopt(this.frame);
		window.addEvent('resize', this.onResize);
		that.fireEvent('show');
	},
	getIFrameHeight: function() {
		return $(window).getSize().y - this.labelElement.getCoordinates().bottom - this.options.heightOffset;
	},
	removeFrame: function() {
		var that = this;
		if(this.frame) {
			that.fireEvent('close');

			var fx = new Fx.Morph(this.frame);
			fx.onComplete = function() {
				that.frame.dispose();
				that.frame = null;
				// Have a scroll bar again
				$(document.body).setStyle('overflow', 'auto');
				that.fireEvent('closed');
			};
			fx.start({
				'opacity': [1, 0],
				'height': [this.frame.getSize().y, 0]
			});
		}
		window.removeEvent('resize', this.onResize);
		this._onResize = null;
		that.fireEvent('remove');
	}
});
