﻿//if (navigator.platform.indexOf("iPad") != -1) {
//    window.onload = function () { window.onload(scroll(0, document.body.scrollHeight)); }
//}

$(document).ready(function () {
    $('a.close, #dialog-overlay').click(function () {
        $('#dialog-overlay, #dialog-box').hide();
        return false;
    });
    $('#popup-overlay').click(function () {
        $('#popup-overlay, #popup-box').hide();
        return false;
    });

    $(window).resize(function () {
        if (!$('#dialog-box').is(':hidden')) dialog();
        //if (!$('#popup-box').is(':hidden')) popup();
    });

    $(document).keyup(function (e) {
        if (e.keyCode == 27) { $('#dialog-overlay, #popup-overlay, #popup-box, #dialog-box').hide(); }
    });
});

function popup(id, pozicija) {
    //if (!$('#popup-box').is(':hidden')) {
    //    $('#popup-overlay').hide();
    //    $('#popup-box').hide();
    //}
    //else {
        var documentHeight = $(document).height();
        var documentWidth = $(document).width();
        var windowHeight = $(window).height();
        var windowWidth = $(window).width();

        if (id != null) {
            $('#popup-box .dialog-content>ul').hide();
            $('#'+id).show();
        }

        var obj = $('#' + pozicija);
        var objPosition = obj.position();

        var objParent = obj.parent();
        var objParentPosition = objParent.position();

        dialogTop = objParentPosition.top + objPosition.top + (obj.height() / 1.5);
        dialogLeft = objParentPosition.left + objPosition.left + (obj.width() / 1.5);

        if ((dialogTop + $('#popup-box').height()) > windowHeight) {
            dialogTop = objParentPosition.top + objPosition.top + (obj.height() / 3.5) - $('#popup-box').height();
        }
        if ((dialogLeft + $('#popup-box').width()) > windowWidth) {
            dialogLeft = objParentPosition.left + objPosition.left + (obj.width() / 3.5) - $('#popup-box').width();
        }
        $('#popup-overlay').css({ height: documentHeight, width: '100%' }).show();
        $('#popup-box').css({ top: dialogTop, left: dialogLeft }).show();
    //}
}

function dialog(id) {
    var documentHeight = $(document).height();
    var documentWidth = $(document).width();
    var windowHeight = $(window).height();
    var windowWidth = $(window).width();

    if (id != null) {
        $('#dialog-box .dialog-content>div').hide();
        $('#' + id).show();
    }

    var dialogTop = (windowHeight / 2) - ($('#dialog-box').height() / 2);
    if (dialogTop < 10) { dialogTop = 10; }
    var dialogLeft = (windowWidth / 2) - ($('#dialog-box').width() / 2);

    $('#dialog-overlay').css({ height: documentHeight, width: '100%' }).show();
    $('#dialog-box').css({ top: dialogTop, left: dialogLeft }).show();
}
