var imgbase = "http://www.nyse.com/images/fa_";
var otherbase = "http://www.nyse.com/audience/";
var myserver="http://www.nyse.com"; 

var last_pinned=0;
var pinned = new Array();
var idx=0;
var norefresh;
var first=true;
var infoupdate=true;
var stoptimer = false;
//var OneLock01="Only one notification can have the details locked in view at a time.\nMultiple notifications can be monitored in the Status Reports page.\nTo go to the Status Reports page, click OK. To remain in this page, click Cancel.\n";
var OneLock01="Only one notification can have the details locked in view at a time.          \nMultiple notifications can be monitored in the Status Reports page.\nTo go to the Status Reports page, click OK.\nTo remain in this page, click Cancel.\n  ";

var addClassFlashIcon=''; //Introduced by Luiz - Store the class to be added in order to flash icon again

function doajax() {
	//r = Math.random();
	//$('#floorstatus').loadIfModified('/floorstatus.htm','',function(){doajax_2();});	
	$.ajax({
	  url: '/floorstatus.htm',
	  ifModified: true,
	  success: function(html){
	    $("#floorstatus").html(html);
	    doajax_2();
	  }
	});
}

function starttimer() {
	doajax();
	try {
			reload_alert_table();
	} catch(er){}
	if (!stoptimer) setTimeout( 'starttimer()', 10000 );
}

function doajax_2() {
	/*Introduced by Luiz - ISS44158It - stores the current id for pinned messages */
	var id='';   
	if (first) {
		for (i=0;i<10;i++) {
			o = document.getElementById('pushpin_'+i);
			if (o) {
				o.src=imgbase+"icon_pin_open.gif";
			}
		}
		//first=false;
	}
	infoupdate=false;
	for (i=0;i<pinned.length;i++) {
		o = document.getElementById('pushpin_'+i);
		if (pinned[i]) {
			o.src=imgbase+"icon_pin_close.gif";
			show_side(i);
			id=i;			
		}
	}
	infoupdate=true;
	
	/*Introduced by Luiz - ISS44158*/
	if (getFlashIcon()!=null){
		id = getFlashIcon();
	}		
	startFlashIcon(id); // Start flashing icon again, if needed
}


function go(l) {
	//$('#floorstatus').empty();
	$('#floorstatus').empty().append(eval('level_'+l));
}

function getTop(id) {
	o = document.getElementById(id)
	oTop = o.offsetTop ;           // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop;
}

function getLeft(id) {
	o = document.getElementById(id)
	oLeft = o.offsetLeft ;           // Get Left position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent Left position
		o = oParent
	}
	return oLeft;
}

function hide_side(id) {
	if (!pinned[id]) $('#box_'+id).hide();	
}

function pin(id) {
	var pin_option = false; /*Introduced by Luiz ISS44158*/
	if (last_pinned>0) {
		if (pinned[id]!=true) {
		  OneLock01=OneLock01.replace(/ /g,String.fromCharCode(160));
		  
			if (confirm (OneLock01)) {
				location.href= myserver+"/marketinfo/indexes/Todays_Alerts.shtml";
		  }
			//pin(last_pinned); // to unpin the last one and pin the new one
			return; // to do nothing
		}
	}
	//alert ('Last Pinned '+last_pinned+"\nPinned[id] "+pinned[id]);
	o = document.getElementById('pushpin_'+id)
	if (pinned[id]) {
		o.src=imgbase+"icon_pin_open.gif";
		pin_option=true;
	}	else {
		o.src=imgbase+"icon_pin_close.gif";
	}
	pinned[id] = !pinned[id];		
	if (pinned[id]) {
		last_pinned=id
	} else {
		last_pinned=0;
	}

	/*Introduced by Luiz ISS44158 - Stops the flashing icon*/
	if (!pin_option){
		stopFloorStatusAnimation(id);		
	}
	hide_side(id);
	return false;
}

/*Introduced by Luiz ISS44158*/
function stopFloorStatusAnimation(id){	
	//Latest message is Green (it considers that the whole box is also green)
	if ($('div').is('#gbox')){ 
		if ($('#green_'+ id).hasClass("accept_gray_flash")){
			$('#green_'+id).removeClass('accept_gray_flash');
			$('#green_'+id).addClass('accept_gray');
			addClassFlashIcon="('#green_'+id).addClass('accept_gray_flash')";
			saveFlashIcon(id);				
		} else if ($('#green_'+ id).hasClass("falert_gray_flash")){
			$('#green_'+id).removeClass('falert_gray_flash');
			$('#green_'+id).addClass('falert_gray');
			addClassFlashIcon="('#green_'+id).addClass('falert_gray_flash')";
			saveFlashIcon(id);		
		} else if ($('#green_'+ id).hasClass("critical_gray_flash")){
			$('#green_'+id).removeClass('critical_gray_flash');
			$('#green_'+id).addClass('critical_gray');
			addClassFlashIcon="('#green_'+id).addClass('critical_gray_flash')";
			saveFlashIcon(id);
		} else if ($('#green_'+ id).hasClass("info_gray_flash")){ //Check if this situation may take place
			$('#green_'+id).removeClass('info_gray_flash');
			$('#green_'+id).addClass('info_gray');
			addClassFlashIcon="('#green_'+id).addClass('info_gray_flash')";
			saveFlashIcon(id);		
		} else if ($('#info_resolved_'+ id).hasClass("info_gray_flash")){
			$('#info_resolved_'+id).removeClass('info_gray_flash');
			$('#info_resolved_'+id).addClass('info_gray');
			addClassFlashIcon="('#info_resolved_'+id).addClass('info_gray_flash')";
			saveFlashIcon(id);	
		} else if ($('#info_'+ id).hasClass("info_flash")){ // introduced by Luiz Round 1 ISS44158
			$('#info_'+id).removeClass('info_flash');
			addClassFlashIcon="('#info_'+id).addClass('info_flash')";
			saveFlashIcon(id);				
		}
	} else if ($('div').is('#ybox')){ //Latest message is Yellow (it considers that the whole box is also Yellow) 
		if ($('#yellow_'+ id).hasClass("falert_flash")){
			$('#yellow_'+id).removeClass('falert_flash');
			addClassFlashIcon="('#yellow_'+id).addClass('falert_flash')";
			saveFlashIcon(id);							
		}
	} else if ($('div').is('#rbox')){ //Latest message is Red (it considers that the whole box is also Red)
		if ($('#red_'+ id).hasClass("critical_flash")){
			$('#red_'+id).removeClass('critical_flash');
			addClassFlashIcon="('#red_'+id).addClass('critical_flash')";
			saveFlashIcon(id);									
		}
	} else if ($('div').is('#ibox')){ //Latest message is Blue (it considers that the whole box is also Blue)
		if ($('#info_resolved_'+ id).hasClass("info_gray_flash")){
			$('#info_resolved_'+id).removeClass('info_gray_flash');
			$('#info_resolved_'+id).addClass('info_gray');
			addClassFlashIcon="('#info_resolved_'+id).addClass('info_gray_flash')";
			saveFlashIcon(id);			
		} else if ($('#info_'+ id).hasClass("info_flash")){
			$('#info_'+id).removeClass('info_flash');
			addClassFlashIcon="('#info_'+id).addClass('info_flash')";
			saveFlashIcon(id);				
		}
	}
	
	return false;
}

/*
Start flashing icon again, if any new message has arrived inside the same 
thread which was already pinned. 
*/
function startFlashIcon(id){
	var curr_amt_msg = $("#box_" +id+ " .msgcell span").length; // Introduced by Luiz - ISS45854
	var stor_amt_msg = $.cookie('msg_right_box'); 
	if (stor_amt_msg!=null && (curr_amt_msg>stor_amt_msg)){ //new message inside the thread.	
		$+addClassFlashIcon;			
	} else {
		stopFloorStatusAnimation(id);
	}
}

/*Introduced by Luiz ISS44158 - It sets the cookie with the flashing icon id*/
function saveFlashIcon(id){	
	if (getFlashIcon() != null){
		flash_id = getFlashIcon();
		if (flash_id != id){
			$.cookie('flash_icon_id', null); // remove cookie (flash icon id)
			$.cookie('msg_right_box', null); // remove cookie (amount of messages on the right box)
		}
		$.cookie('flash_icon_id', id, { expires: null}); //replace cookie
		$.cookie('msg_right_box', $("#box_" +id+ " .msgcell span").length, { expires: null}); //replace cookie		 		
	} else {
		$.cookie('flash_icon_id', id, { expires: null }); //add cookie
		$.cookie('msg_right_box', $("#box_" +id+ " .msgcell span").length, { expires: null}); //add cookie
	}
	return false;
}

/*Introduced by Luiz ISS44158 - It retrieves the cookie which stores the flashing icon id*/
function getFlashIcon(){
	return $.cookie('flash_icon_id');
}

function show_side(id) {
	b = document.getElementById('box_'+id)
	if (b.style.display != 'block') {
		y = getTop('bubble_'+id);
		$('#box_'+id).css({top: y+"px"});
		//$('#box_'+id).css({left: "775px"});
		$('#box_'+id).css({left: "777px"});
		$('#box_'+id).show();	
		b.style.zIndex=++idx;
	}
	//if (infoupdate) $('#info_'+id).css({background:"url('/images/fa_icon_info.gif') no-repeat 2px 2px"});
}

 
 $(document).ready(function() {
   // do stuff when DOM is ready
	$('#floorstatus').load('/floorstatus.htm','',function(){doajax_2();});	
	starttimer();
 });

