﻿$(document).ready(function () {

    // Detekce Calibri fontu
    font.setup();
    if (!font.isInstalled('Calibri')) $('h1').css('font-size', '145%');

    // Reklama
    $(window).scroll(function () {
        var winScrollTop = $(window).scrollTop();
        var markPos = $("#ScrollableMark").position({ scroll: false }).top;
        var footerPos = $("#Footer").position({ scroll: false }).top;
        var adDivHeight = $("#ScrollableDiv").outerHeight();
        if (winScrollTop > markPos) {
            var maxPos = footerPos - adDivHeight;
            if (winScrollTop < maxPos) {
                $("#ScrollableDiv").css("position", "fixed");
                $("#ScrollableDiv").css("top", "0");
            }
            else {
                $("#ScrollableDiv").css("position", "fixed");
                $("#ScrollableDiv").css("top", -winScrollTop + maxPos);
            }
        }
        if (winScrollTop <= markPos) {
            $("#ScrollableDiv").css("position", "relative");
            $("#ScrollableDiv").css("top", $("#ScrollableMark").position);
        }
    });

    // Nastavení SocialBoxu
    if ($("#SocialBox").length > 0) {
        var pos = $("#MainColumn").offset();
        $("#SocialBox").css({ "left": (pos.left - 80) + "px", "top": (pos.top + 11) + "px" });
        $("#SocialBox").show();
    }

    // Kontrola prázdného boxu vyhledávání
    $(function () {
        //alert("tst");
        $('#FrmSearchProducts').submit(function () {
            if ($('#InpField').val().trim() == '') {
                alert("Zadejte prosím název zboží, které chcete vyhledávat");
                $('#InpField').focus();
                return false;
            }
            else {
                // alert("Formulář je neprázdný");
                return true;
            };
        });
    });

    // Text na pozadí vyhledávacího boxu
    textboxHint("InpField");

});

// Detekce fontu
var font = (function () {
    var test_string = 'mmmmmmmmmwwwwwww';
    var test_font = '"Comic Sans MS"';
    var notInstalledWidth = 0;
    var testbed = null;
    var guid = 0;

    return {
        // must be called when the dom is ready
        setup: function () {
            if ($('#fontInstalledTest').length) return;

            $('head').append('<' + 'style> #fontInstalledTest, #fontTestBed { position: absolute; left: -9999px; top: 0; visibility: hidden; } #fontInstalledTest { font-size: 50px!important; font-family: ' + test_font + ';}</' + 'style>');
            $('body').append('<div id="fontTestBed"></div>').append('<span id="fontInstalledTest" class="fonttest">' + test_string + '</span>');
            testbed = $('#fontTestBed');
            notInstalledWidth = $('#fontInstalledTest').width();
        },

        isInstalled: function (font) {
            guid++;

            var style = '<' + 'style id="fonttestStyle"> #fonttest' + guid + ' { font-size: 50px!important; font-family: ' + font + ', ' + test_font + '; } <' + '/style>';

            $('head').find('#fonttestStyle').remove().end().append(style);
            testbed.empty().append('<span id="fonttest' + guid + '" class="fonttest">' + test_string + '</span>');

            return (testbed.find('span').width() != notInstalledWidth);
        }
    };
})();

function textboxHint(id, options) {
    var o = { selector: 'input:text[title]', blurClass: 'TopSearchTextHint' };
    $e = $('#' + id);
    $.extend(true, o, options || {});

    if ($e.is(':text')) {
        if (!$e.attr('title')) $e = null;
    } else {
        $e = $e.find(o.selector);
    }
    if ($e) {
        $e.each(function () {
            var $t = $(this);
            if ($.trim($t.val()).length == 0) { $t.val($t.attr('title')); }
            if ($t.val() == $t.attr('title')) {
                $t.addClass(o.blurClass);
            } else {
                $t.removeClass(o.blurClass);
            }

            $t.focus(function () {
                if ($.trim($t.val()) == $t.attr('title')) {
                    $t.val('');
                    $t.removeClass(o.blurClass);
                }
            }).blur(function () {
                var val = $.trim($t.val());
                if (val.length == 0 || val == $t.attr('title')) {
                    $t.val($t.attr('title'));
                    $t.addClass(o.blurClass);
                }
            });

            // empty the text box on form submit
            $(this.form).submit(function () {
                if ($.trim($t.val()) == $t.attr('title')) $t.val('');
            });
        });
    }
}


/*
function autocompleteDefault(svcRoot) {
$("#InpField").autocomplete(svcRoot + "Autocomplete.dsv", {
delay: 400,
width: 378,
max: 10,
cacheLength: 0,
highlight: false,
scroll: false,
scrollHeight: 300,
mustMatch: true,
selectFirst: false,
formatItem: function (row) {
return row[0];
},
formatResult: function (row) {
return row[1];
}
});
}

function autocompleteContent(svcRoot) {
$("#InpField").autocomplete(svcRoot + "Autocomplete.dsv", {
delay: 400,
width: 385,
max: 15,
cacheLength: 0,
highlight: false,
scroll: false,
scrollHeight: 450,
mustMatch: true,
selectFirst: false,
formatItem: function (row) {
return row[0];
},
formatResult: function (row) {
return row[1];
}
});
}

*/


