var popupClicked = 0;

// Shows the popup window
function showExplanation(element)
{
	var parent = element.parentNode;
	var popupElement = null;
	for (index in parent.childNodes)
	{
		var child = parent.childNodes[index];
		if (child.className=="popup_text") popupElement = child;
	}
	
	if (!document.getElementById("currentPopup"))
	{
		popupElement.style.display = 'block';
		popupElement.id = "currentPopup";
		popupClicked = 1;
	}
}

// Closes the current popup
document.onclick = function (e) 
{
	if (popupClicked==0)
	{
		if (document.getElementById("currentPopup")) 
		{
			document.getElementById("currentPopup").style.display = 'none';
			document.getElementById("currentPopup").id = 'popup_text';
		}
	}
	else
	{
		popupClicked = 0;
	}
}

function setTableTs(ts)
{
	document.forms['siirrot']['tableTs'].value = ts;
	loadWeekOptions(ts);
	selectTableTab(ts);
	selectWeek();
	hideUnwantedTables();
}

// Loads and sets the week, month and year options for given ts
function loadWeekOptions(ts)
{
	ajaxtools.getWeeksMonthsAndYears(ts, function (results) {
		loadWeekOptionsCallBack(results);
		}
		);
}

function loadWeekOptionsCallBack(results)
{
	var weekSelect = document.forms["siirrot"]["week"];
	var selected = weekSelect.selectedIndex;
	var selectedValue = weekSelect[selected].value; 
	
	formatSelect("siirrot", "week");

	var weeks = results["weeks"];
	var currentWeek = results["currentWeek"];
	var thisWeekYear = results["thisWeekYear"];

	if (weeks)
	{
		var selectedFound = false;
		for (n = 0; n < weeks.size(); n++)
		{
			var week = weeks[n][0];
			var year = weeks[n][1];
			var weekYear = week + "/" + year;
			if (!selectedFound && (selectedValue.toString() == weekYear))
			{
				weekSelect.options[weekSelect.length] = new Option(weeks[n][0] + "/" + weeks[n][1],weeks[n][0] + "/" + weeks[n][1], true);
				selectedFound = true;
				weekSelect.options[weekSelect.length - 1].selected = true;
			}
			else if (!selectedFound && week == currentWeek && year == thisWeekYear)
			{
				weekSelect.options[weekSelect.length] = new Option(weeks[n][0] + "/" + weeks[n][1],weeks[n][0] + "/" + weeks[n][1], true);
				weekSelect.options[weekSelect.length - 1].selected = true;
			}
			else
			{
				weekSelect.options[weekSelect.length] = new Option(weeks[n][0] + "/" + weeks[n][1],weeks[n][0] + "/" + weeks[n][1], false);				
			}
		}
	}
	else
	{
		weekSelect.options[weekSelect.length] = new Option(currentWeek + "/" + thisWeekYear, currentWeek + "/" + thisWeekYear, true);
		weekSelect.options[weekSelect.length - 1].selected = true;
	}
	
	// if the previously selected value was not found the week is changed
	if (selectedFound == false)
	{
		selectWeek();
	}
}

function formatSelect(formname,elementname)
{
	var select = document.forms[formname][elementname];
	select.length = 0;
}

function selectTableTab(ts)
{
	if (!document.getElementById("rk_table_tabs"))
	{
		return false;
	}
	var parent = document.getElementById("rk_table_tabs");
	
	for ( var index in parent.childNodes )
	{
		var child = parent.childNodes[index];
		child.className = ''; 
	}
	
	var selectedTab = document.getElementById('tab_' + ts);
	selectedTab.className = 'active';
}

function getTableTs() 
{
	if (document.forms['siirrot'])
	{
		return document.forms['siirrot']['tableTs'].value;
	}
}

function getSelectedDate() 
{
    var c = document.forms['siirrot']['week'].selectedIndex;
	var dates = document.forms['siirrot']['week'].options[c].value;
	return dates.split("/");
}

// User selects date from dropdown menu
function selectWeek() 
{
    loadingAnimation('ajax-loading',true);
    setTimeout("getRowData()",0);
}

// User selects next or previous date
function selIndex(n)
{
    var c = document.forms['siirrot']['week'].selectedIndex - n;
    if (c < 0) c = 0;
    if (c > document.forms['siirrot']['week'].options.length-1) c = document.forms['siirrot']['week'].options.length-1;
    document.forms['siirrot']['week'].selectedIndex = c;
	
    loadingAnimation('ajax-loading',true);
    setTimeout("getRowData()",0);
}

// Fetches the data for selected date using DWR
function getRowData() 
{
	var weekYear = getSelectedDate();
	render('TransferGraphData', naviPath+"?fragment=taulukko&week="+weekYear[0]+"&year="+weekYear[1]+"&ts="+getTableTs());
}

/* ----------------------------------------------------------------------------------------------------- */

function hideUnwantedTables()
{
	if (document.getElementById('ruotsitaulukko'))
		document.getElementById('ruotsitaulukko').style.display = 'none';
	if (document.getElementById('norjataulukko'))
		document.getElementById('norjataulukko').style.display = 'none';
	if (document.getElementById('venajataulukko'))
		document.getElementById('venajataulukko').style.display = 'none';
	if (document.getElementById('virotaulukko'))
		document.getElementById('virotaulukko').style.display = 'none';

	//if (getTableTs()=='ruotsi') document.getElementById('ruotsitaulukko').style.display = '';
	//if (getTableTs()=='norja') document.getElementById('norjataulukko').style.display = '';
	//if (getTableTs()=='venaja') document.getElementById('venajataulukko').style.display = '';
	//if (getTableTs()=='viro') document.getElementById('virotaulukko').style.display = '';
}
