﻿///	<reference path="jquery-1.5-vsdoc.js" >
//$("#ElemenId").css("display", "none");
var winBody = null;
var imgDiv = null;
var hText;
var vText;
var visibleReportDiv = null;

$(window).ready(function()
{
	winBody = $("body");

	//$("h1.ReportHeader").css("cursor", "pointer").click(toggleReport);
	$(".vs").parent("td, th").css("cursor", "pointer").hover(function() { valueCellHover(this, true) }, function() { valueCellHover(this, false) }).click(showHistoryChart);
	$(".hs").parent("td, th").css("cursor", "pointer").hover(function() { headerCellHover(this, true) }, function() { headerCellHover(this, false) }).click(showPartChart);
	$(".hhs").parent("td, th").css("cursor", "pointer").hover(function() { headerCellHover(this, true) }, function() { headerCellHover(this, false) }).click(showHistoryChart);
});

$(window).resize(function()
{
	if (imgDiv != null)
	{
		imgDiv.center();
	}
});

function toggleReport()
{
	var reportDiv = $(this).next('div.ReportData');
	
	if (visibleReportDiv != null)
	{
		visibleReportDiv.hide();
	}
	reportDiv.show();
	visibleReportDiv = reportDiv;
}

function headerCellHover(element, on)
{
	var cell = $(element);
	if (on)
	{
		hText = cell.text();
		cell.addClass('hovering');
	}
	else
	{
		hText = '';
		cell.removeClass('hovering');
	}
}

function valueCellHover(element, on)
{
	var cell = $(element);
	var row = cell.parent();
	var table = row.parent();

	var sibling = cell.prev();
	var index = 0;
	while (sibling.length > 0)
	{
		index++;
		sibling = sibling.prev();
	}

	var firstCellRowSpan = table.children("tr:nth-child(1)").children("th:nth-child(1)")[0].rowSpan;
	var hTH = row.children("th:first-child");
	var vTH = table.children("tr:nth-child(" + firstCellRowSpan + ")").children("th:nth-child(" + (firstCellRowSpan == 1 ? index + 1 : index) + ")");

	if (on)
	{
		hTH.addClass('hovering');
		vTH.addClass('hovering');
		hText = hTH.text();
		vText = vTH.text();

		cell.addClass('hovering');
	}
	else
	{
		hTH.removeClass('hovering');
		vTH.removeClass('hovering');
		hText = '';
		vText = '';
		cell.removeClass('hovering');
	}
}

function showPartChart()
{
	var link = $(this).children('a:first-child')[0];
	showChart((document.URL.indexOf('&language=ENG') == -1 ? 'Jakauma: ' : 'Distribution: ') + hText, link.href);
	return false;
}

function showHistoryChart()
{
	var link = $(this).children('a:first-child')[0];
	if (vText)
	{
		showChart(hText + ' / ' + vText, link.href);
	}
	else
	{
		showChart(hText, link.href);
	}
	return false;
}

function showChart(title, url)
{
	hideGraph();

	//Maby collect resolution from url...?
	
	imgDiv = $('<div style="width: 500px; height: 316px; border: 1px solid #000000; background-color: #ffffff;">' +
                '<div style="height: 20px; font-size: 16px;">' + title + '</div>' +
                '<a href="' + url + '&AsAttachment=True"><img src="' + url + '" alt="" /></a>' +
                '<div style="color: Red; font-weight: bold; position: absolute; top: 0; right: 0px; font-size: 12px; line-height: 12px; padding: 2px; cursor: pointer;" onclick="hideGraph();">X</div>' +
                '</div>');

	imgDiv.appendTo(winBody).center();
}

function hideGraph()
{
	if (imgDiv != null)
	{
		imgDiv.remove();
		imgDiv = null;
	}
}

var tipTimeout = new Object();
function showTip(id)
{
	if (tipTimeout[id])
	{
		clearTimeout(tipTimeout[id]);
	}
	tipTimeout[id] = setTimeout(function() { $('#' + id).show(); }, 250);
}
function hideTip(id)
{
	if (tipTimeout[id])
	{
		clearTimeout(tipTimeout[id]);
	}
	tipTimeout[id] = setTimeout(function() { $('#' + id).hide(); }, 50);
}
function LayerAction(elementId, visibility)
{
	var element = $('#' + elementId);
	element.style.visibility = visibility;
}



// Yleiset jqery laajennokset
$.fn.positionInWindow = function(top, left)
{
	return this.each(function()
	{
		var element = $(this);
		element.css("position", "absolute");
		element.css("top", top + "px");
		element.css("left", left + "px");
	});
}

$.fn.center = function()
{
	var w = $(window);
	var element = $(this);
	var viewPortTop = ((w.height() - element.height()) / 2) + w.scrollTop();
	var viewPortLeft = ((w.width() - element.width()) / 2) + w.scrollLeft();
	return this.positionInWindow(viewPortTop, viewPortLeft);
}
