window.addEvent('domready', function () {
    /* снег
    new mooSnow({
    'container': $$('body')[0],
    'stage': {
    'styles': {
    'height': null
    }
    },
    'snow': {
    'amount': 50,
    'symbol': ["*", "•"],
    'fontSize': [10, 25],
    'opacity': [1, 1]
    }
    });
    */

    //baloons
    if ($('baloons')) {
        new Request({
            url: '/json/four_informers.json?' + Math.floor(Math.random() * 1000000),
            onSuccess: function (responseText) {
                var responseJSON = JSON.decode(responseText);
                if (responseJSON != null && responseJSON != '') {
                    var DATA = responseJSON;
                    var additional_positions = []; //2,3,4,5
                    var preview_arr = [null, null, null, null];

                    //сделаем предпросмотр и подставим данные в текстовые блоки
                    DATA.each(function (item, i) {
                        //если брать с главной, то запишем в массив и выйдем
                        if (item.check) {
                            additional_positions.push(i + 2); //2,3,4,5
                            return false;
                        }
                        var div = new Element('div', { 'class': 'item', 'rel': i + 2 });
                        var p1 = new Element('p', { 'class': 'image_with_mask' }).inject(div);
                        var a1 = new Element('a', { 'href': item.link, 'onclick': 'live_cl(this);', 'target': '_blank' }).inject(p1);
                        new Element('img', { 'alt': '', 'src': item.img_path }).inject(a1);
                        new Element('img', { 'class': 'mask', 'alt': '', 'src': 'http://static.gastronom.ru/img/mask.png' }).inject(a1);
                        var p2 = new Element('p', { 'class': 'text' }).inject(div);
                        new Element('a', { 'href': item.link, 'html': item.title, 'onclick': 'live_cl(this);', 'target': '_blank' }).inject(p2);

                        preview_arr[i] = div;
                    });

                    if (additional_positions.length > 0) {
                        new Request.JSON({
                            url: '/services/four_informers',
                            onSuccess: function (responseJSON, responseText) {
                                var DATA = responseJSON;

                                DATA.each(function (item, i) {
                                    var div = new Element('div', { 'class': 'item' });
                                    var p1 = new Element('p', { 'class': 'image_with_mask' }).inject(div);
                                    var a1 = new Element('a', { 'href': 'http://www.gastronom.ru', 'onclick': 'live_cl(this);', 'target': '_blank' }).inject(p1);
                                    new Element('img', { 'alt': '', 'src': item.Bind_imPath }).inject(a1);
                                    new Element('img', { 'class': 'mask', 'alt': '', 'src': 'http://static.gastronom.ru/img/mask.png' }).inject(a1);
                                    var p2 = new Element('p', { 'class': 'text' }).inject(div);
                                    new Element('a', { 'href': 'http://www.gastronom.ru', 'html': item.Bind_Title, 'onclick': 'live_cl(this);', 'target': '_blank' }).inject(p2);

                                    preview_arr[item.Bind_Position - 2] = div; //2,3,4,5
                                });

                                //подставим в предпросмотр
                                preview_arr.each(function (item) {
                                    item.inject('baloons');
                                });
                            }
                        }).post({ 'positions': additional_positions });
                    } else {
                        //подставим в предпросмотр
                        preview_arr.each(function (item) {
                            item.inject('baloons');
                        });
                    }
                }
            }
        }).get();
    }

    // center_anons for default.aspx and other pages
    if ($('center_anons')) {
        var center_tabs = new MGFX.Tabs('#center_anons .titles h2', '#center_anons .bigpics div', {
            'transitionDuration': 250,
            'tabIsLink': true
        });

        $$('#center_anons .titles h2').each(function (el, i) {
            el.addEvent('mouseenter', function () {
                center_tabs.showSlide(i);
            });
        });
    }

    if ($('short_search_tabs')) {

        $('recipe_btn_send').addEvent('click', function () {
            var reqStr = "/rcp_search.aspx?simple=0&";

            if ($('recipe_name').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') != "Название рецепта") reqStr += "nm=" + $('recipe_name').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
            if ($('m_txtIngredsOn').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') != "Желаемый ингредиент") reqStr += "&ion=" + $('m_txtIngredsOn').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
            reqStr += "&t=" + $('m_listType').value;

            window.location = reqStr;
        });

        //recipe slide
        $$('#recipe_name', '#m_txtIngredsOn').addEvent('keydown', function (e) {
            if (e.key == 'enter') {
                if (e) e.stop();

                $('recipe_btn_send').fireEvent('click');

                return false;
            }
        });

        //resto slide
        $('resto_name').addEvent('keydown', function (e) {
            if (e.key == 'enter') {
                if (e) e.stop();

                var name = $('resto_name').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
                var reqStr = "/rst_search.aspx?srch=1";
                if (name != "Поиск по названию" && name != '') reqStr += '&name=' + name;

                if ($('m_ddlKitchen').getSelected().get('value') > 0) reqStr += '&kit=' + $('m_ddlKitchen').getSelected().get('value');
                if ($('m_ddlType').getSelected().get('value') > 0) reqStr += '&tp=' + $('m_ddlType').getSelected().get('value');
                if ($('m_ddlSubway').getSelected().get('value') > 0) reqStr += '&sbw=' + $('m_ddlSubway').getSelected().get('value');

                window.location = reqStr;

                return false;
            }
        });

        $('resto_btn_send').addEvent('click', function (e) {
            var name = $('resto_name').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
            var reqStr = "/rst_search.aspx?srch=1";
            if (name != "Поиск по названию" && name != '') reqStr += '&name=' + name;

            if ($('m_ddlKitchen').getSelected().get('value') > 0) reqStr += '&kit=' + $('m_ddlKitchen').getSelected().get('value');
            if ($('m_ddlType').getSelected().get('value') > 0) reqStr += '&tp=' + $('m_ddlType').getSelected().get('value');
            if ($('m_ddlSubway').getSelected().get('value') > 0) reqStr += '&sbw=' + $('m_ddlSubway').getSelected().get('value');

            window.location = reqStr;
        });

        new Request({
            'url': '/xml/resto_alphabet.xml',
            'onSuccess': function (responseText, responseXML) {
                $$('#short_search_tabs .alphabet_container')[0].set('html', responseText);

                var lang_spans = $$('#en_ul span', '#ru_ul span');
                var lang_spans_parent = $$('#en_ul', '#ru_ul');

                lang_spans.addEvent('click', function (e) {
                    lang_spans_parent.toggleClass('dn');
                });
            }
        }).get();

        //заполняем выпадающие списки
        new Request({
            'url': '/xml/recipe_kitchen.xml',
            'onSuccess': function (responseText, responseXML) {
                if (window.DOMParser) {
                    parser = new DOMParser();
                    xmlDoc = parser.parseFromString(responseText, "text/xml");
                }
                else // Internet Explorer
                {
                    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async = "false";
                    xmlDoc.loadXML(responseText);
                }

                var x = xmlDoc.getElementsByTagName("kitchen");
                for (i = 0; i < x.length; i++) {
                    new Element('option', {
                        'value': x[i].getAttribute("id"),
                        'html': x[i].getAttribute("title")
                    }).inject('m_ddlKitchen');
                }
            }
        }).get();

        new Request({
            'url': '/xml/resto_type.xml',
            'onSuccess': function (responseText, responseXML) {
                if (window.DOMParser) {
                    parser = new DOMParser();
                    xmlDoc = parser.parseFromString(responseText, "text/xml");
                }
                else // Internet Explorer
                {
                    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async = "false";
                    xmlDoc.loadXML(responseText);
                }

                var x = xmlDoc.getElementsByTagName("type");
                for (i = 0; i < x.length; i++) {
                    new Element('option', {
                        'value': x[i].getAttribute("id"),
                        'html': x[i].getAttribute("title")
                    }).inject('m_ddlType');
                }
            }
        }).get();

        new Request({
            'url': '/xml/resto_metro.xml',
            'onSuccess': function (responseText, responseXML) {
                if (window.DOMParser) {
                    parser = new DOMParser();
                    xmlDoc = parser.parseFromString(responseText, "text/xml");
                }
                else // Internet Explorer
                {
                    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async = "false";
                    xmlDoc.loadXML(responseText);
                }

                var x = xmlDoc.getElementsByTagName("station");
                for (i = 0; i < x.length; i++) {
                    new Element('option', {
                        'value': x[i].getAttribute("id"),
                        'html': x[i].getAttribute("title")
                    }).inject('m_ddlSubway');
                }
            }
        }).get();
    }

    // ctl_Video.ascx
    if ($$('.video_box')) {
        $$('.video_box').each(function (video_box, i) {
            var pages = video_box.getElements('.pages a');
            var images = video_box.getElements('.video_imgs img');
            var title = video_box.getElements('.title')[0];

            if (pages.length > 0) {
                var video_tabs = new MGFX.Tabs(pages, images, {
                    'transitionDuration': 250,
                    'tabIsLink': true,
                    'startIndex': 0,
                    'onShowSlide': function (index) {
                        title.empty();
                        new Element('a', {
                            'href': pages[index].getProperty('href'),
                            'html': images[index].getProperty('alt')
                        }).inject(title);

                        pages.removeClass('active');
                        pages[index].addClass('active');
                    }
                });

                pages.each(function (el, i) {
                    el.addEvent('mouseenter', function () {
                        video_tabs.showSlide(i);
                    });
                });
            }
        });
    }

    // popular_recipes
    if ($$('.popular_recipes')) {
        $$('.popular_recipes').each(function (popular_recipes, i) {
            var pr_box = popular_recipes.getElements('.pr_box')[0];
            var pr_info = pr_box.getNext().set('opacity', 0.9);

            var sampleObjectItems = [];
            pr_box.getElements('img').each(function (el, i) {
                sampleObjectItems.push({
                    title: el.getProperty('alt'),
                    link: el.getParent().getProperty('href')
                });
            });

            new noobSlide({
                mode: 'vertical',
                box: pr_box,
                items: sampleObjectItems,
                size: 218, //размер элемента в сторону движения, в данном случае "высота"
                autoPlay: true,
                addButtons: {
                    previous: popular_recipes.getElements('.prev')[0],
                    stop: popular_recipes.getElements('.stop')[0],
                    play: popular_recipes.getElements('.play')[0],
                    next: popular_recipes.getElements('.next')[0]
                },
                button_event: 'click',
                fxOptions: {
                    duration: 1000,
                    transition: 'cubic:out',
                    wait: false
                },
                onWalk: function (currentItem, currentHandle) {
                    pr_info.empty();
                    new Element('h3').set('html', currentItem.title).inject(pr_info);
                }
            });
        });
    }

    // recipe.aspx - пошаговые рецепты
    if ($$('.rec_steps .step_img').length != 0) {
        var imgs = [];

        $$('.rec_steps .step_img').each(function (el, i) {
            imgs.push(el.store('tip:text', new Element('img', { 'alt': '', src: el.getProperty('title') })));
            el.removeProperty('title');
        });

        new Tips(imgs, {
            className: 'tips_for_image',
            offset: { 'x': 11, 'y': 21 }
        }).addEvents({
            'show': function (tip, el) {
                tip.fade(0.95);
            },
            'hide': function (tip, el) {
                tip.fade('out');
            }
        });
    }

    //вставка информера dp.ru
    var parent_for_dp = $$('.page_default .left_200px', '.page_rcp_group .left_200px', '.page_recipe .left_200px', '.page_shownews .left_200px', '.page_news .news_archive');
    if (parent_for_dp.length > 0) {
        var iframe_dp = new Element('div', {
            'class': 'invert_module',
            'html': '<iframe id="dp_ru" width="100%" scrolling="no" height="290" frameborder="0" marginwidth="0" marginheight="0" src="http://www.dp.ru/parner/gastronom/"></iframe>'
        });
        iframe_dp.inject(parent_for_dp[0], 'bottom');
    }

    //recipe day slide on recipe.aspx
    var body_el = $$('body')[0];
    if (body_el.hasClass('page_recipe') ||
        body_el.hasClass('page_article_recipe') ||
        body_el.hasClass('page_rcp_group') ||
        body_el.hasClass('page_rcp_cat') ||
        body_el.get('class').test('article')) {
        if ($('recipe_day_slide')) {
            var rds = $('recipe_day_slide');
            var predel = 700;
            //if ($$('.social_icon').length > 0) {
                //посчитаем с какого места показывать тизер
                //predel = $$('.social_icon')[0].getCoordinates().top - window.getSize().y - 100;
                //predel = $$('.rec_ingr')[0].getCoordinates().top - window.getSize().y + 200;
            //}

            window.addEvent('scroll', function (e) {
                //покажем тизер на середине экрана
                if (window.getScroll().y > predel) {
                    //если тизер уже появлялся
                    if (!rds.hasClass('active')) {
                        rds.removeClass('dn').addClass('active').set('tween').tween('left', 15);
                        //zeropixel для подсчета показов
                        new Element('img', {
                            'alt': '',
                            'src': 'http://ads.adfox.ru/59799/getCode?p1=bdifq&p2=v&pe=b&pfc=ixow&pfb=xigz&pr=egnjzwa'
                        }).inject(rds);
                    }
                }
                //alert(Browser.Engine.trident);
                //if (Browser.Engine.trident) { alert('ie'); }
                //rds.setStyle('top', window.getScroll().y + 200);
            });
        }
    }
});


//*************************************************************************************************************
//*************************************************************************************************************
//*************************************************************************************************************
//*************************************************************************************************************
//*************************************************************************************************************

ShowAnons = function (e, id) {
    if($(id)) $(id).toggleClass('di');
}
HideAnons = function (id) {
    if($(id)) $(id).toggleClass('di');
}
