﻿/**
 * the IframeEmbedder class
 * replaces link-element with an iframe pointing to the specified in href
 *
 * @author Klaas Dieleman, <klaas[AT]efocus.nl>
 * @author Ralph Meeuws, <ralph.meeuws[AT]efocus.nl>
 * @since 12 apr 2010
 * @version 1.0
 * @copyright eFocus
 *
 * @uses jQuery 1.4.2, <http://www.jquery.com>
 *
 * @param Element a-tag with url to external location to be replaced by iframe element
 */
function IframeEmbedder(elLink){
	if(typeof(elLink) == 'object') {
		this.elLink = elLink;

		this.options = {
			'width'		: 600,
			'height'	: 400,
			'src'		: '',
			'scrolling'	: 'auto'
		};

		this.initialize();
	}
	
};

IframeEmbedder.prototype = {
	/**
	* intializes player with requested options and inserts it
	*/
	initialize: function() {

		if (this.elLink.children().first()) this.elLink.children().first().css('visibility', 'hidden');

		this.options.width = this.elLink.children().first().outerWidth() || this.options.width;
		this.options.height = this.elLink.children().first().outerHeight() || this.options.height;
		this.options.src = jQuery(this.elLink).attr('href');

		this.parseOptions(this.elLink);
		
		this.createIframe();
	},

	/**
	* adds options to given link, reading from specified options in the 'rel'-attribute
	* @param element; link to add options to
	*/
	parseOptions: function(elLink) {
		elLink.linkOptions = {};
		var arrGivenOptions = [];
		var arrParsedOptions = [];

		arrGivenOptions = String(jQuery(elLink).attr('rel')).replace(/ /g, '').split(';');
		arrGivenOptions.splice(0, 1);

		for (i in arrGivenOptions) {
			var strOptionName = String(arrGivenOptions[i]).split('=')[0];
			var strOptionValue = String(arrGivenOptions[i]).split('=')[1];
			arrParsedOptions[strOptionName] = strOptionValue;
		}

		for (i in this.options) {
			if (arrParsedOptions[i] != undefined) {

				var blnOptionFormatInvalid = true;

				switch (i) {
					case 'width':
						blnOptionFormatInvalid = isNaN(arrParsedOptions[i]);
						break;
					case 'height':
						blnOptionFormatInvalid = isNaN(arrParsedOptions[i]);
						break;
					case 'scrolling':
						if (arrParsedOptions[i] == 'auto' || arrParsedOptions[i] == 'yes' || arrParsedOptions[i] == 'no') blnOptionFormatInvalid = false;
						break;
					default:
						this.options[i] = this.options[i];
				}

				if (blnOptionFormatInvalid) {
					this.options[i] = this.options[i];
				} else {
					this.options[i] = arrParsedOptions[i];
				}

			} else {
				elLink.linkOptions = this.options[i];
			}
		}
	},

	/**
	* creates an iframe element and replaces link element with it
	*/
	createIframe: function() {

		this.container = jQuery('<iframe></iframe>');
		
		this.container.attr('frameborder', '0');
		this.container.attr('width', this.options.width);
		this.container.attr('height', this.options.height);
		this.container.attr('src', this.options.src);
		this.container.attr('scrolling', this.options.scrolling);
		
		this.elLink.after(this.container);
		this.elLink.remove();

	}
}
