﻿var resizeToHeight = window.screen.availHeight < 910 ? 630 : 900;
var resizeToWidth = window.screen.availWidth < 1050 ? 990 : 1200;
var size = isMobile ? "640x480" : (window.screen.availWidth < 1050 ? "800x600" : "1024x768");
var objWindow = null;
var objEventElm = null;
var refreshPage = true;
var galleryInitialized = false; // the gallery can only be initialized one time
var openInNewWindow = false;

function GetOpenInNewWindow() {
    if (window.localStorage) {
        var storage = window.localStorage;
        if (!storage.openInNewWindow)
            storage.openInNewWindow = false;
        openInNewWindow = storage.getItem("openInNewWindow").toString() == "true" ? true : false;
    }
    $("#OpenInNewWindowCheckbox").attr("checked", openInNewWindow);
    $("#OpenInNewWindowDiv").show();
}

function SetOpenInNewWindow() {
    openInNewWindow = $("#OpenInNewWindowCheckbox").attr("checked");
    if (window.localStorage) {
        var storage = window.localStorage;
        storage.openInNewWindow = openInNewWindow;
    }
}

$(document).ready(function() {
    if ($('#thumbs a').length > 0) {
        GetOpenInNewWindow();
        $('#thumbs a').attr('href', function() {
            return this.href.replace("/640x480/", "/" + size + "/");
        });
        SetThumbEvent();
    }
});

function SetThumbEvent() {
    $("#thumbs a").unbind("click");
    if (openInNewWindow) {
        $("#thumbs a").click(function() {
            return OpenImage($(this));
        });
    }
    else {
        if (galleryInitialized) {
            window.history.go(0); //reload
            return;
        }
        // select the thumbnails and make them trigger our overlay
        $("#thumbs a").overlay({
            // each trigger uses the same overlay with the id "gallery"
            target: '#gallery',
            // optional exposing effect
            expose: '#f1f1f1'
            // let the gallery plugin do its magic!
        }).gallery({ autohide: (window.screen.availWidth > 900), opacity: 0.7, speed: 'fast' });
        galleryInitialized = true;
    }
}

function OpenImage(elm) {
    // sizecheck
    if (window.screen.availWidth < resizeToWidth)
        resizeToWidth = window.screen.availWidth;
    if (window.screen.availHeight < resizeToHeight)
        resizeToHeight = window.screen.availHeight;

    // remove http and domain from href:
    var href = elm.attr("href") + "";
    href = ("https:" == document.location.protocol) ? href.substring(8) : href.substring(7);
    href = href.substring(href.indexOf("/"));
    var url = "mediashare_showimage.aspx?src=" + href + "&winh=" + resizeToHeight + "&winw=" + resizeToWidth + "&size=" + size;

    try {
        objEventElm = window.event.srcElement;
        if (objEventElm != null)
            objEventElm.style.cursor = "wait";
    }
    catch (e)
            { }

    objWindow = window.open(url, 'imgWindow', 'width=' + (resizeToWidth) + ',height=' + (resizeToHeight) + ',left=0,top=0,status=1,resizable=1,scrollbars=1')

    if (objEventElm != null)
        objEventElm.style.cursor = "hand";

    if (refreshPage)
        CheckForBrokenImages();

    return false; // prevent the link from loading
}

function CheckForBrokenImages() {
    for (var i = 0; i < document.getElementsByTagName("IMG").length; i++) {
        if (!document.getElementsByTagName("IMG")[i].complete) {
            document.getElementsByTagName("IMG")[i].outerHTML = document.getElementsByTagName("IMG")[i].outerHTML;
            //break;
        }
    }
    refreshPage = false; // Only check one time
}

function ShowStreaming(elm) {
    ResetMarkedStreaming();
    elm.className = "ms_sls ms_sls_selected";
}
