


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 sanoma = {
    menuInit: function() {
        $("#menu > ul > li").hover(
            function() {
                $("#menu li.active").removeClass("hover");
                $(this).addClass("hover");
                sanoma.menuSetBG();
              //  Cufon.refresh('#menu > ul > li > a > span');
            },
            function() {
                $(this).removeClass("hover");
                $("#menu li.active").addClass("hover");
                sanoma.menuSetBG();
              //  Cufon.refresh('#menu > ul > li > a > span');
            }
        );
        sanoma.menuSetBG();
    },
    menuSetBG: function(init) {
        /*
        var c = $('#menu > ul > li.hover > a').css('background-color');
        $('#menu > ul > li.hover ul').css('background-color', c);
        */
    },
    foldSelect: function(fold) {
        if ($(fold).hasClass('active')) return false; 
        //var prefix = $(fold).parent().parent().attr('class').replace('folds-', '');
        var prefix = $('ul[class^="folds"]').attr('class').replace('folds-', '');
        var index = 1;
        var active = 1;
        $('div.tabs').find('a').each(function(n) {
            if ($(this).parent().hasClass('active')) {
                active = n + 1;
                $(this).parent().removeClass('active');
            }
            if (this == fold) {
                index = n + 1;
                $(this).parent().addClass('active');
            }
        })
        
        $('#content-' + prefix + '-' + active).hide();
        $('#content-' + prefix + '-' + index).show();
        return false;
    },
    cornersInit: function() {
        /*
        $('#menu > ul > li > a').corner('top 10px cc:#e8e8e8');
        $('#menu-bar2').corner('6px cc:#e9e9e9');
        $('#specialbox h2').corner('10px cc:#eeeeee');
        $('#specialbox .boxin').corner('10px cc:#eeeeee');
        $('.folded .folds-nej a').corner('top 6px cc:#fbfbfb');
        $('.folded .folds-rel a').corner('top 6px cc:#f9f9f9');
        $('.poll li span').corner('3px cc:#ffffff');
        //$('#topstory .desc').corner('6px cc:#e9e9e9');
        //$('#topstory h3').corner('6px cc:#e9e9e9');
        $('.hp-news').corner('bottom 6px cc:#f6f6f6');
        */
    },
    articleEnhance: function() {
        var article = $('.articlebody');
        var images = article.find('img');
        var bquotes = article.find('blockquote');
        var ols = article.find('ol li');
        var uls = article.find('ul li');
        var infobox = article.find('div.infobox');

        if (bquotes.length > 0) {
            bquotes.each(function() {
                var bquote = $(this);
                var bqhtml = bquote.html();
                bquote.addClass('blockquote');
                bquote.html('<div class="inner">' + bqhtml + '</div>');
            });
        }
        if (ols.length > 0) {
            ols.each(function() {
                var ol = $(this);
                var bolhtml = ol.html();
                ol.addClass('ol');
                ol.html('<p>' + bolhtml + '</p>');
            });
        }
        if (uls.length > 0) {
            uls.each(function() {
                var ul = $(this);
                var bulhtml = ul.html();
                ul.addClass('ul');
                ul.html('<p>' + bulhtml + '</p>');
            });
        }
        if (infobox.length > 0) {
            infobox.each(function() {
                var info = $(this);
                var divinfo = info.html();
                info.html('<div class="infobox-top"></div><div class="infobox-main">' + divinfo + '</div><div class="infobox-bottom"></div>');
            });
        }

        $("table tr:first").addClass("head");
        
        // presunuti boxu se souvisejicimi clanky za 1.odstavec nebo misto kotvy <a name="rel-articles">
        if (article.children('p').length > 0) {
            var rel = $('#articles-rel').detach();
            var anchor = article.find('a[name="rel-articles"]');
            if (anchor.length > 0) anchor.eq(0).replaceWith(rel);
            else article.children('p').eq(0).after(rel);
        }
        // zakulaceni rohu obrazku
        if (images.length > 0) {
            images.each(function() {
                var img = $(this);
                img.addClass('round');
                var p = img.parent();
                if (img.parent('a').length == 1) {
                    p.addClass('imageRound');
                }
                else {
                    p = img.wrap('<span class="imageRound" />').parent();
                }
                p.append('<span class="tl png" /><span class="tr png" /><span class="br png" /><span class="bl png" />');
                var w = p.parent();
                img.removeAttr('hspace').removeAttr('vspace');
                if (img.attr('align') == 'right' || img.css('float') == 'right') {
                    p.addClass('imageRound-right');
                }
                if (img.attr('align') == 'middle' || w.css('text-align') == 'center') {
                    p.addClass('imageRound-center').width(img[0].offsetWidth);
                }
            });
        }
    }
}


var topstory = {
    timeout: null,
    index: 0,
    enableAnim: true,
    init: function() {
        var wrapper = $('#topstory > img.round');
        wrapper.hover(
            function() { topstory.enableAnim = false; topstory.stopAnim(); },
            function() { topstory.enableAnim = true; topstory.startAnim(); }
        );
        topstory.content = wrapper.find('.content');
        var items = topstory.content.find('.item');
        topstory.itemW = items.eq(0).width();
        topstory.total = items.length;
        topstory.controls = wrapper.find('.controls');
        for (var i = 0; i < topstory.total; i++) {
            topstory.controls.append('<a href="javascript:void(0)" onmouseover="topstory.shift(' + i + ');"' + (i == topstory.index ? ' class="active"' : '') + '"></a> ');
        }
        topstory.content.css({ width: topstory.total * topstory.itemW + 'px' });
        $(window).load(function() { topstory.startAnim(); });
    },
    shift: function(index) {
        if (index != null) {
            topstory.stopAnim();
            topstory.content.animate({ left: -(topstory.itemW * index) + 'px' }, 400, function() { topstory.index = index; topstory.setControls(); });
        }
        else {
            if (topstory.index == topstory.total - 1) {
                topstory.content.animate({ left: '+=' + (topstory.itemW * (topstory.total - 1)) + 'px' }, 400, function() { topstory.index = 0; topstory.setControls(); });
            }
            else {
                topstory.content.animate({ left: '-=' + topstory.itemW + 'px' }, 400, function() { topstory.index++; topstory.setControls(); });
            }
        }
    },
    setControls: function() {
        topstory.controls.find('a').each(function(i) {
            $(this).removeClass('active');
            if (i == topstory.index) $(this).addClass('active');
        });
    },
    startAnim: function() {
        if (topstory.enableAnim) topstory.timeout = setInterval(function() { topstory.shift(); }, 4000);
    },
    stopAnim: function() {
        clearInterval(topstory.timeout);
    }
}



