﻿/**
 * the PopinHandler class
 * sets up specific per-link options on links with 'rel=shadowbox' attribute
 *
 * @author Klaas Dieleman, <klaas[AT]efocus.nl>
 * @since 23 jun 2010
 * @version 1.1
 * @copyright eFocus
 * @uses jQuery 1.4.2, <http://www.jquery.com>
 * @uses Shadowbox 3.0.3, <http://www.shadowbox-js.com>
 */
function PopinHandler(){
	this.initialize();
};

PopinHandler.prototype = {
	/**
	 * adds PopinHandler object to Window collection as 'PopinHandler'
	 */
	initialize: function(){
		window['PopinHandler'] = this;
	},
	
	/**
	 * sets up links to be handled by PopinHandler
	 */
	init: function() {
		this.arrLinks = this.getLinks();
		this.linkOptions = {
			'width'			: '500',
			'height'		: '500',
			'overlayColor'	: '#000000',
			'overlayOpacity': '0.5',
			'scrollbars'	: 'auto'
		};
		
		this.arrLinks.each(function(index, item) {
			PopinHandler.parseOptions(item);
			PopinHandler.setupOptions(item);
		});
		
    },
    
    /**
     * get all <a>-tags in the DOM with a 'rel'-attribute containing 'shadowbox'
     * @return arrLinks; array of link elements
     */
    getLinks: function() {
		var arrLinks = jQuery("a[rel^='popin']");
		return arrLinks;
    },
    
    /**
     * 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(';');
		
		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.linkOptions) {
			if(arrParsedOptions[i] != undefined) {
				
				var blnOptionFormatInvalid = true;
				
				switch(i) {
					case 'width':
						blnOptionFormatInvalid = isNaN(arrParsedOptions[i]);
						break;
					case 'height':
						blnOptionFormatInvalid = isNaN(arrParsedOptions[i]);
						break;
					case 'overlayOpacity':
						blnOptionFormatInvalid = !(0 <= arrParsedOptions[i] <= 1);
						break;
					case 'overlayColor':
						var hexExp = new RegExp('^#([0-9a-fA-F]{3}){1,2}$');
						blnOptionFormatInvalid = !(hexExp.test(arrParsedOptions[i]));
						break;
					case 'scrollbars':
						if(arrParsedOptions[i] == 'yes' || arrParsedOptions[i] == 'no') blnOptionFormatInvalid = false;
						break;
					default:
						elLink.linkOptions[i] = this.linkOptions[i];
				}
				
				if(blnOptionFormatInvalid) {
					elLink.linkOptions[i] = this.linkOptions[i];
				} else {
					elLink.linkOptions[i] = arrParsedOptions[i];
				}
				
			} else {
				elLink.linkOptions[i] = this.linkOptions[i];
			}
		}
		
    },
    
	/**
     * hides scrollbars of Shadowbox
     */
    hideScrollbars: function() {
		// need to overwrite the iframe constructor in shadowbox.js, because the 'scrolling'-attribute is set explicitly
		Shadowbox.iframe.prototype.append = function(K,au){var S='<iframe id="'+this.id+'" name="'+this.id+'" height="100%" width="100%" frameborder="0" marginwidth="0" marginheight="0" style="visibility:hidden" onload="this.style.visibility=\'visible\'" scrolling="no"';if(Shadowbox.isIE){S+=' allowtransparency="true"';if(Shadowbox.isIE6){S+=" src=\"javascript:false;document.write('');\""}}S+="></iframe>";K.innerHTML=S};
	},
	
	/**
     * always shows scrollbars of Shadowbox
     */
    showScrollbars: function() {
		// need to overwrite the iframe constructor in shadowbox.js, because the 'scrolling'-attribute is set explicitly
		Shadowbox.iframe.prototype.append = function(K,au){var S='<iframe id="'+this.id+'" name="'+this.id+'" height="100%" width="100%" frameborder="0" marginwidth="0" marginheight="0" style="visibility:hidden" onload="this.style.visibility=\'visible\'" scrolling="yes"';if(Shadowbox.isIE){S+=' allowtransparency="true"';if(Shadowbox.isIE6){S+=" src=\"javascript:false;document.write('');\""}}S+="></iframe>";K.innerHTML=S};
	},

	/**
     * restores scrollbars of Shadowbox
     */
    restoreScrollbars: function() {
		// restore the iframe constructor in shadowbox.js
		Shadowbox.iframe.prototype.append = function(K,au){var S='<iframe id="'+this.id+'" name="'+this.id+'" height="100%" width="100%" frameborder="0" marginwidth="0" marginheight="0" style="visibility:hidden" onload="this.style.visibility=\'visible\'" scrolling="auto"';if(Shadowbox.isIE){S+=' allowtransparency="true"';if(Shadowbox.isIE6){S+=" src=\"javascript:false;document.write('');\""}}S+="></iframe>";K.innerHTML=S};
	},
	
	/**
     * adds options to Shadowbox on given link
     * @param element; link on which to set up Shadowbox
     */
    setupOptions: function(elLink) {
						
		Shadowbox.setup(elLink, {
			initialHeight	: elLink.linkOptions.height,
			initialWidth	: elLink.linkOptions.width,
			overlayColor	: elLink.linkOptions.overlayColor,
			overlayOpacity	: elLink.linkOptions.overlayOpacity,
			handleOversize	: 'none',
			onOpen			: function() {
					if(elLink.linkOptions.scrollbars == 'no') PopinHandler.hideScrollbars()
					if(elLink.linkOptions.scrollbars == 'yes') PopinHandler.showScrollbars()
				},
			onClose			: function() {
					PopinHandler.restoreScrollbars();
				}	
		});
		
	}
	
}

// instantiate PopinHandler-object to be available as collection in the Window-object
if(typeof(Shadowbox) != 'undefined') new PopinHandler();