/**
 * @author MURA
 * @copyright insyma AG
 * @projectDescription insyma JavaScript Randomize KeyFacts LANDIschweiz.ch
 * @version 1.0
 */
insymaRandomize = {
	props: {
		randomUlClass: 'insymaRandomize',
		hideClass: 'hide'
	},
	init:function() {
		var RandomList = insymaUtil.elmByClass('ul', insymaRandomize.props.randomUlClass);
		for (var i = 0; i < RandomList.length; i++) {
			var RandChilds = RandomList[i].getElementsByTagName("li");
			var rand = parseInt(Math.random() * RandChilds.length);
			for (var x = 0; x < RandChilds.length; x++) {
				if (!insymaUtil.cssjs("check", RandChilds[x], insymaRandomize.props.ControlClass)) {
					insymaUtil.cssjs("add", RandChilds[x], insymaRandomize.props.hideClass);
					if (rand == x) {
						insymaUtil.cssjs("remove", RandChilds[x], insymaRandomize.props.hideClass);
					}
				}
			}
		}
	}
};
insymaKeyFact = {
	props: {
		labelPrevious: 'vorheriger KeyFact',
		labelNext: 'nächster KeyFact',
		PreviousClass: 'previous',
		NextClass: 'next',
		ControlClass: 'control',
		labelCount: 'LANDI Fact',
		labelCountSeparator: 'von',
		
		keyFactClassName: 'insymaKeyFacts',
		hideClass: 'hide'
		
	},
	init:function() {
		var KeyFactList = insymaUtil.elmByClass('ul', insymaKeyFact.props.keyFactClassName);
		for (var i = 0; i < KeyFactList.length; i++) {
			var KFChilds = KeyFactList[i].getElementsByTagName("li");			
			var rand = parseInt(Math.random() * KFChilds.length);
			for (var x = 0; x < KFChilds.length; x++) {
				if (!insymaUtil.cssjs("check", KFChilds[x], insymaKeyFact.props.ControlClass)){
					insymaUtil.cssjs("add", KFChilds[x], insymaKeyFact.props.hideClass);
					if(rand == x){
						insymaUtil.cssjs("remove", KFChilds[x], insymaKeyFact.props.hideClass);
					}
				}
			}
			if (KFChilds.length > 1){
				var controlLi = insymaUtil.createElm('p', {className: insymaKeyFact.props.ControlClass}, false, KeyFactList[i].parentNode);				
				var anchorPrevious = insymaUtil.createElm('a', {href: '#', className: insymaKeyFact.props.PreviousClass, idx: rand, title: insymaKeyFact.props.labelPrevious}, false, controlLi);
				var PreviousLabel = insymaUtil.createElm('span', false, insymaKeyFact.props.labelPrevious, anchorPrevious);				
				var anchorNext = insymaUtil.createElm('a', {href: '#', className: insymaKeyFact.props.NextClass, idx: rand, title: insymaKeyFact.props.labelNext}, false, controlLi);
				var NextLabel = insymaUtil.createElm('span', false, insymaKeyFact.props.labelNext, anchorNext);
				var factCount = insymaUtil.createElm('em', false, false, controlLi);
				insymaUtil.setText(factCount, insymaKeyFact.props.labelCount + ' ' + (rand+1) + ' ' + insymaKeyFact.props.labelCountSeparator + ' ' + KFChilds.length);
				anchorNext.onclick = function() {
					var KeyFactList = insymaUtil.closestSib(this.parentNode, -1);
					var itemIndex = parseInt(this.getAttribute("idx"));
					if ((itemIndex + 1) < KeyFactList.getElementsByTagName("li").length) {
						insymaKeyFact.showKF(this, itemIndex + 1);
					}
					else {
						insymaKeyFact.showKF(this, 0);
					}
					return false;
				}
				anchorPrevious.onclick = function() {
					var KeyFactList = insymaUtil.closestSib(this.parentNode, -1);
					var itemIndex = parseInt(this.getAttribute("idx"));
					if ((itemIndex - 1) >= 0) {
						insymaKeyFact.showKF(this, itemIndex - 1);
					}
					else {
						insymaKeyFact.showKF(this, (KeyFactList.getElementsByTagName("li").length-1));
					}
					return false;
				}
			}
		}
	},
	showKF:function(objAnch, LiIdx) {
		var ItemIndex = parseInt(LiIdx);
		var KeyFactList = insymaUtil.closestSib(objAnch.parentNode, -1);
		var KFChilds = KeyFactList.getElementsByTagName("li");
		var controlAnchors = objAnch.parentNode.getElementsByTagName("a");
		var factCount = KeyFactList.parentNode.getElementsByTagName("em")[0];
		for (var x = 0; x < KFChilds.length; x++) {
			if (!insymaUtil.cssjs("check", KFChilds[x], insymaKeyFact.props.ControlClass)) {
				insymaUtil.cssjs("add", KFChilds[x], insymaKeyFact.props.hideClass);
				if (ItemIndex == x) {
					insymaUtil.cssjs("remove", KFChilds[x], insymaKeyFact.props.hideClass);
				}
			}
		}
		for (var i = 0; i < controlAnchors.length; i++) {
			insymaUtil.setAttr(controlAnchors[i], {idx: ItemIndex});
		}
		insymaUtil.setText(factCount, insymaKeyFact.props.labelCount + ' ' + (ItemIndex+1) + ' ' + insymaKeyFact.props.labelCountSeparator + ' ' + KFChilds.length);
	}
};
insymaTestimonial = {
	props: {	
		testimonialClassName: 'insymaTestimonial',
		hideClass: 'hide'
		
	},
	init:function() {
		var TestimonialtList = insymaUtil.elmByClass('ul', insymaTestimonial.props.testimonialClassName);
		for (var i = 0; i < TestimonialtList.length; i++) {
			var Childs = TestimonialtList[i].getElementsByTagName("li");			
			var rand = parseInt(Math.random() * Childs.length);
			for (var x = 0; x < Childs.length; x++) {
				insymaUtil.cssjs("add", Childs[x], insymaTestimonial.props.hideClass);
				if(rand == x){
					insymaUtil.cssjs("remove", Childs[x], insymaTestimonial.props.hideClass);
				}
			}
		}
	}
};
insymaUtil.addEvent(window, "load", insymaRandomize.init, false);
insymaUtil.addEvent(window, "load", insymaTestimonial.init, false);
insymaUtil.addEvent(window, "load", insymaKeyFact.init, false);