window.addEvent('domready', function() {
    $$('.gift').each(function(item){
        var id = item.get('id').split('-');
        var list_id = 'gifts-list-'+id[1];
        if ($(list_id)) {
            item.store('tip:text', $(list_id).dispose());
        }
    });
    $$('.save_service_username').each(function(item){
        item.addEvent('click',function(evt){
        var id = this.id.split('_').pop();
        var data = $('service_username_'+ id).get('value');
//        alert(id, data);
        new Request.JSON({
            'url':'service/save_service_username.json',
            'oncomplete':function(res){
//                console.log(res);
            }
        }).post({'data':data,'id':id});
            evt.stop();
        });
    });

    Dropdown.attach('item-enter', 'enter-block', true);
    if (window.location.hash == '#login') {
        $('item-enter').fireEvent('mouseenter',{target:$('item-enter')});
    }
    createCSSButtons();
    createHoverButtons();

    $$('.tab_switcher').each(function(el) {
        if (!el.hasClass('active')) {
            el.addEvents({
                'mouseenter':	onTabMouseOver,
                'mouseleave':	onTabMouseOut,
                'click':		activateTab
            });
        }
    });

    if ($$('.tab_switcher').length && (window.location.hash != '' && document.getElementById('tab-' + window.location.hash.substr(1)))) {
        //		var splitedHash	= window.location.hash.substr(1).split('/');
        //		for (var i = splitedHash.length - 1; i >= 0; --i) {
        //			if (document.getElementById('tab-' + splitedHash[i])) {
        //				activateTab({ 'target': $('tab-' + splitedHash[i]) });
        //			}
        //		}
        //		splitedHash		= null;

        activateTab({ 'target': $('tab-' + window.location.hash.substr(1)) });
    }

    var to_cart_items = $$('.action_to_cart,.button_add_to_cart,.button_add_to_cart_block');
    for(var i =0,l=to_cart_items.length; i<l; i++) {
        to_cart_items[i].addEvent('click', function() {
            var id = this.get('id').split('-').pop();
            var quantity = 1;
            if($('quantity-'+id) && $('quantity-'+id).get('value')) {
                quantity = $('quantity-'+id).get('value');
            }
            app.Cart.add(id, quantity);
            delete id, quantity;
        });
    }
    var to_mail_items = $$('.action_to_mail');
    for(var i =0,l=to_mail_items.length; i<l; i++) {
        to_mail_items[i].addEvent('click', function() {
            var id = this.get('id').split('-').pop();
            window.location.href = base_url+'profile/product_subscribe?id='+id;
        });
    }
    $$('.remove_subscr_row').addEvent('click', function(event){
        var row = event.target.getParent('.subscr_row');
        var id = row.get('id').split('-').pop();
        new Request.JSON({
            'url': 'profile/product_unsubscribe.json',
            onRequest: function(){},
            onComplete: function(rsp){
                if (rsp == true) {
                    row.destroy();
                } else {
                    alert('Попробуйте позже...');
                }
            }
        }).post({id: id});
    })

    ReMooz.assign('.remoozable', {
        'origin':			'img',
        'shadow':			'onOpenEnd', // fx is faster because shadow appears after resize animation
        'resizeFactor':		0.8, // resize to maximum 80% of screen size
        'cutOut':			false, // don't hide the original
        'opacityResize':	0.4, // opaque resize
        'dragging':			true, // disable dragging
        'centered':			true // resize to center of the screen, not relative to the source element
    });

    if (document.getElementById('registration-additional-info')) {
        $('registration-additional-info').addEvent('click', function(evt) {
            evt.target = $(evt.target);
            var txt	= evt.target.get('text');
            txt		= (txt.indexOf('показать') != -1) ? txt.replace('показать', 'скрыть') : txt.replace('скрыть', 'показать');
            evt.target.set('text', txt);
            txt		= null;
            var element = evt.target;
            if (element.get('tag') == 'span') element=element.getParent();

            element.getAllNext().each(function(el) {
                el.toggleClass('none');
            });
        });
    }
    if (document.getElementById('order-additional-info')) {
        $('order-additional-info').addEvent('click', function(evt) {
            evt.target	= $(evt.target);
            var txt		= evt.target.get('text');
            txt			= (txt.indexOf('показать') != -1) ? txt.replace('показать', 'скрыть') : txt.replace('скрыть', 'показать');
            evt.target.set('text', txt);
            txt			= null;

            $('form-container-addon').toggleClass('none');
        });
    }

    if (document.getElementById('form-registration')) {
        $('button-submit-registration-form').addEvent('click', function(evt) {
            $('form-registration').submit();
        });
    }

    $$('.tip_mandatory').each(function(item) {
        var pid = item.get('id').split('-').pop();
        if ($('mandatory-tip-'+pid)) {
            item.store('tip:text', $('mandatory-tip-'+pid).get('html'));
        }
        pid = null;
    });

    $$('.suitable_to').each(function(item) {
        var pid = item.get('id').split('-').pop();
        if ($('suitable-to-'+pid)) {
            item.store('tip:text', $('suitable-to-'+pid).get('html'));
        }
        pid = null;
    });

    $$('.statuses_to').each(function(item) {
        var pid = item.get('id').split('-').pop();
//        alert(pid);
        if ($('statuses-to-'+pid)) {
            item.store('tip:text', $('statuses-to-'+pid).get('html'));
        }
        pid = null;
    });
    $$('.bundle_to').each(function(item) {
        var pid = item.get('id').split('-').pop();
//        alert(pid);
        if ($('bundle-to-'+pid)) {
            item.store('tip:text', $('bundle-to-'+pid).get('html'));
        }
        pid = null;
    });


    /** left menu **/

    $$('.menu .expanded, .menu .collapsed').each(function(item){
        item.addEvent('click', function() {
            var id = this.get('id').split('-').pop();
            if (this.hasClass('collapsed')) {
                this.removeClass('collapsed');
                this.addClass('expanded');
                $('menu-container-'+id).removeClass('none');
            } else {
                this.removeClass('expanded');
                this.addClass('collapsed');
                $('menu-container-'+id).addClass('none');
            }
        });
    });

    $$('.menu .first_level').each(function(item){
        item.addEvent('click', function() {
           item.getPrevious('div').fireEvent('click');
        });
    });

    if (document.getElementById('profile-pass')) {
        $('profile-pass').set('value', '');
    }

    if (document.getElementById('manager-orders-table')) {
        $$('.create_delivery').each(function(item) {
            item.addEvent('click', function(evt) {
                var id = evt.target.get('id').split('-').pop();
                if (!confirm('Создать накладную для заказа №'+id+'?')) {
                    return false;
                }
            });
        });
    }

    $$('.autoremove').each(function(item){
        setAutoRemove(item,item.get('value'));
    });

    if (document.getElementById('selection-panel-submit')) {
        $('selection-panel-submit').addEvent('click', function(evt){
            $('selection-panel-form').submit();
        });
    }

    if (document.getElementById('selection-panel-clear')) {
        $('selection-panel-clear').addEvent('click', function(evt){
            $('selection-clear').value = 1;
            $('selection-panel-form').submit();
        });
    }

    var myTips = new Tips('.show_tip');

    $$('.show_tip').each(function(item) {
        item.addEvent('mouseover', function(evt) {
            var id = evt.target.get('id').split('-').pop();
            if(document.getElementById('tip-text-'+id) && $('tip-text-'+id).value != '') {

            } else {
                var tip = myTips.tip.getFirst('div').getNext('div');
                var tiptitle = new Element('div', {'class':'tip-title'});
                tiptitle.inject(tip);
                var tiptext = new Element('div', {'class':'tip-text'});
                tiptext.inject(tip, 'bottom');
                tiptext.innerHTML = 'Загрузка данных...';
                new Request.JSON({
                    'url': base_url + 'characteristics_articles/load_tips_text.json',
                    onComplete: function(res) {
                        if(res){
                            var div = new Element('div', {'id':'tip-text-'+id, 'class' : 'fleft hidden'});
                            div.inject(document.body);
                            $('tip-text-'+id).set('html', res.result.title+'<br />'+res.body);
                            $('property-help-'+id).store('tip:title', res.title);
                            $('property-help-'+id).store('tip:text', res.body);
                            myTips.attach($('property-help-'+id));
                            tiptitle.innerHTML = res.title;
                            tiptext.innerHTML = res.body;
                        } else {
                            var div = new Element('div', {'id':'tip-text-'+id, 'class' : 'fleft hidden'});
                            div.inject(document.body);
                            $('tip-text-'+id).set('html', 'Нет данных');
                            $('property-help-'+id).store('tip:text', 'Нет данных');
                            myTips.attach($('property-help-'+id));
                            tiptitle.innerHTML = '';
                            tiptext.innerHTML = 'Нет данных';
                        }
                    }
                }).post({
                    'id': id
                });
            }
        });
    });

    var myNewTips = new Tips('.show_panel_tip');
    myNewTips.addEvent('show', function(tip, hovered){
        var tips = tip.getFirst('div').getNext('div');
        if(tips.innerHTML == '') {
            tips.setStyle('width', 'auto');
            var tiptitle = new Element('div', {'class':'tip-title'});
            tiptitle.inject(tips);
            var tiptext = new Element('div', {'class':'tip-text'});
            tiptext.inject(tips, 'bottom');
            tiptext.innerHTML = 'Загрузка данных...';
        }
    });
    myNewTips.tip.set('id', 'my-new-tips');
    $$('.show_panel_tip').each(function(item) {
        item.addEvent('mouseover', function(evt) {
            var id = evt.target.get('id').split('-').pop();
            var tips = $('my-new-tips').getFirst('div').getNext('div');
            if(document.getElementById('tip-text-'+id) && $('tip-text-'+id).value != '') {
                tips.setStyle('width', $('tip-text-'+id).getWidth());
            } else {
                new Request.JSON({
                    'url': base_url + 'characteristics_articles/load_panel_tips_text.json',
                    onComplete: function(res) {
                        if(res.result){
                            var div = new Element('div', {'id':'tip-text-'+id, 'class' : 'fleft hidden'});
                            div.inject(document.body);
                            $('tip-text-'+id).set('html', res.result.title);
                            $('property-help-'+id).store('tip:title', res.result.title);
                            $('property-help-'+id).store('tip:text', res.result.body);
                            myNewTips.attach($('property-help-'+id));
                            if(tips.getFirst('div') != null) {
                                tips.getFirst('div').innerHTML = res.result.title;
                                tips.getLast('div').setStyle('width', 300);
                                tips.getLast('div').innerHTML = res.result.body;
                            } else {
                                var tiptitle = new Element('div', {'class':'tip-title'});
                                tiptitle.inject(tips);
                                var tiptext = new Element('div', {'class':'tip-text'});
                                tiptext.inject(tips, 'bottom');
                                tiptitle.innerHTML = res.result.title;
                                tips.getLast('div').setStyle('width', 300);
                                tiptext.innerHTML = res.result.body;

                            }
                        } else {
                            var div = new Element('div', {'id':'tip-text-'+id, 'class' : 'fleft hidden'});
                            div.inject(document.body);
                            $('tip-text-'+id).set('html', 'Нет данных');
                            $('property-help-'+id).store('tip:text', 'Нет данных');
                            myNewTips.attach($('property-help-'+id));
                            if(tips.getFirst('div') != null) {
                                tips.getFirst('div').innerHTML = '';
                                tips.getLast('div').innerHTML = 'Нет данных';
                            } else {
                                tips.setStyle('width', 'auto');
                                var tiptitle = new Element('div', {'class':'tip-title'});
                                tiptitle.inject(tips);
                                var tiptext = new Element('div', {'class':'tip-text'});
                                tiptext.inject(tips, 'bottom');
                                tiptitle.innerHTML = '';
                                tiptext.innerHTML = 'Нет данных';
                            }
                        }
                    }
                }).post({
                    'id': id
                });
            }
        });
    });

    var myValuesTips = new Tips('.show_panel_value_tip');
    myValuesTips.addEvent('show', function(tip, hovered){
        var tips = tip.getFirst('div').getNext('div');
        if(tips.innerHTML == '') {
            tips.setStyle('width', 'auto');
            var tiptitle = new Element('div', {'class':'tip-title'});
            tiptitle.inject(tips);
            var tiptext = new Element('div', {'class':'tip-text'});
            tiptext.inject(tips, 'bottom');
            tiptext.innerHTML = 'Загрузка данных...';
        }
    });
    myValuesTips.tip.set('id', 'my-values-tips');
    $$('.show_panel_value_tip').each(function(item) {
        item.addEvent('mouseover', function(evt) {
            var id = evt.target.get('id').split('-').pop();
            var elid = evt.target.get('id');
            var value = evt.target.getParent('div').getFirst('input').value;
            var tips = $('my-values-tips').getFirst('div').getNext('div');
            if(document.getElementById('tips-'+elid) && $('tips-'+elid).innerHTML != '') {
                tips.setStyle('width', $('tips-'+elid).getWidth());
            } else {
                new Request.JSON({
                    'url': base_url + 'characteristics_articles/load_panel_values_tips_text.json',
                    onComplete: function(res) {
                        if(res.result && res.result != 'false'){
                            var div = new Element('div', {'id':'tips-'+elid, 'class' : 'fleft hidden'});
                            div.inject(document.body);
                            div.set('html', res.result.title);
                            $(elid).store('tip:title', res.result.title);
                            $(elid).store('tip:text', res.result.body);
                            myValuesTips.attach($(elid));
                            if(tips.getFirst('div') != null) {
                                tips.getFirst('div').innerHTML = res.result.title;
                                tips.getLast('div').setStyle('width', 300);
                                tips.getLast('div').innerHTML = res.result.body;
                            } else {
                                var tiptitle = new Element('div', {'class':'tip-title'});
                                tiptitle.inject(tips);
                                var tiptext = new Element('div', {'class':'tip-text'});
                                tiptext.inject(tips, 'bottom');
                                tiptitle.innerHTML = res.result.title;
                                tips.getLast('div').setStyle('width', 300);
                                tiptext.innerHTML = res.result.body;
                            }
                        } else {
                            var div = new Element('div', {'id':'tips-'+elid, 'class' : 'fleft hidden'});
                            div.inject(document.body);
                            div.set('html', 'Нет данных');
                            $(elid).store('tip:text', 'Нет данных');
                            myValuesTips.attach($(elid));
                            if(tips.getFirst('div') != null) {
                                tips.getFirst('div').innerHTML = '';
                                tips.getLast('div').innerHTML = 'Нет данных';
                            } else {
                                tips.setStyle('width', 'auto');
                                var tiptitle = new Element('div', {'class':'tip-title'});
                                tiptitle.inject(tips);
                                var tiptext = new Element('div', {'class':'tip-text'});
                                tiptext.inject(tips, 'bottom');
                                tiptitle.innerHTML = '';
                                tiptext.innerHTML = 'Нет данных';
                            }
                        }
                    }
                }).post({
                    'id': id,
                    'value' : value
                });
            }
        });
    });

    $$('.print_link').each(function(item){
       item.addEvent('click', function () {
           var oid = item.getProperty('oid');
           if (oid && !item.hasClass('clr')){
               var trPar = item.getParent('tr');
               if (trPar) {
                   var coords = trPar.getCoordinates();
                   var contDiv = new Element('div');
                   var transpDiv = new Element('div',{
                       styles: {
                           'background-color': 'lightgray',
                           'opacity': '0.5',
                           'position': 'absolute',
                           top: coords.top,
                           left: coords.left,
                           width: coords.width,
                           height: coords.height
                       }
                   });
                   var newDiv = new Element('div', {
                       'html': '<div class="text fleft">Пометить заказ №'+oid+' как напечатанный?</div>'+
                                '<div class="print_item fright" vl="0">Нет</div>'+
                                '<div class="print_item fright" vl="1">Да</div>'+
                                '<div class="clear"></div>',
                       'class': 'confirm_container',
                       'styles': {
                            top: coords.top + (coords.height - 41)/2,
                            left: coords.left + (coords.width - 550)/2,
                           'position': 'absolute'
                       }
                   })
//                   console.log(newDiv.getChildren('.print_item'));
                   newDiv.getChildren('.print_item').each(function(subitm) {
                       subitm.addEvent('click', function () {
                           if (parseInt(subitm.getProperty('vl'))) {
                                new Request.JSON({
                                    'url': 'service/order_editor_save.json?id='+oid,
                                    'onComplete': function (rsp) {
                                       if (rsp) {
                                           item.addClass('clr');
                                       } else {
                                           alert('Изменения не удались');
                                       }
                                   }
                               }).post({'data[printed]':1});
                           }
                           newDiv.set('tween',{'onComplete':function(){contDiv.destroy();}});
                           transpDiv.destroy();
                           newDiv.tween('opacity',0);
                       });
                    })
               }
               transpDiv.inject(contDiv);
               newDiv.inject(contDiv);
               contDiv.inject(trPar.getParent('.font12'));
           }
//           oid = null;
           trPar = null;
       })
    });

    $$('.profile_menu_tabs span').each(function(item){
        item.addEvent('click',function(){
            var tab_id = parseInt(item.getProperty('tab'));
            $$('.profile_tabs.active_tab').removeClass('active_tab');
            $('profile-tab-'+tab_id).addClass('active_tab');
            $$('.profile_menu_tabs span.active').removeClass('active');
            item.addClass('active');
        })
    });

    if (document.getElementById('delivery-plan-container')) {
        loadDeliveryPlan(null);
    }

    if (document.getElementById('lpsm-container')) {
        lpsmLoadLog();
        $('lpsm-date').addEvent('change',function() {lpsmLoadLog();});
    }

    if($('dp-close-modal')){
        $('dp-close-modal').addEvent('click',function(){
            Modal.hide('dp_operations-box');
        })
    }
});

function onTabMouseOver(evt) {
    evt.target = $(evt.target);
    if (evt.target.hasClass('tab_switcher')) {
        var trgtClassArr	= evt.target.get('class').trim().split('_');
        if (trgtClassArr[trgtClassArr.length - 1] != 'hover') {
            evt.target.set('class', evt.target.get('class').trim() + '_hover');
        }
        trgtClassArr		= null;
    }
}

function onTabMouseOut(evt) {
    evt.target.set('class', evt.target.get('class').split('_').erase('hover').join('_'));
}

function activateTab(evt) {
    if (!evt.target.hasClass('tab_switcher')) {
        evt.target			= evt.target.getParent('.tab_switcher');
    }

    var at = evt.target.getParent('.categories_tabs').getElement('.tab_switcher.active');

    at.removeClass('active');
    at.addEvents({
        'mouseenter':	onTabMouseOver,
        'mouseleave':	onTabMouseOut,
        'click':		activateTab
    });
    $('panel-' + at.get('id').substr(4)).addClass('none');

    evt.target.set('class', evt.target.get('class').split('_').erase('hover').join('_'));
    evt.target.addClass('active');
    evt.target.removeEvent('mouseenter',	onTabMouseOver);
    evt.target.removeEvent('mouseleave',	onTabMouseOut);
    evt.target.removeEvent('click',			activateTab);

    var id = evt.target.get('id').substr(4);
    $('panel-' + id).removeClass('none');
    if(id == 'spares' || id == 'tuning') {
        loadSpares(id);
    } else {
        loadIndexTab(id);
    }

    //	window.location.hash	= window.location.hash.split('/').erase(at.get('id').substr(4)).join('/');
    //	window.location.hash	+= '/' + evt.target.get('id').substr(4);

    at = null;
}

function loadSpares(id, force, page) {
    if(id == 'spares' || id == 'tuning') {
        if(!$('panel-'+id).get('html').trim().length || force) {
            $('panel-'+id).set('html', '<p class="font12 mt10">Идет загрузка списка товаров...</p>');
            new Request({
                'url': base_url + 'catalog/load_spares.html',
                onComplete: function(res) {
                    $('panel-'+id).set('html', res);
                    tips.attach($$('.attach_tip'));
                }
            }).get({
                'id_product': id_product,
                'spare_type': id == 'spares' ? 1 : 2,
                'page_num': page ? page : 1
            });
        }
    }

}


function loadIndexTab(id) {
    if ($('panel-'+id) && ($('panel-'+id).get('html') == '')) {
        $('panel-'+id).set('html', '<p class="font12 mt10">Идет загрузка списка товаров...</p>');
        new Request({
            'url': base_url + 'index/load_spares.html',
            onComplete: function(res) {
                $('panel-'+id).set('html', res);
                tips.attach($$('.attach_tip'));
            }
        }).get({
            'spare_type': id
        });
    }
}

function loadAdditionals(type, id_product, id_list) {
    $(type+'-additionals-'+id_list).set('html', '<p class="font12 mt20">Идет загрузка...</p>');
    new Request({
        'url': base_url + 'catalog/load_additionals.html',
        onComplete: function(res) {
            $(type+'-additionals-'+id_list).set('html', res);
            tips.attach($$('.attach_tip'));
        }
    }).post({
        'id_product': id_product,
        'type': type,
        'id_list': id_list
    });
}

function showSortPanel() {
    $$('.sort_item').each(function(item){
        item.toggleClass('none');
    });
    $('change-link').addClass('none');
}
function toggleLinked(id) {
        $('linked_'+id).toggleClass('none');
}

function loadDeliveryPlan(mdate) {
    new Request({
        'url': '/service/delivery_plan_on_date/',
        onRequest: function() {
            $('delivery-plan-loader').removeClass('none');
            $('delivery-plan-container').addClass('none');
            $('delivery-plan-container').set('html', '');
        },
        onComplete: function(res) {
            $('delivery-plan-loader').addClass('none');
            $('delivery-plan-container').set('html', res);
            $('delivery-plan-container').removeClass('none');
        }
    }).get({
        'manual_date': mdate
    });
}

function loadDeliveryPlanOrderInfo(id_order,pre) {
    var item = $((pre ? pre+'-' : '')+'order-info-handler-'+id_order);
    var coords = item.getCoordinates();

    $('dp-order-info-container').setStyle('display','block');
    $('dp-order-info-container').setStyle('top',coords.top+coords.height-3);
    //$('dp-order-info-container').setStyle('left',coords.left-450);
    $('dp-order-info-container').setStyle('left',$('delivery-plan-container').offsetLeft+10);
    //$('dp-order-info-container').innerHTML = '<iframe style="border: none;" width="900" height="500" src="service/delivery_plan_order_info/id_order:'+id_order+'/"></iframe>';
    new Request({
        'url': 'service/delivery_plan_order_info/',
        onRequest: function() {
            $('dp-order-info-container').innerHTML = '<div id="delivery-plan-loader" class="dp_loader">загрузка информации</div>'
        },
        onComplete: function(res) {
            $('dp-order-info-container').innerHTML = res;
        }
    }).get({
        'id_order': id_order
    });
}

function dpHideOrderInfo() {
    $('dp-order-info-container').setStyle('display','none');
}

function dpOrderPrint(oid, pre) {
   var item = null;

   if (oid && (item = $((pre ? pre+'-' : '')+'print-link-'+oid)) && !item.hasClass('clr')){
       var trPar = item.getParent('td.dp_order_cont');
       if (trPar) {
           var coords = trPar.getCoordinates();
           var contDiv = new Element('div');
           var transpDiv = new Element('div',{
               styles: {
                   'background-color': 'lightgray',
                   'opacity': '0.5',
                   'position': 'absolute',
                   top: coords.top,
                   left: coords.left,
                   width: coords.width,
                   height: coords.height
               }
           });
           var newDiv = new Element('div', {
               'html': '<div class="text fleft">Пометить заказ №'+oid+' как напечатанный?</div>'+
                        '<div class="print_item fright" vl="0">Нет</div>'+
                        '<div class="print_item fright" vl="1">Да</div>'+
                        '<div class="clear"></div>',
               'class': 'confirm_container',
               'styles': {
                    top: coords.top + (coords.height - 41)/2,
                    left: coords.left + (coords.width - 550)/2,
                   'position': 'absolute'
               }
           });
//                   console.log(newDiv.getChildren('.print_item'));
           newDiv.getChildren('.print_item').each(function(subitm) {
               subitm.addEvent('click', function () {
                   if (parseInt(subitm.getProperty('vl'))) {
                        new Request.JSON({
                            'url': 'service/order_editor_save.json?id='+oid,
                            'onComplete': function (rsp) {
                               if (rsp) {
                                   item.addClass('clr');
                                   if (item.hasClass('dp_print')) {
                                       item.removeClass('dp_print');
                                       item.addClass('dp_print_active');
                                   }
                               } else {
                                   alert('Изменения не удались');
                               }
                           }
                       }).post({'data[printed]':1});
                   }
                   newDiv.set('tween',{'onComplete':function(){contDiv.destroy();}});
                   transpDiv.destroy();
                   newDiv.tween('opacity',0);
               });
            })
       }
       transpDiv.inject(contDiv);
       newDiv.inject(contDiv);
       contDiv.inject(trPar);
   }
   return true;
}

function dp_showOperations(id_order) {
    $('dp_ord_id').set('text',id_order);
    $('dp-op-edit').set('href','service/view_order/id:'+id_order+'/');
    $('dp-op-load').set('href','service/load_order/id:'+id_order+'/');
    Modal.show('dp_operations-box');
}

function toggleRouteClosed(type, date) {
    var status = $('route_closed_'+type).checked;

    new Request.JSON({
        'url': 'service/change_route_closed/',
        'onRequest': function () {
            $('route_closed_'+type).set('disabled','disabled');
        },
        'onComplete': function (rsp) {
            $('route_closed_'+type).set('disabled','');
        }
    }).get({
        'closed': (status ? 1 : 0),
        'date': date
    });
}

function lpsmLoadLog() {
    var file = $('lpsm-date').value;
    new Request({
        'url': '/service/load_log_prod_subs_mail/',
        onRequest: function() {
            $('lpsm-loader').removeClass('none');
            $('lpsm-container').addClass('none');
            $('lpsm-container').set('html', '');
        },
        onComplete: function(res) {                                    //
            $('lpsm-loader').addClass('none');
            $('lpsm-container').set('html', res);
            $('lpsm-container').removeClass('none');
        }
    }).get({
        'filename': file
    });
}

function loadJS(url) {
	var s = document.createElement("script");
	//s.setAttribute("type", "text/javascript");
	//s.setAttribute("src", url);
    s.type = "text/javascript";
    s.src = url;
	//var h = document.getElementsByTagName("head")[0];
	//h.appendChild(s);
	document.body.appendChild(s);
}

window.addEvent('load', function() {
    loadJS('http://connect.facebook.net/en_US/all.js#xfbml=1');
    loadJS('https://apis.google.com/js/plusone.js');
});
