/*
 * 
 * 	Coder: Jens Hoffmann (c) dkd 2007
 * 	Add Click Action Events to Links
 * 
 */

var addMouseEvent = {

// Add all Events to all Menus (Called by init.js)

	init: function() {
		this.removeAllEvents();
		if (window.ie) {
			this.fixAbsolutURLs();
		}
		this.addMainMenuEvents();
		this.addCtaMenuEvents();
		this.addRootLineEvents();
		this.addFooterMenuEvents();
		this.addContentEvents();
	},

// Remove the baseURL from all URLs

	fixAbsolutURLs: function() {
		 var baseURL		= $$('base')[0].getProperty('href');
		 var css_mainMenu   = "div#mainMenu li a";
		 var css_staticMenu = "div#staticMenu li a";
		 var css_footer		= "div#footer a";
		 var css_rootline   = "div#rootline a";
		 var css_content	= "div#col3_content a";
		 var allATags   	= $$(css_mainMenu, css_staticMenu, css_footer, css_rootline, css_content);
			 allATags.forEach( function(el) {
				// Fix for IE6/7 (If the baseURL is in the Link URL, remove it now)
					var elHref = el.getProperty('href');
					if ((elHref !== null || elHref !== "") && elHref.contains(baseURL)) {
						el.setProperty('href', elHref.replace(baseURL, ""));
					}
			 });
	},

// Search for all Menu-A-Tags and add all needed Events to them

	addMainMenuEvents: function() {
		
		var css_statment = "div#mainMenu li a";
		var menuElements = $$(css_statment);
	
		menuElements.forEach(
			function(el) {

				var clickAction = '';
				var getHref = el.getProperty('href');
				var parID   = el.getParent().getProperty('id');
				var getClass = el.getProperty('class');
				
				var getTarget = el.getProperty('target');
				
				if ( (getTarget != null) && (getTarget == "_blank") ) {
					//do nothing, it's external URL
				} else {
					// if page is a shortcut reload the hole page like the clickAction rootline
					if (getClass !== null && getClass == 'shortcut'){
						clickAction = "rootline";
					} else {
						if (parID !== null && parID == "mainMenu-backlink") {
							clickAction = "mainMenuL"; 		// Move Menu to the Left (back)
						} else {
							if ($ES('img', el).length > 0) {
								clickAction = "mainMenuR";	// Move Menu to the Right (isSub)
							} else {
								clickAction = "ctaMenu";	// Don't Move Menu, just switch Content 
							}
						}
					}
					addMouseEvent.onClickEvent(el, clickAction, getHref);
					addMouseEvent.onMouseOverEvent(el, parID);
					addMouseEvent.onMouseOutEvent(el, parID);
					addMouseEvent.onFocusEvent(el);	
				}
				
			}
		);

	},
	
	addCtaMenuEvents: function() {
	
		var css_statment = "div#staticMenu li a";
		var menuElements = $$(css_statment);
		var clickAction = "ctaMenu";

		menuElements.forEach(
			function(el) {
				var getHref = el.getProperty('href');
				var parID   = el.getParent().getProperty('id');
				addMouseEvent.onClickEvent(el, clickAction, getHref);
				addMouseEvent.onMouseOverEvent(el, parID);
				addMouseEvent.onMouseOutEvent(el, parID);
				addMouseEvent.onFocusEvent(el);
			}
		);
					
	},
	
	addFooterMenuEvents: function() {

		var css_statment_snd = "a#soundBTN";
		var menuElements_snd = $$(css_statment_snd);
	
		menuElements_snd.forEach(
			function(el) {
				var clickAction = "soundMenu";
				var getHref = el.getProperty('href');
				addMouseEvent.onClickEvent(el, clickAction, getHref);
				addMouseEvent.onFocusEvent(el);
			}
		);
		
		var css_statment = "div#footer a";
		var menuElements = $$(css_statment);
	
		menuElements.forEach(
			function(el) {
				var clickAction = '';					
				var elID = el.getProperty('id');
				var getHref = el.getProperty('href');
				if (elID !== null && elID.slice(0,8) == "langLink") { // If the A tag has as ID something with langLink in front check the lang
					var langTAG = getHref.slice(0, 2); // Cut the Lang out of the Href (de, en, sp)
					if (langTAG == "de") {
						clickAction = "langMenuDE";
					} else if (langTAG == "en") {
						clickAction = "langMenuEN";
					} else if (langTAG == "sp") {
						clickAction = "langMenuSP";
					}
					addMouseEvent.onClickEvent(el, clickAction, getHref);
					addMouseEvent.onFocusEvent(el);
				} else if (el.hasClass('stnLink')) {
					if (el.getProperty("target") != "_blank") {
						clickAction = "ctaMenu";
						addMouseEvent.onClickEvent(el, clickAction, getHref);
						addMouseEvent.onFocusEvent(el);
					}
				}
			}
		);
				
	},
	
	addRootLineEvents: function() {

		var css_statment = "div#rootline a";
		var menuElements = $$(css_statment);
		var clickAction = "rootline";
	
		menuElements.forEach(
			function(el) {
				var getHref = el.getProperty('href');
				addMouseEvent.onClickEvent(el, clickAction, getHref);
				addMouseEvent.onFocusEvent(el);
			}
		);

	},

	addContentEvents: function() {

		var clickAction = "content";
		var css_statment = "div#col3_content a";
		var menuElements = $$(css_statment);
		var iii=0;
				
		menuElements.forEach(
			function(el) {
				
				var getHref  = el.getProperty('href');
				var getID    = el.getProperty('id');
				var parClass = el.getParent().getProperty('class');
				
				if (!( // IF NOT ...
					 getHref  === null                    		 		      ||  // No Href no Ajax (eg: Anker Target)
					 getHref  === ""                       		 	  	      ||  // No Href no Ajax (IE Version)
					(getHref  !== null && getHref.search(/www./)	   != -1) ||  // Posible External Link
				    (getHref  !== null && getHref.search(/http:/)	   != -1) ||  // Posible External Link
				    (getHref  !== null && getHref.search(/#/)		   != -1) ||  // Anker Link
				    (getHref  !== null && getHref.search(/javascript/) != -1) ||  // Javascript Link
				    (getHref  !== null && getHref.search(/mailto/)	   != -1) ||  // eMail Link
				    (getHref  !== null && getHref.search(/\.pdf/)	   != -1) ||  // PDF File
				    (getHref  !== null && getHref.search(/\.avi/)	   != -1) ||  // AVI File
				    (getHref  !== null && getHref.search(/\.flv/)	   != -1) ||  // FLV File
				    (getHref  !== null && getHref.search(/\.mov/)	   != -1) ||  // MOV File
				    (getHref  !== null && getHref.search(/\.mp3/)	   != -1) ||  // MP3 File
				    (getHref  !== null && getHref.search(/\.jpg/)	   != -1) ||  // JPG File
				    (getHref  !== null && getHref.search(/\.gif/)	   != -1) ||  // GIF File
				    (getHref  !== null && getHref.search(/\.png/)	   != -1) ||  // PNG File
				     parClass == "up" 								          ||  // Scroller up btn
				     parClass == "down"									          // Scroller down btn
				)) {
					addMouseEvent.onClickEvent(el, clickAction, getHref);
					addMouseEvent.onFocusEvent(el);
				}

			}
		);

		// init file uplad fields
		new iframeFileUpload();
	},
	
// Event Function used by the add Functions

	onClickEvent: function(obj, action, target) {
		obj.addEvent('click', function(e) {
			e = new Event(e).stop();
				clickMan.clickAction(action, target);
				addMouseEvent.debugOutput(action, target);
			e.stop();
		});
	},
	
	onMouseOverEvent: function(obj, id) {
		obj.addEvent('mouseover', function() {
			iMenu.mouseOverFade(id);
		});
	},

	onMouseOutEvent: function(obj, id) {
		obj.addEvent('mouseout', function() {
			iMenu.mouseOutFade(id);
		});
	},

	onFocusEvent: function(obj) {
		obj.addEvent('focus', function() {
			// Remove the Cursor from link obj
			this.blur();
		});
	},

// Reset / Remove all Events

	removeAllEvents: function() {
		$$("a").forEach(function(el){
			el.removeEvents();
		});
	},

// Debug all A Tag on the Side and show a List of all found A-Tag Objects
	debugOutput: function(action, target) {
		var debugIT = false;
		if (debugIT) {
			if (window.ie) {
				alert ( "clickMan: " + action + " - " + target );
			} else {
				console.log( "clickMan: %o - %o", action, target );
			}
		}
	}

};
