
  		/**
 * 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: none
 * @description: cookie handling
 */

mf.cookie = {
	
	/**
	 * create a cookie
	 * @param name {String} name of cookie
	 * @param value {Sring} value of cookie
	 * @param days {Int} days until the cookie expires
	 */
	createCookie: function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";
	},
	
	/**
	 * read a cookie
	 * @param name {String} name of cookie
	 */
	readCookie: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	/**
	 * delete a cookie
	 * @param name {String} name of cookie
	 */
	eraseCookie: function(name) {
		mf.cookie.createCookie(name,"",-1);
	}
};
/**
 * component: downloads
 * @description: shows all choosen downloads
 */

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


mf.layerDownloads = {
	/**
	 * @param none
	 */
	init: function () {
	mf.layerDownloads.select();
	
	if (mf.layerDownloads.comp = mf.components.getComp('mfHeader', 'mfLayerDownloadClose')) {
		mf.layerDownloads.addCloseEvent();
		mf.layerDownloads.addShadow();
	}
	if (mf.layerDownloads.comp = mf.components.getComp('mfContentMain', 'mfMediaGallery')) {
		mf.layerDownloads.tip();
	}
		
	},
	/**
	 * select/unselect downloads on click
	 */
	select: function() {
		if(mf.layerDownloads.links = mf.components.getComp('mfContentMain', 'mfMediaGallery .mfMediaGalleryRow a.mfDelDl')) {
			mf.layerDownloads.links.on('click',function(e,t){
				var cur = Ext.get(t);
				e.preventDefault();
				var pathstore =  escape(cur.dom.getAttribute('rel'));
				if(cookie = mf.cookie.readCookie('mfDownloads')) {						
					selection = cookie.replace(pathstore + '#','');
				}else {
					selection = pathstore + '#';
				}
				//write cookie
				mf.cookie.createCookie('mfDownloads',selection,1);
				window.location.reload();
				//count values
				parent.mf.rightDownloadCounter.init();
			});
		}
		if(mf.layerDownloads.links = mf.components.getComp('mfContentMain', 'mfMediaGallery a.mfFlushDl')) {
			mf.layerDownloads.links.on('click',function(e,t){
				// delete all
				mf.cookie.eraseCookie('mfDownloads');
				window.location.reload();
				parent.mf.rightDownloadCounter.init();
			});
		}
	},
	/**
	 * closes the shadowbox, which contains the layer page and removes the layer
	 * @param none
	 */
	closeShadowbox: function() {
		parent.mf.mediaGallery.setAllDisabled();
		parent.mf.mediaGallery.readCookie();
		window.setTimeout(parent.Shadowbox.close, 100);
		
		return false;
	},	
	/**
	* add the functionality to close the shadowbox
	* @param none
	*/
	addCloseEvent: function() {
		mf.layerDownloads.comp.first().select('a').on('click', mf.layerDownloads.closeShadowbox, this, {stopEvent: true});
	},
	
	/**
	 * add a shadow
	 * @param none;
	 */
	addShadow: function() {
		mf.layerDownloads.comp.first().boxWrap('mfBoxShadow');
	},
	/**
	 * loads the tooltip
	 * @param none
	 */
	tip: function(){
		mf.layerDownloads.comp.each(function(el) {
			el.select('p.mfWrap a.mfToolTipMedia').each(function(item){
				var url = item.dom.getAttribute('rel');
				if (url && url.indexOf('#') == -1) {
					new Ext.ToolTip({
						target: item,
						width: 200,
						hideDelay: 0,
						showDelay: 100,
						trackMouse: true,
						shadow: "frame",
						shadowOffset: 5,
						floating: true,
						autoLoad: {
							url: url
						},
						dismissDelay: 15000,
						mouseOffset: [50,0] 
					});
				}
			});
			Ext.QuickTips.init();
		});
	}
};

/**
 * component: article functions
 * @description: enables functionality to show/hide the download box
 * Attention: is connected to media gallery
 */

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


mf.articleFunctions = {	

	/**
	 * initialize function
	 * @param mediaGalleryDomContainer {Object} HTML block "div.mediaGallery", which contains a full view image and links
	 * @param mediaGalleryDomOverview {Object} HTML block, which contains the thumbnail view of the images and links
	 */
	init: function(mediaGalleryDomContainer, mediaGalleryDomOverview) {
		if (mf.articleFunctions.comp = mf.components.getComp('mfContentMain', 'mfArticleFunctions')) {
			mf.articleFunctions.resetDownloadBoxListener();
			mf.articleFunctions.setDownloadBoxListener();
			mf.articleFunctions.setPrintListener();
			
			if(mediaGalleryDomContainer) {		
				mediaGalleryDomContainer.down('div.mfArticleFunctions p a.mfBack').on('click', function(e){
					mediaGalleryDomContainer.dom.innerHTML = mediaGalleryDomOverview;
					mf.articleFunctions.resetDownloadBoxListener();
					mf.articleFunctions.setDownloadBoxListener();
					mf.mediaGallery.init();
				},this,{stopEvent: true});
			} 
			
			if(Ext.isIE7) {
				mf.articleFunctions.comp.setStyle('z-index','-1');
			}
		}
	},

	/**
	 * set listener on link and print the window content
	 * @param none
	 */
	setPrintListener: function() {
		mf.articleFunctions.comp.each(function(el) {
			if (el.down('p a.mfPrintArticle')) {
				el.down('p a.mfPrintArticle').on('click', function(e) {
					window.print();
				},this,{stopEvent: true}); 
			}
		});		
	},

	/**
	 * set listener on link and display or hide download "popup"
	 * @param none
	 */
	setDownloadBoxListener: function() {
		mf.articleFunctions.comp.each(function(el) {
			// show download box
			el.select('p a.mfDownloadArticle').on('click', function(e, t){
				Ext.get(t).up('div.mfArticleFunctions').down('div.mfArticleDownloads').removeClass('mfHd').setOpacity(0).setOpacity(1,{animate: true});
				if (Ext.isIE) {
					Ext.get(t).up('div.mfArticleFunctions').setStyle('z-index', '1');
				}
			},this,{stopEvent: true});

			// hide download box
			el.select('.mfArticleDownloads h2 a').on('click', function(e, t){
				Ext.get(t).up('div.mfArticleDownloads').addClass('mfHd');
				if (Ext.isIE) {
					Ext.get(t).up('div.mfArticleFunctions').setStyle('z-index', '-1');
				}
			},this,{stopEvent: true});
		});
	},

	/**
	 * reset listeners
	 * @param none
	 */
	resetDownloadBoxListener: function() {
		mf.articleFunctions.comp.each(function(el) {
			el.select('.mfArticleDownloads h2 a').removeAllListeners();
			el.select('.mfDownloadArticle').removeAllListeners();
		});
	}
};
/**
 * component: right teaser
 * @description: adds shadows around the download counter box
 */

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


mf.rightDownloadCounter = {

	/**
	 * initialize function
	 * @param none
	 */
	init: function() {
		var dom =  Ext.DomHelper;
		if (mf.rightDownloadCounter.comp = mf.components.getComp('mfContentRight', 'mfRightDownloadCount')) {		
			if (cookie = mf.cookie.readCookie('mfDownloads')) {
				var images = cookie.split('#').length - 1;
				dom.overwrite('mfRightDownloadCount','<b>' + images + '</b>');
			} 
			else {
				dom.overwrite('mfRightDownloadCount','<b>0</b>');
			}
		}
	}
}


  		
			/**
 * 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);
	 }
};



			
