﻿$(function() {
    $(':input.focus').focus();
});

var webForms = {
    countSigns: function (textField, counterId, minSigns, maxSigns) {
        var counterElement = document.getElementById(counterId);
        var timeout;
        if (timeout) {
            window.clearTimeout(timeout);
        }
        timeout = window.setTimeout(function () {
            counterElement.innerHTML = "Napsáno " + textField.value.length + " znaků (povoleno " + minSigns + "-" + maxSigns + ")";
        }, 100);
        if (textField.value.length > maxSigns || textField.value.length < minSigns) {
            counterElement.style.color = "red";
        }
        else {
            counterElement.style.color = "black";
        }
    }
}

var rating = {
    starWidth: 0,
    starSelector: "a",
    starAreaSelector: ".rating",
    infoSelector: ".ratinginfo",
    infoBuffer: "",
    infoTimeout: null,

    init: function (ratingSelector) {
        if (typeof ratingSelector == "undefined") return false;

        rating.starSelector = ratingSelector + " " + rating.starSelector;
        rating.starAreaSelector = ratingSelector + " " + rating.starAreaSelector;
        rating.infoSelector = ratingSelector + " " + rating.infoSelector;
        rating.infoBuffer = $(rating.infoSelector).text();
        rating.starWidth = $(rating.starAreaSelector).width() / $(rating.starSelector).length;

        this.placeStars();

        // hover efekt
        $(rating.starSelector).hover(function () {
            if (rating.infoTimeout != null) {
                clearTimeout(rating.infoTimeout);
                rating.infoTimeout = null;
            }
            var index = $(rating.starSelector).index($(this)) + 1;
            $(rating.starSelector + ":lt(" + index + ")").addClass("hover");
            $(rating.starAreaSelector + " .ratingin").addClass("hover");
            $(rating.infoSelector).text($(this).attr("title"));
        },
            function () {
                $(rating.starSelector).removeClass("hover");
                $(rating.starAreaSelector + " .ratingin").removeClass("hover");
                rating.infoTimeout = setTimeout(function () { $(rating.infoSelector).text(rating.infoBuffer) }, 100);
            });

        // obsluha onclicku
        $(rating.starSelector).click(function () {
            var index = $(rating.starSelector).index($(this)) + 1;
            //alert (index);
            //return false;
        });

        // pokud jiz bylo hlasovano bude se to uzivateli objevovat
        if ($(rating.infoSelector + " .moreinfo").length > 0) {
            $(rating.starAreaSelector).hover(function () { $(rating.infoSelector + " span").toggle() }, function () { $(rating.infoSelector + " span").toggle() });
        }
    },

    // umisteni interaktivni vrstvy nad oznamovaci
    placeStars: function () {
        $(rating.starSelector).each(function (index) {
            var left = index * rating.starWidth;
            $(this).css("left", left);
        });
    }
}

var gSearch = {
    gUrl: "http://ajax.googleapis.com/ajax/services/search/web?q={0}&start={1}&key=ABQIAAAABxxGqCplXLgQw_RKqu5UixTUacwdf_RSPIF6DlXbhZ2Q4jNyIRQZH-GIDC3-MoOktJM-dG16z2vmuA&rsz=large&v=1.0&callback=?",
    gResultsPerPage: 8,
    maxResultsPages: 3,

    search: function (q) {
        if (typeof q == "undefined") return false;

        // vlozime q do gUrl
        var gUrl = this.gUrl.replace("{0}", q);

        this.getResults(gUrl, 0);
    },

    getResults: function (gUrl, start) {
        var replacedUrl = gUrl.replace("{1}", start);

        $.getJSON(replacedUrl, function (data) {
            if (data.responseData != null && data.responseStatus == "200") {
                $.each(data.responseData.results, function (i, item) {
                    var htmlItem = $("<div/>").addClass("item");
                    $("<h2/>").html('<a href="' + decodeURIComponent(item.url) + '">' + item.title.replace(" | Prostři.cz", "") + '</a>').appendTo(htmlItem);
                    $("<p>").html(item.content).appendTo(htmlItem);

                    htmlItem.appendTo("#results");
                    //gSearch.results.push(item.titleNoFormatting);
                });
                if (data.responseData.results.length == gSearch.gResultsPerPage) {
                    start = start + gSearch.gResultsPerPage;
                    if (start > (gSearch.maxResultsPages - 1) * gSearch.gResultsPerPage) return true;
                    else gSearch.getResults(gUrl, start);
                }
            }
        });
    }
}

var topstoryImages = [];
var topstories = {
    duration: 5000,
    timeout: null,
    items: 4,

    init: function() {
        topstories.items = $("#topstories li").length;
        if (topstories.items > 1) {
            $("#topstories li").each(function(index) {
                var image = topstoryImages[index];
                var thisEle = $(this);
                $(this).hover(function() {
                    window.clearTimeout(topstories.timeout);
                    $("#topstories li.active").removeClass("active");
                    $("#topstoryimage a img").attr("src", image);
                    $("#topstoryimage a.img").attr("href", thisEle.find("a:first").attr("href"));
                    var thisElement = $(this);
                    thisElement.addClass("active");
                }, function() {
                    topstories.duration = 1000;
                    topstories.updateWithTimeout();
                    topstories.duration = 6000
                });
            });

            topstories.updateWithTimeout();
        }
    },

    update: function() {
        var nextActiveElement = null;
        if ($("#topstories li.active").next("#topstories li.active ~ li").length == 1) {
            nextActiveElement = $("#topstories li.active").next("#topstories li.active ~ li");
        }
        else {
            nextActiveElement = $("#topstories li:first");
        }
        if (nextActiveElement != null) {
            $("#topstories li.active").removeClass("active");
            nextActiveElement.addClass("active");
            var image = topstoryImages[$("#topstories li").index(nextActiveElement)];
            $("#topstoryimage a img").attr("src", image);
            $("#topstoryimage a.img").attr("href", $("#topstories li.active a:first").attr("href"));

            topstories.updateWithTimeout();
        }
    },

    updateWithTimeout: function() {
        topstories.timeout = window.setTimeout(function() { topstories.update() }, topstories.duration);
    }
}


function articleEnhance(c) {
    var o = $(c);
    o.removeClass(c.replace('.', '')).addClass(c.replace('.', '') + '-js');
    o.find('p,div').each(function() {
        var block = $(this);
        if (block.html() == '&nbsp;' || $.trim(block.html()) == '') {
            block.remove();
            return;
        }
        if (block.text().toLowerCase().indexOf('tip:') != -1) {
            block.addClass('hl').html(block.html().replace(/tip:/i, '<span>TIP:</span>').replace(/\&nbsp;/g,'')).after('<div class="hr"/>');
            return;
        }
    });
    o.find('li').wrapInner('<div/>');
}

var specialbox = {
    timeout: null,
    init: function() {
        $("#specialbox > .item").hover(
                function() {
                    var ele = $(this);
                    specialbox.timeout = window.setTimeout(function() {
                        ele.find(".boxin").slideDown();
                        ele.addClass("slided");
                    }, 500);
                },
                function() {
                    window.clearTimeout(specialbox.timeout);
                    if ($(this).hasClass("slided")) {
                        $(this).find(".boxin").slideUp();
                        $(this).removeClass("slided");
                    }
                }
            ).eq(Math.round(Math.random() * ($("#specialbox > .item").length) - 1)).find('.boxin').slideDown().end().addClass('slided');
    }
}

