var brz_date = new Date();
var brz_offset = brz_date.getTimezoneOffset();
initiate = function(){
	if(doc_loaded==false || typeof($)!='function' || constants_loaded==false || wm_loaded==false){
		setTimeout("initiate();",100);
	}else{
		load_json_object();
	}
};

render_overview = function(data){
	$('.psp_data').remove();
	for(i=0;i<7;i++){
		d = get_udate_obj(data.stamps[i]*1000,data.tz_diff);
		$('#hist_date_'+i).html(wm_psp_strings['MONTHS'][d.getMonth()] + " " + d.getDate());
	}
	
	//section rendering: last update
	if(typeof(data.last_update)!='undefined' && data.last_update!=''){
		var psp_last_update = get_last_update_string(data.last_update,data.tz_diff);
		if(psp_last_update!=''){
			$('#psp_last_update').html(psp_last_update);
		}
	}
	//section rendering : services current status 
	if(typeof(data.services)!='undefined'){
		$.each(data.services, function(service_id,service_info){
			curr_st = typeof(service_info.current_status)!='undefined' ? service_info.current_status : (typeof(service_info.hist[0])!='undefined' ? service_info.hist[0] : '');
			service_curr_status_desc = wm_psp_strings['TXT_NA'];
			var status_class = 'status-bg-green';
			var service_curr_status_desc = wm_psp_strings['TXT_STATUS_NORMAL'];
			if(curr_st == 'warn'){
				status_class = 'status-bg-yellow';
				service_curr_status_desc = wm_psp_strings['TXT_STATUS_PERF_ISSUES'];
			}else if(curr_st == 'err'){
				status_class = 'status-bg-red';
				service_curr_status_desc = '<span class="red">'+wm_psp_strings['TXT_STATUS_SERV_DISR']+'</span>';
			}
			$('#curr_status_icon_'+service_id).html('<div class="status-icon-container '+status_class+'"></div>');
			$('#curr_status_desc_'+service_id).html(service_curr_status_desc);
			if(typeof(service_info.uptime_today)!='undefined'){
				$('#up_today_'+service_id).html(service_info.uptime_today);
			}
			if(typeof(service_info.curr_perf)!='undefined'){
				$('#curr_perf_'+service_id).html(service_info.curr_perf);
			}
		});
	}
	$('#section_curr_status').show();
	
	//section rendering : announcements
	var ann_num = 0;
	var row_class = '';
	var row_class_strd = 'top';
	if(typeof(data.ann)!='undefined'){
		$.each(data.ann, function(i,ann){
			d = get_udate_obj(ann.stamp * 1000,data.tz_diff);
			ann_date_smpl = d.getMonth() + " " + d.getDate() + ", " +  d.getFullYear();
			var service_name = wm_psp_strings["TXT_GENERAL_NOTE"];
			if(typeof(data.services[ann.serv])!= 'undefined' && typeof(data.services[ann.serv].name)!= 'undefined'){
				service_name = '<a href="/'+data.fid+'/'+ann.serv+'" title="'+wm_psp_strings['TIP_VIEW_DETAILS']+'">'+data.services[ann.serv].name+'</a>';
			}
			var hours = d.getHours(); hours = hours<10 ? hours = '0'+hours : hours;
			var minutes = d.getMinutes(); minutes = minutes<10 ? minutes = '0'+minutes : minutes;
			var date = wm_psp_strings['MONTHS'][d.getMonth()] + " " + d.getDate() + ", " +  d.getFullYear()+' '+hours+':'+minutes;
			$('#table_ann').append('<tr class="'+row_class+' '+row_class_strd+' psp_data"><td>'+service_name+'</td><td>'+ann.note+'</td><td>'+date+'</td></tr>');
			row_class = row_class=='' ? 'oddrow' : '';
			ann_num+=1;
			row_class_strd = 'top top-dotted-border';
		});
	}
	ann_num==0 ? $('#section_ann').hide() : $('#section_ann').show();
	
	//section rendering : history
	var hist_num = 0;
	row_class = '';
	if(typeof(data.services)!='undefined'){
		hist = new Array();
		$.each(data.services, function(service_id,service_info){
			$.each(service_info.hist, function(j,st){
				st_icon = wm_psp_strings['TXT_NA'];
				st_click_action = st_hover_action = '';
				icon_container_class = 'status-bg-green';
				notes_div = '';
				has_notes = false;
				if(st=="warn"){
					icon_container_class = 'status-bg-yellow';
				}else if(st=="err"){
					icon_container_class = 'status-bg-red';
				}
				hist_stamp = data.stamps[j];
				nd = get_udate_obj(hist_stamp*1000,data.tz_diff);
				hist_date = nd.getMonth() + " " + nd.getDate() + ", " +  nd.getFullYear();
				hist_date_pr = wm_psp_strings['MONTHS'][nd.getMonth()] + " " + nd.getDate();
				
				//locate service notes
				if(typeof(data.hno)!='undefined'){
					$.each(data.hno, function(k,service_hno){
						nd = get_udate_obj(service_hno.stamp*1000,data.tz_diff);
						service_hno_date = nd.getMonth() + " " + nd.getDate() + ", " +  nd.getFullYear();
						if(service_hno.serv == service_id && hist_date==service_hno_date){
							var hours = nd.getHours(); hours = hours<10 ? hours = '0'+hours : hours;
							var minutes = nd.getMinutes(); minutes = minutes<10 ? minutes = '0'+minutes : minutes;
							notes_div += '<div class="top left white-bg hover-note-inner"><span class="hover-note-date">'+hist_date_pr+', '+hours + ':' + minutes+'</span> '+service_hno.note+'</div>';
							has_notes = true;
						}
					});
				}
				if(typeof(service_info.uptimes) != 'undefined' && typeof(service_info.uptimes[j]) != 'undefined'){
					notes_div = '<div class="top centered white-bg hover-note-inner"><span class="hover-note-uptime">'+wm_psp_strings['TXT_UPTIME']+' '+hist_date_pr+':</span> '+service_info.uptimes[j]+'</div>' + notes_div;
				}
				if(notes_div != ''){
					note_hover_action = "focus_hover_note('hist_note_"+service_id+"_"+ hist_stamp+"');";
					if(has_notes && icon_container_class) icon_container_class += '-notes';
					st_hover_action = "show_hover_note('hist_note_"+service_id+"_"+ hist_stamp+"',event)";
					st_click_action = "stick_toggle_note('hist_note_"+service_id+"_"+ hist_stamp+"')";
					notes_div = '<span style="position:relative;"><div id="hist_note_'+service_id+'_'+ hist_stamp+'" class="grey-bg top nodisplay hover_notes"  onmouseover="'+note_hover_action+'">'+notes_div+'</div></span>';
				}
				st_click_action = st_click_action ? ' onclick="'+st_click_action+'"' : '';
				st_hover_out_action = st_hover_action ? ' onmouseout="'+st_hover_action+'"' : '';
				st_hover_in_action = st_hover_action ? ' onmouseover="'+st_hover_action+'"' : '';
				st_icon = '<span id="hist_img_'+service_id+'_'+hist_stamp+'" class="'+icon_container_class+' history-status-icon-container" '+st_click_action+' '+st_hover_in_action+' '+st_hover_out_action+'></span>';
				st_icon += notes_div;
				$('#hist_cell_'+service_id+'_'+j).html(st_icon);
			});
		});
	}
	$('#section_hist').show();
	$('#legend').show();
	$('#page_loader').hide();
	psp_loaded = true;
};

render_service = function(data){
	$('.psp_data').remove();
	$('#page_loading').hide();
	for(i=0;i<7;i++){
		d = get_udate_obj(data.stamps[i]*1000,data.tz_diff);
		$('#hist_date_'+i).html(wm_psp_strings['MONTHS'][d.getMonth()] + " " + d.getDate());
	}
	
	//section rendering: last update
	if(typeof(data.last_update)!='undefined' && data.last_update!=''){
		var psp_last_update = get_last_update_string(data.last_update,data.tz_diff);
		if(psp_last_update!=''){
			$('#psp_last_update').html(psp_last_update);
		}
	}
	
	//section rendering: current status table
	service_curr_status_desc = 'n/a';
	icon_container_class = '';
	curr_st = typeof(data.current_status)!='undefined' ? data.current_status : (typeof(data.hist[0])!='undefined' ? data.hist[0] : '');
	if(curr_st == 'ok'){
		icon_container_class = 'status-bg-green';
		service_curr_status_desc = wm_psp_strings['TXT_STATUS_NORMAL'];
	}else if(curr_st == 'warn'){
		icon_container_class = 'status-bg-yellow';
		service_curr_status_desc = wm_psp_strings['TXT_STATUS_PERF_ISSUES'];
	}else if(curr_st == 'err'){
		icon_container_class = 'status-bg-red';
		service_curr_status_desc = '<span class="red">'+wm_psp_strings['TXT_STATUS_SERV_DISR']+'</span>';
	}else{//@@migration to 3.1
		icon_container_class = 'status-bg-green';
		service_curr_status_desc = wm_psp_strings['TXT_STATUS_NORMAL'];
	}
	if(icon_container_class!=''){
		$('#curr_status_icon').addClass(icon_container_class + ' status-icon-container');
	}
	
	$('#curr_status_desc').html(service_curr_status_desc);
	$('#section_curr_status').show();
	
	//section rendering: chart images
	if(typeof(data.charts)!='undefined' && data.charts.length!=0){
		chart_img = new Array();
		$.each(data.charts, function(chart_name,src){
			chart_img[chart_name] = new Image();
			chart_img[chart_name].src = src;
			chart_img[chart_name].onLoad = load_chart(chart_img[chart_name],'chart_'+chart_name+'_container');
			$('#caption_'+chart_name+'_chart').html(wm_psp_strings['TXT_CAPTION_CHART'][chart_name]);
		});
		$('#section_charts').removeClass('hidden');
	}
	
	
	//section rendering: extra chart images
	if(typeof(data.extra_charts)!='undefined' && data.extra_charts.length!=0){
		extra_chart_img = new Array();
		$.each(data.extra_charts, function(chart_name,src){
			extra_chart_img[chart_name] = new Image();
			extra_chart_img[chart_name].src = src;
			extra_chart_img[chart_name].onLoad = load_chart(extra_chart_img[chart_name],'chart_'+chart_name+'_container');
			$('#caption_'+chart_name+'_chart').html(wm_psp_strings['TXT_CAPTION_CHART'][chart_name]);
		});
		$('#section_charts_avail').show();
	}
	
	//section rendering: history
	$.each(data.hist, function(j,st){
		var st_icon = wm_psp_strings['TXT_NA'];
		var st_click_action = '';
		var st_hover_action = '';
		var notes_div = '';
		var has_notes = false;
		var icon_container_class = 'status-bg-green';
		if(st=="warn"){
			icon_container_class = 'status-bg-yellow';
		}else if(st=="err"){
			icon_container_class = 'status-bg-red';
		}
		var hist_stamp = data.stamps[j];
		var nd = get_udate_obj(hist_stamp*1000,data.tz_diff);
		var hist_date = nd.getMonth() + " " + nd.getDate() + ", " +  nd.getFullYear();
		var hist_date_pr = wm_psp_strings['MONTHS'][nd.getMonth()] + " " + nd.getDate();
		var note_hover_action = "focus_hover_note('hist_note_"+ hist_stamp+"');";
		//locate service notes
		if(typeof(data.hno)!='undefined'){
			$.each(data.hno, function(k,service_hno){
				nd = get_udate_obj(service_hno.stamp*1000,data.tz_diff);
				service_hno_date = nd.getMonth() + " " + nd.getDate() + ", " +  nd.getFullYear();
				if(hist_date==service_hno_date){
					var hours = nd.getHours(); hours = hours<10 ? hours = '0'+hours : hours;
					var minutes = nd.getMinutes(); minutes = minutes<10 ? minutes = '0'+minutes : minutes;
					notes_div += '<div class="top left white-bg hover-note-inner"><span class="hover-note-date">'+hist_date_pr+', '+hours + ':' + minutes+'</span> '+service_hno.note+'</div>';
					has_notes = true;
				}
			});
		}
		if(typeof(data.uptimes) != 'undefined' && typeof(data.uptimes[j]) != 'undefined'){
			notes_div = '<div class="top centered white-bg hover-note-inner"><span class="hover-note-uptime">'+wm_psp_strings['TXT_UPTIME']+' '+hist_date_pr+':</span> '+data.uptimes[j]+'</div>' + notes_div;
		}
		if(notes_div != ''){
			if(has_notes) icon_container_class += '-notes';
			st_hover_action= "show_hover_note('hist_note_"+ hist_stamp+"',event);";
			st_click_action = "stick_toggle_note('hist_note_"+ hist_stamp+"');";
			notes_div = '<span style="position:relative;"><div id="hist_note_'+ hist_stamp+'" class="psp_data grey-bg top nodisplay hover_notes" onmouseover="'+note_hover_action+'">'+notes_div+'</div></span>';
			
		}
		st_click_action = st_click_action ? ' onclick="'+st_click_action+'"' : '';
		var st_hover_in_action = st_hover_action ? ' onmouseover="'+st_hover_action+'"' : '';
		var st_hover_out_action = st_hover_action ? ' onmouseout="'+st_hover_action+'"' : '';
		st_icon = '<span id="hist_img_'+ hist_stamp+'"'+st_click_action+''+st_hover_in_action+''+st_hover_out_action+' class="'+icon_container_class+' history-status-icon-container"></span>';
		st_icon += notes_div;
		$('#hist_data_'+j).html(st_icon)
	});
	$('#section_hist').removeClass('hidden');
	
	//section rendering: announcements
	var row_class = '';
	var row_class_strd = 'top';
	var ann_num = 0;
	if(typeof(data.ann)!='undefined'){
		$.each(data.ann, function(i,ann){
			d = get_udate_obj(ann.stamp*1000,data.tz_diff);
			ann_date_smpl = d.getMonth() + " " + d.getDate() + ", " +  d.getFullYear();
			var date = wm_psp_strings['MONTHS'][d.getMonth()] + " " + d.getDate() + ", " +  d.getFullYear();
			$('#table_ann').append('<tr class="psp_data '+row_class+' '+row_class_strd+'"><td>'+ann.note+'</td><td>'+date+'</td></tr>');
			row_class = row_class=='' ? 'oddrow' : '';
			ann_num+=1;
			row_class_strd = 'top top-dotted-border';
		});
	}
	ann_num==0 ? $('#section_ann').hide() : $('#section_ann').show();
	
	$('#legend').show();
	$('#page_loader').hide();
	psp_loaded = true;
};

load_json_object = function(){
	$('#page_loader').show();
	if(typeof(json_object_url)!='undefined'){
		$.ajax({
			dataType: 'jsonp',
			jsonp: 'jsonp_callback',
			url: json_object_url
		});
	}
};

clear_reload_timer = function(){
	if(typeof(reload_timer)!='undefined') clearInterval(reload_timer);
}

show_hover_note = function(note_id,e){
	focus_hover_note(note_id);
	if($('#'+note_id).attr('stick')!='true'){
		$('#'+note_id).toggle();
	}
};

stick_toggle_note = function(note_id){
	if($('#'+note_id).attr('stick')!='true'){
		$('#'+note_id).attr('stick','true');
	}else{
		$('#'+note_id).attr('stick','false');
	}
}

focus_hover_note = function(note_id){
	$('.hover_notes').css('z-index',1000);
	$('#'+note_id).css('z-index',1001);
}

get_last_update_string = function(last_update_stamp,offset){
	var psp_last_update = '';
	var last_update_stamp = parseInt(last_update_stamp);
	if(isNaN(last_update_stamp)==false){
		var d = get_udate_obj(last_update_stamp * 1000,offset);
		var hours = d.getHours(); hours = hours<10 ? hours = '0'+hours : hours;
		var minutes = d.getMinutes(); minutes = minutes<10 ? minutes = '0'+minutes : minutes;
		var date = d.getDate(); date = date<10 ? date = '0'+date : date;
		var month = d.getMonth()+1; month = month<10 ? month = '0'+month : month;
		psp_last_update = d.getFullYear() + '-' +month + '-' + date + ' ' + hours+':'+minutes;
		psp_last_update = wm_psp_strings['MONTHS'][d.getMonth()] + " " + d.getDate() + ", " +  d.getFullYear()+ ' ' + hours+':'+minutes;
		psp_last_update += ' UTC'+(offset>0 ? '+':'')+offset+'';
	}
	return psp_last_update;
};

load_chart = function(chart_img,container){
	$('#'+container).html('<img alt="" class="floatleft" src="'+chart_img.src+'"/>');
};

get_udate_obj = function(millis, offset){
	millis = millis + (brz_offset * 60000);
	nd = new Date(millis + (3600000*offset));
	return nd;
}
$('#page_loader').show();
if(typeof($)=='function'){$(document).ready(function(){initiate();});}else{initiate();}
