var arRates = new Array();
var arCurrency = new Array();
var count = 0;
var req,timer;
var cur_from = 'USD', cur_to = 'INR';
$(document).ready(function(){
	rows = $('#tblExchangeRate tr td:first-child');
	cells = $('#tblExchangeRate tr td');
	count = rows.length - 1;
	for(i = 1; i <= count; i++)arCurrency[i-1] = [rows.eq(i).text(), rows.eq(i).attr('title')];
	for(i = 1; i <= count; i++){
		arRates[i - 1] = new Array();
		for(j = 0; j < count; j++)arRates[i-1][j] = cells.eq(i * 15 + j + 1).text();
	}
	simple_tooltip();
	cur_from = $('#cur_from').val();
	cur_to = $('#cur_to').val();
	$('.currencies a').click(function(){
		$this = $(this);
		$this.addClass('selected').siblings().removeClass('selected');
		ar = $this.attr('id').split('_');
		eval('cur_'+ar[0] + ' = "'+ar[1]+'";$("#cur_'+ar[0]+'").val("'+ar[1]+'");');
		$.cookie('cur_' + ar[0], ar[1]);
		getConversionRate();
		return false;
	}).each(function(){$(this).attr('title', 'Convert ' + $(this).attr('id').replace('_', ' '))});
	$('#frmConvert').submit(function(){getConversionRate();return false;});
	//$('#from_'+cur_from+',#to_'+cur_to).click();
	$('#invert').click(function(){$('#from_'+cur_to+',#to_'+cur_from).click();return false;});
});
function getConversionRate(){
	if(req)req = null;
	clearTimeout(timer);
	timer=setTimeout("req=null;$('#frmConvert').unbind().submit()", 8000);
	$('#curResult').css('background-image', 'url(/news/images/currency-loading.gif)');
	$.getJSON('currency.php?mode=json&from='+cur_from+'&to='+cur_to+'&amount='+$('#amount').val(), null, showResult);
}
function showResult(json){
	$('#curResult').css('background-image', 'none');
	clearTimeout(timer);
	if(json.error)alert(json.error)
	else $('#curResult').html(json.amount_from+' '+json.cur_from+' = '+json.amount_to+' '+json.cur_to);
}
function simple_tooltip(){
	var tooltip = $('#infoTip');
	$('#tblExchangeRate tr td').each(function(i){
		if($(this).hasClass('title'))return;
		$(this).mouseover(function(){
			$this = $(this);
			$elRow = $this.parent();
			row = $elRow.parent().children().index($elRow) - 1;
			column = $elRow.children().index($this) - 1;
			$this.addClass('current').siblings().addClass('hilite');
			$this.parent().parent().find('td').filter(':nth-child(' + (column + 2) + ')').addClass('hilite') 
			if(row == column)return;
			content = '<div class="title">Conversion Rate</div><p>1 ' + arCurrency[row][0] + ' = ' + arRates[row][column] + ' ' +arCurrency[column][0]+'<br/>';
			content += '1 ' + arCurrency[column][0] + ' = ' + arRates[column][row] + ' ' +arCurrency[row][0]+'</p>';
			content += '<div class="legend">* '+ arCurrency[row][0]+' = '+arCurrency[row][1]+'<br/>* '+arCurrency[column][0]+' = '+arCurrency[column][1]+'</div>';
			tooltip.html(content);
			tooltip.stop().css({opacity:1, display:"none"}).fadeIn(250);
		}).mousemove(function(kmouse){
			tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
		}).mouseout(function(){
			tooltip.stop().fadeOut('fast');
			$this.parent().parent().find('td').removeClass("hilite current");
		});

		//}
	});
}
