﻿/*
This will be used for the chart cache solution and is included from
ArticleStockChartsColumn.ascx.cs.
It is decided to finish it in coming sprint though, but instead of removing what is already made, 
I keep it as is now as it will not break anything else.
Per Lundkvist
*/

function calcChartHeight(divText) {
	var textHeight = 0;

	var headHeight = $(divText).children("p").get(0);
	if ($(headHeight) != null && $(headHeight).height()) 
		textHeight = textHeight + $(headHeight).outerHeight(true);
	var objectsHeight = $(divText).children("table").get(0);
	if ($(objectsHeight) != null && $(objectsHeight).height()>0) 
		textHeight = textHeight + $(objectsHeight).outerHeight(true);

if ($(divText).children("table.twoCol").length > 0)
    textHeight = textHeight - $($(divText).children("table.twoCol").get(0)).outerHeight(true);
	if (textHeight < 0) return 0;
	return textHeight; //*10/10;
}

/*
* ----------------------------------------------------------------------------------------------
* JavaScript to make sure chart in a teaser have the correct height depending on teaser ingress
* ----------------------------------------------------------------------------------------------
*/
function setEttanGraphHeight(oElement, sImageURL, iAttempt) {
	var chartHeaderHeight = 39;
    var iGraphHeight = 100;
    var iGraphMinHeight = 60;
    var iGraphMaxHeight = 160;
    var imageChartID = oElement.replace('div_', '');

    try {
    	var textHeight = 0;
    	var textInnerHeight = 0;
    	var textsomeHeight = 0;

    	var divText = $("#" + oElement).children("div.clear").get(0);
    	textInnerHeight = $(divText).innerHeight();

    	if (textInnerHeight > 0) {
    		textHeight = calcChartHeight(divText);

    		var aChart = $(divText).children(".right-img").get(0);
    		var saveHtml = $(aChart).html();
    		$(aChart).empty();
    		$(aChart).append("<div class='tempchart' style='width:165px; height:" + textHeight + "px'></div>");

   			var textHeight = calcChartHeight(divText);
    		$($(aChart).children('.tempchart').get(0)).height(textHeight);
    		textHeight = calcChartHeight(divText);
    		textHeight = textHeight - chartHeaderHeight + 7; //7 is sum of margings of the link to chart 
    		$(aChart).html(saveHtml);

            // Find height of ingress
            try {
            	iGraphHeight = textHeight;
                if (iGraphHeight < iGraphMinHeight) {
                    iGraphHeight = iGraphMinHeight;
                }
                if (iGraphHeight > iGraphMaxHeight) {
                    iGraphHeight = iGraphMaxHeight;
                }
               } catch (ex1) {
                iGraphHeight = 100;
            }

            // Set image-src of graph with calculated height
            document.getElementById(imageChartID).src = sImageURL.replace('#height#', iGraphHeight);

            // Check if the image seem to be the graph or still having the "Hämtar graf..."-image (checking height)
            setTimeout("checkGraphHeightReload('" + imageChartID + "', 0)", 1500);

        } else {
            setTimeout("setEttanGraphHeight('" + oElement + "', '" + sImageURL + "', " + iAttempt + ")", 1500);  // Try again in 1 sec because the ingress may not have been fully loaded
        }

    } catch (ex3) {    //The graphdiv is not yet loaded
        if (iAttempt < 10) {   // Only try X times to not continuing forever trying
            iAttempt++;
            setTimeout("setEttanGraphHeight('" + oElement + "', '" + sImageURL + "', " + iAttempt + ")", 1500);  // Try again in 1 sec because the ingress may not have been fully loaded
        }
    }
}



/*
* Check if graphimage seem to have a valid height, otherwise try to reload image
* If height is not valid it could be because "Hämtar graf..."-image is still showing.
* If it is the very first time graph is created, image needs a reload to actually show as it is created upon first request.
*/
function checkGraphHeightReload(oElement, iAttempt) {
    var imageChartID = oElement.replace('div_', '');
    var currDate = new Date();

    try {
        if (iAttempt > 0) {
            if (document.getElementById(oElement).offsetHeight < 40) {
                document.getElementById(imageChartID).src = document.getElementById(imageChartID).src + '?hr=' + currDate.getMilliseconds();
            }
        } else {
            iAttempt++;
            setTimeout("checkGraphHeightReload('" + oElement + "', " + iAttempt + ")", 1500);  //Try again because the DIV may not have been fully loaded yet
        }
    } catch (ex) {
        if (iAttempt < 10) {   // Only try X times to not continuing forever trying
            iAttempt++;
            setTimeout("checkGraphHeightReload('" + oElement + "', " + iAttempt + ")", 1500);  //Try again because the DIV may not have been fully loaded yet
        }
    }
}

