if (typeof jQuery == 'undefined')
	alert("MISSING JQUERY");

$j = jQuery.noConflict();

function QuotesClient(opts) {
	
	if (!(this instanceof QuotesClient)){
		return new QuotesClient(opts);
	}
	
	this.opts = {
		domain:		'http://tradingservices.dealserv.net/QuotesProvider',
		path:			'/quotesprovider.asmx/',
		method:		'GetAllJsonQuotes',
		data:			{},
		callback: processQuotes, // This is a function object!
		dataType:	'jsonp',
		interval: 3000 // Default 3 seconds
	}
	$j.extend(this.opts,opts);
		
	this.send = function () {
		return $j.get(
			this.opts.domain+this.opts.path+this.opts.method,
			this.opts.data,
			this.opts.callback,
			this.opts.dataType
		);
	};
	
	this.request = function () {
		if(this.opts.interval) {
			var that = this;
			this.timerid = setInterval(function () { that.send() },this.opts.interval);
		} 
		this.send();
	}
	
	this.getAllJSON = function () {
		this.opts.method = 'GetAllJsonQuotes';
		this.request();
	};
	
	this.getPairsJSON = function (pairs) {
		if (!pairs)
			throw "MISSING_PAIRS";
		
		var delimiter = ',', pairstr = '';
		for (var i=0; i<pairs.length; i++) { pairstr += pairs[i] + delimiter; }
		
		this.opts.data.Instruments = pairstr;
		this.opts.method =  'GetJsonQuotes';
		this.request();
	};
	
	return this;
}

function _qfAsHTMLTableShort(quotes) {
	var tbl = '\
		<table class="_qf" cellspacing="0" cellpadding="0">\
			<thead>\
				<tr class="_qfHeading">\
					<th>Symbol</th>\
					<th><span class="up">&#9650;</span><span class="down">&#9660;</span></th>\
					<th>Bid</th>\
					<th>Ask</th>\
				</tr>\
			</thead>\
			<tbody>\
				';
				for (var i=0; i<quotes.length; i++) {
					tbl += '\
						<tr id="'+quotes[i].Instrument+'" class="_qfPair">\
							<td>'+quotes[i].Instrument+'</td>\
							<td class="direction '+((quotes[i].Direction == 'up') ? 'up' : 'down')+'">'+((quotes[i].Direction == 'up') ? '&#9650;' : '&#9660;')+'</td>\
							<td>'+quotes[i].Bid+'</td>\
							<td>'+quotes[i].Ask+'</td>\
						</tr>\
					';
				}
				tbl += '\
			</tbody>\
		</table>\
	';
	return tbl;
}

function _qfAsHTMLTableFull(quotes,precision) {
	if (!precision)
		precision = 2;
	var tbl = '\
	<table class="_qf" cellspacing="0" cellpadding="0">\
			<thead>\
				<tr class="_qfHeading">\
					<th>Symbol</th>\
					<th><span class="up">&#9650;</span><span class="down">&#9660;</span></th>\
					<th>Bid</th>\
					<th>Ask</th>\
					<th>Low</th>\
					<th>High</th>\
					<th>Change</th>\
				</tr>\
			</thead>\
		<tbody>';
		for (var i=0; i<quotes.length; i++) {
			tbl += '\
				<tr id="'+quotes[i].Instrument+'" class="_qfPair">\
					<td>'+quotes[i].Instrument+'</td>\
					<td class="direction '+((quotes[i].Direction == 'up') ? 'up' : 'down')+'">'+((quotes[i].Direction == 'up') ? '&#9650;' : '&#9660;')+'</td>\
					<td>'+quotes[i].Bid+'</td>\
					<td>'+quotes[i].Ask+'</td>\
					<td>'+quotes[i].Low+'</td>\
					<td>'+quotes[i].High+'</td>\
					<td>'+(quotes[i].Change).toFixed(precision)+'%</td>\
				</tr>\
			';
		}
		tbl += '\
		</tbody>\n\
	</table>';
	return tbl;
}
