﻿var slideshow = {

    rotationSpeed: 7000,
    transitionSpeed: 1000,

    preLoadSlide: function(index, loaddirectly) {
        if ($("#i" + lrgIds[index]).length) {
        } else {
            var img = $("<img/>").attr("src", lrgImgs[index]).attr("id", "i" + lrgIds[index]).css("display", "none").appendTo(".tp-imgs");
            if (loaddirectly) {
                $("#i" + lrgIds[index]).load(function() {
                    slideshow.rotateSlides(index);
                });
            }
        }
    },

    rotateSlides: function(index) {
        if ($("#i" + lrgIds[index]).length) {
            $(".tp-imgs img:not(#i" + lrgIds[index] + ")").fadeOut(slideshow.transitionSpeed);
            $(".tp-captions p").css("display", "none");
            $(".tp-captions p#c" + lrgIds[index]).css("display", "block");
            $(".tp-nav img").removeClass("active");
            $(".tp-nav img#t" + lrgIds[index]).addClass("active");

            if (index % 6 === 0) {
                currentpage = Math.round(((index + 1) / 6) + 0.499999) - 1;
                slideshow.goToPage(currentpage);
            }

            $("#i" + lrgIds[index]).fadeIn();
            currentslide = 0;
            slideindex = index;
            if (index < slidecount - 1) {
                currentslide = index + 1;
            }
            slideshow.preLoadSlide(currentslide);
            rt = setTimeout("slideshow.rotateSlides(" + currentslide + ")", slideshow.rotationSpeed);
        } else {
            slideshow.preLoadSlide(index, true);
        }
    },

    goToPage: function(page) {
        $(".tp-nav").animate({ left: -(page * 336) + "px" });
    },

    initializeNav: function() {
        if (slidecount > 6) {
            var prev = $("<span/>").addClass("prev").append("Previous").appendTo(".tp-nav-wrapper");
            var next = $("<span/>").addClass("next").append("Next").appendTo(".tp-nav-wrapper");
            prev.click(function() {
                if (currentpage > 0) {
                    currentpage = currentpage - 1;
                    slideshow.goToPage(currentpage);
                }
            });
            next.click(function() {
                if (currentpage < totalpages) {
                    currentpage = currentpage + 1;
                    slideshow.goToPage(currentpage);
                }
            });
        }
        $(".tp-nav img").click(function() {
            clearTimeout(rt);
            slideshow.rotateSlides($(".tp-nav img").index(this));

        });
    },

    initialize: function() {
        var i = 0;
        var imgs;
        var ids;

        // Start replace image src code
        // Added as a temporary solution to remove the call to http://cdnmc.taylorpond.com
        /*$(".tp-img-rotator img").each(function() {
        this.src = this.src.replace("http://cdnmc.taylorpond.com", "");
        });*/
        // End replace image src code

        $(".tp-nav img").each(function() {
            if (i === 0) {
                $(this).addClass("active");
                imgs = this.src.replace("thumbs", "large");
                ids = this.id.replace("t", "");
            } else {
                imgs += "~" + this.src.replace("thumbs", "large");
                ids += "~" + this.id.replace("t", "");
            }
            i++;
        });
        lrgImgs = imgs.split("~");
        lrgIds = ids.split("~");
        $(".tp-captions p#c" + lrgIds[0]).css("display", "block");
        slidecount = i;
        slideindex = 0;
        currentpage = 0;
        totalpages = (Math.round((slidecount / 6) + 0.499999)) - 1;
        $(".tp-nav").css("width", (56 * slidecount) + "px");
        if (slidecount > 1) {
            slideshow.initializeNav();
            slideshow.preLoadSlide(1);
            rt = setTimeout("slideshow.rotateSlides(1)", slideshow.rotationSpeed);
        }
    }

}

function saveOrder() {
    //var sto = setTimeout("$('#ctl00_ctl00_loading').css('display', 'block')", 1000);
    $.ajax({
        type: "GET",
        url: document.URL,
        data: "saveorder=" + $("#sortable").sortable('toArray'),
        success: function(msg) {
            if (msg !== "success") {
                $("#images-error").fadeIn();
            } else {
                $("#images-error").fadeOut();
            }
        }
    });
}
$(document).ready(function() {
    if ($(".tp-img-rotator").length) {
        slideshow.initialize();
    }
    if ($("#sortable").length) {
        $("#sortable").sortable({
            update: function(event, ui) {
                saveOrder();
            },
            placeholder: 'highlight'
        });
    }
});
$("#nav ul li a:not(#nav ul li ul li a)").FontEffect({
    outline: true,
    outlineColor1: "#507F3B",
    outlineColor2: "#507F3B",
    outlineWeight: 1
});

function openLSWindowTandR(year, wkno, lang, tabno, eventid, ref_file, width, height, hasScrollBars) {
    // ADD NAME FIELD and make sure it get's focus!!!
    var theWidth = width;
    var theHeight = height;
    var scrollBars = "scrollbars";
    if (hasScrollBars == false) scrollBars = "scrollbars=0";
    if ((theWidth == "") || (theWidth == null)) theWidth = 500;
    if ((theHeight == "") || (theHeight == null)) theHeight = 668;
    var theLeft = (screen.availWidth - theWidth) / 2;
    var theTop = (screen.availHeight - theHeight) / 2;
    var strCheckRef = escape(ref_file);

    var lsURL = "http://www.protennislive.com/frameset.asp?year=" + year + "&wkno=" + wkno + "&lang=" + lang + "&tabno=" + tabno + "&eventid=" + eventid + "&ref=" + strCheckRef;
    var popupWin = window.open(lsURL, '_' + Math.round(Math.random() * 1000000), 'top=' + theTop + ',left=' + theLeft + ',menubar=0,toolbar=0,location=0,directories=0,status=0,' + scrollBars + ',width=' + theWidth + ', height=' + theHeight);
}

$("a[href$='/ScheduleResults/LiveScoring.aspx'], .livescoring").click(function() {
    openLSWindowTandR(2010, '31', 'en', 2, '0408', 'www.mercuryinsuranceopen.com/');
    return false;
});
