var EventSlider = Class.create();
EventSlider.prototype = {
		initialize : function() {
},
initSuper : function(language, mode, size, leftLimit, rightLimit) {
	this.calendarSize = (size) ? parseInt(size,10) : 12;
	this.language = (language) ? language : 'en';
	this.currentDate = new Date();
	this.month = null;
	this._initMonth();
	this.activeMon = parseInt(this.currentDate.getMonth(),10) + 1;
	this.activeYear = parseInt(this.currentDate.getFullYear(),10);
	this.activeMonId = this.activeMon + "-" + this.activeYear;
	this.leftLimit = parseInt(leftLimit,10);
	this.rightLimit = parseInt(rightLimit,10);
	this.leftMonthLimit = null;
	this.timelineArray = null;
	this.timelineIdArray = null;
	this.drag = false;
	this._startX = 0;
	this._startY = 0;
	this._offsetX = 0;
	this._offsetY = 0;
	this._dragElement;
	this._dragLeftElement;
	this._dragRightElement;
	this._oldZIndex = 0;
	this._initRange();
	this._initialHide();
},
_initMonth : function() {
	switch (this.language) {
	case 'de':
		this.month = new Array('Jan', 'Feb', 'Mrz', 'Apr', 'Mai', 'Jun',
				'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez');
		break;
	case 'en':
		this.month = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
				'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
		break;
	}
},
_initRange : function() {
	var limit = 8;
	var timelineYearBegins = parseInt(this.activeYear,10) - 2;
	this.timelineIdArray = new Array();
	$A($R(1, limit)).each( function(value) {
		$A($R(1, 12)).each( function(month) {
			var eventId = month + "-" + timelineYearBegins;
			this.timelineIdArray.push(eventId);
		}.bind(this));
		timelineYearBegins++;
	}.bind(this));
	var pos = this.timelineIdArray.indexOf(this.activeMonId) - 3;
	this.leftMonthLimit = pos;
	this.leftMonthOffset = 44;
	this.rightYearLimit = this.activeYear + limit - 2;
	this.rightMonthLimit = (limit - 1) * 12 + 1;
	this._drawRuler(pos);
	this.createSlider();
},
_drawRuler : function(from) {
	$('eventruler').update();
	var till = (from + this.calendarSize);
	this.currentRange = $R(from, till);
	for ( var i = from; i < till; i++) {
		this._addMonth(this.timelineIdArray[i], 1);
	}
},
_addMonth : function(timelineId, pos) {
	var tId = timelineId.split("-");
	var x = tId[0];
	var y = tId[1];
	var mon = this.month[x - 1];
	var monElm = new Element('a', {
		'class' :'eventCalItem',
		id :timelineId
	}).update(mon);
	var divElm = new Element('div', {
		'class' :'eventCalItemHolder'
	}).update(monElm);
	var topElm = new Element('div', {
		'class' :'eventCalItemTop'
	}).update('<span class="space">&nbsp;</span>');
	var bottomElm = new Element('div', {
		'class' :'eventCalItemBottom'
	}).update('<span class="space">&nbsp;</span>');
	var holder = new Element('div', {
		'class' :'eventCalMon'
	});
	if (this._isYearChange(timelineId)) {
		holder.addClassName('leftYearBorder');
		var spanElm = new Element('span', {
			'class' :'eventCalItemYear'
		}).update(y);
		topElm.update(spanElm);
	}
	holder.appendChild(topElm);
	holder.appendChild(divElm);
	holder.appendChild(bottomElm);
	if (pos > 0) {
		$('eventruler').appendChild(holder);
	} else {
		Element.insert($('eventruler'), {
			top :holder
		});
	}
},
_isYearChange : function(value) {
	var y = parseInt(value.split("-")[1],10);
	var pos = this.timelineIdArray.indexOf(value) - 1;
	var ny = parseInt(this.timelineIdArray[pos].split("-")[1],10);
	return (y > ny) ? true : false;
},
_isNewYear : function(oldYear) {
	var pos = this.timelineIdArray.indexOf(this.activeMonId);
	var ny = parseInt(this.timelineIdArray[pos].split("-")[1],10);
	return (oldYear < ny && ny < this.rightYearLimit) ? true : false;
},
_isYearBack : function(oldYear) {
	var pos = this.timelineIdArray.indexOf(this.activeMonId);
	var ny = parseInt(this.timelineIdArray[pos].split("-")[1],10);
	return (oldYear > ny) ? true : false;
},
createSlider : function() {
	var sliderElm = "<div id='eventsliderpanelleft' class='drag'><a href='javascript:void(0);' id='slider_left'>&nbsp;</a></div>";
	sliderElm += "<div id='eventslider_drag' class='drag'><a href='javascript:void(0);' id='slider_drag'>&nbsp;</a></div>";
	sliderElm += "<div id='eventsliderpanelright' class='drag'><a href='javascript:void(0);' id='slider_right'>&nbsp;</a></div>";
	this.slider = new Element('div', {
		id :'eventsliderpanel'
	}).update(sliderElm);
	$(this.activeMonId).up().addClassName('active');
	$(this.activeMonId).addClassName('active');
	Element.insert($(this.activeMonId).up(), {
		after :this.slider
	});
	Event.observe($('eventsliderpanelleft'), 'click', this.moveSlider
			.bindAsEventListener(this));
	Event.observe($('eventsliderpanelright'), 'click', this.moveSlider
			.bindAsEventListener(this));
	this.eventMouseDown = this.startDrag.bindAsEventListener(this);
	this.eventMouseUp = this.endDrag.bindAsEventListener(this);
	this.eventMouseMove = this.update.bindAsEventListener(this);
	this.sliderDrag = $('eventslider_drag');
	Event.observe(this.sliderDrag, "mousedown", this.eventMouseDown);
	Event.observe(document, "mouseup", this.eventMouseUp);
	Event.observe(document, "mousemove", this.eventMouseMove);
},
startDrag : function(e) {
	if (e == null) {
		e = window.event;
	}
	var target = Event.findElement(e, 'div');
	if ((e.button == 1 && window.event != null || e.button == 0)
			&& target.className == 'drag') {
		this._startX = e.clientX;
		this._startY = e.clientY;
		this._offsetX = this.extractNumber(target.style.left);
		this._offsetY = this.extractNumber(target.style.top);
		this._oldZIndex = target.style.zIndex;
		target.style.zIndex = 10000;
		this._dragElement = target;
		this._dragLeftElement = $('eventsliderpanelleft');
		this._dragLeftElement.style.zIndex = 10000;
		this._dragRightElement = $('eventsliderpanelright');
		this._dragRightElement.style.zIndex = 10000;
		document.body.focus();
		document.onselectstart = function() {
			return false;
		};
		target.ondragstart = function() {
			return false;
		};
		return false;
	}
	Event.stop(e);
},
update : function(e) {
	if (e == null) {
		var e = window.event;
	}
	if (this._dragElement) {
		var step = this._offsetX + e.clientX - this._startX;
		var move = step + 'px';
		var co = this._dragElement.cumulativeOffset()[0];
		if (this.leftLimit < co && this.rightLimit + 10 > co) {
			this._dragElement.style.left = move;
			this._dragLeftElement.style.left = move;
			this._dragRightElement.style.left = move;
		} else {
			this.endDrag(e);
		}
	}
},
endDrag : function(e) {
	if (this._dragElement != null) {
		var xPos = this._dragElement.cumulativeOffset()[0];
		this._dragElement.style.zIndex = this._oldZIndex;
		Event.findElement(e, 'div').stopObserving("mousemove");
		document.onselectstart = null;
		this._dragElement.ondragstart = null;
		this._dragElement = null;
		this._findDropTarget(xPos);
	}
	Event.stop(e);
},
_findDropTarget : function(xValue, yValue) {
	$$('div.eventCalItemHolder a.eventCalItem').each( function(item) {
		var x = item.cumulativeOffset()[0];
		var offset = x + 42;
		if (x <= xValue && offset > xValue) {
			this._removeSlider();
			var pos = this.timelineIdArray.indexOf(item.id);
			var activePos = this.timelineIdArray.indexOf(this.activeMonId);
			if (pos > activePos) {
				this._setUp(pos);
			} else if (pos < activePos) {
				this._setDown(pos);
			} else {
				this._setActiveMon();
				this.createSlider();
			}
			this.showContent();
		}
	}.bind(this));
},
extractNumber : function(value) {
	var n = parseInt(value,10);
	return n == null || isNaN(n) ? 0 : n;
},
_removeSlider : function() {
	$(this.activeMonId).removeClassName('active');
	$(this.activeMonId).up().removeClassName('active');
	$(this.slider).remove();
},
_addSlider : function() {
	$(this.activeMonId).addClassName('active');
	$(this.activeMonId).up().addClassName('active');
	$(this.activeMonId).up().appendChild(this.slider);
},
moveSlider : function(event) {
	var trigger = Event.findElement(event, 'div');
	if (trigger) {
		this._removeSlider();
		if (trigger.down().id == 'slider_left') {
			this._calcUpDown(false);
		} else if (trigger.down().id == 'slider_right') {
			this._calcUpDown(true);
		}
		this.showContent();
	}
},
_setUp : function(pos) {
	if (pos < this.timelineIdArray.length - this.calendarSize) {
		if (pos < this.rightMonthLimit) {
			this.activeMonId = this.timelineIdArray[pos];
			if (this._isNewYear(this.activeYear)) {
				this._drawRuler(this.timelineIdArray
						.indexOf(this.activeMonId) - 3);
				this._setActiveMon();
				this._hideNonYearContent();
			} else if (pos == this.currentRange.end) {
				this._setActiveMon();
				this._hideNonYearContent();
				var from = this.timelineIdArray.indexOf(this.activeMonId)
				- this.calendarSize + 1;
				this._drawRuler(from);
			}
		} else {
		}
	}
	this._setActiveMon();
	this.createSlider();
},
_setDown : function(pos) {
	if (pos > 1) {
		if (pos >= this.leftMonthLimit) {
			this.activeMonId = this.timelineIdArray[pos];
			if (this._isYearBack(this.activeYear)) {
				var from = this.timelineIdArray.indexOf(this.activeMonId)
				- this.calendarSize + 3;
				this._drawRuler(from);
				this._setActiveMon();
				this._hideNonYearContent();
			} else if (pos == this.currentRange.start) {
				this._setActiveMon();
				this._hideNonYearContent();
				var from = this.timelineIdArray.indexOf(this.activeMonId) - 1;
				this._drawRuler(from);
			}
		} else {
			this.activeMonId = this.timelineIdArray[pos];
			this._setActiveMon();
			this._hideNonYearContent();
			this
			._drawRuler(this.timelineIdArray
					.indexOf(this.activeMonId) - 1);
		}
	}
	this._setActiveMon();
	this.createSlider();
},
_setActiveMon : function() {
	var tmp = this.activeMonId.split("-");
	this.activeMon = parseInt(tmp[0],10);
	this.activeYear = parseInt(tmp[1],10);
},
_setPosition : function(id) {
	this.activeMonId = id;
	this._setActiveMon();
	this._hideNonYearContent();
	var from = this.timelineIdArray.indexOf(this.activeMonId)
	- this.calendarSize + 1;
	this._drawRuler(from);
	this.createSlider();
},
_calcUpDown : function(up) {
	if (up) {
		var pos = this.timelineIdArray.indexOf(this.activeMonId) + 1;
		this._setUp(pos);
	} else {
		var pos = this.timelineIdArray.indexOf(this.activeMonId) - 1;
		this._setDown(pos);
	}
},
showContent : function() {
},
createContent : function(xhr) {
},
_setNavigationLinks : function() {
},
_hideNonYearContent : function() {
},
_initialHide : function() {
}
}
var SmallIREventSlider = Class.create();
SmallIREventSlider.prototype = Object
.extend(
		new EventSlider,
		{
			initialize : function(language, mode, size, leftLimit,
					rightLimit) {
			this.initSuper(language, mode, size, leftLimit,
					rightLimit);
			this.data = null;
			this.redirectUrl = (location.hostname
					.indexOf('stage.siemens.com') != -1) ? "/cc/corp/investor/"
							: "/investor/";
			new Ajax.Request(this.redirectUrl + this.language
					+ '/data/events.xml', {
				method :'get',
				onSuccess :this._loadContent.bind(this),
				onFailure : function(oXHR) {
			}
			});
		},
		_loadContent : function(oXHR) {
			var events = oXHR.responseXML.documentElement;
			this.data = $A(events.getElementsByTagName('event'));
			this.showContent();
		},
		_getAttribute : function(element, attributeName) {
			return (element.attributes.getNamedItem(attributeName)) ? element.attributes
					.getNamedItem(attributeName).nodeValue
					: "&#160;";
		},
		showContent : function() {
			var foundOne = false;
			$('eventbodycontent').update();
			if ($('eventheadline'))
				$('eventheadline').remove();
			this.data.each( function(item) {
				var eventDate = item.attributes
				.getNamedItem('date').nodeValue;
				if (this._compare(eventDate, this.activeMon)) {
					foundOne = true;
					var linkItem = new Element('a', {
						'class' :'ir_collapsed'
					}).update(eventDate + "&#160;|&#160;"
							+ this._getAttribute(item, 'title'));
					var key = this._getAttribute(item, 'key');
					Event.observe(linkItem, 'click', function(e) {
						this.jumpToCalendar(key);
						Event.stop(e);
					}.bindAsEventListener(this));
					var child = new Element('p', {
						'class' :'eventline'
					}).update(linkItem);
					$('eventbodycontent').appendChild(child);
				}
			}.bind(this));
			if (foundOne) {
				var headlineTxt = (this.language == 'de') ? "<p>Events</p>"
						: "<p>Events</p>";
				var headline = new Element('div', {
					'class' :'p clearfix'
				}).update(headlineTxt);
				var container = new Element('div', {
					id :'eventheadline'
				}).update(headline);
				Element.insert($('eventbodycontent'), {
					before :container
				});
			}
		},
		_compare : function(e1, e2) {
			var localYear = parseInt(e1.substring(0, 4),10);
			var localMonth = parseInt(e1.substring(5, 7),10);
			return (localYear == this.activeYear && localMonth == e2) ? true
					: false;
		},
		jumpToCalendar : function(key) {
			if (this.language == "de") {
				window.location.href = this.redirectUrl
				+ "de/finanzpublikationen_events/finanzkalender.htm?key="
				+ key + "&id=" + this.activeMonId;
			} else {
				window.location.href = this.redirectUrl
				+ "en/publications_events/financial_calendar.htm?key="
				+ key + "&id=" + this.activeMonId;
			}
		}
		});
var MediumIREventSlider = Class.create();
MediumIREventSlider.prototype = Object
.extend(
		new EventSlider,
		{
			initialize : function(language, mode, size, leftLimit,
					rightLimit) {
			this.initSuper(language, mode, size, leftLimit,
					rightLimit)
					this.data = null;
			this.redirectUrl = (location.hostname
					.indexOf('stage.siemens.com') != -1) ? "/cc/corp/investor/"
							: "/investor/";
			this._navigateTo();
			new Ajax.Request(this.redirectUrl + this.language
					+ '/data/events.xml', {
				method :'get',
				onSuccess :this._loadContent.bind(this),
				onFailure : function(oXHR) {
			}
			});
		},
		_loadContent : function(oXHR) {
			var events = oXHR.responseXML.documentElement;
			this.data = $A(events.getElementsByTagName('event'));
			this.showContent();
		},
		_navigateTo : function() {
			var id = getUrlParameter('id');
			if (id) {
				this._setPosition(id);
			}
		},
		_showKey : function() {
			var key = getUrlParameter('key');
			if (key) {
				var linkId = "linkblock" + key;
				var divId = "block" + key;
				if ($(divId)) {
					expandItem(divId, linkId);
				}
			}
		},
		_getAttribute : function(element, attributeName) {
			return (element.attributes.getNamedItem(attributeName)) ? element.attributes
					.getNamedItem(attributeName).nodeValue
					: "&#160;";
		},
		showContent : function() {
			var foundOne = false;
			$('eventbodycontent').update();
			if ($('eventheadline'))
				$('eventheadline').remove();
			this.data
			.each( function(item) {
				var eventDate = item.attributes
				.getNamedItem('date').nodeValue;
				if (this
						._compare(eventDate, this.activeMon)) {
					foundOne = true;
					var key = this._getAttribute(item,
							'key');
					var blockKey = 'block' + key;
					var linkBlockKey = "link" + blockKey;
					var title = this._getAttribute(item,
					'title');
					var linkItem = new Element('a', {
						'class' :'ir_collapsed',
						id :linkBlockKey
					}).update(eventDate + "&#160;|&#160;"
							+ title);
					Event.observe(linkItem, 'click',
							function(e) {
						expandItem(blockKey,
								Event.findElement(
										e, 'a'));
						Event.stop(e);
					}.bindAsEventListener(this));
					var child = new Element('p', {
						'class' :'eventline'
					}).update(linkItem);
					$('eventbodycontent')
					.appendChild(child);
					var description = new Element('p')
					.update(this._getAttribute(
							item, 'description'));
					var childContent = new Element('div', {
						'class' :'expandcontent',
						id :blockKey
					}).update(description);
					var eventlinks = $A(item
							.getElementsByTagName('link'));
					var llist = new Element('ul', {
						'class' :'link'
					});
					eventlinks.each( function(elm) {
						var url = "javascript:openWindow('"
							+ this._getAttribute(elm,
							'link') + "')";
						var li = new Element('li');
						var el = new Element('a', {
							'class' :'external',
							href :url
						}).update(this._getAttribute(elm,
						'title'));
						li.update(el);
						llist.appendChild(li);
					}.bind(this));
					var linklist = new Element('div', {
						'class' :'list quick-links'
					}).update(llist);
					childContent.appendChild(linklist);
					var eventdownload = $A(item
							.getElementsByTagName('download'));
					var dlist = new Element('ul', {
						'class' :'link'
					});
					eventdownload
					.each( function(elm) {
						var url = this
						._getAttribute(elm,
								'url');
						var type = this
						._getAttribute(elm,
								'type');
						var classType = "external";
						switch (type) {
						case 'pdf':
							classType = "pdf";
							break;
						case 'zip':
							classType = "zip";
							break;
						case 'doc':
							classType = "doc";
							break;
						case 'xls':
							classType = "xls";
							break;
						case 'ppt':
							classType = "ppt";
							break;
						case 'pps':
							classType = "ppt";
							break;
						}
						var fileSize = parseInt(this._getAttribute(elm,'size'),10);
						fileSize = (fileSize > 1024) ? "&#160;&#91;" + Math.round(fileSize*10 / 1024)/10 + "&#160;MB&#93;"
								: "&#160;&#91;" + fileSize + "&#160;KB&#93;";
						var txt = this._getAttribute(elm,'title') + fileSize;
						var li = new Element('li');
						var el = new Element('a', {'class' :classType,href :url}).update(txt);
						li.update(el);
						dlist.appendChild(li);
					}.bind(this));
					var downlist = new Element('div', {
						'class' :'list quick-links'
					}).update(dlist);
					childContent.appendChild(downlist);
					childContent.hide();
					if (this._isFutureEvent(eventDate)) {
						childContent.appendChild(this
								._createMailForm(title,
										key, eventDate));
					}
					$('eventbodycontent').appendChild(
							childContent);
					var submitFormKey = "submitForm-" + key;
					if ($(submitFormKey)) {
						Event.observe(
								$(submitFormKey),
								"click",
								function(e) {
									this
									.submitMailForm(key);
									Event.stop(e)
								}
								.bindAsEventListener(this));
					}
				}
			}.bind(this));
			if (foundOne) {
				var headlineTxt = (this.language == 'de') ? "<p>Events</p>"
						: "<p>Events</p>";
				var headline = new Element('div', {
					'class' :'p clearfix'
				}).update(headlineTxt);
				var container = new Element('div', {
					id :'eventheadline'
				}).update(headline);
				Element.insert($('eventbodycontent'), {
					before :container
				});
				this._showKey();
			}
		},
		_compare : function(e1, e2) {
			var localYear = parseInt(e1.substring(0, 4),10);
			var localMonth = parseInt(e1.substring(5,7),10);
			return (localYear == this.activeYear && localMonth == e2) ? true
					: false;
		},
		_isFutureEvent : function(d) {
			var eventDate = d.split("-");
			var day = parseInt(eventDate[2],10);
			var month = parseInt(eventDate[1],10);
			var year = parseInt(eventDate[0],10);
			var cDay = parseInt(this.currentDate.getDate(),10);
			var cMonth = parseInt(this.currentDate.getMonth(),10) + 1;
			var cYear = this.currentDate.getYear();
			if (cYear < 999) {
				cYear += 1900;
			}
			return ((year > cYear) || ((month > cMonth) && (year == cYear)) || ((day > cDay) && (month == cMonth) && (year == cYear))) ? true : false;
		},
		_createMailForm : function(title, key, value) {
			var uidDiv = "remindMe-" + key;
			var uidForm = "remindevent-" + key;
			var submitForm = "submitForm-" + key;
			var formTxt = '<form id="' + uidForm + '" class="remindevent" action="" enctype="application/x-www-form-urlencoded" method="post">';
			formTxt += '<input type="HIDDEN" name="lang" value="' + this.language + '" >';
			formTxt += '<input type="HIDDEN" name="Event" value="' + title + '" >';
			formTxt += '<input type="HIDDEN" name="EventDate" value="' + value + '" >';
			if (this.language == 'de') {
				formTxt += '<div class="first"><span class="target">E-Mail Erinnerung</span>';
				formTxt += '<select name="EventDay" id="daysbefore" class="gui-select"><option value="1">1 Tag</option><option value="3">3 Tage</option><option value="7">7 Tage</option></select></div>';
				formTxt += '<div class="second"><span class="target"> im Voraus</span></div><div class="first"><span class="target">E-Mail</span><input name="email" id="emailaddress" value="" type="text"></div>';
				formTxt += '<div class="third"><a id="' + submitForm + '" class="generic-button" href="javascript:void(0);"><span><span>Senden</span></span></a></div></form>';
			} else {
				formTxt += '<div class="first"><span class="target">E-Mail for this event</span>';
				formTxt += '<select name="EventDay" id="daysbefore" class="gui-select"><option value="1">1 Day</option><option value="3">3 Days</option><option value="7">7 Days</option></select></div>';
				formTxt += '<div class="second"><span class="target"> in advance</span></div><div class="first"><span class="target">E-Mail</span><input name="email" id="emailaddress" value="" type="text"></div>';
				formTxt += '<div class="third"><a id="' + submitForm + '" class="generic-button" href="javascript:void(0);"><span><span>submit</span></span></a></div></form>';
			}
			return new Element('div', {
				id :uidDiv,
				'class' :'remindMeForm clearfix'
			}).update(formTxt);
		},
		submitMailForm : function(key) {
			var params = $("remindevent-" + key).serialize();
			var localText = (this.language == 'de') ? "Vielen Dank f&uuml;r Ihr Interesse an diesem Investor Relations Event. Wir haben Ihren Benachrichtigungswunsch erhalten, und werden Sie im Voraus an diesen Event per E-Mail erinnern."
					: "Thank you very much for your interest in this Investor Relations Event. We have received your entry for an email alert and will send you a reminder email in advance.";
			new Ajax.Request('proxy.php', {
				method :'post',
				parameters :params,
				onSuccess : function(response) {
				var txt = new Element('p').update(localText);
				$('remindMe-' + key).update(txt);
			},
			onFailure : function(response) {
				var txt = new Element('p').update(localText
						+ response.statusText);
				$('remindMe-' + key).update(txt);
			}
			});
		}
		});