/*! $Id: navigations.js 9121 2010-03-24 09:25:34Z kschaefer $ */
/*global jQuery: false, window: false */
//noinspection BadExpressionStatementJS
"use strict";

function deactivateDataTableNavigation(element) {
	element.style.visibility = "hidden";
}

function hoverEffectNavLvlOneBtn(status, element) {
	if ('hover' === status) {
		element.getElementsByTagName('td')[0].style.backgroundPosition = "0px -43px";
		element.getElementsByTagName('a')[0].style.color = "white";
	}
	else if ('normal' === status) {
		element.getElementsByTagName('td')[0].style.backgroundPosition = "0px 0px";
		element.getElementsByTagName('a')[0].style.color = "black";
	}
	else {
		alert("Fehler in Parameteruebergabe.");
	}
}
function hoverEffectNavLvlFourBtn(status, element) {
	if ('hover' === status) {
		element.getElementsByTagName('td')[0].style.backgroundPosition = "0px -37px";
		element.getElementsByTagName('a')[0].style.color = "white";
	}
	else if ('normal' === status) {
		element.getElementsByTagName('td')[0].style.backgroundPosition = "0px 0px";
		element.getElementsByTagName('a')[0].style.color = "black";
	}
	else {
		alert("Fehler in Parameteruebergabe.");
	}
}

function doBannerSlide(newValueMarginLeft) {
	jQuery("#teaserSlider").animate({
		marginLeft: newValueMarginLeft
	}, 600);
}

/* Banner Slider */
function bannerSlide(numberTeaserPerPage, direction) {
	var newValueMarginLeft,
		thisValueMarginLeft,
		slideStep;

	switch (numberTeaserPerPage) {
	case 1:
		slideStep = 293;
		break;
	case 3:
		slideStep = 879;
		break;
	default:
		slideStep = 879;
	}

	thisValueMarginLeft = parseInt(document.getElementById("teaserSlider").style.marginLeft, 10);

	if ('left' === direction && -1758 < thisValueMarginLeft) {
		newValueMarginLeft = (thisValueMarginLeft - slideStep) + "px";
		doBannerSlide(newValueMarginLeft);
	}
	if ('right' === direction && 0 > thisValueMarginLeft) {
		newValueMarginLeft = (thisValueMarginLeft + slideStep) + "px";
		doBannerSlide(newValueMarginLeft);
	}
}

jQuery(document).ready(function () {
	jQuery(window).unload(function () {
		jQuery(".navigationLvl2, .navigationLvl3, .soccercard").css("visibility", "hidden");
	});
	jQuery("#teaserSlider").animate({
		marginLeft: "0px"
	}, 600);
	jQuery(".navigationLvl2, .navigationLvl3").css("visibility", "hidden");
	jQuery(".callToActionLink").hover(function () {
		jQuery(this).siblings(".closer").css("backgroundPosition", "0 -25px");
	}, function () {
		jQuery(this).siblings(".closer").css("backgroundPosition", "0 0");
	});
	jQuery(".callToActionMainActionLink").hover(function () {
		jQuery(this).siblings(".closer").css("backgroundPosition", "0 -25px");
	}, function () {
		jQuery(this).siblings(".closer").css("backgroundPosition", "0 0");
	});
	jQuery(".modelDataTableCategoryLinkOpenClose").click(function () {
		jQuery(this).css("backgroundPosition", "0 -39px");
		jQuery(this).parent("tr").next(".category").children(".categoryContent").css("border", "1px solid red");
	});
	jQuery("#modelName").css("cursor", "pointer");
	jQuery("#modelName").click(function () {
		location.replace("lacetti-overview.html");
	});
	//The difference between Menu-Holder-Element and first model-element results y-position for soccer-cards:
	//soccerCardTopPosition = document.getElementById("navigationFirstModel").offsetTop;
	soccerCardTopPosition = jQuery("#navigationFirstModel").offset();
});

function findPos(obj) {
	var curLeft = 0,
		curtTop = 0,
		curObj = obj;

	while (curObj.offsetParent) {
		curLeft += curObj.offsetLeft;
		curtTop += curObj.offsetTop;
		curObj = curObj.offsetParent;
	}
	return [curLeft, curtTop];
}

/****************************************************************/
/***** Navigation with new sensitivity (March 23,  2010) ********/


/********
    Catch old function calls from pages, don't have the new compiled code:
*/
function handle_main_nav(){return true;}

/*******/


jQuery(document).ready(function(){
/* Library: Object Tools */
var objectTools = {
	/* Variable */
	isTypeHtmlElement : function(element) {
		return (typeof(element)==='object' && element.nodeName && typeof(element.nodeType==='number'))?true:false;
	},
	isTypeString : function(element) {
		return (typeof(element)==='string');
	},
	isTypeBoolean : function(element) {
		return (typeof(element)==='boolean');
	},
	isTypeNumber : function(element) {
		return (typeof(element)==='number');
	},
	isTypeArray : function(element) {
		return (typeof(element)==='object' && element instanceof Array);
	},
	/* DOM */
	getDomElement : function(element) {
		var selector = '';
		if(jQuery){
			switch(arguments[1])
			{
				case 'id':
					selector = '#';
					break;
				case 'class':
					selector = '.';
					break;
				default:
					selector = '';
			}

			return jQuery(selector + element);
		}
		else {
			switch(arguments[1])
			{
				case 'id':
					return document.getElementById(element);
					break;
				case 'class':
					return document.getElementsByName(element)[0];
					break;
				default:
					return document.getElementsByTagName(element)[0];
			}
		}
	},
	/* Objects-handling */
	oExtend : function(subClass, superClass) {
		var F = function() {};
		F.prototype = superClass.prototype;
		subClass.prototype = new F();
		subClass.prototype.constructor = subClass;
	},
    /* Objects-converting */
    oConv : function(a){
        var O = {};
        for(var i=0,l=a.length;i<l;i++){
            O[a[i]]=null;
        }
        return O;
    }
};





/*** Live Menu ****/

	/****************/
	/* Obj liveMenu */

	function LiveMenu(element) {
		this.init(element);

	}

	/****************/
	/* Obj MenuItem prototyping */

    /* public: */

    LiveMenu.prototype.delay = 290;

	LiveMenu.prototype.elementHoverClassName = "hover";

	LiveMenu.prototype.elementSelectedClassName = "selected";

	LiveMenu.prototype.init = function(element) {
		this.rootElement = element;
	};

	LiveMenu.prototype.apply = function() {
			/* Check for jQuery: */
            if(! jQuery) {
                alert("Framework 'jQuery' not found!");
                return false;
            }

			/* Get and check html-element: */
			try {
				(objectTools.isTypeHtmlElement(this.rootElement)) ? true : this.rootElement = objectTools.getDomElement(this.rootElement, 'id');
			}
			catch(err) {
				alert("The html-element can not be found or is not of type ID-Element.\n"+err);
				return false;
			}
			/* Check structure, element has to be an UL: */
			try {
				(this.rootElement.nodeName==='UL' || this.rootElement.nodeName==='ul') ? true : false;
			}
			catch(err) {
				alert("Selected element is not of type 'UL'!");
				return false;
			}
			/**/
			this._scanItems();
	};

    /* private: */

	LiveMenu.prototype._stack = [];

	LiveMenu.prototype._getRoot = function () {
		return this.rootElement;
	};

	LiveMenu.prototype._addStack = function (v) {
		window.clearTimeout(this._stack[0]);
		/* Write new: */
		this._stack[0] = v;
	};
	LiveMenu.prototype._emptyStack = function () {
		while (0 < this._stack.length) {
			window.clearTimeout(this._stack[0]);
			this._stack.shift();
		}
	};

	LiveMenu.prototype._scanItems = function() {
		var menuItems = jQuery(this.rootElement).find("li");
		var rootElement = this.rootElement;
		jQuery(menuItems).each(function(i){
			menuItems[i] = new MenuItem(jQuery(this), rootElement);
			menuItems[i].constructor = menuItems[i]; /* ?? */
			});

	};

	/****************/
	/* Obj menuItem */

	function MenuItem(htmlElement,rootElement) {
		this.htmlElement = htmlElement;
		this.rootElement = rootElement;
		this.addEvents();
	}
	/***************/


	/************/
	/* Extend both: */

	objectTools.oExtend(MenuItem,LiveMenu);
	/***********/


	/****************/
	/* Obj menuItem prototyping */

	MenuItem.prototype.addEvents = function() {
		var tO = this;
		var tE = this.htmlElement;
		jQuery(this.htmlElement).hover(
				function() {
					//console.profile('hover');

					//console.profile('hover');
					var jFoo = jQuery(this);
					var jBar = jFoo.siblings("li");
					jBar.removeClass(tO.elementHoverClassName);
					jBar.removeClass(tO.elementSelectedClassName);
					jFoo.addClass(tO.elementHoverClassName);

					//console.profileEnd();

					tO.activateParentItems(this);
					//console.profileEnd();
				},
				function() {
					//if(jQuery.contains( tO.rootElement, e.relatedTarget)!=0) {
						jQuery(this).removeClass(tO.elementHoverClassName);
					//}
					//tO.emptyStack();
					tO._addStack(window.setTimeout(function(){tO.hideMenu(tE)}, tO.delay));
					//return false;
				}).
			mouseover(function(){
					//console.info("MouseOver, " + jQuery(tE).find("a:first").text());
				//	jQuery(this).addClass(tO.elementHoverClassName);
					tO._addStack(window.setTimeout(function(){tO.showMenu(tE)}, tO.delay));
					return false;
				});
	};

	MenuItem.prototype.activateParentItems = function(menuItem) {
		var elParent = jQuery(menuItem).parent("ul:first");
		if(jQuery(elParent).attr("id") != jQuery(this.rootElement).attr("id"))
		{
			elParent = jQuery(elParent).parent("li");
			jQuery(elParent).addClass(this.elementHoverClassName);
			this.activateParentItems(elParent);
		}
	};

	MenuItem.prototype.showMenu = function(element) {
		/* Remove items-attribute 'selected' from all elements oh this level and childs: */
        //liveDebug.info(this,"Zeige Menu");
		var jqE = jQuery(element);
		var jqS = jqE.siblings("li");
		jqS.removeClass(this.elementSelectedClassName);
		jqE.find("li").removeClass(this.elementSelectedClassName);
		/* Add 'selected' to parent: */
		jqE.parent("ul").parent("li").addClass(this.elementSelectedClassName);
		/* Close all child.menus of that level: */
		jqS.find("ul").css("visibility","hidden");
		jqE.find("ul:first").css("visibility","visible");
	};

	MenuItem.prototype.hideMenu = function(element) {
		var jqE = jQuery(element);
        var jqS = jqE.siblings("li");
        jqS.find("ul").css("visibility","hidden");        
		jqE.removeClass(this.elementSelectedClassName).find("ul").css("visibility","hidden");
	};

//	


function adjustSoccercards() {
    var soccerCardTopPosition = jQuery("#navigationFirstModel").offset();
    var sc = jQuery("ul#navigationLvl1").find("ul.soccercard");
    jQuery.each(sc, function(){
        var marginParent = jQuery(this).offset();
        jQuery(this).css("top", "-" + (parseInt(marginParent.top, 10) - parseInt(soccerCardTopPosition.top, 10)) + "px");
    })
}

/******************/

adjustSoccercards();

var s_menuHtmlElementId = "navigationLvl1";
var mainMenu = new LiveMenu(s_menuHtmlElementId);
mainMenu.apply();



});
