String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}

$(document).ready( function(){
    try {document.execCommand("BackgroundImageCache",false,true); }
    catch (e) {}
    
    if (Editor()) Editor_Load();
    else 
    {
        ConfigureGrid();
        
        if ($.browser.msie) {
            $(".ContentPart img").each(
                function () { 
                    if (this.src.toLowerCase().indexOf(".png") > -1)
                    {
                        this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src +"', sizingMethod='image')"
                        this.src = "/TeamHilgersom.Website/Gfx/pic_blank.gif";
                    }
                }
            )
        }
    }
});

function GetGalleryIndex(id) 
{
    var tmp_iIndex; 
    var tmp_sCurrent = $("#" + id).css("background-image");
    $.each( $("#" + id).attr("images").split(","), function (index) { if (tmp_sCurrent.indexOf(this) > -1) { tmp_iIndex = index; return false; } } );
    return tmp_iIndex;
}

function SetGalleryIndex(id, index)
{
    var tmp_oGallery = $("#" + id);
    var tmp_oImages = tmp_oGallery.attr("images").split(",");
    if (index < 0) index = tmp_oImages.length - 1;
    else if (index >= tmp_oImages.length) index = 0;
    
    var tmp_sDescription = tmp_oGallery.attr("description" + index);
    if (tmp_sDescription == null) tmp_sDescription = "";
    
    tmp_oGallery.css("background-image","url(" + tmp_oImages[index] + ")");
    $("#" + id + "[showNumbers='True'] .GalleryPartDescription").text("Afbeelding " + (index + 1) + " van " + tmp_oImages.length + (tmp_sDescription == "" ? "" : ": " + tmp_sDescription));
    $("#" + id + "[showNumbers='False'] .GalleryPartDescription").each(function ( ) { $(this).text(tmp_sDescription); if (tmp_sDescription == "") $(this).hide(); else $(this).show(); } );
}

function GalleryNext(galleryIndex)
{
    $(".GalleryPart").each( function (index) { if (galleryIndex == null || index == galleryIndex) SetGalleryIndex(this.id, GetGalleryIndex(this.id) + 1); });
}
function GalleryPrevious(galleryIndex)
{
    $(".GalleryPart").each( function (index) { if (galleryIndex == null || index == galleryIndex) SetGalleryIndex(this.id, GetGalleryIndex(this.id) - 1); });
}

function Editor()
{
    return (typeof(Editor_Load) == "function");
}

function ConfigureGrid()
{
    $(".GridRow .PanelPart").each(
        function () { 
            if (parseInt($(this).attr("scope")) == 0) ConfigureGridPanel($(this), parseInt($(this).attr("rowSpan")), parseInt($(this).attr("colSpan")), $(this).attr("link"), $(this).attr("backgroundColor"), $(this).attr("hoverColor"));
            else ConfigureGridRow(parseInt($(this).parents(".GridRow").attr("index")), parseInt($(this).attr("rowSpan")), parseInt($(this).attr("colSpan")), $(this).attr("link"), $(this).attr("backgroundColor"), $(this).attr("hoverColor"));
        }
    );
}

function OpenUrl(url)
{
    if (url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("www."))
    {
        window.open(url);
    }
    else
    {
        document.location.href = url;
    }
}

function ConfigureGridPanel(panel, rowSpan, colSpan, url, backgroundColor, hoverColor)
{
    var tmp_oOverlay = document.createElement("DIV");
    $(tmp_oOverlay)
        .css("opacity", .2)
        .addClass("Overlay")
        .addClass(hoverColor)
        .css("height", ((115 * rowSpan) - 15) +"px")
        .css("width", ((185 * colSpan) - 15) + "px")
        .add(panel)
        .filter( function () { return url != null } ).click( function () { OpenUrl(url);return false; } ).addClass("OverlayLink");

    panel.before(tmp_oOverlay).add(tmp_oOverlay).filter ( function () { return hoverColor != null } ).hover(
              function () { $(tmp_oOverlay).show(); },
              function () { $(tmp_oOverlay).hide(); }
        );
}

function ConfigureGridRow(index, rowSpan, colSpan, url, backgroundColor, hoverColor)
{
    $("#GridRow_"+ index).addClass("GridRowSeparator");

    var tmp_oOverlay = document.createElement("DIV");
    $(tmp_oOverlay)
        .css("opacity", .2)
        .addClass("Overlay")
        .addClass(hoverColor)
        .css("height", ((115 * rowSpan) - 15) +"px")
        .css("width", ((185 * 5) - 15) + "px")
        .add( $(".GridRow").slice(index, index+rowSpan) )
        .filter( function () { return url != null } ).click( function () { OpenUrl(url);return false; } ).addClass("OverlayLink");

    $("#GridRow_"+ index).prepend(tmp_oOverlay);

    $(".GridRow").slice(index, index+rowSpan).filter ( function () { return hoverColor != null } ).hover(
              function () { $("#GridRow_" + index + " .Overlay").show(); },
              function () { $("#GridRow_" + index + " .Overlay").hide(); }
        );
    
}





