﻿/// <reference path="jquery-1.4.2.min.js" />
//Minify JavaScript http://www.vlead.in/resources/tools/minify-javascript/

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}

$(document).ready(function () {
    // *** SPECIALNI COMBO ***
    var maxRowForFilter = 10; //kdyz je vice nez 10 radku aktivuje se filter
    var maxRowInCombo = 20; //Maximalni pocet radku v seznamu
    var fontSizeInCombo = 7; //Upravit podle potreby aby to davalo pak tecky
    var speedOpenCombo = 100; //Rychlost otvirani seznamu
    var zindex = 500;   //Nastaveni Z-indexu pro tooltipy

    //Nastaveni Selectu a pridani skrytych textboxu
    var zIndex = 1000;
    $("div[id*='comboBoxSpecial']").each(function () {
        strClass = $(this).attr('class');
        strClass = "comboBoxSpecial " + strClass;
        $(this).attr('class', strClass);
        zIndex -= 1;
        $(this).css("z-index", zIndex);
        shortName = $(this).attr('id').substring(15);
        $(this).children().prepend('<input id="addressFilter' + shortName + '" type="text" style="display:none;" class="txtBoxSearchIco" />');
        $(this).children().append('<input id="comboBoxInput' + shortName + '" value="" type="hidden" />');
        $(this).children().append('<input id="comboBoxWidth' + shortName + '" value="" type="hidden" />');
    })

    //Vymeni specialni combo za normalni combo
    $("div.comboBoxSpecial > div").hide();
    $("div.comboBoxSpecial > span").each(function () {
        selText = $(this).parent().find("option:selected").text().trim();
        //        if (selText.length > $(this).width() / fontSizeInCombo) {
        //            textForSpan = selText.substring(0, Math.floor(($(this).width() / fontSizeInCombo) - 3)) + " ...";
        //            $(this).attr("title", selText);
        //            $(this).text(textForSpan);
        //        } else {
        //            $(this).text(selText);
        //        }
        $(this).attr("title", selText);
        $(this).text(selText);
    })
    $("div.comboBoxSpecial > span").show();

    //filtrovani adress
    $("div.comboBoxSpecial input").keyup(function () {
        countRow = $(this).siblings("select").children().length;
        searchText = $(this).val().trim().toLowerCase();
        $(this).siblings("select").empty();
        $(this).siblings("select").append($(this).siblings("input[id*='comboBoxInput']").val()); //insert all items
        $(this).siblings("select").children().each(function () {
            if ($(this).text().toLowerCase().indexOf(searchText) == -1) {
                $(this).remove();
            }
        })
    })

    //Schovani comba
    $(":not(.comboBoxSpecial *)").click(function (event) {
        strNodeName = event.target.nodeName.toLowerCase();
        if (strNodeName != "span" && strNodeName != "input") {
            $("div.comboBoxSpecial > div").hide();
            /*$("div.comboBoxSpecial > select").add(contentSpecialCombo);*/
            //event.stopImmediatePropagation();
        }
    });

    //Vyplneni spanu
    $("div.comboBoxSpecial select").click(function () {
        selText = $(this).find("option:selected").text().trim();
        //        if (selText.length > $(this).parent().parent().find("span").width() / fontSizeInCombo) {
        //            textForSpan = selText.substring(0, Math.floor(($(this).parent().parent().find("span").width() / fontSizeInCombo) - 3)) + " ...";
        //            $(this).parent().parent().find("span").attr("title", selText);
        //            $(this).parent().parent().find("span").text(textForSpan);
        //        } else {
        //            $(this).parent().parent().find("span").text(selText);
        //        }
        $(this).parent().parent().find("span").attr("title", selText);
        $(this).parent().parent().find("span").text(selText);
    })

    //Otevreni comba a nastaveni potrebnych parametru
    $("div.comboBoxSpecial > span").click(function () {
        var setting = 1;
        if ($(this).next().children("input[id*='comboBoxInput']").val() == "") {	//bylo to prazdne tak zalohuj
            $(this).next().children("input[id*='comboBoxInput']").val($(this).next().children("select").html());
        } else {
            setting = 0;
        }

        var count = $(this).next().children("select").children().length;
        count = count > maxRowInCombo ? maxRowInCombo : count;
        count = count == 1 ? 2 : count; //nesmi byt nikdy jedna jinak nebude seznam
        if (setting == 1) {
            $(this).next().children("select").attr("size", count);
        }
        $(this).next().slideToggle(speedOpenCombo);

        comboWidth = $(this).next().children("select").innerWidth();
        comboWidth = (comboWidth < $(this).innerWidth()) ? $(this).innerWidth() : comboWidth; //kdyz je mensi nez puvodni combo tak vrat velikost puvodniho comba
        if ($(this).next().children("input[id*='comboBoxWidth']").val().length == 0) {
            $(this).next().children("input[id*='comboBoxWidth']").val(comboWidth);
        } else {
            comboWidth = $(this).next().children("input[id*='comboBoxWidth']").val()
        }

        $(this).next().children("select").width(comboWidth - 6);
        if (count > maxRowForFilter) {
            $(this).next().children("input").width(comboWidth - 12);
            $(this).next().children("input").css("display", "block");
        } else {
            if (setting == 1) {
                $(this).next().children("input").css("display", "none");
            }
        }
    });

    // *** SPECIALNI TOOLTIP ***
    $(".specialTooltip").each(function () {
        $(this).children("span").css("display", "none");
        contentTooltip = $(this).children("span").html();
        $(this).children("span").empty();
        contentTooltip = '<span class="sttArrowTop"></span><span class="sttContents">' + contentTooltip + '</span>';    //<span class="sttArrowBottom"></span>
        $(this).children("span").append(contentTooltip);
    })

    $(".specialTooltip img").each(function () {
        zindex -= 1;
        $(this).parent().css("z-index", zindex);
    })

    $(".specialTooltip a, .specialTooltip img").hover(function () {
        if (navigator.userAgent.indexOf("MSIE") != -1 && $(this).context.tagName.toLowerCase() == "img") {
            $(this).attr("alt", "");
        }
        centerThis = $(this).outerWidth(true) / 2;
        centerTooltip = $(this).parent().children("span").outerWidth(true) / 2
        leftThis = $(this).offset().left;
        leftTooltip = leftThis + centerThis - centerTooltip;
        rightTooltip = leftThis + centerThis + centerTooltip;
        leftTooltip = leftTooltip < 0 ? 0 : leftTooltip; //Kdyz pretece vlevo tak uprav pozici
        leftTooltip = rightTooltip > $("body").outerWidth(true) ? leftTooltip - (rightTooltip - $("body").outerWidth(true)) : leftTooltip; //Kdyz pretece vpravo tak uprav pozici
        leftForTooltip = leftTooltip - leftThis;
        leftForArrow = (leftForTooltip * -1) + centerThis - 10;
        topTooltip = $(this).outerHeight(true) + 2; //Odsazeni od prvku 2px;
        heightTooltip = $(this).parent().children("span").outerHeight(true); //Vyska tooltipu
        $(this).parent().children("span").css("left", leftForTooltip);

        //        if ($(this).parent().offset().top + heightTooltip + 15 > $("body").outerHeight(true) + window.pageYOffset) {
        //            $(this).parent().children("span").css("bottom", topTooltip);
        //            zindex = 1;
        //            $(".specialTooltip img").each(function () {
        //                zindex += 1;
        //                $(this).parent().css("z-index", zindex);
        //            })
        //        } else {
        $(this).parent().children("span").css("top", topTooltip);
        //            zindex = 100;
        //            $(".specialTooltip img").each(function () {
        //                zindex -= 1;
        //                $(this).parent().css("z-index", zindex);
        //            })
        //        }

        $(this).parent().children("span").children(".sttArrowTop").css("top", 0);
        //        $(this).parent().children("span").children(".sttArrowBottom").css("top", heightTooltip - 20);
        $(this).parent().children("span").children(".sttContents").css("-moz-box-shadow", "0px 5px 10px #666"); 	//Shadow
        $(this).parent().children("span").children(".sttContents").css("-webkit-box-shadow", "0px 5px 10px #666"); //Shadow
        $(this).parent().children("span").children(".sttContents").css("box-shadow", "0px 5px 10px #666"); 		//Shadow
        $(this).parent().children("span").children("span[class*='sttArrow']").css("left", leftForArrow);

        $(this).parent().children("span").stop(true, true).delay(500).fadeIn(350);
    }).mouseout(function () {
        $(".specialTooltip").children("span").stop(true, true).fadeOut(350);
    })

    // *** VYMENA OBRAZKU V DETAILU POLOZKY ***
    $("#pictureInRightPanel > a").click(function (event) {
        event.preventDefault();
        $("#pictureInRightPanel > a").removeAttr("class");
        $(this).attr("class", "selected");
        urlStr = $(this).find('img').attr("src");
        urlStr = urlStr.replace("/smallmedium/", "/large/");

        if ($.browser.msie && $.browser.version.substring(0, 2) == "6.") { //Kdyz je IE6.x musi se obnovit stranka aby obnovil korektne styly.
            var oldUrl = $("#bigImage > a > img").attr("src");
            var oldFile = oldUrl.substring(oldUrl.lastIndexOf("/") + 1, oldUrl.length);
            var newFile = urlStr.substring(urlStr.lastIndexOf("/") + 1, urlStr.length);
            var url = location.href.replace(oldFile, newFile);
            location.href = url;
        } else {
            $("#bigImage > a > img").fadeOut("slow", function () {
                $("#bigImage > a > img").attr("src", urlStr);
            });
            $("#bigImage > a > img").fadeIn();
        }

    });

});

function ShowMessageWait(strMsg) {
    $("#messages").find("#lMessageBody").text(strMsg);
    $("#messages").fadeIn(500);
    $(".waitMessage").css("-moz-box-shadow", "0px 5px 10px #666"); 	//Shadow
    $(".waitMessage").css("-webkit-box-shadow", "0px 5px 10px #666"); //Shadow
    $(".waitMessage").css("box-shadow", "0px 5px 10px #666"); 		//Shadow
}

function HideMessageWait() {
    $("#messages").fadeOut(500);
}
