img1 = new Image();
img2 = new Image();
img1.src = "../images/bg.jpg";
img2.src = "../images/loading.gif";

function AnimationScripts() {
    var self = this;
    var width = 0;
    var commonWidth = 0, commonHeight = 0;

   if (document.images) {
                  img1 = new Image();
                  img2 = new Image();
                  img1.src = "../images/bg.jpg";
                  img2.src = "../images/loading.gif";
              }

    function main() {
        var newHeight, newWidth, newImageHeight;

        $("#images > div").each(function (i) {
            var div = $(this);
            var img = $(this).find("div img");
            var theImage = new Image();
            newHeight = $("#images").parents("#carousel").innerHeight();
            newWidth = $("#images").parents("#carousel").innerWidth();
            newImageHeight = newHeight - 100;

            theImage.src = img.attr("src");
            if (newImageHeight > theImage.height) {
                newImageHeight = theImage.height;
            }
            if (newWidth > theImage.width) {
                newWidth = theImage.width;
            }

            if (img.height() > img.width()) {
                h = newImageHeight;
                var w = Math.ceil(img.width() / img.height() * h);
                if (w > newWidth) {
                    var w = newWidth;
                    var h = Math.ceil(img.height() / img.width() * w);
                }
            } else {
                var w = newWidth;
                var newHeight = img.height();
                var h = Math.ceil(img.height() / img.width() * w);

                if (h > newImageHeight) {
                    var h = newImageHeight;
                    var w = Math.ceil(img.width() / img.height() * h);
                }
            }

            div.css({ height: h, width: w });
            div.css("visibility", "visible");
            img.css({ height: h, width: w });
        });

        newHeight = $("#images").parents("#carousel").innerHeight();
        newWidth = $("#images").parents("#carousel").innerWidth();
        $("#images").carouFredSel({
            height: newHeight,
            width: newWidth,
            align: "center",
            items: 1,
            scroll: {
                items: 1,
                duration: 700
            },
            direction: "up"
        }).width(newWidth);
    }
    function timelineGeneral() {
        if (getInternetExplorerVersion() < -1) {

            hideLoading(contentId);
        }
        else {
            hoverEvents('#background-slideshow, #timeline-container');
            // hoverEvents('#background-slideshow img, #timeline-container');
            var newWidth = $(this).attr("innerWidth");
            var itemsCount = 6;
            if (newWidth < 1200)
                itemsCount = 4;
            $("#timeline-container div.timeline-wrapper").carouFredSel({
                curcular: false,
                infinite: false,
                auto: false,
                align: "center",
                items: itemsCount,
                scroll: {
                    items: 1,
                    duration: 1500
                },
                prev: {
                    button: "#timeline_prev",
                    key: "left"
                },
                next: {
                    button: "#timeline_next",
                    key: "right"
                }
            });

            $("#images #background-slideshow div.background-wrapper").carouFredSel({
                curcular: false,
                infinite: false,
                auto: false,
                align: "center",
                items: 1,
                scroll: {
                    fx: "crossfade",
                    items: 1,
                    duration: 1500
                },
                prev: {
                    button: "#timeline_prev",
                    key: "left"
                },
                next: {
                    button: "#timeline_next",
                    key: "right"
                }
            });

            $("#timeline-container  div").css("visibility", "visible");
            hideLoading(contentId);
        }

    }

    function TimeLine() {
        if (window.location.hash == '#History') {
            $('#hover-point').remove();
        } 
        if (getInternetExplorerVersion() > -1) {
            $("#background-slideshow").css("display", "none");
            $("#timeline-container").css("display", "none");
            hideLoading(contentId);
        }
        else {
            var newWidth = $(this).attr("innerWidth");

            if (newWidth > 1200) {
                var newHeight = $("#images").parents("#carousel").innerHeight();

                $(".timeline-images").css("width", "960px");
                $(".timeline-images").css("height", "600px");
                $("#images #background-slideshow img").each(function () {
                    var newTop = (newHeight - $(this).height()) / 2;
                    $(this).css("margin-top", newTop);
                    $(this).css("margin-bottom", newTop);
                });

                var newTop = $("#images #background-slideshow img").offset().top + 257;
                $("#timeline-container").css("width", "852px");
                $("#timeline-container").css("height", "200px");
                $("#timeline-container").css("top", newTop);
            }
            else {
                $(".timeline-images").css("width", "676px");
                $(".timeline-images").css("height", "508px");
                var newHeight = $("#images").parents("#carousel").innerHeight();
                $("#images #background-slideshow img").each(function () {
                    var newTop = (newHeight - $(this).height()) / 2;
                    $(this).css("margin-top", newTop);
                    $(this).css("margin-bottom", newTop);
                });

                var newTop = $("#images #background-slideshow img").offset().top + 216;
                $("#timeline-container").css("width", "568px");
                $("#timeline-container").css("height", "200px");
                $("#timeline-container").css("top", newTop);
            }

            if ((newWidth > 1200 && width < 1200) || (newWidth < 1200 && width > 1200)) {
                timelineGeneral();
            }
            width = newWidth;
        }
    }
    function ajaxCarouselLoad() {

        $("#carousel").css("top", "0");
        $("#carousel").css("height", "100%");

        $('#images').waitForImages(function () {
            setTimeout(main, 1000);
            $(window).resize(function () {
                OnResize("main()");
            });
            hideLoading(contentId);
        });
        //hoverEvents('#images');
    
            hoverEvents('#hover-point');
      
        //hoverEvents('#images > div div');
    }

    function ajaxQuotesLoad() {
        if (document.all && !document.querySelector) {
            hideLoading(contentId);
        }
        else {
            $('#images #background-slideshow').waitForImages(function () {
                Quotes();
                $(window).resize(function () {
                    OnResize("Quotes()");
                });
                hideLoading(contentId);
            });
        }
    }

    function Quotes() {
        var newHeight = $("#images").parents("#carousel").innerHeight();
        var newWidth = $("#images").parents("#carousel").innerWidth();

        $("#quotes").carouFredSel({
            height: newHeight,
            align: "center",
            items: 1,
            scroll: {
                fx: "fade",
                items: 1,
                duration: 1500
            }
        });
        $("#quotes").css("height", newHeight);
        $("#images #background-slideshow").css("visibility", "visible");

        var left = $("#images #background-slideshow img").offset().left - $("#images").offset().left;
        $("#quotes .quote").each(function () {
            var quotesTop = (newHeight - $(this).height()) / 2;
            $(this).css("margin-top", quotesTop);
            $(this).css("margin-bottom", quotesTop);
            $(this).css("width", ($("#images #background-slideshow").find("img").width() - 80));
            $(this).css("margin-left", left);
        });

        hoverEvents('#images');

        var quotesTop = (newHeight - $("#images #background-slideshow").find("img").height()) / 2;
        $("#images #background-slideshow").css("margin-top", quotesTop);
        $("#quotes .quote").css("visibility", "visible");
    }

    this.NavigationMenu = function () {

        var count = $("#navigationSection .Level1 > span").length;
        var width = 100 / count;
        $("#navigationSection .Level1 > span").css("width", width + "%");
        if (document.all && !document.querySelector) {
            $("#navigationSection .Level1 > span").last().find("a").css("border", "none");
            $("#navigationSection .Level1 > span").last().css("width", (width - 1) + "%");
        }

        $("#navigationSection .Level1 > span").hover(function () {
            var subMenu = $(this).find("div");
            subMenu.width($(this).width())
            subMenu.fadeIn(); //css("display", "block");
            var offset = $(this).position();
            var width = Math.abs($(this).width() - subMenu.width()) / 2;
            var newLeft = offset.left - width;
            subMenu.css("left", newLeft);
            $(this).find("div .Level3").css("display", "none");
            var elementHeight = subMenu.height();
            subMenu.css("top", -elementHeight);
        },
            function () {
                $(this).find("div").fadeOut(); //.css("display", "none");
            });

        $("#navigationSection .Level2 span").hover(function () {
            var subMenu = $(this).find("div");
            //subMenu.css("display", "block");
            subMenu.fadeIn();
            var offset = $(this).position();
            var newLeft = offset.left - $(this).width() - 2;
            subMenu.css("left", newLeft);
            var elementHeight = offset.top - subMenu.height() + $(this).height();
            subMenu.css("top", elementHeight);
        },
        function () {
            $(this).find("div").fadeOut(); //css("display", "none");
        });

    }

    this.Initialize = function () {
        $(document).ready(function () {
            self.NavigationMenu();

            $("#leftBackground").css("opacity", "0.7");
            //$("#leftBackground").css("opacity", "1");
            $("#navigationSection").css("opacity", "1");

            $("#rightBackground").css("opacity", "0.5");
            //$("#rightBackground").css("opacity", "1");

            var flag = true;
            $("#right, #AgentsLink, #accordion h3, #accordion a, #accordion div, #navigationSection span a, .page, #navigationSection, #Content, #rightBackground, .pagination, .pagination ul, #paginable-content").mouseover(function () {
                //$("#right").mouseover(function () {
                var anchor = get_anchor_name()
                if ($("#rightBackground").css("opacity") != 0.92 && flag) {
                    $("#rightBackground").stop().animate({ opacity: 0.92 }, "slow");
                    if (anchor == "Home") {
                        //$("#icons-section").css("opacity", "1");
                        //$("#icons-section").stop().fadeIn();
                    }
                    else {
                        //$("#icons-section").css("display", "none");
                    }
                }

                $("#images").trigger("pause");

            });
            /*$("#right, #AgentsLink, #accordion h3, #accordion a, #accordion div, #navigationSection span a, .page, #navigationSection, #Content, #rightBackground, .pagination, .pagination ul, #paginable-content").mouseout(function () {
                //$("#right").mouseout(function () {
                var anchor = get_anchor_name()
                if ($("#rightBackground").css("opacity") != 0.5 && flag) {
                    $("#rightBackground").stop().animate({ opacity: 0.5 }, "slow");
                    if (anchor == "Home") {
                        // $("#icons-section").stop().fadeOut();
                    }
                    else {
                        $("#icons-section").css("display", "none");
                    }
                }

                
            });*/
            $("#right").hover(function () {
                $("#rightBackground").stop().animate({ opacity: 0.92 }, "slow");
            },
            function () {
                $("#rightBackground").stop().animate({ opacity: 0.5 }, "slow");
                $("#images").trigger("play", true);
            }
            );

            $("#navigationSection").hover(function () {
                var anchor = get_anchor_name()
                flag = false;
                //$("#rightBackground").stop().animate({ opacity: 0.5 }, "slow");
                if (anchor == "Home") {
                    //$("#icons-section").stop().fadeOut();
                }
                else {
                    //$("#icons-section").css("display", "none");
                }
            },
            function () {
                flag = true;
                return false;
            });

            click_menu_item();

            //self.ExecuteComplex("ajaxCarouselLoad()");
        });
    }

    function MakeTrailersInCenter() {
        var anchor = get_anchor_name();
        if (anchor == "Trailers") {
            $(".page").css("text-align", "center");
        }
        else {
            $(".page").css("text-align", "left");
        }
        $(".page").css("width", "100%");
    }

    function OnResize() {
        var newWidth = $(this).attr("innerWidth");
        var newHeight = $(this).attr("innerHeight");
        if (document.all && !document.querySelector) {
            newWidth = $("html").width();
            newHeight = $("html").height();
        }

        if (newWidth > 1200 && newHeight > 760) {
            $("body").css("font-size", "16px");
            //$("#icons-section").css("margin-bottom", "68px");
            $(".icons-item a").find("div").css("display", "block");
        }
        else {
            $("body").css("font-size", "14px");
            //$("#icons-section").css("margin-bottom", "20px");
            $(".icons-item a").find("div.icon-caption").css("display", "none");
        }

        ResizeContent();
        pager();
        makeYoutubeLinks();
        for (var i = 0; i < arguments.length; i++) {
            eval(arguments[i]);
        }
    }

    this.ComplexExecute = function (funcName) {
        $(window).unbind("resize");
        $(window).resize(function () {
            if (funcName == "TimeLine()") {
                var newWidth = $(this).attr("innerWidth");
                if (newWidth > 1200) {
                    width = 1000;
                }
                else {
                    width = 1300;
                }
                OnResize(funcName);
            }
            else {
                OnResize();
            }
        }).resize();

        makeYoutubeLinks();
        eval(funcName);
        ResizeContent();
        pager();
        MakeTrailersInCenter();
        setTimeout(function () { hideLoading(contentId); }, 10000);

    }

    this.SimpleExecute = function (funcName) {
        eval(funcName);
        setTimeout(function () { hideLoading(contentId); }, 10000);

        $("#accordion").accordion(
        { 
            autoHeight: false,
            collapsible: false,
            active: ".active"
        });

        $(".widget-title").css("opacity", "0.6");
        $(".res-link a").css("opacity", "0.85");
        $("#accordion div a").css("opacity", "0.6");
        $(".titles a.TitlesLink").css("opacity", "0.6");
    }


    /*----------------------------------Helper Functions----------------------------------*/
    function pager() {
        $("#Content").ready(function () {
            var newHeight = $("#rightBackground").innerHeight()
                - 100
                - parseInt($("#Content").css("padding-top"), 10)
                - parseInt($("#Content").css("padding-bottom"), 10)
                - parseInt($("#Content h2").css("margin-top"), 10)
                - parseInt($("#Content h2").css("margin-bottom"), 10)
                - $("#Content h2").innerHeight()
                - parseInt($("#Content .hor-line").css("margin-top"), 10)
                - parseInt($("#Content .hor-line").css("margin-bottom"), 10)
                - $("#Content .hor-line").innerHeight();

            var anchor = get_anchor_name();
            if (anchor == "Resources") {
                newHeight = 100;
            }
            if (anchor == "Quotes") {
                $('#left').stop().animate({ left: '-30%' }, { duration: 2200, method: 'easeInExpo' });
                $('#right').stop().animate({ right: '-40%' }, { duration: 2200, method: "easeInExpo" });
            }
            if (anchor == "Home") {
                if (!($.browser.msie && $.browser.version == 8)) {
                    $("#icons-section").css("opacity", "1");
                    $("#icons-section").stop().fadeIn();
                }
            }
            else {
                $("#icons-section").css("display", "none");
            }

            if ($.browser.mozilla && navigator.platform.indexOf('Mac')) {
                newHeight = newHeight - 50;
            }

            $('.pagination').css("margin-top", newHeight - 50);
            $("#paginable-content").MyPagination({ height: newHeight, fadeSpeed: 400 });
            if ($(".pagination ul li").size() > 3) {
                $(".pagination ul").css("display", "inline-block");
                if (document.all && !document.querySelector) {
                    $(".pagination ul").css("display", "inline");
                    $(".pagination ul").css("zoom", "1");
                }
            }

            var iconsMarginBottom = (newHeight + 100 - $(".page").height() - $("#icons-section").height()) / 2;
            if (iconsMarginBottom > 68) {
                $("#icons-section").css("margin-bottom", iconsMarginBottom + "px");
            }

        });
    }

    function makeYoutubeLinks() {

        $("#accordion").accordion(
        { 
            autoHeight: false,
            collapsible: false,
            active: ".active"
        });
        $("#Content hr").css("opacity", "0.4");
        $(".widget-title").css("opacity", "0.6");
        $(".res-link a").css("opacity", "0.85");
        $("#accordion div a").css("opacity", "0.6");
        $(".titles a.TitlesLink").css("opacity", "0.6");

				$("#accordion .accordion-content").each(function(index) {
					if ( $(this).height() > 300 ) $(this).height(300);
				});

				//$("#accordion .accordion-content").jScrollPane();
				$("#submissions-guid").jScrollPane();

        $(".youtube-gallery-item >  a").each(function () {
            href = $(this).attr('href');
            var img_url = $.jYoutube(href, 'small');
            $(this).find("img").attr("src", img_url);
        });
        
        fancyBox();

        /*$(".timeline-quotes").carouFredSel({
        height: "200px",
        width: "200px",
        align: "center",
        items: 1,
        scroll: {
        fx: "fade",
        items: 1,
        duration: 700
        }
        });*/

        $(".timeline-quotes").cycle({
            fx: 'fade',
            speed: 2500
        });

        $(".quot").fancybox({
            'titlePosition': 'inside',
            'transitionIn': 'none',
            'transitionOut': 'none'
        });


        $("#contemporary-author-sites-pane").jScrollPane();
    }

    function hoverEvents(selector) {
        $(selector).hover(
			    function () {
			        $('#left').stop().animate({ left: '-30%' }, { duration: 2200, method: 'easeInExpo' });
			        $('#right').stop().animate({ right: '-40%' }, { duration: 2200, method: "easeInExpo" });
			        //$("#rightBackground").stop().animate({ opacity: 0.5 }, "slow");
			    },
			    function () {
			        $('#left').stop().animate({ left: '1%' }, { duration: 800, method: 'easeInOutQuad' });
			        $('#right').stop().animate({ right: '1%' }, { duration: 800, method: 'easeInOutQuad' });
			        //$("#rightBackground").stop().animate({ opacity: 0.92 }, "slow");
			    });
			}

	

    function getInternetExplorerVersion() {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
    }

    function get_anchor_name() {
        var url = document.location;
        var strippedUrl = url.toString().split("#");
        if (strippedUrl.length > 1) {
            return strippedUrl[1];
        }
        else {
            return "##none##";
        }
    }

    function click_menu_item() {
        var anchor = get_anchor_name();
        if (anchor != "##none##") {
            $("#navigationSection a").each(function () {
                var text = $(this).text();
                if (text == anchor) {
                    eval($(this).attr("href"));
                }
            });
        }
        else {
            eval($("#navigationSection a:first").click().attr("href"));
        }
    }

    function ResizeContent() {
        var newHeight = $("#rightBackground").height() - 50 - parseInt($("#Content").css("padding-top"), 10) - parseInt($("#Content").css("padding-bottom"), 10);
        $("#Content").height(newHeight);

        newHeight = newHeight - 124
                - parseInt($("#Content").css("padding-top"), 10)
                - parseInt($("#Content").css("padding-bottom"), 10)
                - parseInt($("#Content > h2").css("margin-top"), 10)
                - parseInt($("#Content > h2").css("margin-bottom"), 10)
                - $("#Content > h2").height()
                - parseInt($("#Content .hor-line").css("margin-top"), 10)
                - parseInt($("#Content .hor-line").css("margin-bottom"), 10)
                - $("#Content .hor-line").height();
        $("#submissions-guid").height(newHeight);
        $("#contemporary-author-sites-pane").height(newHeight);
    }

    function fancyBox() {
        $(".video").click(function () {
            $.fancybox({
                'padding': 0,
                'autoScale': false,
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'speedIn': 600,
                'speedOut': 200,
                'title': this.title,
                'width': 640,
                'height': 385,
                'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                'type': 'swf',
                'swf': {
                    'wmode': 'transparent',
                    'allowfullscreen': 'true'
                },
                onClosed: function () {
                    $("#images").trigger("play", true);
                },
                onComplete: function () {
                    $("#images").trigger("pause");
                }
            });

            return false;
        });
    }
    /*--------------------------------------------------------------------*/
}

