﻿$(document).ready(function() {
    // --------------------------------------------------
    // Initialization
    // --------------------------------------------------

    $(".nojs").hide();
    $(".js").show();
    $(".popup").fancybox({ 'overlayOpacity': 0.7, 'zoomSpeedIn': 0, 'zoomOpacity': false });

    $('textarea.editor').tinymce({
        // Location of TinyMCE script
        script_url: rootPath + 's/js/tiny_mce/tiny_mce.js',
        language: "ru",
        theme: "advanced",
        skin: "o2k7",
        plugins: "netImageBrowser,advimage,advlink,media,contextmenu,inlinepopups,table,fullscreen,paste",
        paste_auto_cleanup_on_paste: true,
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,|,bullist,numlist,|,outdent,indent,blockquote",
        theme_advanced_buttons2: "fullscreen,forecolor,cut,copy,paste,pastetext,pasteword,|,undo,redo,|,link,unlink,anchor,image,hr,netImageBrowser,media,cleanup,code,",
        theme_advanced_buttons3: ",tablecontrols,",
        //theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        extended_valid_elements: "hr[class|width|size|noshade]",
        file_browser_callback: "ajaxfilemanager",
        paste_use_dialog: false,
        theme_advanced_resizing: true,
        theme_advanced_resize_horizontal: true,
        apply_source_formatting: true,
        force_br_newlines: true,
        force_p_newlines: false,
        relative_urls: false,
        content_css: rootPath + "s/css/editor.css",
        width: "100%",
        height: "20em"
    });

    // Menu animation

    $(".mainlist-li>a").click(function() {
        var sublist = $(this).parent().find(".sublist");
        if (sublist.is(":visible")) {
            sublist.slideUp("fast");
        } else {
            sublist.slideDown("fast");
        }
        return false;
    });

    // --------------------------------------------------
    // Search
    // --------------------------------------------------

    // visual
    //$("input[name=searchString]").css("color", "#ccc");
    var searchPrompt = "Введите строку для поиска";
    $("input[name=searchString]").val(searchPrompt);
    $("input[name=searchString]").focus(function() {
        $(this).css("color", "black");
        if ($(this).val() == searchPrompt) {
            $(this).val("");
        }
    });

    // search execution
    function search() {
        var searchString = $("input[name=searchString]").val();
        var searchMode = $("input[name=searchMode]:checked").val();
        var onlyWithPhoto = $("input[name=onlyWithPhoto]:checked").val();
        var onlyFromManufacturer = $("input[name=onlyFromManufacturer]:checked").val();
        window.location = rootPath + 'search/?searchString=' + encodeURIComponent(searchString) + "&searchMode=" + searchMode + "&onlyWithPhoto=" + onlyWithPhoto + "&onlyFromManufacturer=" + onlyFromManufacturer;
    }
    $("#search-button").click(function() { search(); });
    $("input[name=searchString]").keydown(function(event) {
        if (event.keyCode == '13') {
            event.preventDefault();
            search();
        }
    });

    $("select[name=BannerType]").change(function() {
        if ($(this).val() == 7) {
            $("select[name=SubgroupsList]").show("fast");
        } else {
            $("select[name=SubgroupsList]").hide("fast");
        }
    });


    $(".addPriceItemToCart").click(function() {
        var link = $(this);
        var priceItemId = $(this).attr('priceItemId');
        $.ajax({
            method: 'post',
            data: 'priceItemId=' + priceItemId,
            url: rootPath + 'price/addtocart',
            async: false,
            success: function(data) {
                data = $.evalJSON(data);
                $(link).parent().html('<img src=\"' + rootPath + 's/i/icons/ok.gif\" />');
            }
        });
        return false;
    });

    $(".removePriceItemFromCart").click(function() {
        var link = $(this);
        var priceItemId = $(this).attr('priceItemId');
        $.ajax({
            method: 'post',
            data: 'priceItemId=' + priceItemId,
            url: rootPath + 'price/removefromcart',
            async: false,
            success: function(data) {
                data = $.evalJSON(data);
                //$(link).parent().html('<img src=\"' + rootPath + 's/i/icons/ok.gif\" />');
            }
        });
        return false;
    });

});
