var lsRun = true;
if (lsRun == true) {

	$j(document).ready(
			function() {

				if (typeof (bSetBackgroundColor) == 'undefined') {
					var bSetBackgroundColor = false;
				}

				if ($j('#pushbutton').length > 0) {
					var iIntervall = $j('#pushbutton').attr('intervall');
				} else {
					var iIntervall = 1;
				}

				var iTimeOut = 1000 * 60 * iIntervall;
				var oTimeStart = new Date();
				var iRnd = myRandom(61, 71);

				if (typeof (bNeedPush) !== 'undefined') {
					var lsPage = new PushPage();
					lsPage.context.setDomain(LSdomain);
					lsPage.context.setDebugAlertsOnClientError(false);
					lsPage.context.setRemoteAlertsOnClientError(false);

					lsPage.onEngineReady = function(lsEng) {
						lsEng.connection.setLSHost(LShost);
						lsEng.connection.setLSPort(LSport);
						lsEng.connection.setAdapterName("LIVE");
						lsEng.changeStatus("STREAMING");
					};

					lsPage.bind();
					lsPage.createEngine("SLEngine", "/js/lightstreamer/",
							"SHARE_SESSION", null);

					var schema = new Array('fCurRate', 'fDifRelative',
							'fDifAbsolute', 'fBid', 'fAsk', 'fOpen', 'fClose',
							'fMax', 'fMin', 'sTrend', 'sDate', 'iDate', 'sIsin',
							'sThnName', 'iBarLength');
					var aISIN = sISIN.split(',');
					jQuery.each(aISIN, function(i, sIsin) {
						aISIN[i] = 'isin_' + sIsin;
					});

					var myTable = new OverwriteTable(aISIN, schema, "MERGE");
					myTable.setDataAdapter('IndicesAdapter');
					myTable.onItemUpdate = updateItem;
					myTable.onChangingValues = formatValues;
					myTable.setSnapshotRequired(false);
					myTable.setRequestedMaxFrequency(1.0);
					myTable.setPushedHtmlEnabled(true);

					var backColor = new Object();
					backColor['dn'] = "#ff5555";
					backColor['up'] = "#00ff00";
					backColor['nc'] = "#feffaf";
					if (bSetBackgroundColor == false) {
						backColor['no'] = '';
					} else {
						backColor['no'] = '#ffffff';
					}

					var frontColor = new Object();
					frontColor['dn'] = "#ffffff";
					frontColor['up'] = "#0a0a0a";
					frontColor['nc'] = "#0a0a0a";
					frontColor['no'] = '';
					if (bSetBackgroundColor == false) {
						frontColor['no'] = '';
					} else {
						frontColor['no'] = '#0a0a0a';
					}
				}

				function updateItem(item, updateInfo, itemName) {

					var oTimeNow = new Date();
					if (oTimeNow.getTime() - oTimeStart.getTime() > iTimeOut) {
						pushTheButton();
						return;
					}

					if (updateInfo == null) {
						return;
					}
					if (updateInfo.getOldValue("fCurRate") == null) {
						return;
					}
					if (updateInfo.getNewValue("fCurRate") == null) {
						return;
					}
					if (updateInfo.getNewValue("fCurRate") == updateInfo
							.getOldValue("fCurRate")) {
						return;
					}

					if (updateInfo.getNewValue("fCurRate")
							- updateInfo.getOldValue("fCurRate") > 3) {
						return;
					}

					updateItems(updateInfo, 'fCurRate', itemName);
					updateItems(updateInfo, 'fDifRelative', itemName);
					updateItems(updateInfo, 'fDifAbsolute', itemName);
					updateItems(updateInfo, 'fBid', itemName);
					updateItems(updateInfo, 'fAsk', itemName);
					updateItems(updateInfo, 'fMin', itemName);
					updateItems(updateInfo, 'fMax', itemName);
					updateItems(updateInfo, 'fOpen', itemName);
					updateItems(updateInfo, 'fClose', itemName);
					updateItems(updateInfo, 'sDate', itemName);
					updateItems(updateInfo, 'sTrend', itemName);

				}

				function updateItems(Info, field, itemName) {
					if (Info.isValueChanged(field)) {
						Info.addField("#cbh-" + field, Info.getNewValue(field),
								true);
						Info.addField("#tf-" + field, Info.getNewValue(field),
								true);
						Info.addField("#cbs-" + field, Info.getNewValue(field),
								true);
						Info.addField("#fb-" + field, Info.getNewValue(field),
								true);
					}
				}

				function formatValues(item, itemUpdate, key) {

					if (itemUpdate == null) {
						return;
					}
					if (itemUpdate.getServerValue("fCurRate") == null) {
						return;
					}
					if (itemUpdate.getServerValue("fDifAbsolute") == null) {
						return;
					}
					if (itemUpdate.getServerValue("fDifRelative") == null) {
						return;
					}
					if (itemUpdate.getServerValue("sDate") == null) {
						return;
					}
					if (itemUpdate.getServerValue("sTrend") == null) {
						return;
					}

					var sTrend = itemUpdate.getServerValue("sTrend");
					var sBackColor = backColor[sTrend];
					if ($j('.ratelist_datafield_' + key).length > 0) {
						backColor['no'] = '#fff';
					}

					if ($j('.cbh_fDifRelative_' + key).length > 0) {
						$j('.cbh_fCurRate_' + key).text(
								itemUpdate.getServerValue("fCurRate"));
						$j('.cbh_fDifRelative_' + key).text(
								itemUpdate.getServerValue("fDifRelative"));
					}

					itemUpdate.setHotToColdTime(1000);
					itemUpdate.setHotTime(600);
					itemUpdate.setAttribute("fCurRate", sBackColor,
							backColor['no'], "backgroundColor");
					itemUpdate.setAttribute("fDifAbsolute", sBackColor,
							backColor['no'], "backgroundColor");
					itemUpdate.setAttribute("fDifRelative", sBackColor,
							backColor['no'], "backgroundColor");
					itemUpdate.setAttribute("fBid", sBackColor,
							backColor['no'], "backgroundColor");
					itemUpdate.setAttribute("fAsk", sBackColor,
							backColor['no'], "backgroundColor");
					itemUpdate.setAttribute("fMax", sBackColor,
							backColor['no'], "backgroundColor");
					itemUpdate.setAttribute("fMin", sBackColor,
							backColor['no'], "backgroundColor");

					$j('#' + key + '_bar').css( {
						width : itemUpdate.getServerValue('iBarLength')
					});
					setTrendImage('#cbs_trend', sTrend);
					setChartImage('#cbs_tick_chart_' + itemUpdate
							.getServerValue("sIsin"), itemUpdate
							.getServerValue("iDate"));
					setMetaTitle(itemUpdate.getServerValue("sIsin"), itemUpdate
							.getServerValue("sThnName"), itemUpdate
							.getServerValue("fCurRate"), itemUpdate
							.getServerValue("fDifAbsolute"), itemUpdate
							.getServerValue("fDifRelative"));
				}

				function setTrendImage(sId, sTrend) {
					if ($j(sId).length > 0) {
						var aOldTrend = $j(sId).attr('src').match(
								/trend_(.*).png/);
						if (aOldTrend[1] != sTrend) {
							$j(sId).attr(
									'src',
									$j(sId).attr('src').replace(
											/trend_(.*).png/,
											'trend_' + sTrend + '.png'));
						}
					}
					return;
				}

				function setChartImage(sId, iDate) {
					if (iDate == 'undefined') {
						return;
					}

					if ($j(sId).length > 0) {
						iNow = parseInt(iDate / iRnd);
						iNow = iNow * iRnd;
						var aOldDate = $j(sId).attr('src').match(
								/(\d+){8,}_Chart/);
						var iOldDate = aOldDate[0].replace('_Chart', '');
						var iDif = Math.abs(parseInt(iNow - iOldDate));
						if (iDif >= iRnd) {
							$j(sId)
									.attr(
											'src',
											$j(sId).attr('src').replace(
													iOldDate, iNow));
						}
					}
					return;
				}

				function setMetaTitle(sIsin, sThnName, fCurRate, fDifAbsolute,
						fDifRelative) {
					if (sIsin == 'DE000LUSDAX6') {
						document.title = sThnName + ' ' + fCurRate + ' '
								+ fDifAbsolute + ' / ' + fDifRelative;
					}
					return;
				}

				function setStatusBar(sThnName, fCurRate, fDifAbsolute,
						fDifRelative) {
					return;
				}

				function myRandom(min, max) {
					var args = arguments.length;
					if (args === 0) {
						min = 1;
						max = 11;
					}
					return Math.floor(Math.random() * (max - min + 1)) + min;
				}

				if (typeof (bNeedPush) !== 'undefined') {
					lsPage.addTable(myTable, 'courselist');
				}

				function letTheButtonBlink() {
					var i = 0;
					window.setInterval(function() {
						i++;
						$j('#pushbutton').css('color',
								(i % 2 == 0 ? 'red' : ''));
					}, 500);
				}

				function pushTheButton() {
					if ($j('#pushbutton').attr('status') == 'on') {
						letTheButtonBlink();
						$j('#pushbutton').attr('status', 'off').html(
								'Push aktivieren').css('color', 'red');
						lsPage.removeTable('courselist');
					}
				}

				$j('#pushbutton').click(
						function() {
							if ($j('#pushbutton').attr('status') == 'on') {
								$j('#pushbutton').attr('status', 'off').html(
										'Push aktivieren').css('color', 'red');
								letTheButtonBlink();
								lsPage.removeTable('courselist');
							} else {
								location.reload();
							}
						});
			});
}

