﻿
// This ths the core method use to expand he page as per the content and inset size. This method get called only when "autoexpand"  check is selected in template settings
// val : its a concatinate string that contain respectve footer element name and the distance between bottom of content box and top of element. It also hold inset differences too.

var hdnCMSVal = '';
var hdnCMSActiveTabBottom = 0;
var hdnCMSPrevTestimonialBottom = 0;
var hdnCMSContentBoxBottom = 0;

function onInitialLoad() {
    var activeTab = $(".active").find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
    if (activeTab.charAt(0) == '#') {
        $(activeTab).fadeIn();
        var divCenterListHeight = $(activeTab).attr('offsetHeight');
        var tabHeight = $("#divTabs").height();
        var maxInsetTop = 0;
        var maxInsetId = 'InsetId';
        var position = $("#divTabs").position();
        //alert((position.top + divCenterListHeight));
        hdnCMSActiveTabBottom = (position.top + divCenterListHeight);
        if (hdnCMSActiveTabBottom > hdnCMSPrevTestimonialBottom) {
            //if (parseInt(tabHeight) < divCenterListHeight) 
            {
                var diff = parseInt(divCenterListHeight) - parseInt(tabHeight);
                UpdateFooterItemsTop(hdnCMSVal, diff); //sr2103
            }
        } //if
    } //if
}

function AutoExpand(val, maxInsetTop, maxInsetHeight, blogsStr) {

 hdnCMSActiveTabBottom = 0; //SR2103
    hdnCMSPrevTestimonialBottom = 0; //SR2103
    hdnCMSVal = val; //SR2103
    
        if (!CMS_SecondCalled) {
        CMS_Auto_Val = val;
        CMS_Max_Inset_Top = maxInsetTop;
        CMS_Max_Inset_Height = maxInsetHeight;
        CMS_Blog_Str = blogsStr;
        CMS_SecondCalled = true;
       // setTimeout("CallAutoExpandAgain();", 3000);
    }
    /*Following code is being use to newtralise  Custom scroller effect in autoexpand.*/
    try
    {
        $("#scrollbar1").css("height", "auto");
        $(".scrollbar").hide();
        $("#divCustomScrollViewport").removeClass();
        $("#divCustomScrollOverview").removeClass();
    }
    catch(customscroll){
    }
    var divPageContentsHeight = document.getElementById('divPageContents').offsetHeight;
    var contentboxTop = document.getElementById('contentbox').offsetTop;
    var contentboxHeight = document.getElementById('contentbox').offsetHeight;
    var contentBackgroundTop = document.getElementById('contentBackground').offsetTop;
    var topHeight = document.getElementById('topdiv').offsetHeight;
    var contentBoxGap = (contentBackgroundTop + topHeight) - contentboxTop;
    var BottomdivOffsetTop = document.getElementById('bottomdiv').offsetTop;
    var contentBoxInsetGap = contentboxTop - maxInsetHeight;
    
    BottomdivOffsetTop = contentBackgroundTop + topHeight + document.getElementById('middlediv').offsetHeight
    var contentboxBottomdivGap = BottomdivOffsetTop - (contentboxHeight + contentboxTop);
    
    try {
        if (document.getElementById('divPageContentsFooter').offsetHeight > 0) {
            divPageContentsHeight += document.getElementById('divPageContentsFooter').offsetHeight;
        }
    }
    catch (e) {
    }

    try {
        if (document.getElementById('pnlForm').offsetHeight > 0) {
            divPageContentsHeight += document.getElementById('pnlForm').offsetHeight;
        }
    }
    catch (e) {
    }
    /*--------------------------------------------------------------------------------------*/
    try {
        
        if (document.getElementById('ucEBSGroupBooking_hdnGPPage') != null) {
            var itmsArr = document.getElementById('ucEBSGroupBooking_hdnGPPage').value.split("-");
            var itemPadding = 0;
            for (l = 0; l < itmsArr.length - 1; l++) {
                itemPadding = itemPadding + (itmsArr[l] * 36) + 70;
            }
            contentboxHeight = contentboxHeight + itemPadding + 30;
        }
        } catch (ee) {
            }
    try
    {
    if (document.getElementById('ucEBSCreateTicket_hdnAddEBSTicketPage') != null) {
        if (divPageContentsHeight > contentboxHeight)
            divPageContentsHeight = divPageContentsHeight + 50;
        else
            contentboxHeight = contentboxHeight + 380;
        }
    }
    catch(ee2){
    }

    try {
        if (document.getElementById('ListClientProjects1_hdnProjectListCnt') != null) {
            var itemPadding = document.getElementById('ListClientProjects1_hdnProjectListCnt').value;
            itemPadding = parseInt(itemPadding) * 70;
            itemPadding = itemPadding + 100;
            contentboxHeight = contentboxHeight + itemPadding;
        }
    }
    catch (ee3) {
    }
    /*--------------------------------------------------------------------------------------*/
    divPageContentsHeight = divPageContentsHeight + 20;
    if (divPageContentsHeight <= contentboxHeight) {
        divPageContentsHeight = contentboxHeight;
    }
    var topDifference = 0;
    if (contentboxTop > (contentBackgroundTop + topHeight)) {
        topDifference =  contentboxTop - (contentBackgroundTop + topHeight);
    }
    else if (contentboxTop < (contentBackgroundTop + topHeight)) {
    //divPageContentsHeight = divPageContentsHeight - (contentBackgroundTop + topHeight - contentboxTop);
    topDifference = contentboxTop - (contentBackgroundTop + topHeight);
    }
    divPageContentsHeight = divPageContentsHeight + topDifference;
    
    var padding = 0;
    var flag = 0;
    var contentboxActualHeight = document.getElementById('contentbox').clientHeight;
    var middelDivActualHeight = document.getElementById('middlediv').offsetHeight;
    //-----------------------------------------------------------------------------------------------------------
    var contentboxoffsetHeight = 0;
    document.getElementById('contentbox').style.height = 'auto';
    contentboxoffsetHeight = document.getElementById('contentbox').offsetHeight;
    document.getElementById('contentbox').style.height = contentboxActualHeight - 20 + 'px';

    var contentboxInsetDiff = maxInsetTop - contentboxTop;
    //-----------------------------------------------------------------------------------------------------------

    /*--------------BLOG BOX --------*/
    var blogsStr_array = blogsStr.split(",");
    var blogHtDiff = 0;
    var blogHtDiffForfooterControls = 0;
    var blogAffectingFooterControls = "^";
    if (blogsStr_array.length > 1) { //if compact and expanded view only.   No auto epxand present for blog scroll view.
        var blogBoxTop = parseInt(blogsStr_array[2]);
        var blogBoxActualHeight = parseInt(blogsStr_array[3]);
        document.getElementById('divBlogs').style.height = 'auto';
        var divBlogsOffsetHt = document.getElementById('divBlogs').offsetHeight;
        //IF blog box actual height is grater then must maintain the same.
        if (blogBoxActualHeight > divBlogsOffsetHt) //blogsStr_array[1] =4  means compact view
        {
            if (blogsStr_array[1] != "4")//blog compact view
            {
                document.getElementById('divBlogs').style.height = blogBoxActualHeight + 'px';
                divBlogsOffsetHt = blogBoxActualHeight;
            }
        }
        switch (blogsStr_array[1]) {
            case "1": //out content area
            case "2": //in content area
                if ((contentboxoffsetHeight + contentboxTop) < (blogBoxTop + divBlogsOffsetHt)) {
                    blogHtDiff = (blogBoxTop + divBlogsOffsetHt) - (contentboxoffsetHeight + contentboxTop);
                }
                if ((blogBoxActualHeight + blogBoxTop) < (contentboxTop + contentboxActualHeight)) {
                    blogHtDiff = blogHtDiff + ((contentboxTop + contentboxActualHeight) - (blogBoxActualHeight + blogBoxTop));
                }
              //  alert("case 1 blogHtDiff : " + blogHtDiff);
                break;
            case "3": // below blog area not full proof yet
                document.getElementById('divBlogs').style.height = blogBoxActualHeight;
                //                var blogNewTop = (contentboxoffsetHeight + contentboxTop) + (blogBoxTop - (contentboxTop + contentboxActualHeight));
                //                //alert(blogNewTop);
                //                document.getElementById('divBlogs').style.top = blogNewTop + 'px';
                //                if (divBlogsOffsetHt > blogBoxActualHeight) {
                //                    blogHtDiffForfooterControls = divBlogsOffsetHt - blogBoxActualHeight;
                //                if (blogHtDiffForfooterControls < 0)
                //                    blogHtDiffForfooterControls = 0;
                //                blogAffectingFooterControls = blogAffectingFooterControls +  blogsStr_array[4];
                //                }
                break;
            case "4": //alert(divBlogsOffsetHt + "  " +  document.getElementById('ucBlogs_divBlogsCompact').offsetHeight);
                document.getElementById('divBlogs').style.overflow = "hidden";
                if ((divBlogsOffsetHt + 80 + blogBoxTop) > (contentboxActualHeight + contentboxTop)) {
                    blogHtDiff = (divBlogsOffsetHt + 80 + blogBoxTop) - (contentboxActualHeight + contentboxTop);
                    document.getElementById('divBlogs').style.height = (divBlogsOffsetHt + 80) + 'px';
                    if (callCounter == 1) {
                        setTimeout("CallAutoExpandAgain();", 800);
                        callCounter = 2;
                    }
                }
                break;

        }
    }
    /*-------------- blog box ends -------------*/
    if ($('.blogbox').css("display") == "none")
        blogHtDiff = 0;
    if (contentboxActualHeight < contentboxoffsetHeight)
     {
         if (blogHtDiff > 0) {
             document.getElementById('contentbox').style.height = contentboxoffsetHeight + blogHtDiff + "px";
         }
         else {
            // alert("in else ");
             document.getElementById('contentbox').style.height = 'auto';
          }

          var middledivNewHeight = document.getElementById('contentbox').offsetHeight + contentboxBottomdivGap + topDifference;
        if (middelDivActualHeight < middledivNewHeight) 
        {
            document.getElementById('middlediv').style.height = (middledivNewHeight) + 'px';
        }
        flag = 1;
    }
    else if (blogHtDiff > 0) {
    //alert(blogHtDiff);
        document.getElementById('contentbox').style.height = contentboxActualHeight + blogHtDiff - 20 + 'px';
        var middledivNewHeight = contentboxActualHeight + contentboxBottomdivGap + topDifference + blogHtDiff ;
        if (middelDivActualHeight < middledivNewHeight) 
        {
            document.getElementById('middlediv').style.height = (middledivNewHeight) + 'px';
        }
        flag = 1;
    }
    if (flag == 0) {
        if (((maxInsetTop + maxInsetHeight) > (contentboxTop + contentboxoffsetHeight)) && ((maxInsetTop) < (contentboxTop + contentboxoffsetHeight))) {
            document.getElementById('contentbox').style.height = contentboxInsetDiff - 20 + maxInsetHeight + 'px';
            var middledivNewHeight = ((maxInsetHeight + contentboxBottomdivGap + contentboxInsetDiff + contentBoxInsetGap));
            if (middelDivActualHeight < middledivNewHeight) {
                document.getElementById('middlediv').style.height = (middledivNewHeight) + 'px';
                
            }
        }
        else {
        var middledivNewHeight = ((divPageContentsHeight + contentboxBottomdivGap) - 20);
        /////var middledivNewHeight = ((document.getElementById('contentbox').offsetHeight + contentboxBottomdivGap) - 20);
        if (middelDivActualHeight < middledivNewHeight) {
            document.getElementById('middlediv').style.height = (middledivNewHeight) + 'px';
        }        
     }
 }
 var contentboxHeight1 = document.getElementById('contentbox').offsetHeight;
 CalculateDivHeights(val, padding, contentboxTop, contentBoxGap, contentboxActualHeight, contentboxHeight1); //, blogHtDiffForfooterControls, blogAffectingFooterControls
 refreshTestimonial(); ////SR2103
 TabOnloadCall(); ////SR2103
}


function CalculateDivHeights(val, padding, contentboxTop, contentBoxGap, contentboxActualHeight, contentboxHeight1) //, blogHtDiffForfooterControls, blogAffectingFooterControls
{
    var mytool_array = val.split("--");
    padding = padding + 20;
   // alert(val);
    //alert(contentboxHeight1 + ' ' + contentboxActualHeight);
    contentBoxdiff = contentboxHeight1 - (contentboxActualHeight + 20);
    var contentboxHeight1 = document.getElementById('contentbox').offsetHeight;
    for (i = 0; i < mytool_array.length - 1; i++) {
        var mytooltemp_array = mytool_array[i].split(',');
        try {
            var calculatedTop = 0;
            var actualTop = document.getElementById(mytooltemp_array[0]).offsetTop;
            var BottomdivOffsetHeight = document.getElementById('bottomdiv').offsetHeight;

            BottomdivOffsetHeight = 0;
            //if (mytooltemp_array[0] != "footer1" && mytooltemp_array[0] != "")
            if (mytooltemp_array[0] != "") {
              //  alert(mytooltemp_array[0] + "   " + actualTop + "   " + contentBoxdiff + "  " + mytooltemp_array[2] + "  "+ mytooltemp_array[3]);
                calculatedTop = actualTop + contentBoxdiff + 20;  //document.getElementById('middlediv').offsetHeight + contentboxTop + BottomdivOffsetHeight + padding;
            }
            if (mytooltemp_array[0].search("pnlInset") != -1) {
                calculatedTop = actualTop + contentBoxdiff + 20;  
               // calculatedTop = document.getElementById('middlediv').offsetHeight + contentboxTop + parseInt(mytooltemp_array[1]) + parseInt(mytooltemp_array[2]) + padding;
            }
            else if (mytooltemp_array[0].search("inset_") != -1) {
                calculatedTop = actualTop + contentBoxdiff + 20;  
                //calculatedTop = document.getElementById('middlediv').offsetHeight + contentboxTop + parseInt(mytooltemp_array[1]) + parseInt(mytooltemp_array[2]);
            }
            if (parseInt(calculatedTop) > parseInt(actualTop)) {
                document.getElementById(mytooltemp_array[0]).style.top = calculatedTop + 'px';
            }
        }
        catch (e) {
        }
    }
}


var CMS_Auto_Val;
var CMS_Max_Inset_Top;
var CMS_Max_Inset_Height
var CMS_Blog_Str;
var CMS_SecondCalled = false;
var callCounter = 0;

function CallAutoExpandAgain() {
    AutoExpand(CMS_Auto_Val, CMS_Max_Inset_Top, CMS_Max_Inset_Height, CMS_Blog_Str);
}
function CallAutoExpandAgain(cntr) {
    callCounter = cntr;
    AutoExpand(CMS_Auto_Val, CMS_Max_Inset_Top, CMS_Max_Inset_Height, CMS_Blog_Str);
}

/*this method is used by testimonial change(refreshTestimonial) and change tab () methods to change the footer items position*/
function UpdateFooterItemsTop(val, diff) {
    //alert(val);
    var maxheight = 0;
    var mytool_array = val.split("--");
    for (i = 0; i < mytool_array.length - 1; i++) {
        var mytooltemp_array = mytool_array[i].split(',');
        try {
            var calculatedTop = 0;
            var actualTop = document.getElementById(mytooltemp_array[0]).offsetTop;
            //var actualTop = parseInt(mytooltemp_array[3]);
            //alert(mytooltemp_array[0] + "  " +mytooltemp_array[1] + "  "+ mytooltemp_array[2]);
            if (mytooltemp_array[0] != "") {
                calculatedTop = actualTop + diff;  //document.getElementById('middlediv').offsetHeight + contentboxTop + BottomdivOffsetHeight + padding;
            }
            if (mytooltemp_array[0].search("pnlInset") != -1 || mytooltemp_array[0].search("inset_") != -1) {
                actualTop = parseInt(mytooltemp_array[3]);
                //   alert("actualTop " + actualTop + "  diff: " + diff);
                calculatedTop = actualTop + diff;
            }
            //          if (parseInt(calculatedTop) > parseInt(actualTop)) 
            {
                document.getElementById(mytooltemp_array[0]).style.top = calculatedTop + 'px';
            }
            if (maxheight <= calculatedTop) {
                maxheight = calculatedTop;
                maxElementHeight = document.getElementById(mytooltemp_array[0]).offsetHeight;
                document.getElementById('wrapper').style.height = (maxheight + maxElementHeight) + 'px';
            }
        }
        catch (e) {
        }
    }
}

/*This method is used to change testimonial content after every 10 sec.*/
function refreshTestimonial() {
    var divClientId = $('.testimonialContent1').attr("id");
    if (divClientId) {
        var divID_array = divClientId.split("_");
        var controlID = divID_array[0] + "_";

        var hdnValue = document.getElementById(controlID + "hdntesti").value;
        var totalQuotes = document.getElementById(controlID + "hdnTotalQuotes").value;
        for (i = 0; i <= hdnValue; i++) {
            QuoteId = document.getElementById(controlID + "divQuote" + i);
            QuoteId.style.display = "none";
        }

        currentQuoteId = document.getElementById(controlID + "divQuote" + totalQuotes);
        currentQuoteId.style.display = "block";

        totalQuotes--;
        if (totalQuotes < 0) { totalQuotes = hdnValue; }
        document.getElementById(controlID + "hdnTotalQuotes").value = totalQuotes;
        setTimeout('refreshTestimonial()', 5000);

        var divTestimonialBoxOffsetHeight = $('#ucTestimonialBox_BoxLeft').attr('offsetHeight');
        //alert(divTestimonialBoxOffsetHeight);
        var divTestimonialBoxTop = $('#divTestimonialBox').attr('offsetTop');
        var TestimonialNewBottomPosition = divTestimonialBoxOffsetHeight + divTestimonialBoxTop + 22;
        var TestimoialDefaultHt = 300; //hdnCMSPrevTestimonialBottom
        if (hdnCMSPrevTestimonialBottom == 0)
            hdnCMSPrevTestimonialBottom = TestimonialNewBottomPosition;
        if (hdnCMSPrevTestimonialBottom > hdnCMSActiveTabBottom && hdnCMSPrevTestimonialBottom > hdnCMSContentBoxBottom) {
            hdnCMSPrevTestimonialBottom = TestimonialNewBottomPosition;
            //if (TestimonialNewBottomPosition > (TestimoialDefaultHt + divTestimonialBoxTop))
            //{
            var diff = TestimonialNewBottomPosition - (TestimoialDefaultHt + divTestimonialBoxTop) + 50;
            UpdateFooterItemsTop(hdnCMSVal, diff); //sr2103
            //}
        }
    }
}



/*---------------------------------------------------------------------------------------------------*/

function TabOnloadCall(val) {
    //  alert(hdnCMSVal);
    var hdnVal = $("#hdnDefaultTabNumber").val();
    //Default Action
    $(".tab_content").hide(); //Hide all content
    if (hdnVal != "") {
        $("ul.tabs li:nth-child(" + hdnVal + ")").addClass("active").show(); //Activate second tab
        $(".tab_content:nth-child(" + hdnVal + ")").show(); //Show tab content
        var position = $("#divTabs").position(); //sr2103
        if (!position)
            return;
        var tabHeight = $("#divTabs").height();
        var divCenterListHeight = $(".active").attr('offsetHeight') + $(".tab_content").attr('offsetHeight') + $("#divTabContainerFooter").attr('offsetHeight');
        hdnCMSActiveTabBottom = (parseInt(position.top) + parseInt(divCenterListHeight)); //sr2103
        if (hdnCMSActiveTabBottom > hdnCMSPrevTestimonialBottom) {
            var diff = parseInt(divCenterListHeight) - parseInt(tabHeight);
            // alert("in: " + diff + "  ::" + hdnCMSVal);
            if (hdnCMSVal == '')
                hdnCMSVal = val;
            UpdateFooterItemsTop(hdnCMSVal, diff); //sr2103
        }
    }
}
$(document).ready(function() {
   // if (hdnCMSVal != '')
        TabOnloadCall();
});

//On Click Event
$("ul.tabs li").click(function() {
    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
    if (activeTab.charAt(0) == '#') {
        //alert(activeTab.charAt(0) + activeTab.charAt(1) + activeTab.charAt(2));
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active");
        //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        $(activeTab).fadeIn();


        var divCenterListHeight = $(activeTab).attr('offsetHeight') + $(".active").attr('offsetHeight') + $("#divTabContainerFooter").attr('offsetHeight');
        //alert(divCenterListHeight);
        var tabHeight = $("#divTabs").height();
        var position = $("#divTabs").position(); //sr2103
        //alert(position);
        if (!position) {

            return;
        }
        var maxInsetTop = 0;
        hdnCMSActiveTabBottom = (parseInt(position.top) + parseInt(divCenterListHeight)); //sr2103
        if (hdnCMSActiveTabBottom > hdnCMSPrevTestimonialBottom) {
            var diff = parseInt(divCenterListHeight) - parseInt(tabHeight);
            //  alert("click: " + diff);
            UpdateFooterItemsTop(hdnCMSVal, diff); //sr2103
        } //if
        else {
            var divTestimonialBoxOffsetHeight = $('#ucTestimonialBox_BoxLeft').attr('offsetHeight');
            var divTestimonialBoxTop = $('#divTestimonialBox').attr('offsetTop');
            var TestimonialNewBottomPosition = divTestimonialBoxOffsetHeight + divTestimonialBoxTop + 22;
            var TestimoialDefaultHt = 250; //hdnCMSPrevTestimonialBottom
            if (hdnCMSPrevTestimonialBottom == 0)
                hdnCMSPrevTestimonialBottom = TestimonialNewBottomPosition;
            if (hdnCMSPrevTestimonialBottom > hdnCMSActiveTabBottom) {
                hdnCMSPrevTestimonialBottom = TestimonialNewBottomPosition;
                var diff = TestimonialNewBottomPosition - (TestimoialDefaultHt + divTestimonialBoxTop) + 50;
                UpdateFooterItemsTop(hdnCMSVal, diff); //sr2103
            }
        }

        return false;
    }
});


/*Added By Chandrashekhar: use to hide the custom scroll and show diffult scroll bar if autoexpand is not set for page template*/
function HidePageCustomScroll() {
    try {
        //   $("#scrollbar1").css("height", "auto");
       // alert("hi");
        $(".scrollbar").hide();
        $("#divCustomScrollViewport").removeClass();
        $("#divCustomScrollOverview").removeClass();
    }
    catch (customscroll) {
    }
}
