var dataHolder = new Array();
var currentNominals = new Array();
var span = 0;

function navAway (value, root) {
	location.href = '/' + root + '/rates/roaming/' + value;
}

function changeArrow(elem, link){

	if (elem.hasClass("open")) {
		elem.fadeOut(150, function () {
			elem.removeClass('open').addClass('closed').fadeIn(150);
			$(link).text( link.attr( 'opentext' ) );
		});
	} else {
		elem.fadeOut(150, function () {
			elem.removeClass('closed').addClass('open').fadeIn(150);
			$(link).text( link.attr( 'hidetext' ) );
		});
	}
}
$(document).ready(
		function ()
		{
			renewBindings();
		}
);

if (!lang) {
	var lang = 'lv';
}

function renewBindings() {

	var langOffset = (lang == 'lv' ? 128 : 108);
	$('.renewType').unbind();
	$('.renewType').bind('change', function (e) {
		$.get(rateGetter,
				{rate_id: $(this).attr('rel'), renewal_id: $(this).val()},
				function (data, other) {
					var dataArray = data.split('**&&**');
					if (dataArray == undefined || dataArray.length != 2) {
						return;
					}
					var footer = dataArray[0];
					data = dataArray[1];
					rateId = data.substr(0, 1);
					data = data.substr(1);
					dataHolder['rate-' + rateId] = new Array();
					currentNominals['rate-' + rateId] = new Array();
					if ($('#rate-' + rateId + ' .ratesFootnote .ratesIndicator').hasClass("open")) {
						changeArrow($('#rate-' + rateId + ' .ratesIndicator'), $('#rate-' + rateId + ' .ratesFootnote a'));
						$('#footer-' + rateId).slideToggle(300);
						$('#rate-' + rateId + ' .secondaryContainer').slideToggle(300, function (){
							$('#rate-' + rateId + ' .ratesTablesContainer').fadeOut(200, function () {
								$('#footer-' + rateId).html(footer);
								$('#rate-' + rateId + ' .ratesTablesContainer').html(data).fadeIn(200, function(){renewBindings();});
								$('#rate-' + rateId + ' .secondaryContainer').css('height',
										$('#rate-' + rateId + ' .secondaryContainer tr.ratesHidden').length * 26 + 'px');
							});
						});
					} else {
						$('#rate-' + rateId + ' .ratesTablesContainer').fadeOut(200, function () {
							$('#footer-' + rateId).html(footer);
							$('#rate-' + rateId + ' .ratesTablesContainer').html(data).fadeIn(200, function(){renewBindings();});
							$('#rate-' + rateId + ' .secondaryContainer').css('height',
									$('#rate-' + rateId + ' .secondaryContainer tr.ratesHidden').length * 26 + 'px');
						});
					}

				}
		);
	});

	$('div.secondaryContainer').each(
		function() {
		    $(this).css('height', $(this).find('tr.ratesHidden').length * 30 + 'px');
		   }
	);

	$('.ratesFootnote a').unbind();

	$('.ratesFootnote a').bind("click", function (e){
		changeArrow($($(this).attr('rel') + ' .ratesIndicator'), $(this));
		$($(this).attr('rel') + ' div.secondaryContainer').slideToggle(300);
		$('#footer-' + $(this).attr('rateid')).slideToggle(300);

		return false;
	});

	$('div.tooltip').hover(
			function (e) {
				x = $(this).offset().left - $(this).parents('.ratesContent').offset().left;
				y = $(this).offset().top -  $(this).parents('.ratesContent').offset().top + 25;

				$(this).find('.tooltipInfo').css('left', x + 'px').css('top', y + 'px').fadeIn(200);


			},
			function (e) {
				$(this).find('.tooltipInfo').fadeOut(200);
			}
	);
	$('div.tooltip table').attr('border', '0');

	$('img.displayTicker').hover(
			function (e) {
				x = $(this).offset().left - $(this).parents('.ratesContent').offset().left;
				y = $(this).parents('.ratesTablesContainer').offset().top - langOffset;

				$('#ticker').css('left', x + 'px').css('top', y + 'px').fadeIn(0);


			},
			function (e) {
				$('#ticker').fadeOut(0);
			}
	);

	$('img.displayFactual').hover(
			function (e) {
				x = $(this).offset().left - $(this).parents('.ratesContent').offset().left;
				y = $(this).parents('.ratesTablesContainer').offset().top - langOffset;


				$('#factual').css('left', x + 'px').css('top', y + 'px').fadeIn(0);


			},
			function (e) {
				$('#factual').fadeOut(0);
			}
	);
	$('table.ratesTable thead th.amounts').unbind();
	$('table.ratesTable thead th.amounts').hover(
			function (e) {
				$(this).addClass('amountHovered');
			},
			function (e) {
				$(this).removeClass('amountHovered');
			}
		);
	$('table.ratesTable thead th.amounts').css('cursor', 'pointer').bind('click',
			function (event) {
				switchToSimple(this);
			}
	)

}

function switchToSimple(cell) {
	$('table.ratesTable thead th.amounts').removeClass('amountHovered')
	$('table.ratesTable thead th.amounts').unbind('hover');
	$('.tooltipInfo').fadeOut(200);
	// get references
	myParent = $(cell).parents('.ratesBox');
	myRateHolder = $(cell).parents('.ratesTable');
	cellId = $(cell).attr('id');
	// check if have to switch back to complex
	if (currentNominals[myParent.attr('id')] == $(cell).attr('rel')) {


		myParent.find('.primary').removeClass('simple').addClass('complex');
		myParent.find('.secondary').removeClass('simple').addClass('complex');

		myParent.find('.primary').html(dataHolder[myParent.attr('id')]['top']);
		myParent.find('.secondary').html(dataHolder[myParent.attr('id')]['bottom']);

		currentNominals[myParent.attr('id')] = 0;

		renewBindings();
		$('table.ratesTable thead th.amounts').removeClass('amountHovered')
		$('table.ratesTable thead th.amounts').unbind('hover');
		return;
	}

	if (currentNominals[myParent.attr('id')] > 0) {
		myParent.find('.primary').html(dataHolder[myParent.attr('id')]['top']);
		myParent.find('.secondary').html(dataHolder[myParent.attr('id')]['bottom']);
		cell = $("#" + cellId);
		currentNominals[myParent.attr('id')] = 0;
		renewBindings();
		$('table.ratesTable thead th.amounts').removeClass('amountHovered')
		$('table.ratesTable thead th.amounts').unbind('hover');
		switchToSimple(cell);
		return;
	}

	//store html and current nominal
	dataHolder[myParent.attr('id')] = new Array();
	dataHolder[myParent.attr('id')]['top'] = myParent.find('.primary').html();
	dataHolder[myParent.attr('id')]['bottom'] = myParent.find('.secondary').html();
	currentNominals[myParent.attr('id')] = $(cell).attr('rel');



	// change table class and the clicked cell class
	myRateHolder.removeClass('complex').addClass('simple').attr('cellspacing', '0');
	myParent.find('.secondary').removeClass('complex').addClass('simple').attr('cellspacing', '0');
	$(cell).addClass('simpleSelected');

	// modify table to show legend for simple view
	span = myRateHolder.find('tr.legend th').length;
	myRateHolder.find('tr.legend').html('<th>&nbsp;</th><th colspan="' + (span - 1) + '" class="simpleLegend">' + $('#simpleContentHolder th').html() + '</th>');

	// switch data
	switchData(myParent, $(cell).attr('rel'), false);


}

function switchData(rateHolder, num, forceReload) {

	primary = rateHolder.find('tr.ratesShown, tr.ratesHidden').each(function () {
		$(this).find('.ratesFormula').each(function (i) {
			if (num != (i + 1)) {
				$(this).remove();
			} else {
				$(this).attr('colSpan', span - 1).addClass('verbose');
			}
		});
	});
}

$(function () {
	$.nyroModalSettings({
		type: 'iframe',
		autoSizable: false,
    	minWidth: 300,
    	width: 350,
    	height: 350,
    	minHeight: 300,
    	titleFromIframe: false
	});
	$('a.popup-rates').nyroModal();
});

