// JavaScript Document

function showColorPalette() {
    var colorsdiv = document.getElementById('colorsdiv');
    colorsdiv.style.display = "block";
    return false;
}

function hideColorPalette() {
    var colorsdiv = document.getElementById('colorsdiv');
    colorsdiv.style.display = "none";
}

ShowResetDefaultPopUp = function() {
    var changeAssurancePlan = ResHelper.GetElementByIdName("ChangeAssurancePlan");

    if (changeAssurancePlan) {
        if (changeAssurancePlan.checked) {
            var elem = ResHelper.GetElementByIdName("ChangeAssurancePlanPopUp");
            elem.style.display = "block";
            ResHelper.ShowModalContainer(elem.innerHTML);
            elem.style.display = "none";
        }
        else {
            var elem = ResHelper.GetElementByIdName("AutoSignUpUnselectedPopUp");
            elem.style.display = "block";
            ResHelper.ShowModalContainer(elem.innerHTML);
            elem.style.display = "none";
        }
    }
    return false;
}

$(document).ready(function() {


    var colorPopupIndex = 0;
    var widgetCaptionIndex = 1;
    var widgetContentIndex = 2;
    var widgetCaptionClassName = $($('.widget')[0]).children()[widgetCaptionIndex] == undefined ? "" : $($('.widget')[0]).children()[widgetCaptionIndex].className;
    var mainMenuClassName = document.getElementById("mainMenu").className;
    var footerClassName = document.getElementById("footer").className;

    var logoColor = "blue";

    if ($("#wrapper")[0].offsetWidth <= 1280)
        $(".middleArea").css("width", "48%");
    else
        $(".middleArea").css("width", "49%");


    /*----------------- Drag and drop Widgets ------------------------------*/

    /*$(".leftArea, .middleArea, .rightArea").sortable({
    placeholder: 'ui-state-highlight',
    handle: '.moveWidget',
    cursorAt: 'top, left',
    forcePlaceholderSize: true,
    tolerance: 'pointer',
    connectWith: '.leftArea, .middleArea, .rightArea',
    stop: function(){
    $(".colorsPopup").css('left','auto');
    $(".colorsPopup").css('top','auto');
    }
						
    }).disableSelection();*/

    /*----------------- Drag and drop Main Menu ------------------------------*/

    $("#mainMenuContainer, #menu1, #menu2, #menu3, #menuLeft, #menuMiddle1, #menuMiddle2, #menuMiddle3, .leftArea, .middleArea, .rightArea").sortable({
        placeholder: 'ui-state-highlight',
        handle: '.moveWidget',
        cursorAt: 'top, left',
        forcePlaceholderSize: true,
        tolerance: 'pointer',
        connectWith: '#mainMenuContainer, #menu1, #menu2, #menu3, #menuLeft, #menuMiddle1, #menuMiddle2, #menuMiddle3, .leftArea, .middleArea, .rightArea',
        start: function() {
            if ($('.ui-state-highlight').css('height') != '70px')
                $('.ui-state-highlight').css('height', '70px');
        },
        stop: function() {
            $(".colorsPopup").css('left', 'auto');
            $(".colorsPopup").css('top', 'auto');

            if ($('#mainMenuContainer').children().length == 0)
                $('#logo').css('background', 'url(App_Themes/TC1/images/long_foster_logo2.png)');
            else
                $('#logo').css('background', 'url(App_Themes/TC1/images/long_foster_logo_' + logoColor + '.png)');


            // When we move the main navigation to the main content we need to resize the width of the widgets containers and the width of the main menu container
            if ($("#menuLeft").children().length != 0 || $("#menuMiddle1").children().length != 0 || $("#menuMiddle2").children().length != 0 || $("#menuMiddle3").children().length != 0) {
                if ($("#menuLeft").children().length != 0) {
                    $("#menuMiddle1").css("width", "5px");
                    $("#menuMiddle2").css("width", "5px");
                    $("#menuMiddle3").css("width", "5px");
                    $("#menuLeft").css("width", "90px");
                }
                else if ($("#menuMiddle1").children().length != 0) {
                    $("#menuLeft").css("width", "5px");
                    $("#menuMiddle2").css("width", "5px");
                    $("#menuMiddle3").css("width", "5px");
                    $("#menuMiddle1").css("width", "90px");
                }
                else if ($("#menuMiddle2").children().length != 0) {
                    $("#menuLeft").css("width", "5px");
                    $("#menuMiddle1").css("width", "5px");
                    $("#menuMiddle2").css("width", "90px");
                    $("#menuMiddle3").css("width", "5px");
                }
                else if ($("#menuMiddle3").children().length != 0) {
                    $("#menuLeft").css("width", "5px");
                    $("#menuMiddle1").css("width", "5px");
                    $("#menuMiddle2").css("width", "5px");
                    $("#menuMiddle3").css("width", "90px");
                }

                var num = 90000 / document.getElementById("areasContainer").offsetWidth;

                if ($("#wrapper")[0].offsetWidth <= 1280)
                    $(".middleArea").css("width", (document.getElementById("areasContainer").offsetWidth - num) * 44 / 100);
                else
                    $(".middleArea").css("width", (document.getElementById("areasContainer").offsetWidth - num) * 45 / 100);
            }
            else {
                $("#menuLeft").css("width", "5px");
                $("#menuMiddle1").css("width", "5px");
                $("#menuMiddle2").css("width", "5px");
                $("#menuMiddle3").css("width", "5px");

                if ($("#wrapper")[0].offsetWidth <= 1280)
                    $(".middleArea").css("width", "48%");
                else
                    $(".middleArea").css("width", "49%");
            }


        }

    }).disableSelection();


    /*--------------- Drag color palette popup-----------------------------*/

    $(".colorsPopup").draggable({
        handle: '.movePopup'
    });


    /*--------------- Open Color palette popup ---------------------------*/

    openColorPopup = function(myLink) {
        $($($(myLink).parents()[5]).children()[colorPopupIndex]).fadeIn(); ;
    }

    /*--------------- Close Color palette popup ---------------------------*/

    closeColorPopup = function(myLink) {
        $($get('colorsdiv')).fadeOut();
        //$($($(myLink).parents()[6]).children()[colorPopupIndex]).fadeOut();

    }
    /*--------------- Close Reset palette popup ---------------------------*/

    closeResetPopup = function(myLink) {
        $($get('ResetLayer')).fadeOut();
    }

    /*------------ Open Menu Color palette -------------------------------*/

    openMenuColorPopup = function(myLink) {

        if ($("#menuMiddle3").children().length != 0)
        // If menuMiddle3 contains the menu we will move the color popup to the left
            $($("#mainMenu").children()[0]).css('left', '-280px');

        $($("#mainMenu").children()[0]).css('display', 'block');
    }

    /*------------ Open Footer Color palette -------------------------------*/

    openFooterColorPopup = function(myLink) {
        $("#footerContent > .colorsPopup").css('display', 'block');
    }


    /*-------------- Collapse/Expand widget -------------------------------*/

    collapseExpandWidget = function(myLink) {

        var myContent = $($(myLink).parents()[5]).children()[widgetContentIndex];

        if ($(myContent).css('display') == "block")
            $(myContent).slideUp();
        else
            $(myContent).slideDown();

    }


    /*------------- Remove Widget -----------------------------------------*/

    removeWidget = function(myLink) {

        var myWidget = $(myLink).parents()[5];

        $(myWidget).fadeOut(
						function() {
						    $(myWidget).remove(); // Remove completely the Node
						}
					);

    }

    /*------------- Change Top Widget Color --------------------------------*/

    changeWidgetTopColor = function(color) {
        if ($get('divSaveMsg') != null)
            $get('divSaveMsg').style.display = "none";
        var colorPref = $get(prefix + 'hidColorPref').value;
        var colorPrefSplit = colorPref.split(',');
        $(".widgetCaption").each(function() {
            if (color == '')
                $(this)[0].className = "widgetCaption";

            else if ($(this)[0].className.search(color) == -1) {
                $(this)[0].className = "widgetCaption"; //widgetCaptionClassName; // Initialize widget caption class name
                $(this).addClass(color);
            }
        });

        //color preference delimited by comma in the order mentioned: widget, header, footer, links        
        colorPrefSplit[0] = color;
        $get(prefix + 'hidColorPref').value = colorPrefSplit.toString();
    }

    /*------------  Change Menu & Footer Color --------------------------------------*/

    changeMenuColor = function(color) {
        if ($get('divSaveMsg') != null)
            $get('divSaveMsg').style.display = "none";
        document.getElementById("mainMenu").className = mainMenuClassName;
        $("#mainMenu").addClass(color);
        document.getElementById("footer").className = footerClassName;
        $("#footer").addClass(color);
        //color preference delimited by comma in the order mentioned: widget, Menu, footer, links
        var colorPref = $get(prefix + 'hidColorPref').value;
        var colorPrefSplit = colorPref.split(',');
        colorPrefSplit[1] = color;
        $get(prefix + 'hidColorPref').value = colorPrefSplit.toString();

    }

    /*------------  Change Footer Color - not in Use --------------------------------------*/

    changeFooterColor = function(color) {
        document.getElementById("footer").className = footerClassName;

        $("#footer").addClass(color);

        //color preference delimited by comma in the order mentioned: widget, Menu, footer, links
        var colorPref = $get(prefix + 'hidColorPref').value;
        var colorPrefSplit = colorPref.split(',');
        colorPrefSplit[1] = color;
        $get(prefix + 'hidColorPref').value = colorPrefSplit.toString();

    }


    /*------------ Change Links And Buttons Color ---------------------------*/

    changeLinksColor = function(color) {
        //color preference delimited by comma in the order mentioned: widget, Menu, footer, links
        if ($get('divSaveMsg') != null)
            $get('divSaveMsg').style.display = "none";
        var colorPref = $get(prefix + 'hidColorPref').value;
        var colorPrefSplit = colorPref.split(',');

        $("a").each(function() {
            $(this).removeClass(colorPrefSplit[2]);

            // if you added a new color to the palette please add this instruction : 
            // $(this).removeClass(<your color name>); 

            $(this).addClass(color);
        });
        colorPrefSplit[2] = color;
        $get(prefix + 'hidColorPref').value = colorPrefSplit.toString();

    }
});


var DHTML = (document.getElementById || document.all || document.layers);
function getObj(name) {
    if (document.getElementById) {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
    }
    else if (document.all) {
        this.obj = document.all[name];
        this.style = document.all[name].style;
    }
    else if (document.layers) {
        this.obj = document.layers[name];
        this.style = document.layers[name];
    }
}
function showPageCover() {
    var widCover = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth) + "px";
    var htCover = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) + "px";
    //    document.getElementById('pageCover').style.width = widCover;
    //    document.getElementById('pageCover').style.height = htCover;
    document.getElementById('pageCover').style.display = 'block';
    document.getElementById('pageCover').style.visibility = 'visible';
}
function toggle(div, flag) {
    if (!DHTML) return;
    var x = new getObj(div);
    x.style.display = (flag) ? 'none' : 'block'
    x.style.visibility = (flag) ? 'hidden' : 'visible';
}


