$(function ()
{
    $('.2columns ul').addClass("dontsplit");
    $('.2columns').columnize(
    {
        columns: 2
    });

    // slides
    $('.slideshow, .slideshow1').cycle(
    {
        fx: 'fade',
        speed: 1500,
        timeout: 3500
    });

    $('.slideshow2').cycle(
    {
        fx: 'fade',
        speed: 1500,
        timeout: 3500,
        delay: 1000
    });
    $('.slideshow3').cycle(
    {
        fx: 'fade',
        speed: 1500,
        timeout: 3500,
        delay: 2000
    });

    /* !search */
    $('#search_trigger').live('click', function ()
    {
        $('#search').fadeTo(1, 0).show().fadeTo(200, 1);
    });

    $('#search').click(function (event)
    {
        event.stopPropagation();
    });

    $('html').click(function (event)
    {
        if (!$(event.target).is('#search > *'))
        {
            $('#search').fadeTo(200, 0).hide();
        }
    });

    var count = 0;

    $("#search_field").live("keyup", function ()
    {
        if ($(this).val() != "" && $(this).val().length > 2)
        {
            $(".sitemap").hide();
            $(".search").show();

            $.getJSON(url + '?json=1&s=' + $(this).val(), function (json)
            {
                var html = '';
                count = json.count;

                for (var i = 0; i < count; i++)
                {
                    html += '<li><a href="' + json.posts[i].url + '"><strong>' + json.posts[i].title_plain + '</strong> ' + json.posts[i].excerpt + '</a></li>';
                }

                $('.search').html(html);
            });

        }
        else
        {
            $(".sitemap").show();
            $(".search").hide();
        }
    });

    /* !toodete funktsioonid */

    function left()
    {
        if (current_lt > 0)
        {
            $('.arrow_right').show();
            current_lt = current_lt - 6;
            current_gt = current_gt - 6;

            $('.product').show();
            $('.product:lt(' + current_lt + ')').hide();
            $('.product:gt(' + current_gt + ')').hide();
        }
        else
        {
            $('.arrow_left').hide();
        }
    }

    function right()
    {
        if (current_gt < products)
        {
            $('.arrow_left').show();

            current_lt = current_lt + 6;
            current_gt = current_gt + 6;

            $('.product').show();
            $('.product:lt(' + current_lt + ')').hide();
            $('.product:gt(' + current_gt + ')').hide();
        }
        else
        {
            $('.arrow_right').hide();
        }
    }


    // nooled klaviatuuril
    var products = $('.product').size();
    var current_lt = 0;
    var current_gt = 5;
    $('.product:lt(6)').show();

    // vasak
    $(document).keydown(function (e)
    {
        if (e.keyCode == 37)
        {
            left()
            return false;
        }
        // parem
        if (e.keyCode == 39)
        {
            right();
            return false;
        }
    });

    $('.arrow').fadeTo(0, 0.2);
    $('.arrow').hover(

    function ()
    {
        $(this).stop().fadeTo('fast', 1);
    }, function ()
    {
        $(this).stop().fadeTo('slow', 0.2);
    });

    // nupud
    $('#left').click(function ()
    {
        left()
        return false;
    });

    $('#right').click(function ()
    {
        right();
        return false;
    });

    $("a[href*='youtube.com']").click(function ()
    {
        $.fancybox(
        {
            'padding': 1,
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'title': this.title,
            'overlayColor': '#000',
            'width': 680,
            'height': 495,
            'href': this.href = this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '&autoplay=1',
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
            }
        });

        return false;
    });
});

