
  		/**
 * project: messe frankfurt - web
 * type: javascript
 * @description: loader for component based javascript (layer)
 * @author: Stefan Heimann, 26.02.2009
 * @copyright: Day Software 2008-2009. All rights reserved.
 */



/**
 * component: none
 * @description: create a namespace for the "Messe Frankfurt"
 */

Ext.namespace('mf');
/**
 * component: none
 * @description: generic function to determine if a component exists in the page and retrieves the component
 */
mf.components = {
	/**
	 * selects the component
	 * @param none
	 * @return Ext.CompositeElement || Ext.Element || null
	 */
	getComp: function(containerId, componentClass) {
		var component = null;
		var container = Ext.get(containerId);
		if (container) {
			if (componentClass) {
				if (container.select('.' + componentClass).elements.length > 0) {
					component = container.select('.' + componentClass);
				}
			} else {
				component = container;
			}
		}
		return component;
	}
};
/**
 * component: none
 * @description: adds container classes to restyle the shadow boxes
 */

// init component
Ext.onReady(function() {
	mf.shadow.init();
});

mf.shadow = {

	/**
	 * initialize function
	 * @param none
	 */
	init: function() {
		if (mf.shadow.rightComp = mf.components.getComp('mfContentRight')) {
			mf.shadow.rightShadow();
		}
	},
	
	/**
	 * 	reset css settings for the shadow in right column
	 *  @param: none
	 */
	rightShadow: function() {
		mf.shadow.rightComp.addClass('mfContentRightShadow');
	}
}

/**
 * component: none
 * @description: generic window functions
 */

Ext.onReady(function() {
	mf.window.init();
});

mf.window = {
	
	init: function() {
		this.checkAnchor();
		this.setScrollToListener();
	},
	
	/**
	 * gets a given parameter from the URL of the browser window
	 * @param parameterName {String} parameter to search for
	 * @return {String} extracted value for the URL parameter
	 */
	getParameterFromURL: function(parameterName) {
		var regex = new RegExp("[\\?&]" + parameterName + "=([^&#]*)");
		var results = regex.exec(window.location.href);
		
		if (results == null) {
			return "";
		} else {
			return results[1];
		}
	},
	
	/**
	 * check url for anchor hashes and jump to this anchor if found some
	 */
	checkAnchor: function() {
		mf.window.scrollTo(window.location.href.split('#')[1]);
	},
	
	/**
	 * set onclick listener for scrolling to anchor
	 */
	setScrollToListener: function() {
		//scann links for hashes
		Ext.get('mfContentMain').select('a').each(function(el) {
			var href = Ext.get(el).dom.href;
			if (href && href.indexOf('#') != -1) {
				el.on('click', function(e,t) {				
					mf.window.scrollTo(href.split('#')[1],t.href);
				},this,{stopEvent: true});
			}				
		});
	},
	
	/**
	 * scroll to 
	 */
	scrollTo: function(name,target) {
		if(!name || name == "") return;
				
		//offset fixed header
		var offset = (Ext.isIE) ? 65 : 70;
		var wrapper;
		if(wrapper = mf.components.getComp('mfWrapper')) {
			//offeset fixed banner advertising
			if(wrapper.hasClass('mfSuperbanner') || wrapper.hasClass('mfHockeystick')) {
				offset += 90;
			}
		}
		//get anchor target and scroll
		var anchor = Ext.get(Ext.query("*[name=" + name + "]")).first();
		if(anchor && anchor.hasClass('mfRTEAnchor')) {
			window.scrollTo(0,(anchor.getY() - offset));	
			if(Ext.isIE6) {
				Ext.get('mfContentWrapper').scroll('b',(anchor.getY() - offset));
			}
		} else {
			//no anchor on this page - follow href
			if(target) window.location.href = target;
		}
	}
}
/**
 * component:
 * 
 * @description: Emulate an mailto: link
 */
mf.mailAddress= {

    /**
     * Renders an ectypal 'mailto' link start tag that can't be scanned by mail
     * harvesters.
     * 
     * <b>Attention!</b>
     * This functionality usually should be automatically be done by 
     * MailAddressProtector.java.
     * The Java Script version is just for places that can't be reached from the
     * server engine, because the mail address is generated at client side using 
     * Java Script for instance.
     * 
     * This start tag has to be terminated by {@link #renderMailtoEnd()}!
     * 
     * @param mailAddress
     *            The email address to be called
     * @return String to be included into HTML code instead of
     *         <code>&lt;a href:"mailto:<i>mailaddress</i>"&gt;</code>
     * @see renderMailtoEnd()
     */
     renderMailtoStart: function (mailaddress) {
        // Split mail address in multiple parts to 
        // mailaddress = recipient1 + recipient2 + '@' + domain1 + domain2
        var topSplit = marker.contactData.email.split("@");
        var recipient = topSplit[0];
        var domain = topSplit[1];
        var split = recipient.length / 2;
        var recipient1 = recipient.substring(0, split);
        var recipient2 = recipient.substring(split);
        split = domain.length / 2;
        var domain1 = domain.substring(0, split);
        var domain2 = domain.substring(split);
        var methodCall = 'mf.mailAddress.openMailTool(\'' 
            + recipient1 + '\', \'' + recipient2 + '\', \'' + domain1 + '\', \'' + domain2 + '\')';

        return '<span onclick=\"' + methodCall + '\" class=\"mfMailaddress\">';
    },

    /**
     * Renders the termination of {@link #renderMailtoStart(String)}
     * 
     * <b>Attention!</b>
     * This functionality usually should be automatically be done by 
     * MailAddressProtector.java.
     * The Java Script version is just for places that can't be reached from the
     * server engine, because the mail address is generated at client side using 
     * Java Script for instance.
     *
     * @return String to be included into HTML code to terminate the
     *         {@link #renderMailtoStart(String)} tag.
     */
    renderMailtoEnd: function () {
        return '</span>';
    },

    /**
     * Open the mail tool. 
     * The email address is split into four parts: 
     * [firstAddress][secondAddress]@[firstDomain][secondDomain]
     * @param firstAddress 
     * @param secondAddress
     * @param firstDomain
     * @param secondDomain
     */
     openMailTool: function (firstAddress, secondAddress, firstDomain, secondDomain) {
        window.location.href="ma" + "il" + "to" + ":" 
        + firstAddress + secondAddress + "@" + firstDomain + secondDomain;
     }

};




/**
 * component: close link
 * @description: closes the shadowbox layer
 */

// init component
Ext.onReady(function(){
	mf.closeLink.init();
});

mf.closeLink = {
	/**
	 * init the component
	 * @param: none
	 */
	init: function() {
		if (mf.closeLink.comp = mf.components.getComp('mfHeader', 'mfLayerClose')) {
			mf.closeLink.addCloseEvent();
			mf.closeLink.addShadow();
		}
	},

	/**
	 * closes the shadowbox, which contains the layer page and removes the layer
	 * @param none
	 */
	closeShadowbox: function() {
		window.setTimeout(parent.Shadowbox.close, 100);
		return false;
	},

	/**
	* add the functionality to close the shadowbox
	* @param none
	*/
	addCloseEvent: function() {
		mf.closeLink.comp.first().select('a').on('click', mf.closeLink.closeShadowbox, this, {stopEvent: true});
	},
	
	/**
	 * add a shadow
	 * @param none;
	 */
	addShadow: function() {
		mf.closeLink.comp.first().boxWrap('mfBoxShadow');
	}
}



  		
			/**
 * component: myaccount generic layer functions 
 * @description: add error messages for forms, add iframe resize functionality
 */

mf.myAccountGeneric = {

	/**
	 * resizes the shadowbox after loading iframe content
	 * @param compClass {String} css class of component to resize
	 * @param skipOffset {Boolean} switch to skip the height offset
	 */
	resize: function(compClass, skipOffset) {
		var sbox = parent.Ext.get('shadowbox');
		var widthOffset = sbox.getBorderWidth('lr') + sbox.getPadding('lr');
		var heightOffset = 15;
		var maxWidth = Ext.isIE6 ? 428 : 424;

		// get iframe and disable scrolling
		var sboxContent = parent.Ext.get('shadowbox_content');
		sboxContent.dom.scrolling = 'no';

		// get component
		var comp = Ext.select('.' + compClass);

		if (comp.getCount() > 0) {
			// get component size
			var compSize = comp.first().getSize();

			// calculate width
			var width = compSize.width < maxWidth ? maxWidth : compSize.width;
			width += widthOffset;
			// calculate height
			var height = skipOffset ? compSize.height : compSize.height + heightOffset;

			// get shadowbox content and assign new height (needed for the resize event)
			parent.Shadowbox.getContent().height = height;

			// resize shadowbox
			parent.mf.shadowbox.resize(width, height);
		}

		// reenable scrolling
		window.setTimeout(function() {
			sboxContent.dom.scrolling = 'auto';
		}, 1000);
	 }
};



			
