<?
	if(1 == 0 && $_SESSION['__id']){
		//$l_name = get_item('select DisplayName from zaposlenik where rbr_users_outer_db = '.$_SESSION['__id']);
		$l_name = '';
		
		$x = get_data('select * from alarm_type order by rbr ', 1);
		
		$la_color = get_data('select color from alarm_color order by rbr ', 1);
		
		$l_const_sljedeci_trigger = 120;
		$l_const_trajanje_prikazivanja = 30;
		$l_const_trigger_time = $l_const_sljedeci_trigger - $l_const_trajanje_prikazivanja;
		
		
		$l_sql = 'select * '.
			' from alarm '.
			' where rbr_zaposlenik = '.$_SESSION['__id'].
			' and ('.
				' (next_trigger_time < unix_timestamp(now())) or '.
				' (shown = 1 and next_trigger_time - unix_timestamp(now()) > '.$l_const_trigger_time.') or '.
				' (on_hold = 1) '.
			' ) '.
			' order by next_trigger_time ';
		$y = get_data($l_sql, 1);
		
		$l_rbr = '(0';
		$z = get_data('select rbr from alarm '.
			' where shown = 1 and next_trigger_time - unix_timestamp(now()) between 0 and '.$l_const_trigger_time.' and on_hold = 0 ', 1);
		for($i = 0; $i < sizeof($z['rbr']); $i++){
			$l_sql = 'update alarm set shown = 0 where rbr = '.$z['rbr'][$i];
			sql_exe($l_sql);
			
			$l_rbr .= ', '.$z['rbr'][$i];
		}
		$l_rbr .= ')';
		
		$z = get_data('select rbr from alarm where next_trigger_time < unix_timestamp(now()) and rbr not in '.$l_rbr.' and on_hold = 0 ', 1);
		for($i = 0; $i < sizeof($z['rbr']); $i++){
			$l_sql = 'update alarm set next_trigger_time = unix_timestamp(now()) + '.$l_const_sljedeci_trigger.', shown = 1 where rbr = '.$z['rbr'][$i];
			sql_exe($l_sql);
		}
		//print $l_sql;
		
		$a_data = get_data('select rbr, source_date_field, db_table, select_field, trigger_time from alarm_type order by rbr ', 1);
		
		$l_card = '';
		for($i = 0; $i < sizeof($y['rbr']); $i++){
			
			$a_time = get_data('select '.$a_data['source_date_field'][$y['rbr_alarm_type'][$i] - 1].' as x, '.
				' TIMESTAMPDIFF(SECOND, now(), '.$a_data['source_date_field'][$y['rbr_alarm_type'][$i] - 1].') as y'.
				' from '.$a_data['db_table'][$y['rbr_alarm_type'][$i] - 1].
				' where '.$a_data['select_field'][$y['rbr_alarm_type'][$i] - 1].' = "'.$y['id'][$i].'" ', 0);
				
			//print $l_time." +++ ".$a_data['trigger_time'][$y['rbr_alarm_type'][$i] - 1]."<br>";
			//print substr($a_time['x'], 0, 10)." ::: ".u_godina().'-'.u_mjesec(0, 1).'-'.u_datum(0, 1)."<br>";
			
			
			
			if(substr($a_time['x'], 0, 10) < u_godina().'-'.u_mjesec(0, 1).'-'.u_datum(0, 1)){
				$l_background = 'style="background:'.$la_color['color'][sizeof($la_color['color']) - 1].'"';
			}else{
				$l_div = round($a_data['trigger_time'][$y['rbr_alarm_type'][$i] - 1] / (sizeof($la_color['color']) - 1));
				for($j = 0; $j < sizeof($la_color['color']) - 1; $j++){
					//print $a_time['y'].' => '.$j." => ".$l_div."<br>";
					if($a_time['y'] >= $j * $l_div && $a_time['y'] <= ($j + 1) * $l_div){
						$l_background = 'style="background:'.$la_color['color'][sizeof($la_color['color']) - 1 - $j - 1].'"';
						//print $l_div.' +++ '.$j." -- ".$l_background."<br>";
						break;
					}
				}
				if($l_background == ''){
					if($a_time['y'] > $a_data['trigger_time'][$y['rbr_alarm_type'][$i] - 1]){
						$l_background = 'style="background:'.$la_color['color'][0].'"';
					}else{
						$l_background = 'style="background:'.$la_color['color'][sizeof($la_color['color']) - 2].'"';
					}
				}
			}
			
			//print $l_background." BG <br>";
			
			if($y['on_hold'][$i] == 1){
				$l_img_on_hold = 'pin-on.gif';
			}else{
				$l_img_on_hold = 'pin.gif';
			}
			
			$l_card .= '<li id="alarm-'.$y['rbr'][$i].'" '.$l_background.'>';
			$l_card .= '<img src="res/images/alarm.gif" border="0" class="img-left" title="Alarm">';
			$l_card .= '<a href="#" onclick="alarm_close('.$y['rbr'][$i].'); return false;" class="right"><img src="res/images/x.gif" border="0" class="img-right"></a>';
			$l_card .= '<a href="#" onclick="alarm_pin('.$y['rbr'][$i].'); return false;" class="right"><img src="res/images/'.$l_img_on_hold.'" border="0" class="img-right" id="pin-img-'.$y['rbr'][$i].'"></a>';
			$l_card .= '<a href="default.php?'.$x['form_link'][$y['rbr_alarm_type'][$i] - 1].'&c='.$y['id'][$i].'" class="link">'.$x['ime'][$y['rbr_alarm_type'][$i] - 1].'</a>';
			$l_card .= '<p>'.$l_name.'</p>';
			$l_card .= '</li>'."\n";
			
			$l_ima_li = get_item('select rbr from alarm where rbr = '.$y['rbr'][$i].' and unix_timestamp(now()) - next_trigger_time > 0 and shown = 0 and on_hold = 0 ');
			if($l_ima_li){
				$l_sql = 'update alarm set next_trigger_time = unix_timestamp(now()) + '.$l_const_sljedeci_trigger.', shown = 1 '.
					' where rbr = '.$l_ima_li;
				sql_exe($l_sql);
			}
			
		}
		
		
?>
		<div id="alarm">
			<ul class="card"><?=$l_card?></ul>
		</div>
<?
	}
?>
