/*
            Copyright (C) 2011 by NAUTIS, http://nautis.pl
    */
$().ready(function()
{
    $("#photo_frame").corners();
    //$(".product_name").corners("top");
    $(".top-info").corners("top");
    $(".products-container").not(".top-info").corners();


    $(".single_product_entry").corners("bottom");

    // przelaczanie obrazka glownego
    $(".photo_thumb").mouseover(function()
    {
        $("#main_photo").attr("src", $(this).find("img").attr("src"));
        $("#main_photo_link").attr("title", $(this).attr("title"));
        $("#main_photo_link").attr("href", $(this).attr("href"));
    });


    $(".product_picture").add('.photo_thumb').add('#main_photo_link').each(function()
    {
        $(this).attr("href", $(this).attr("rel"));
        $(this).removeAttr("rel");
    })

    $(".photo_thumb").each(function()
    {
        $(this).attr("rel", "thumb");
    });

    $("#main_photo_link").add(".photo_thumb").add(".product_picture").nyroModal(
    {
        bgColor: '#00305A'
    });

    $("#question_link").click(function()
    {
        $("#question_div").toggle(300);
        return false;
    });

    $("#button_cancel").click(function()
    {
        $("#question_div").hide(300);
    });

    $(".td_buy_navail").attr("title", " - Produkt chwilowo niedostępny");
    $(".td_buy_navail").tooltip(
    {
        track: true,
        delay: 1,
        showBody: ' - '
    });

    $(".td_must_order").attr("title", " - Produkt dostępny tylko na zamówienie, realizacja zamówienia może potrwać ok. 3 tygodni.");
    $(".td_must_order").tooltip(
    {
        track: true,
        delay: 1,
        showBody: ' - '
    });

    $("#q_button_cancel").click(function()
    {
        $("#question_div").hide(200);
        $("#q_email, #q_content").val('');
    });

    $("#q_button_ask").click(function()
    {
        var pytanie = {};
        pytanie['lng'] = $("#q_lng").val();
        pytanie['cat1'] = $("#q_cat1").val();
        pytanie['cat2'] = $("#q_cat2").val();
        pytanie['nazwa_p'] = $("#q_nazwa_p").val();
        pytanie['nadawca'] = $("#q_email").val();
        pytanie['tresc'] = $("#q_content").val();
        pytanie['stw'] = 1;

        var sending = '';
        var sent = '';
        var err_ajx = '';

        switch ($("#q_lng").val())
        {
        case 'pl':
            sending = 'Wysylanie zapytania...';
            sent = 'Zapytanie zostalo wyslane';
            err_ajx = 'BLAD. Sprobuj jeszcze raz...';
            break;
        case 'de':
            sending = 'Sending question...';
            sent = 'Question has been sent';
            err_ajx = 'ERROR. Try sending your question again...';
            break;
        case 'ru':
            sending = 'Sending question...';
            sent = 'Question has been sent';
            err_ajx = 'ERROR. Try sending your question again...';
            break;
        case 'en':
            sending = 'Sending question...';
            sent = 'Question has been sent';
            err_ajx = 'ERROR. Try sending your question again...';
            break;
        }

        $("#q_span").html(sending)

        $.post("/ask_about_item/", pytanie, function(data)
        {
            if(data != '1') $("#q_span").html(data)
            else
            {
                alert(sent);
                $("#question_div").hide(200);
                $("#q_email, #q_content").val('');
            }
        });
    });

    if($("#title").height() > 30) $("#titlebar").height($("#title").height());

    $(".tabbed-bar").on("click", ".inactive-tab-title", function()
    {
        $(this).siblings(".active-tab-title").attr("class", "inactive-tab-title");
        $(this).siblings(".left-active-tab").attr("class", "left-inactive-tab");
        $(this).siblings(".right-active-tab").attr("class", "right-inactive-tab");
        $(this).siblings(".middle-left-active-tab").attr("class", "middle-inactive-tab");
        $(this).siblings(".middle-right-active-tab").attr("class", "middle-inactive-tab");

        $(this).prev(".middle-inactive-tab").attr("class", "middle-right-active-tab");
        $(this).prev(".left-inactive-tab").attr("class", "left-active-tab");
        $(this).next(".middle-inactive-tab").attr("class", "middle-left-active-tab");
        $(this).next(".right-inactive-tab").attr("class", "right-active-tab");

        $(this).attr("class", "active-tab-title");

        $(".open-by-tab").hide(0);
        if($(this).attr("opentab").length && $("#" + $(this).attr("opentab")).length) $("#" + $(this).attr("opentab")).show(0);
    });

    $("#faq-search").keyup(function(e)
    {
/*        var a = e.which;
        if(!(        
            (e.which >= 48 && e.which <= 57)  || // cyfry        
            (e.which >= 65 && e.which <= 90)  || // litery
            (e.which >= 96 && e.which <= 105) || // cyfry na kl. numerycznej
                             // Backspace  Spacja  Delete  ;   *    +    -    ,    ,    .    /    `    [    \    ]    '
            $.inArray((int)e.which, [8,         32,     46,     59, 106, 107, 109, 110, 188, 190, 191, 192, 219, 220, 221, 222])
            ))
            return false;         
*/        
        var szukane = $(this).val();
        if(szukane.length < 3)
        {
            switch ($("#q_lng").val())
            {
                case 'pl':
                    $("#faq-results").html('Szukana fraza jest za krótka.');
                    break;
                case 'de':
                    $("#faq-results").html('Search query too short.');
                    break;
                case 'ru':
                    $("#faq-results").html('Search query too short');
                    break;
                case 'en':
                    $("#faq-results").html('Search query too short.');
                    break;
            }
            return false;
        }
        
        $("#faq-loader").show();
        
        $.getJSON('/go/_faq/ajxget.php', {
            search: szukane,
            lang: $("#q_lng").val()
        }, function(data)
        {
            $("#faq-loader").hide();
            if(data.pytania.length == 0)
            {
                    switch ($("#q_lng").val())
                    {
                        case 'pl':
                            $("#faq-results").html('Nie znaleziono nic pasującego do Twojego zapytania.');
                            break;
                        case 'de':
                            $("#faq-results").html('Sorry, we couldn\'t find anything matching your query.');
                            break;
                        case 'ru':
                            $("#faq-results").html('Sorry, we couldn\'t find anything matching your query.');
                            break;
                        case 'en':
                            $("#faq-results").html('Sorry, we couldn\'t find anything matching your query.');
                            break;
                    }
                    return false;
            }
            $("#faq-results").html('');
            $.each(data.pytania, function(a, b)
            {
                $("#faq-results").append('<div id="faq-results-container_' + b.id + '" class="faq-results-container"><div class="faq-results-question">' + b.pytanie + '</div><div class="faq-results-answer">' + b.odpowiedz + '<a class="faq-readmore" href="http://' + ($("#q_lng").val() == 'pl' ? 'sklep.wesem.pl/faq/' : 'shop.wesem.com/' + $("#q_lng").val() + '/faq/') + '#' + b.id + '"></a></div></div>');
                $(".faq-results-question").corners("top");
                $(".faq-results-answer").corners("bottom");
            });
        });
    });

    $("#faq-results").on("click", ".faq-results-container", function()
    {
        window.location = $(this).find("a").attr("href");
        return false;
    });

});
