﻿var testHeight = 0;
var widths = '';
$(document).ready(function() {

    //        for (var x = 0; x < 3; x++) {
    //            divs = $('.contentBoxcontent')[0].getElementsByTagName('td')[x].getElementsByTagName('Div');
    //            setDivAuto(divs);
    //        }


    var moduleCount = parseInt($('.contentbox .contentBoxcontent').length);
    for (var m = 0; m < moduleCount; m++) {
        // var module = $('.contentbox .contentBoxcontent .tableBoxcontent');
        var module = $('.contentbox .contentBoxcontent');

        for (var td = 0; td < $(module[m]).find('.tableBoxcontent').length; td++) {
            divs = $(module[m]).find('.tableBoxcontent')[td].getElementsByTagName('Div');  //module.getElementsByTagName('td')[td].getElementsByTagName('Div');
            if (divs.length > 0) {
                setDivAuto(divs);
                //if(!module[m].id.startsWith("FeaturedProduct"))
                $(module[m]).find('.tableBoxcontent')[td].style.height = testHeight+7 + "px";
            }
            testHeight = '';
            widths = "";
        }
    }

    var finalHtml = $('#htmlSection').html();
    try
    {
        finalHtml = finalHtml.replace(/CURSOR: move;/g, "");
    }
    catch(e)
    {
    }
    
    try
    {
        finalHtml = finalHtml.replace('Generic Content', 'GenericContent');
    }
    catch(e)
    {
    }

 try
    {
    $('#htmlSection').html("");
      }
    catch(e)
    {
    }
    
     try
    {
    $('#htmlSection').html(finalHtml);
      }
    catch(e)
    {
    }
    
     try
    {
    $('#htmlSection')[0].style.visibility = 'visible';
      }
    catch(e)
    {
    }
});

String.prototype.startsWith = function(str)
{ return (this.match("^" + str) == str) }


function sortDESC(a, b) { return (b - a); }

//Method to set auto for after the actual content is rendered.
function setDivAuto(divs) {
    var divIndex = 0;
    while (divIndex <= divs.length - 1) {
        changedHeight = 0;

        for (var i = divIndex + 1; i < divs.length; i++) {
            divs[i].style.top = (parseInt($(divs[i]).position().top) + changedHeight) + "px";
            changedHeight = checkDivPositions(divs[divIndex], divs[i]);
        }
        divIndex += 1;
    }

    divs[divs.length - 1].style.height = "auto";
    var lastHeight = $(divs[divs.length - 1]).height();
    $(divs[divs.length - 1]).height(lastHeight);
    widths += parseInt(divs[divs.length - 1].style.top) + lastHeight;
    var arr = widths.split('|');
    for (var z = 0; z < arr.length; z++) {
        arr[z] = parseInt(arr[z]);
    }

    testHeight = arr.sort(sortDESC)[0]; //$(divs[divs.length - 1]).height() + parseInt(divs[divs.length - 1].style.top);
}


///Method to Send appropriate DIV control for Expansion based on the content
function checkDivPositions(srcDV, destDV) {
    var isOver = false;
    var newHeight = 0;
    var srcLeft = parseInt(srcDV.style.left);
    var destleft = parseInt(destDV.style.left);

    var srcTop = parseInt(srcDV.style.top);
    var destTop = parseInt(destDV.style.top);

    var defaultHeight = $(srcDV).height();
    srcDV.style.height = "auto";
    newHeight = parseInt($(srcDV).height());

    srcTop = parseInt(srcTop) + parseInt($(srcDV).height());
    srcLeft = parseInt(srcLeft) + parseInt($(srcDV).width());

    if (defaultHeight > newHeight) {
        widths += parseInt(srcDV.style.top) + defaultHeight + '|';
    }
    else {
        widths += parseInt(srcDV.style.top) + newHeight + '|'
    }


    if (srcLeft >= destleft && srcTop >= destTop) {
        isOver = true;
    }
    else {
        isOver = false;
    }

    if (isOver == true) {
        if (parseInt(srcDV.style.top) < (parseInt(destDV.style.top) + parseInt($(srcDV).height()))) {
            srcDV.style.height = defaultHeight;
            return getDivXY(srcDV, destDV);
        }
        else {
            srcDV.style.height = defaultHeight;
            return getDivXY(destDV, srcDV);
        }
    }
    else
        return 0;
}


///Method to Set the height of the controls and overcoming the Content Overlap
function getDivXY(srcDV, destDV) {
    var actualHeight = 0;
    var newHeight = 0;
    var diffHeight = 0;

    var srcTop = srcDV.style.top;
    var destTop = destDV.style.top;

    actualHeight = parseInt($(srcDV).height());
    srcDV.style.height = "auto";
    newHeight = parseInt($(srcDV).height());


    if (actualHeight < newHeight && $(destDV).position().left + $(destDV).width() > $(srcDV).position().left) {
        if (parseInt(destDV.style.top) < (parseInt(srcTop) + newHeight))
            diffHeight = newHeight - actualHeight;
        destDV.style.top = parseInt(destDV.style.top) + diffHeight;
    }
    else if (actualHeight == newHeight && $(destDV).position().left + $(destDV).width() > $(srcDV).position().left) {
        diffHeight = (parseInt(srcTop) + newHeight) - parseInt(destDV.style.top);
        destDV.style.top = parseInt(destDV.style.top) + diffHeight;
    }
    return diffHeight;
}



