﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

$(function()
{
    cmp__slideShow();
});

function cmp__slideShow()
{
    var baseClassId = "campbells";
    $('.' + baseClassId + 'SlideShow').each(
    function(i)
    {
        var PageTitle = document.title.replace(/\W/g, "_");
        var navTop = $(this).attr('topNav');
        var navLeft = $(this).attr('leftNav');
        var configFile = $(this).attr('configFile');
        var directory = $(this).attr('panelDir');
        var delayTime = $(this).attr('delay');
        var positionBlock = $(this).attr('blockColor');
        var positionBlockCurrent = $(this).attr('currentColor');
        var startDate = $(this).attr('StartDate');
        var ss = $(this);
        var imageDir = $(this).attr('imageDir');
        var navigateFlag = true;
        var movingOffPage = false;
        $(window).bind("beforeunload", function()
        {
            navigateFlag = false;
        });
        var fadeNext = function()
        {
            if (navigateFlag)
                moveNext();
        }

        var moveNext = function()
        {
            var currentVisible = ss.children('div .' + baseClassId + 'SlideShowSub:visible');
            var nextVisible = currentVisible.next();
            if (!nextVisible.is('.' + baseClassId + 'SlideShowSub'))
                nextVisible = ss.children('div .' + baseClassId + 'SlideShowSub:first');
            ss.children('div .' + baseClassId + 'SlideShowSub').each(function(ssi)
            {
                if (this == nextVisible[0])
                    ss.find("." + baseClassId + "SlideShowPosition:eq(" + ssi + ")").click();
            });
        }

        var movePrev = function()
        {
            var currentVisible = ss.children('div .' + baseClassId + 'SlideShowSub:visible');
            var prevVisible = currentVisible.prev();
            if (!prevVisible.is('.' + baseClassId + 'SlideShowSub'))
                prevVisible = ss.children('div .' + baseClassId + 'SlideShowSub:last');
            ss.children('div .' + baseClassId + 'SlideShowSub').each(function(ssi)
            {
                if (this == prevVisible[0])
                    ss.find("." + baseClassId + "SlideShowPosition:eq(" + ssi + ")").click();
            });
        }

        var fadeThis = function(e)
        {
            var currentVisible = ss.children('div .' + baseClassId + 'SlideShowSub:visible');
            var nextVisible = ss.children('div .' + baseClassId + 'SlideShowSub:eq(' + e.data.index + ')');
            nextVisible.fadeIn('show');
            currentVisible.fadeOut('slow');
            ss.find("." + baseClassId + "SlideShowPosition").attr('style', 'cursor:pointer;width:15px;background-color:' + positionBlock);
            ss.find("." + baseClassId + "SlideShowPosition:eq(" + e.data.index + ")").attr('style', 'cursor:pointer;width:15px;background-color:' + positionBlockCurrent);
        }

        $.getJSON(directory + '/' + configFile, function(json)
        {
            var htmlString = "";
            ss.attr("style", "position:relative");
            $.each(json, function(index, entry)
            {
                jQuery.ajax({ url: directory + '/' + entry['url'], success: function(html)
                {
                    try
                    {
                        if (DD_belatedPNG != undefined)
                        {
                            html = html.replace(/background\-image:\s*url\(/g, "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale', src=");
                            html = html.replace(/BACKGROUND\-IMAGE:\s*url\(/g, "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale', src=");
                            html = html.replace(/opacity:\s*\d*\.*(\d+);/, "filter=alpha(opacity=$1);")
                        }
                    } catch (ex) { }

                    htmlString += '<div class="' + baseClassId + 'SlideShowSub" title="' + entry['title'] + '" style="position: absolute; top: 0px; left: 0px; z-index:10">' + html + '</div>';
                    if (index == json.length - 1)
                    {
                        htmlString += '<br clear="all" />';
                        var tableString = '<table id="' + baseClassId + 'NavTable" border="0" cellspacing="0" cellpadding="0" style="position: absolute; top: ' + navTop + 'px; left: ' + navLeft + 'px; z-index:1000;"><tr style="height:15px"><td><img style="cursor:pointer;" src="' + imageDir + '/ico-move-previous.gif" /></td>';
                        for (i = 0; i < json.length; i++)
                        {
                            if (i == 0)
                                tableString += '<td style="width:7px"></td><td class="' + baseClassId + 'SlideShowPosition" style="cursor:pointer;width:15px;background-color:' + positionBlockCurrent + '"></td>';
                            else
                                tableString += '<td style="width:7px"></td><td class="' + baseClassId + 'SlideShowPosition" style="cursor:pointer;width:15px;background-color:' + positionBlock + '"></td>';
                        }
                        htmlString += tableString + '<td style="width:7px"></td><td><img style="cursor:pointer;" src="' + imageDir + '/ico-move-next.gif" /></td></tr></table>';
                        ss.append(htmlString);
                        ss.children('div .' + baseClassId + 'SlideShowSub').hide();
                        ss.children('div .' + baseClassId + 'SlideShowSub:first').show();

                        ss.find("." + baseClassId + "SlideShowPosition").each(function(ssIndex, ss)
                        {
                            $(this).bind("click", { index: ssIndex }, fadeThis);
                        });

                        ss.find("#" + baseClassId + "NavTable img:first").click(movePrev);
                        ss.find("#" + baseClassId + "NavTable img:last").click(moveNext);

                        ss.find("#" + baseClassId + "NavTable").hover(function() { navigateFlag = false; }, function() { navigateFlag = true; });

                        if (json.length > 1)
                            setInterval(fadeNext, delayTime);
                        // Add Omniture tagging
                        $.expr[':'].external = function(obj)
                        {
                            return !obj.href.match(/^mailto\:/) && !obj.href.match(/^javascript\:/)
                                && (obj.hostname != location.hostname);
                        }

                        $.expr[':'].internal = function(obj)
                        {
                            return !obj.href.match(/^mailto\:/) && !obj.href.match(/^javascript\:/)
                                && (obj.hostname == location.hostname);
                        }

                        $('.' + baseClassId + 'SlideShowSub').each(function(i)
                        {
                            var panelTitle = $(this).attr('title');
                            var panelTitle = panelTitle.replace(/\W/g, "_");

                            $(this).find('a:internal').each(function(i)
                            {
                                var href = $(this).attr("href");
                                var linkText = $(this).text();
                                linkText = linkText.replace(/\W/g, "_");


                                if (href.indexOf("?") > -1)
                                    href += "&INTCID=" + panelTitle + "|" + linkText;
                                else
                                    href += "?INTCID=" + panelTitle + "|" + linkText;
                                $(this).attr("href", href);
                            });


                            $(this).find('a:external').each(function(i)
                            {
                                var href = $(this).attr("href");
                                var linkText = $(this).text();
                                linkText = linkText.replace(/\W/g, "_");

                                var xaxs = function(obj, ihref)
                                {
                                    if (s_omtr.eVar1) var temp = s_omtr.eVar1;
                                    var s = s_gi(s_account);
                                    s.linkTrackVars = 'eVar1';
                                    s.eVar1 = panelTitle + "|" + linkText;
                                    s.tl(obj, 'o', 'internal Click');
                                    if (temp) s_omtr.eVar1 = temp;
                                }

                                $(this).click(function() { if ((screen != null) && (typeof (s_omtr) != 'undefined')) xaxs(this, href); });
                            });


                        });



                        $(".overlay").mouseover(function() { navigateFlag = false; }).mouseout(function() { if (!movingOffPage) navigateFlag = true; })

                    }
                }, async: false
                });
            });

        });
    });
};


