﻿// Fix to stop HTML mode displaying while Flash loads
if (!(window.swfobject && swfobject.hasFlashPlayerVersion('10.0.22'))) shouldDisplayFlash(false);

if (shouldDisplayFlash() && !isFlashDisabledOnPage) { 
   
    // Hide whilst the document is still loading
    document.writeln('<style type="text/css">.wrapper { display: none; } .top-carousal { display: none; }</style>');
    // Rewrite the URLs to include the Deeplink anchor for requests to friendly URLs that don't have the 
    if (!location.hash) appendDeeplinkAnchorToLocation();
}


$(document).ready(function() {
    if (!shouldDisplayFlash() || isFlashDisabledOnPage) {
        var autoScrollInterval = null;

        //code for getting the back and next buttons as per language selected.
        var languagesel = $(".SelectedLanguage").val();
        var backimg = "/encordia/images/back-btn-" + languagesel + ".gif";
        var nextimg = "/encordia/images/next-btn-" + languagesel + ".gif";
        $("div.scrollingHotSpotLeft").css({ minWidth: '75px', width: '75px', height: '81px', backgroundImage: 'url(' + backimg + ')', backgroundRepeat: 'no-repeat', backgroundPosition: 'center center', position: 'absolute', backgroundColor: '#FFF', zIndex: '201', left: '0', opacity: '1', filter: 'alpha(opacity=100)' });
        $("div.scrollingHotSpotRight").css({ minWidth: '75px', width: '75px', height: '81px', backgroundImage: 'url(' + nextimg + ')', backgroundRepeat: 'no-repeat', backgroundPosition: 'center center', position: 'absolute', backgroundColor: '#FFF', zIndex: '201', left: '1121px', opacity: '1', filter: 'alpha(opacity=100)' });
        //end code for getting the back and next buttons as per language selected.

        $(".wrapper").show();
        $("#FlashContent").hide();

        // instantiating common variables
        var totalwidth = 0;
        var imgcount = 0;
        var thisimage = "#" + $(".selectedImageId").val();
        var thumbtxt = 0;
        var parentimage = "";
        var parentwidth = 0;
        var offset = $(".wrapper").offset().left;
        // end instantiating common variables

        if (($(".pagename").val() != "") && ($(".pagename").val() != undefined)) {
            $(".top-carousal").show();

            //function to for center aligning the images and text.
            function imageload(e, upperval) {
                if (e.offset().left < upperval - 2) {
                    for (j = e.offset().left; j < upperval - 2; j++) {
                        scrolleft();
                        if (e.offset().left == j) {
                            $(".bkground").css({ display: "none" });
                            break;
                        } else {
                            j = e.offset().left;
                            if (j > upperval) {
                                var adjust = j - upperval;
                                if ($(".scrollingHotSpotRight:hidden") && imgcount < 7) {
                                    $("#makeMeScrollable1").css({ left: -adjust + "px" });
                                } else {
                                    $(".scrollableArea").css({ left: -adjust + "px" });
                                }
                                $(".bkground").css({ display: "none" });
                                break;
                            }
                        }
                    }
                } else {
                    for (j = e.offset().left; j > upperval + 2; j--) {
                        scrollright();
                        if (e.offset().left == j) {
                            $(".bkground").css({ display: "none" });
                            break;
                        } else {
                            j = e.offset().left;
                            if (j < upperval - 2) {
                                var adjust = upperval - j;
                                if ($(".scrollingHotSpotRight:hidden") && imgcount < 7) {
                                    $("#makeMeScrollable1").css({ left: adjust + "px" });
                                } else {
                                    $(".scrollableArea").css({ left: adjust + "px" });
                                }
                                $(".bkground").css({ display: "none" });
                                break;
                            }
                        }
                    }
                }
            }
            //end function to for center aligning the images and text.

            //function to for scrolling left the images and text.
            function scrolleft() {
                $(".scrollableArea").prepend($(".scrollableArea").children(":last-child"));
                $(".scrollWrapper").scrollLeft($(".scrollWrapper").scrollLeft() - $(".scrollableArea").children(":last-child").outerWidth());
            }
            //end function to for scrolling left the images and text.

            //function to for scrolling right the images and text.
            function scrollright() {
                $(".scrollableArea").append($(".scrollableArea").children(":first-child"));
                $(".scrollWrapper").scrollLeft($(".scrollWrapper").scrollLeft() - $(".scrollableArea").children(":first-child").outerWidth());
            }
            //end function to for scrolling right the images and text.

            $("#makeMeScrollable1").css({ width: '300px' });

            jQuery.each($(".panelimg"), function() {
                imgcount = imgcount + 1; //calculate no of images to show/hide the nav buttons
                if ($(".pagename").val() == "CollectionPage") {
                    if ($(this).width() < 100) {
                        $(this).width("100px");
                    }
                    totalwidth = totalwidth + $(this).width() + 20;
                }
            });


            // If Story or Events page action
            if ($(".pagename").val() != "CollectionPage") {
                totalwidth = 0
                jQuery.each($(".image-thumb"), function() {
                    var imagelen = $(this).width();
                    if ($(".pagename").val() == "StoryPage") {
                        thumbtxt = $(this).nextAll("div").width();
                        thumbtxt = thumbtxt + 1;

                        if ($.browser.mozilla) {
                            $(this).nextAll("div").css({ wordBreak: 'keep-all' });
                        }
                        if ($.browser.msie) {
                            if ($.browser.version == "6.0") {
                                $(".thumb-text").css({ wordBreak: 'keep-all', left: '6px', position: 'absolute', top: '60px', width: thumbtxt + 'px' });
                            } else if ($.browser.version == "7.0") {
                                if ($(".SelectedLanguage").val() == "en") {
                                    $(".thumb-text").css({ wordBreak: 'keep-all', left: '6px', position: 'absolute', top: '60px', width: thumbtxt + 'px' });
                                } else {
                                    $(".thumb-text").css({ wordBreak: 'keep-all', left: '6px' });
                                }
                            } else {
                                $(this).nextAll("div").css({ wordBreak: 'keep-all', left: '6px', position: 'absolute', top: '60px', width: thumbtxt + 'px' });
                            }
                        } else {
                            $(this).nextAll("div").css({ wordBreak: 'keep-all', left: '6px', position: 'absolute', top: '60px', width: thumbtxt + 'px' });
                        }

                        if (imagelen > thumbtxt || thumbtxt == 0) {
                            parentwidth = imagelen;
                            if (imagelen < 50) {
                                $(this).width("100px");
                                parentwidth = 100;
                            }
                        } else {
                            parentwidth = thumbtxt;
                            if (thumbtxt < 50) {
                                parentwidth = 60;
                            }
                        }

                        $(this).parents(".subnav-thumb").width(parentwidth).css({ textAlign: 'center' });
                        $(this).css({ float: 'none' });
                        $(this).children("a").css({ float: 'none' });
                        $(this).children("a").children("img").css({ float: 'none' });

                        parentwidth = parentwidth + 20;

                    } else {
                        parentwidth = imagelen + 20;
                        if (imagelen < 50) {
                            parentwidth = 120;
                        }
                        $(this).parents(".subnav-thumb").width(parentwidth - 20);
                    }
                    totalwidth = totalwidth + parentwidth; //calculate the total width
                });

                $(".panelimg").parents(".subnav-thumb").css({ backgroundColor: '#fff', opacity: '0.4', filter: 'alpha(opacity=40)' });
                $(thisimage).parents(".subnav-thumb").css({ backgroundColor: '#f6f4f3', opacity: '1.0', filter: 'alpha(opacity=100)' });
                imageoffset = $(thisimage).parents(".subnav-thumb").offset().left;

                parentimage = $(thisimage).parents(".subnav-thumb");
                if ($(".pagename").val() == "StoryPage") {
                    if (parentimage.width() > 133) {
                        var adjusted = (parentimage.width() - 133) / 2;
                        actualposition = offset + (535 - adjusted);
                    } else {
                        var adjusted = (110 - parentimage.width()) / 2;
                        actualposition = offset + (535 + adjusted);
                    }
                } else {
                actualposition = offset + 545;
                }
            } else { // logic for collection screen
                $(".panelimg").css({ backgroundColor: '#fff', opacity: '0.4', filter: 'alpha(opacity=40)' });
                $(thisimage).css({ backgroundColor: '#f6f4f3', opacity: '1.0', filter: 'alpha(opacity=100)' });
                imageoffset = $(thisimage).offset().left;
                parentimage = $(thisimage);
                actualposition = offset + 553;
            }

            if (imgcount < 7) {
                $(".scrollingHotSpotRight").hide();
                $(".scrollingHotSpotLeft").hide();
                $("#makeMeScrollable1").css({ width: totalwidth + 'px', margin: '0px auto', height: '81px', position: 'relative' });
                $("div.scrollWrapper").css({ position: 'relative', width: '100%', height: '81px', backgroundColor: '#FFF' });
                $("div.scrollableArea").css({ width: totalwidth + 'px', position: 'relative', height: '81px' });
            } else {
                $(".scrollingHotSpotRight").show();
                $(".scrollingHotSpotLeft").show();
                $("#makeMeScrollable1").css({ width: '100%', height: '81px', position: 'relative', paddingTop: '5px' });
                $("div.scrollWrapper").css({ position: 'relative', overflow: 'hidden', width: '100%', height: '81px', backgroundColor: '#FFF' });
                $("div.scrollableArea").css({ position: 'relative', width: 'auto', height: '81px', padding: '0', margin: '0' });                
            }

            if (imageoffset > (actualposition + 2) || imageoffset < (actualposition - 2)) {
                imageload(parentimage, actualposition); // onload run function to center the selected image and text
            }

            $(".bkground").css({ display: "none" });


            // onmousemove run function to center the selected image and text
            $(".content-container:parent, .header").mouseover(function() {
                if ($(".pagename").val() != "CollectionPage") {
                    imageoffset = $(thisimage).parents(".subnav-thumb").offset().left;
                    if ($(".pagename").val() == "StoryPage") {
                        parentimage = $(thisimage).parents(".subnav-thumb");
                        if (parentimage.width() > 133) {
                            var adjusted = (parentimage.width() - 133) / 2;
                            actualposition = offset + (535 - adjusted);
                        } else {
                            var adjusted = (110 - parentimage.width()) / 2;
                            actualposition = offset + (535 + adjusted);
                        }
                    } else {
                        actualposition = offset + 545;
                        parentimage = $(thisimage);
                    }
                } else {
                    imageoffset = $(thisimage).offset().left;
                    parentimage = $(thisimage);
                    actualposition = offset + 553;
                }

                if (imageoffset > (actualposition + 2) || imageoffset < (actualposition - 2)) {
                    imageload(parentimage, actualposition);
                }
            });

            $(".panelimg").hover(
                function() {
                    if ($(".selectedImageId").val() != $(this).attr("id")) {
                        if ($(".pagename").val() != "CollectionPage") {
                            $(this).parents(".subnav-thumb").css({ backgroundColor: '#f6f4f3', opacity: '1.0', filter: 'alpha(opacity=100)' });
                        } else {
                            $(this).css({ backgroundColor: '#f6f4f3', opacity: '1.0', filter: 'alpha(opacity=100)' });
                        }
                    }
                },
                function() {
                    if ($(".selectedImageId").val() != $(this).attr("id")) {
                        if ($(".pagename").val() != "CollectionPage") {
                            $(this).parents(".subnav-thumb").css({ backgroundColor: '#fff', opacity: '0.4', filter: 'alpha(opacity=40)' });
                        } else {
                            $(this).css({ backgroundColor: '#fff', opacity: '0.4', filter: 'alpha(opacity=40)' });
                        }
                    }
                }
            );
        }

        $(function() {
            $("div#makeMeScrollable1").smoothDivScroll({ scrollingSpeed: 12, mouseDownSpeedBooster: 1, autoScrollDirection: "endlessloop", autoScrollSpeed: 1, visibleHotSpots: "always" });
        });
    }
});

