﻿initMainMenu = function () {
    pages = new Array();
    pages.push(new Page("index.htm", "Home"));
    pages.push(new Page("sc_president.htm", "President's Corner"));
    pages.push(new Page("sc_activities.htm", "Activities"));
    pages.push(new Page("sc_gallery.htm", "Photo Gallery"));
    pages.push(new Page("sc_videos.htm", "Video Gallery"));
    pages.push(new Page("sc_postillion.htm", "Postillion"));
    pages.push(new Page("sc_jass.htm", "Jass Egge"));
    pages.push(new Page("sc_otherclubs.htm", "Swiss Organizations"));
    pages.push(new Page("sc_sponsors.htm", "Sponsors"));
    pages.push(new Page("sc_links.htm", "Links"));
    pages.push(new Page("sc_membership.htm", "Membership"));
    pages.push(new Page("sc_contact.htm", "Contact Us"));
}

Page = function (href, title) {
    this.href = href;
    this.title = title;
};


var bCaptionOn;

$(document).ready(function () {
    if ($("#leftNavContainer").length > 0) {
        initMainMenu();
        buildMainMenu(getActivePage());
    }

    if ($("#postillionMenuContainer").length > 0) {
        buildPostillionMenu();
        $("#PostillionLinks_2012").show(); /*********** manual change **********/

    }

    if ($("#presidentMenuContainer").length > 0) {
        buildPresidentMenu();
        $("#PresidentLinks_Message").show(); /*********** manual change **********/

    }

    if ($("#jassMenuContainer").length > 0) {
        buildJassMenu();
        $("#jassLinks_Championships_2011").show(); /*********** manual change **********/
        initJassScores2011("score2Best");
    }


    if ($("#galleryMenuContainer").length > 0) {
        buildGalleryMenu();
    }
    if ($("#galleryLinksContainer").length > 0) {
        initGalleryLinks();
        buildGalleryLinks(galleryMenuItems[0]);
        bCaptionOn = false;
    }

    if ($("#videoMenuContainer").length > 0) {
        buildVideoMenu();
    }
    if ($("#videoLinksContainer").length > 0) {
        initVideoLinks();
        buildVideoLinks(videoMenuItems[0]);
    }


    if ($("#imageViewer").length > 0) {
        $("#ivBtnReturnGallery").click(function () {
            $("#imageViewer").hide();
            $("#gallerySelector").show();
        });

        $("#ivBtnFullSize").click(function () {
            var url = "http://www.swissclubdc.org/" + $("#ivMainImg").attr("src").replace("Viewer_", "");
            window.open(url, "", "width=800,height=600,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=0");
        });

        $("#ivBtnImgCaption").click(function (e) {
            if (!bCaptionOn) {
                $("#ivMainCaption").slideDown("slow");
                $(this).text("Hide Caption");
                bCaptionOn = true;
            }
            else {
                $("#ivMainCaption").slideUp("slow");
                $(this).text("Show Caption");
                bCaptionOn = false;
            }
            e.preventDefault();
        });

        $("#ivMainImg").click(function (e) {
            showNextImage();
            e.preventDefault();
        });

        $("#ivBtnNext").click(function (e) {
            showNextImage();
            e.preventDefault();
        });

        $("#ivBtnPrevious").click(function (e) {
            showPreviousImage();
            e.preventDefault();
        });

    }
});


/**************************************************/

buildMainMenu = function (activePage) {
    var txt = [], j = 0;
    $.each(pages, function (i) {
        txt[j++] = "<div class='leftNavItem'><a href='" + pages[i].href + "'";
        if (pages[i].href == activePage) txt[j++] = " class='active'";
        txt[j++] = ">" + pages[i].title + "</a></div>";
    });
    $("#leftNavContainer").append(txt.join(""));
}

buildPostillionMenu = function () {
    var txt = [], j = 0;
    $.each(postillionMenuItems, function (i) {
        var myClass = "postillionMenuItem";
        if (i == 0) { myClass += " active"; }
        txt[j++] = "<a href='#' class='" + myClass + "'>";
        txt[j++] = postillionMenuItems[i];
        txt[j++] = "</a>";
    });
    $("#postillionMenuContainer").append(txt.join(""));

    $(".postillionMenuItem").click(function () {
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        $("#postillionLinksContainer").children("div").hide();
        $("#PostillionLinks_" + $(this).text()).show();
    });
}

buildPresidentMenu = function () {
    var txt = [], j = 0;
    $.each(presidentMenuItems, function (i) {
        var myClass = "presidentMenuItem";
        if (i == 0) { myClass += " active"; }
        txt[j++] = "<a href='#' class='" + myClass + "'>";
        txt[j++] = presidentMenuItems[i].replace("_", " ");
        txt[j++] = "</a>";
    });
    $("#presidentMenuContainer").append(txt.join(""));

    $(".presidentMenuItem").click(function () {
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        $("#presidentLinksContainer").children("div").hide();
        $("#PresidentLinks_" + $(this).text().replace(" ", "_")).show();
    });
}


buildJassMenu = function () {
    var txt = [], j = 0;
    $.each(jassMenuItems, function (i) {
        var myClass = "jassMenuItem";
        if (i == 0) { myClass += " active"; }
        txt[j++] = "<a href='#' class='" + myClass + "'>";
        txt[j++] = jassMenuItems[i].replace("_"," ");
        txt[j++] = "</a>";
    });
    $("#jassMenuContainer").append(txt.join(""));

    $(".jassMenuItem").click(function () {
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        $("#jassLinksContainer").children("div").hide();
        $("#jassLinks_" + $(this).text().replace(" ", "_")).show();
        if ($(this).text().indexOf("2011") != -1) initJassScores2011("score2Best");
        else if ($(this).text().indexOf("10") != -1) initJassScores2010("score2Best");
        else if ($(this).text().indexOf("09") != -1) initJassScores2009("score2Best");
        else if ($(this).text().indexOf("08") != -1) initJassScores2008("score2Best");
        else if ($(this).text().indexOf("07") != -1) initJassScores2007("score2Best");
        else if ($(this).text().indexOf("06") != -1) initJassScores2006("score2Best");
    });
}

buildGalleryMenu = function () {
    var txt = [], j = 0;
    $.each(galleryMenuItems, function (i) {
        var myClass = "galleryMenuItem";
        if (i == 0) { myClass += " active"; }
        txt[j++] = "<a href='#' class='" + myClass + "'>";
        txt[j++] = galleryMenuItems[i];
        txt[j++] = "</a>";
    });
    $("#galleryMenuContainer").append(txt.join(""));

    $(".galleryMenuItem").click(function () {
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        buildGalleryLinks($(this).text());
    });
}

buildGalleryLinks = function (selectedYear) {
    $("#galleryLinksContainer").text("");
    var txt = [], j = 0;
    $.each(galleryLinks, function (i) {
        if (galleryLinks[i].year == selectedYear) {
            txt[j++] = "<table class='galleryLink' cellpadding='0' cellspacing='0'>";
            txt[j++] = "<tr><td>";
            txt[j++] = "<a href='#' galleryID='" + galleryLinks[i].id + "' galleryPath='" + galleryLinks[i].path + "' hasCaptions='" + galleryLinks[i].hasCaptions + "' galleryTitle='" + galleryLinks[i].year + ": " + galleryLinks[i].title + "' class='galleryAnchor'>";
            txt[j++] = "<img src='" + galleryLinks[i].path + galleryLinks[i].src + "' alt=''/></a>";
            txt[j++] = "</td></tr><tr><td class='title'>";
            txt[j++] = "<a href='#' galleryID='" + galleryLinks[i].id + "' galleryPath='" + galleryLinks[i].path + "' hasCaptions='" + galleryLinks[i].hasCaptions + "' galleryTitle='" + galleryLinks[i].year + ": " + galleryLinks[i].title + "' class='galleryAnchor'>";
            txt[j++] = galleryLinks[i].title + "</a></td></tr>";
            txt[j++] = "<tr><td class='details'>";
            txt[j++] = galleryLinks[i].date + " [" + galleryLinks[i].imgCount + " images]";
            txt[j++] = "</td></tr></table>";
        }
    });
    $("#galleryLinksContainer").append(txt.join(""));

    $(".galleryAnchor").click(function (event) {
        $("#gallerySelector").hide();
        $("#imageViewer").show();
        selectedGalleryID = $(this).attr("galleryID");
        initImageViewer(selectedGalleryID);
        selectedPath = $(this).attr("galleryPath");
        selectedTitle = $(this).attr("galleryTitle");
        if ($(this).attr("hasCaptions") == "no") {
            $("#ivBtnImgCaption").hide();
            $("#ivMainCaption").hide();
        }
        else {
            $("#ivBtnImgCaption").show().text("Hide Caption");
            $("#ivMainCaption").show();
            bCaptionOn = true;
        }
        buildImageViewer(selectedPath, selectedTitle);
        $(".imageAnchor[thumbID='0']").click();
        $("#ivMainCaption").text(Pictures[0].title);
        $("#ivBtnPrevious").hide();
    });
}

buildVideoMenu = function () {
    var txt = [], j = 0;
    $.each(videoMenuItems, function (i) {
        var myClass = "galleryMenuItem";
        if (i == 0) { myClass += " active"; }
        txt[j++] = "<a href='#' class='" + myClass + "'>";
        txt[j++] = videoMenuItems[i];
        txt[j++] = "</a>";
    });
    $("#videoMenuContainer").append(txt.join(""));

    $(".videoMenuItem").click(function () {
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        buildVideoLinks($(this).text());
    });
}

buildVideoLinks = function (selectedYear) {
    $("#videoLinksContainer").text("");
    var txt = [], j = 0;
    $.each(videoLinks, function (i) {
        if (videoLinks[i].year == selectedYear) {
            txt[j++] = "<table class='galleryLink' cellpadding='0' cellspacing='0'>";
            txt[j++] = "<tr><td>";
            txt[j++] = "<a href='" + videoLinks[i].src + "' target='_blank' class='galleryAnchor'>";
            txt[j++] = "<img src='" + videoLinks[i].imgSrc + "' alt=''/></a>";
            txt[j++] = "</td></tr><tr><td class='title'>";
            txt[j++] = "<a href='" + videoLinks[i].src + "' target='_blank' class='galleryAnchor'>";
            txt[j++] = videoLinks[i].title + "</a></td></tr>";
            txt[j++] = "<tr><td class='details'>";
            txt[j++] = videoLinks[i].details;
            txt[j++] = "</td></tr></table>";
        }
    });
    $("#videoLinksContainer").append(txt.join(""));
}


buildImageViewer = function (path, title) {
    sPath = path;
    sTitle = title;
    iImgCt = Pictures.length;
    grpSz = 15;
    grpCt = parseInt(Math.ceil(iImgCt / grpSz));

    $("#ivThumbs").text("");
    var txt = [], j = 0;
    if (iImgCt > grpSz) {
        txt[j++] = "<div class='thumbsGroupSelectorDiv'>";
        for (var k = 0; k < grpCt; k++) {
            var myClass = (k == 0) ? "thumbsGroupSelector active" : "thumbsGroupSelector";
            txt[j++] = "<a href='#' groupID='" + k + "' class='" + myClass + "'>" + (k + 1) + "</a>"
        }
        txt[j++] = "</div>";
    }
    $.each(Pictures, function (i) {
        if (i % grpSz == 0) {
            var myClass = (i == 0) ? "thumbsGroup" : "thumbsGroup hidden";
            txt[j++] = "<div class='" + myClass + "' id='thumbsGroup" + parseInt(Math.floor(i / grpSz)) + "'>";
        }
        txt[j++] = "<table class='thumb' cellpadding='0' cellspacing='0'>";
        txt[j++] = "<tr>";
        var myClass = (i == 0) ? "thumbTD active" : "thumbTD";
        txt[j++] = "<td class='" + myClass + "'>";
        txt[j++] = "<a href='#' thumbID='" + i + "' class='imageAnchor'>";
        txt[j++] = "<img src='" + sPath + prefixThumb + Pictures[i].src + "' alt=''/></a>";
        txt[j++] = "</td></tr></table>";
        if (i % grpSz == (grpSz - 1) || i == (iImgCt - 1)) {
            txt[j++] = "</div>";
        }
    });
    $("#ivThumbs").append(txt.join(""));
    iImgGrp = 0;
    iImg = 0;

    $(".thumbsGroupSelector").click(function (e) {
        $("#thumbsGroup" + iImgGrp).hide();
        var iNewID = parseInt($(this).text()) - 1;
        $("#thumbsGroup" + iNewID).show();
        $(".thumbsGroupSelector").removeClass("active");
        $(this).addClass("active");
        iImgGrp = iNewID;
        e.preventDefault();
    });

    $(".imageAnchor").click(function (e) {
        $(".thumbTD").removeClass("active");
        $(this).parent().addClass("active");
        iImg = parseInt($(this).attr("thumbID"));
        if (iImg > 0) { $("#ivBtnPrevious").show(); }
        else { $("#ivBtnPrevious").hide(); }
        if (iImg >= (iImgCt - 1)) { $("#ivBtnNext").hide(); }
        else { $("#ivBtnNext").show(); }
        $("#ivMainImg").attr("src", sPath + prefixViewer + Pictures[iImg].src);
        $("#ivMainCaption").text(Pictures[iImg].title);
        //$("#ivTopTitle").text = "my title";
        showTitle(iImg);
        e.preventDefault();
    });
}

showTitle = function (i) {
    //document.getElementById("ivTopTitle").innerHTML = sTitle + ": Image " + (i + 1) + " of " + iImgCt;
    $("#ivTopTitle").html(sTitle);
    var sHTML = "Image " + (i + 1) + " of " + iImgCt;
    $("#imgCountDetails").html(sHTML)
}


showNextImage = function () {
    iImg++;
    if (iImg > 0) { $("#ivBtnPrevious").show(); }
    if (iImg >= (iImgCt - 1)) { $("#ivBtnNext").hide(); }
    if (iImg < iImgCt) {
        $("#ivMainImg").attr("src", sPath + prefixViewer + Pictures[iImg].src);
        $("#ivMainCaption").text(Pictures[iImg].title);
        $(".thumbTD").removeClass("active");
        $(".imageAnchor[thumbID='" + iImg + "']").parent().addClass("active");
        if (iImg % grpSz == 0) {
            var nextGrp = iImgGrp + 1;
            $(".thumbsGroupSelector[groupID='" + nextGrp + "']").click();
        }
        showTitle(iImg);
    }
}

showPreviousImage = function () {
    iImg--;
    if (iImg < 1) { $("#ivBtnPrevious").hide(); }
    if (iImg < (iImgCt - 1)) { $("#ivBtnNext").show(); }
    if (iImg != -1) {
        $("#ivMainImg").attr("src", sPath + prefixViewer + Pictures[iImg].src);
        $("#ivMainCaption").text(Pictures[iImg].title);
        $(".thumbTD").removeClass("active");
        $(".imageAnchor[thumbID='" + iImg + "']").parent().addClass("active");
        if (iImg % grpSz == grpSz - 1) {
            var prevGrp = iImgGrp - 1;
            $(".thumbsGroupSelector[groupID='" + prevGrp + "']").click();
        }
        showTitle(iImg);
    }
}


getActivePage = function () {
    try {
        var href = location.href ? document.location.href : document.location;
        if (href == "") return "index.htm";
        else return href.substring(href.lastIndexOf("/") + 1, href.length);
    } catch (e) { return ""; }
}

postillionMenuItems = new Array("2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", "2004");
jassMenuItems = new Array("Championships_2011","10","09","08","07","06","Rules","Scoring");
presidentMenuItems = new Array("Message", "Past_Presidents");



