var SYSTEM_COLOR = "color: #FF7400; font-weight: bold;";
var SYSTEM_USERNAME = "";

var debug = false;

var numFlashes = 5;

//Built a series of subroutines located in /chat/js so I didn't have to keep sifting through so much code to get to a particular section

//Preload background image for taskbar notification
$('<img />')
    .attr('src', '/chat/images/chat_notify_bg.png')
    .css("display", "none")
    .load(function(){
        $('#mc_chat_form').append( $(this) );
    });

function formatCurrency(num) {
	if(num === undefined) num = 0;		//Invalid number so return
	
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+

	num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

//Reset chat area
function resetChat(){
	$("#mc_chat_content").html("");
	$("#mc_bid_area").hide();
	$("#mc_chat_room_users").html("");
	handleChat();
}



$(document).ready(function(){
//Check for existence of taskbar before trying to modify any of its elements
	if(!$("#mc_chat_container").is(":visible")){
		return;
	}

//Center chat taskbar - could probably be done in css but it was giving me fits so I did it here
	var tb_width = $("#mc_chat_container").width();
	var tb_l = ($(window).width() - tb_width) / 2;
	$("#mc_chat_container").css("left", tb_l);
	
	var ompLeft = (($("#mc_chat_container").width() - $("#offlineMsgPopup").width()) / 2) + tb_l;

//Line up chat popup boxes with taskbar & set transparency
	//doing through jQuery circumvents cross browser problems & saves typing
	//main chat popup
	var opLevel = .94;		//Opacity level of chat popup areas
	
	$("#mc_main_chat").css({ "left": tb_l, "width": tb_width, "opacity": opLevel });
	$("#mc_products").css({ "left": tb_l, "opacity": opLevel  });
	$("#mc_contacts").css({ "left": $("#open_mc_contacts").offset().left, "opacity": opLevel  });
	$("#mc_chat_tabs").css({ "opacity": opLevel, "width": tb_width } );
	$("#offlineMsgPopup").css({ "left": ompLeft, "opacity": opLevel });
	
	$("#mc_chat_container").css("opacity", 0.9);		//Opacity level of taskbar

	
//Set hover highlight for selling products
	$(".mc_selling_product_link").live("hover",
		function(e){
			if(e.type == "mouseenter"){
				$(this).addClass("sellingHover");			
			} else {
				$(this).removeClass("sellingHover");
			}
		}
	);

//Set clicks for chat taskbar
	defineTaskbarClick();

//Define click function for tabs
	defineTabClick();
	
//Define click function for contacts
	defineContactClick();
	
//Define click function for products
	defineProductClick();
	
//Define submit chat form
	defineSubmitChat();
	
//Define change start price
	defineChangeStartPrice();
	
//Define bidding form
	defineBidForm();
	
//Setup offline message form
	handleOfflineMessage();
	
//Page init
	handleSidebars();
	handleChat({"pageInit": true});

//Set interval for slow refresh
	setInterval(function(){
		handleSidebars();
	}, 5000);

//Set interval for fast refresh
	setInterval(function(){
		handleChat();
		chatNotify();
	}, 1000);
	
	
	


});
