﻿$ektron(function() {
    /********************************************/
    /// How can we help you page
    /********************************************/
    $ektron("#challenges li").click(function() {
        var $item = $ektron(this);
        var $items = $item.parent().children('li');
        var index = $items.index($item);
        $items.removeClass('selected');
        $item.addClass('selected');

        var $solutions = $item.parent().parent().children('ul.solutions');
        $solutions.removeClass('selected');
        $solutions.eq(index).addClass('selected');
    });

    var $helpCarousel = $ektron("#help-you-carousel");
    var id = getParameterByName('id');

    var $current = $helpCarousel.find('li a[contentid=' + id + ']');
    $current.addClass('selected');

    $helpCarousel.find('li.prev a').click(function() {
        // find currently selected item
        var $current = $ektron(this).parents('ul:first').find('a.selected').parent();
        var $prev = $current.prevAll('li.link');
        if ($prev.size() > 0) {
            window.location.href = $prev.children('a').attr('href');
        }
    });
    $helpCarousel.find('li.next a').click(function() {
        // find currently selected item
        var $current = $ektron(this).parents('ul:first').find('a.selected').parent();
        var $next = $current.nextAll('li.link');
        if ($next.size() > 0) {
            window.location.href = $next.children('a').attr('href');
        }
    });
});

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

// Modal Popups
$ektron(document).ready(function() {

    // $ektron(".scrollable").scrollable({ circular: true, itemFallbackDimension: "200px" });

    function fixHowWeCanHelpyou() {
        var challengesHeight = $("#challenges").height();
        $("ul.solutions").each(function() {
            $(this).css("min-height", challengesHeight);
            if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
                $(this).height(challengesHeight);
            }
            // alert(challengesHeight);
        });
    }
    function loginNavNew() {
        $(".hoverLoginButton").hover(function() {
            $(".loginHide").show();
        }, function() {
            $(".loginHide").hide();
        });
    }
    loginNavNew();
    fixHowWeCanHelpyou();

    function createModals() {
        $ektron("a[rel='modalContact']").colorbox({ iframe: true, width: "30%", height: "345px", scrolling: false,
            onComplete: function() {
                //alert("test");
                $ektron("#cboxTitle").remove();
            }

        });
        $ektron("a[rel='loadModal']").click(function(event) {
            event.preventDefault();
            var htmlContent = $ektron(".sungardModal").html();
            $ektron.colorbox({ open: true, html: htmlContent });
            return false;
        })
    }

    //begin video modals

    var i = 0;
    var playListLength = ($ektron(".playListSungard li").length - 1);




    //    alert("the playlist length is " + playListLength);

    var status = new Object;



    function videoModals() {
try
{
        $ektron(".videoPlayerModalSungard").hide();

        $ektron(".modalVideoLibrary").colorbox({ width: "540px", height: "420px", scrolling: false, inline: true, href: ".sungardModalShow",
            onOpen: function() {
                $ektron("#cboxWrapper").addClass("taco");
            }
        });

        $ektron("a[rel='moviegallery']").colorbox({ width: "540px", height: "420px", scrolling: false, inline: true, href: ".sungardModalShow",
            onOpen: function() {
                $ektron("#cboxWrapper").addClass("taco");
            },
            onComplete: function() {
                $ektron("#cboxNext, #cboxPrevious, #cboxPrevious, #cboxCurrent").hide();
                currentSlide = '';
            },
            onClosed: function() {

            }
        });
        setVideoDiv();
        videoNextButtonSungard();
}
catch(err)
{
}
    }

    function videoNextButtonSungard() {
        var currentSlide = '';
        $ektron(".sungardVideoNext").click(function() {
            currentSlide = $(".currentSlideNumberSungard").text();
            currentSlide = currentSlide - 1;
            var hideNext = currentSlide + 1;
            if (hideNext == playListLength) {
                $(".sungardVideoNext").parent("li").hide();
                $(".sungardVideoPrev").parent("li").show();
            }
            else if (hideNext == 0) {
                $(".sungardVideoNext").parent("li").show();
            }
            else {
                $(".sungardVideoNext").parent("li").show();
                $(".sungardVideoPrev").parent("li").show();
            }
            $ektron(".playListSungard li").eq(currentSlide).next("li").children(".frameBoxVideo").children("a").click();
        });

        $ektron(".sungardVideoPrev").click(function() {
            currentSlide = $(".currentSlideNumberSungard").text();
            currentSlide = currentSlide - 1;
            var hidePrev = currentSlide - 1;
            if (hidePrev == 0) {
                $(".sungardVideoNext").parent("li").show();
                $(".sungardVideoPrev").parent("li").hide();
            }
            else {
                $(".sungardVideoPrev").parent("li").show();
                $(".sungardVideoNext").parent("li").show();
            }
            $ektron(".playListSungard li").eq(currentSlide).prev("li").children(".frameBoxVideo").children("a").click();
        });
    }
    function setVideoDiv() {
        $ektron(".playListSungard li a").click(function() {
            var number = $ektron(".playListSungard li a").index(this);
            i = number;

            if (playListLength == 0) {
                $(".sungardVideoNext").hide();
                //alert("hide me now");
            }

            if (number == 0) {
                $(".sungardVideoPrev").parent("li").hide();
                $(".sungardVideoNext").parent("li").show();
            }
            else if (number == playListLength) {
                $(".sungardVideoPrev").parent("li").show();
                $(".sungardVideoNext").parent("li").hide();
            }
            else {
                $(".sungardVideoPrev").parent("li").show();
            }
            var sungardVideoUrl = $ektron(this).attr("name");
            var sungardVideoTitle = $ektron(this).text();
            var sungardVideoTitleOriginal = sungardVideoTitle.length;
            //alert(sungardVideoTitleOriginal);
            sungardVideoTitle = (sungardVideoTitle).substring(0, 78);
            $f("player1",
			{
			    //src: "../flash/flowplayer-3.2.5-0.swf",
			    src: "/flash/flowplayer-3.2.5-0.swf",
			    wmode: "opaque",
			    key: "#@cf7ff940fa7c8ca9de2" //place the new license key number here.
			},

			{
			    clip: {
			        url: sungardVideoUrl,
			        autoPlay: false,
			        autoBuffering: true,
			        scaling: "fit"
			    },
			    plugins: {

			        controls: {
			            // setup a background image
			            //url: 'flowplayer.controls-tube-3.2.3.swf',
			            //playlist: false,
			            background: '#617990',
			            /* you may want to remove the gradient */
			            // backgroundGradient: 'none',

			            // these buttons are visible
			            all: false,
			            scrubber: true,
			            play: true,
			            mute: true,
			            time: false,
			            fullscreen: false,

			            // custom colors
			            bufferColor: '#000000',
			            progressColor: '#000000',
			            buttonColor: '#bdd0e1',
			            buttonOverColor: '#ffffff',

			            // tooltip background color (since 3.1.0)
			            tooltipColor: '#cc0000',

			            //slider border color
			            //sliderBorderColor: "#455869",
			            sliderBorder: "none",

			            // custom dimension
			            height: 25,
			            width: 400,
			            scrubberHeightRatio: .25,

			            // setup auto hide
			            //autoHide: 'always',			

			            // a little more styling 			
			            width: '100%',
			            bottom: 0,
			            left: '50%'
			        }
			    }

			,
			    onLoad: function() {
			        if (sungardVideoTitleOriginal > 78) {
			            $ektron("#player1").css({ position: "relative" }).append("<div class='titleSungardVideo'><div class='photoInfoVideo'><div class='videoTitleSungardPad'>" + sungardVideoTitle + "...</div></div></div>");
			        }
			        else {
			            $ektron("#player1").css({ position: "relative" }).append("<div class='titleSungardVideo'><div class='photoInfoVideo'><div class='videoTitleSungardPad'>" + sungardVideoTitle + "</div></div></div>");
			        }
			        $ektron("#player1 img").remove();
			    }

			});
        });
    }
    //end video modals
    //begin functions for videoLibrary
    function setVideoLibraryDiv() {
        $ektron(".modalVideoLibrary").click(function() {

            var sungardVideoUrl = $ektron(this).attr("name");
            var sungardVideoTitle = $ektron(this).html();
            //alert("the video url is" + sungardVideoUrl);
            //alert("the title is" + sungardVideoTitle);
            $f("player1",
			{
			    src: "../flash/flowplayer-3.2.5-0.swf",
			    wmode: "opaque"
			},

			{
			    clip: {
			        url: sungardVideoUrl,
			        autoPlay: false,
			        autoBuffering: true
			    },
			    plugins: {

			        controls: {
			            // setup a background image
			            //url: 'flowplayer.controls-tube-3.2.3.swf',
			            //playlist: false,
			            background: '#617990',
			            /* you may want to remove the gradient */
			            // backgroundGradient: 'none',

			            // these buttons are visible
			            all: false,
			            scrubber: true,
			            play: true,
			            mute: true,
			            time: false,
			            fullscreen: false,

			            // custom colors
			            bufferColor: '#000000',
			            progressColor: '#000000',
			            buttonColor: '#bdd0e1',
			            buttonOverColor: '#ffffff',

			            // tooltip background color (since 3.1.0)
			            tooltipColor: '#cc0000',

			            //slider border color
			            //sliderBorderColor: "#455869",
			            sliderBorder: "none",

			            // custom dimension
			            height: 25,
			            width: 400,
			            scrubberHeightRatio: .25,

			            // setup auto hide
			            //autoHide: 'always',			

			            // a little more styling 			
			            width: '100%',
			            bottom: 0,
			            left: '50%'
			        }
			    }

			,
			    onLoad: function() {
			        $ektron("#player1").css({ position: "relative" }).append("<div class='titleSungardVideo'>" + sungardVideoTitle + "</div>");
			        $ektron("#player1 img").remove();
			    }

			});
        });
    }

    //end functions for videoLibrary

    //begin function to move taxonomy megamenu
    function closeMegaMenu() {
        $ektron(".megaMenuClose, .megaMenuCloseLt, .megaMenuCloseRt, #header, #loginNav").hover(function() {
            $ektron(".megaMenucontainer").css({ position: "absolute", top: "-2500px", left: "0", visibility: "hidden" }).fadeOut();
            //alert("close");
        });
    }
    function appendContainersToMenu() {
        $ektron(".megaMenucontainer").css({ position: "absolute", top: "25px", left: "0", visibility: "visible" }).fadeIn();
        $ektron(".megaMenucontainer").append("<div class='megaMenuClose'></div>");
        $ektron(".megaMenucontainer").append("<div class='megaMenuCloseLt'></div>");
        $ektron(".megaMenucontainer").append("<div class='megaMenuCloseRt'></div>");
    }
    var menuIsVisible = $ektron(".megaMenucontainer").is(":visible");
    function taxonomyMegaMenu() {
        var taxonomyHtml = $ektron(".megaMenucontainer").html();
        // alert(taxonomyHtml);

        $ektron(".megaMenucontainer").css({ position: "absolute", top: "-2500px", left: "0" }).fadeOut();
        $ektron(".megaMenucontainer").appendTo(".sungardmenu");
        topNavDropDowns();

        $ektron(".megaMenucontainer").hide();
        $ektron("#megamenu-by-alpha").hide();

        $ektron(".taxonomyMenuPlacement").hover(function() {
            var menuIsVisible = $ektron(".megaMenucontainer").is(":visible");
            var megaMenuHeight = 0;
            megaMenuHeight = $ektron(".megaMenucontainer").height();
            //alert(megaMenuHeight);
            if (menuIsVisible == true) {
                //$ektron(".megaMenucontainer").fadeOut();
                $ektron(".megaMenucontainer").css({ position: "absolute", top: "-2500px", left: "0", visibility: "hidden" }).fadeOut();
                //$ektron(".jcarousel-container-horizontal").css("z-index", "2000");
            }
            else {
                appendContainersToMenu();
                $ektron(".megaMenuCloseLt, .megaMenuCloseRt").height(megaMenuHeight);
                closeMegaMenu();
                // $ektron(".jcarousel-container-horizontal").css("z-index","-1");
            }
        });
        closeMegaMenu();
    }
    //end function to move taxonomy megamenu
    //begn hide breadcrumb on home page
    function hideBread() {
        var animationLength = $ektron("#homeWrap").length;
        if (animationLength >= 1) {
            $ektron(".breadCrumb").remove();
        }
    }
    //end hide breadcrumb on home page
    //begin top nav
    function topNavDropDowns() {
        $ektron(".ekflexmenu_startlevel").hover(function() {
            $ektron(this).addClass("hoverTop");
            var subMenu = $ektron(this).children(".ekflexmenu_submenu_items_hidden").html();
            // alert(subMenu);
            $ektron(this).children(".ekflexmenu_submenu_items_hidden").fadeIn();
            if (menuIsVisible == true) {
                $ektron(".sungardmenu .megaMenucontainer").fadeOut();
            }
        }, function() {
            $ektron(this).removeClass("hoverTop");
            $ektron(this).children(".ekflexmenu_submenu_items_hidden").fadeOut();
        });
        $ektron("#solution_main").mouseover(function() {
            $ektron(".ekflexmenu_startlevel .ekflexmenu_submenu_items_hidden, .ekflexmenu_startlevel .ekflexmenu_submenu_items").fadeOut();
        });
    }
    //end top nav
    //mover personaliztion
    $ektron(".EktronPersonalizationWrapper").prependTo("#container");
    //end move personalization

    //begin remove empty list containers
    function removeEmptyListDivs() {
        $ektron("#lists:empty").remove();
    }
    //end remove empty list containers.
    //begin columnmize megamenu
    function columnizeMegaMenu() {
        $ektron("#megamenu-by-category").columnize({ columns: 4, dontsplit: ".header" });

        var lengthcolumn4 = $(".column:eq(3)").find("li").text().replace(" ", "").length;
        //alert(lengthcolumn4);

		/* DK - 7-7-2011
		/* ORIGINAL LINE COMMENTED OUT
        if (lengthcolumn4 == 0) {
            var lastItemInColumn3 = $(".column:eq(2)").find(".header:last").html();
            $(".column:eq(2)").find(".header:last").hide();
            //alert("more html is needed " + lastItemInColumn3);
            $(".column").eq(3).html("<ul><li class='header'>" + lastItemInColumn3 + "</li></ul>");
        }
		*/
		/* DK - 7-7-2011
		/* NEW CODE APPLIED */
        if (lengthcolumn4 == 0) {
            var lastItemInColumn3 = $(".column:eq(2)").find(".header:last").html();;
			if ( $('.column').eq(3).html() ) {
				$(".column:eq(2)").find(".header:last").hide();
				$(".column").eq(3).html("<ul><li class='header'>" + lastItemInColumn3 + "</li></ul>");
			}
        }
		/* END NEW CODE */
		
		
        /*$ektron("#megamenu-by-category .header, #megamenu-by-alpha .header,").addClass("dontend");*/
        $ektron("#megamenu-by-alpha").columnize({ columns: 4, dontsplit: ".header" });
        //alert("test");
    }
    //end columnize megamenu

    //begin nav filter
    function changeMegaFilter() {
        var categoryLength = $ektron("#byCategoryMega").text();
        var alphaLength = $ektron("#byAlphaMega").text();

        if (categoryLength == 0) {
            $ektron("#byCategoryMega").text("Products By Category");
        }
        if (alphaLength == 0) {
            $ektron("#byAlphaMega").text("Products A-Z");
        }

        $ektron("#byCategoryMega").click(function() {
            // alert("category");
            appendContainersToMenu();
            var megaMenuHeight = 0;
            $ektron("#megamenu-by-alpha").hide();
            $ektron("#megamenu-by-category").fadeIn();
            megaMenuHeight = $ektron(".megaMenucontainer").height();
            $ektron(".megaMenuCloseLt, .megaMenuCloseRt").height(megaMenuHeight);
            closeMegaMenu();
        });
        $ektron("#byAlphaMega").click(function() {
            //alert("alpha");
            appendContainersToMenu();
            var megaMenuHeight = 0;
            $ektron("#megamenu-by-category").hide();
            $ektron("#megamenu-by-alpha").fadeIn();
            megaMenuHeight = $ektron(".megaMenucontainer").height();
            $ektron(".megaMenuCloseLt, .megaMenuCloseRt").height(megaMenuHeight);
            //alert(megaMenuHeight);
            closeMegaMenu();
        });
    }
    //end nav filter

    /********************************************/
    /// Search box ENTER for IE
    /********************************************/

    // Clear text on click.
    $ektron(".searchbox").focus(function() { $ektron(this).val(""); });


    function searchKeypress() {
        $ektron(".searchbox").keypress(function(e) {
            var evt = e ? e : window.event;
            var code = evt.keyCode || evt.which;
            if (code == 13) {
                var text = $ektron(".searchbox").val();
                $ektron("#container .searchButton").trigger("click");
                e.preventDefault();
            }
        });
    }

    // Remove ability to comment on search
    function removeCommentsFromSearch() {
        $(".searchWrapper img[alt = 'Add Comment']").each(function() {
            $(this).remove();
        });
    }

    $(".basicSearchSubmit, .advancedSubmit").click(function() {
        removeCommentsFromSearch();
    });

    // To remove Alphabet A - Z when no data
    function removeAlphabetic() {
        if ($ektron("#megamenu-by-alpha").length > 0) {
        }
        else {
            $ektron("#byAlphaMega").hide();
        }
    }

    function filterListings() {
        //get url param
        function sungardQueryFilter() {
            function getQueryParam(sMyQueryParamName) {
                var sQueryParams = window.location.search.substring(1);
                var aQueryParams = sQueryParams.split("&");
                for (i = 0; i < aQueryParams.length; i++) {
                    var aParam = aQueryParams[i].split("=");
                    if (aParam[0] == sMyQueryParamName) return aParam[1];
                }
                return ("");
            }
            var sQueryVar = "Selected";
            var queryUrlMenuITem = (getQueryParam(sQueryVar));
            //end get url param
            if (queryUrlMenuITem != '') {
                //begin add selected Class
                $ektron(".sungardFilterNav  li a[href='?Selected=" + queryUrlMenuITem + "']").parent("li").addClass("sungardSelectedCategory").attr("id", "tabSelected");
                //begin add selected Class
            }
            else {
                $ektron(".sungardFilterNav li").eq(0).addClass("sungardSelectedCategory").attr("id", "tabSelected");
                //alert(queryUrlMenuITem);
            }
        }
        sungardQueryFilter();
        $ektron(".sungardFilterNav li").hover(function() {
            //sungardQueryFilter();
            $ektron(".sungardFilterNav li").not("#tabSelected").removeClass("sungardSelectedCategory");
            $ektron(this).addClass("sungardSelectedCategory");
        }, function() {
            $ektron(this).not("#tabSelected").removeClass("sungardSelectedCategory");
        });
    }
    function shareThisRemove() {
        var shareThisNumber = $ektron(".st_sharethis_button").length;
        if (shareThisNumber > 1) {
            $ektron(".shareThisFooter").remove();
        }
    }

    function scrollContainers() {

        $ektron('.scrollMeUp').jScrollPane(
				{
				    showArrows: true,
				    verticalArrowPositions: 'os',
				    horizontalArrowPositions: 'os',
				    autoReinitialise: true
				}
				);
    }

    function modalContent() {
        //function LoadContactForm(RecipientEmail) {
        $ektron(".modalContact").each(function() {
            var modalHref = $ektron(this).attr("href");
            var emailAddress = $ektron(this).attr("rev");
            var newHref = modalHref + emailAddress;
            //alert(newHref);
            $ektron(this).attr("href", newHref);
        });

        $ektron(".modalContact").click(function() {
            $ektron(this).colorbox({ iframe: true, innerWidth: 425, innerHeight: 240, scrolling: false });
        });

    }
    function frameBoxHeight() {
        var arr = new Array();
        $(".frameBoxVideo").each(function() {
            var frameBoxHeightValue = $(this).height();
            //alert(frameBoxHeightValue);
            arr.push(frameBoxHeightValue);
            Array.max = function(array) {
                return Math.max.apply(Math, array);
            }
            $(".slidePeers, .relatedFrame").height(Array.max(arr));

            //alert("the largest value is " + Array.max(arr));
        });
        try {
            $(".frameBoxVideo").height(Array.max(arr));
        } catch (err) {
            //alert(err);
        }
    }


    //begin initiate functions
    //sungardNextAndPreviousButtons(); //this operates video modals in solutions.aspx file
    removeCommentsFromSearch();
    frameBoxHeight();
    setVideoLibraryDiv();
    videoModals();
    modalContent(); //this works with modals on the solutions.aspx file
    //scrollContainers();
    shareThisRemove();
    filterListings();
    searchKeypress();
    columnizeMegaMenu();
    changeMegaFilter();
    removeEmptyListDivs();
    hideBread();
    taxonomyMegaMenu();
    createModals();
    removeAlphabetic(); // To remove Alphabet A - Z when no data
});

// BSM ---- Hides duplicates in Megamenu alpha sort
$ektron(document).ready(function() {
    var seen = {};
    $ektron("#megamenu-by-alpha li").each(function() {
        var txt = $ektron(this).text();
        if (seen[txt])
            $ektron(this).remove();
        else
            seen[txt] = true;
    });
    $ektron('#footer li:last').addClass('last');
});
