order.js

$(function(){
    serviceHeight();
    $('#seconds').hide();
    inputFocus();
    
/*开始接受订单*/
$('.btn_order').click(function(){
    $('.pause').attr('checked',false);
    $('.btn_order').attr('request',1);
    if( !$(this).hasClass('btn_stop') ){
        $('.addmenu').find('i').text('');
        getOrder();
    }
    $(this).toggleClass('btn_stop');
    $('.layer_center').toggleClass('refuse');

    //return false;
    
});
    
    
$('input.btn_order').focus(function(){
    $(this).blur();
});    

/*隔行换色*/
    stripesTable();

/*退回订单*/
$('.back').click(function(){
    var orderID = $.trim($('.order_no strong').text());
    if(confirm('您确定要退回订单吗?')){
        ajaxSetup('back',orderID);
    }
})

/*取消订单*/
$('.esc').click(function(){
    var orderID = $.trim($('.order_no strong').text());
    if(confirm('您确定要取消订单吗?')){
        ajaxSetup('cancel',orderID);
    }
})






/*form 提交表单*/

$('#from_menu').submit(function(event){
    event.preventDefault();
        /*发送修改 删除记录*/
    var modifyNode = $('ul.uldishes li.modify');
    var modify_items= [];
    for(var i=0;i<modifyNode.length;i++){
        //if( modifyNode.eq(i).hasClass('added')){ continue;}
        var cur_modify =  modifyNode.eq(i);
        var ID = cur_modify.attr('data-id');
        var num = cur_modify.find('.amount').find('.inp').val();
        var msg = cur_modify.find('.msg').text();
        var item ={};
        item.menu_id =ID;
        item.num = num;
        item.msg = msg;
        var item ='{"menu_id":"'+ID+'","num":"'+num+'", "msg":"'+msg+'"}';
        modify_items.push(item); 
    }
    var addNode = $('ul.uldishes li.added');
    var add_items = [];
    for(var j=0;j<addNode.length;j++){
        var cur_add =  addNode.eq(j);
        var addID = cur_add.attr('data-id');
        var addnum = cur_add.find('.amount').find('.inp').val();
        var addmsg = cur_add.find('.msg').text();
        var additem = '{"menu_id":"'+addID+'", "num":"'+addnum+'", "msg":"'+addmsg+'"}';
        add_items.push(additem); 
    }
    /*费用*/
    var mealfee = $.trim($('.mealfee strong').text());
    var supplement = $.trim($('.supplement strong').text());
    var service = $.trim($('.service strong').text());
    var total = $.trim($('.total strong').text());
    var commet = $.trim($('.mtext').text());
    console.log('modify_items:'+modify_items);
    console.log('del:'+del_items);
    console.log('add:'+ add_items);
    var cost ='{"meal":"'+ mealfee+'","extra":"'+supplement+'","service":"'+service+'","total":"'+total+'","commet":" '+commet+'"}';
    console.log(cost);

    /*ajax*/
//    $.ajax({
//        type:'GET',
//        url:'ajax.txt',
//        data:{ modify:[modify_items],remove:del_items,add:[add_items]},
//        success:function(){
//            console.log('modify:['+ modify_items+']');
//        }
//    })
    /*提交成功反馈 发送修改 删除的菜品*/
     $.ajax({
        type:'POST',
        url:'http://huangchen.redcms.com/order/edit.html',
        data:'type=submit&act=edit&oid='+order_id+'&modify=['+modify_items+']&remove='+del_items+ '&add=['+add_items+']&cost='+cost,
        success:function(txt){
            alert('提交成功');
            if( txt == 1){
                $.fancybox.close();
                clearTimeout(request);
                setTimeout(function(){
                    $.fancybox({
                        'href':'#order_tips01',
                        'showCloseButton':false
                    });
                },350);
                setTimeout(function(){
                    $.fancybox.close();
                    maskLayer();
                },1000);
                if( $('.btn_order').attr('request') == 1){
                    alert(111);
                     getotherOrder();
                }
            }else{
                //alert('提交失败');
            }
        },error:function(){
            alert('提交时出错');
        }
     })
    /*暂停接受订单 复选框选中*/
    if( $('.pause').is(':checked') ){
        $('.btn_order').removeClass('btn_stop');
        $('.btn_order').addClass('btn_start');
        $('.layer_center').removeClass('refuse');
        $('.btn_order').attr('request',0);
    }
    
//$('.deliver em').html(timeBuckets);    
    

    
})



/*退回订单/取消订单 ajax*/
function ajaxSetup(operation,ID){
        $.ajax({
            type:'GET',
            url:'http://huangchen.redcms.com/order/edit.html',
            data:'type='+operation+'&act=edit&oid='+order_id+'',
            success:function(){
                console.log('成功'+operation+'订单');
            },
            error:function(){
                console.log(operation+'订单失败');
            }
        });
        $.fancybox.close();
        clearTimeout(movement);
        /*遮罩层消失*/
        maskLayer();
        /*暂停接受订单 复选框选中*/
        if( $('.pause').is(':checked') ){
            $('.btn_order').removeClass('btn_stop');
            $('.btn_order').addClass('btn_start');
            $('.layer_center').removeClass('refuse');
            $('.btn_order').attr('request',0);
        }
        if( $('.btn_order').attr('request') == 1){
            getotherOrder();
        }
}

/*Input 聚焦*/
function inputFocus(){
    $('input.inp_msg').val('此处为菜品备注信息');
    $('input.inp_msg').css('color','#c6c6c6');
    $('input.inp_msg').focus(function(){
        if( $(this).val() == '此处为菜品备注信息') {
            $(this).val('');
            $(this).css('color','#000');
        }
    }).blur(function(){
        if( $(this).val() == ''){
            $(this).val('此处为菜品备注信息');
            $(this).css('color','#c6c6c6');
        }
    })
    /*textarea*/
    $('.mtext').css('color','#c6c6c6');
    $('.mtext').val('此处为订单备注信息');
    $('.mtext').focus(function(){
        if( $(this).val() == '此处为订单备注信息'){
            $(this).val('');
            $(this).css('color','#000');    
        }
    }).blur(function(){
        if( $(this).val() == ''){
            $(this).val('此处为订单备注信息');
            $(this).css('color','#c6c6c6');
        }
    })
}

/*数量增加*/
$('.add').live('click',function(){
        var inp_text = $(this).siblings('.inp_short');
        var num = inp_text.val();
        if( num > 0 ){ num++}
        inp_text.val(num);
        /*总价*/
        totalPrice($(this));
        mealFee();
        service();
        total();
        $(this).parents('li').addClass('modify');
        return false;
})
/*数量减少*/
$('.minus').live('click',function(){
        var inp_text = $(this).siblings('.inp_short');
        var num = inp_text.val();
        if( num > 1){ num--};
        inp_text.val(num);
        /*总价*/
        totalPrice($(this));
        mealFee();
        service();
        total();
        $(this).parents('li').addClass('modify');
        return false;
})
$('.amount .inp_short').live('blur',function(){
        totalPrice($(this));
        mealFee();
        service();
        total();
})


/*总价*/
$('.price').each(function(){
    totalPrice($(this));
})



/*删除某菜单*/
var del_items=[];
$('.del').live('click',function(){
    if(confirm('确定要删除此菜品吗?')){
        var liNode = $(this).parents('li');
        var deleteId = liNode.attr('data-id');
        if( !liNode.hasClass('added')){
            del_items.push(deleteId);
        }
        liNode.remove();
        mealFee();
        service();
        total();
        stripesTable();
        $('.addmenu').find('i').text('');
    }
    return false;
})


/*补差金额输入框*/
$('.supply').blur(function(){
    addFee();
    service();
    total();
})


/*餐费*/
mealFee();


/*补差金额*/
addFee();


/*服务费*/
service();


/*合计*/
total();

var flag_done = 0;
/*搜索建议*/
$('.inp_match').val('');
$('.inp_match').keyup(function(event){
    var keycode = event.which;
    //alert($('#jsmenulist ul').html());
    if( enter == 1 && !$('#jsmenulist ul li').hasClass('adopt')){ 
        //alert(111);

        enter =0;
    }
    if( keycode == 38 || keycode ==40 || keycode ==37 || keycode == 39 || keycode == 13){
        console.log('不执行搜索');
        //$('#jsmenulist ul li').unbind('hover',mouseOver());
        changekey(keycode);
        return;
    }
    matchSearch($(this),keycode);
    //$('.inp_match').removeClass('inp_adopt');
    $('.addmenu').find('i').text('');
    $('ul.uldishes li').removeClass('already');
    
    
})
$('.inp_match').focus(function(){
    //alert('focus');
    $('div.addmenu').data('key',0);
    //console.log('focus');
    //matchSearch($(this));
    //$('.addmenu').find('i').text('');
    //$('.inp_match').removeClass('inp_adopt');
    //$('ul.uldishes li').removeClass('already');
})


$('.inp_match').blur(function(){
    //$('#jsmenulist').fadeOut('fast');
})

/*阻止enter键默认提交表单功能*/
$('body').keydown(function(event){
    if( event.which == 13){
        return false;
    }
})


$(document).click(function(){
    $('#jsmenulist').hide();
})




$('#jsmenulist li').live('click',function(){
    $('#jsmenulist li').removeClass('adopt');
    $(this).addClass('adopt');
    $('.inp_match').focus().val($(this).find('span').text() +'  '+$(this).find('em').text());
    $('.inp_match').addClass('inp_adopt');
})


/*添加菜品*/
$('#js_addmenu').click(function(){
    if( $.trim( $('.inp_match').val())==''){ $('.addmenu').find('i').text('请填写菜品编号!'); return;}
    console.log('inp_adopt内容:'+$('.inp_adopt').html());
    if($('#jsmenulist ul li').hasClass('now')){
        $('#jsmenulist li.now').addClass('adopt');
        $('.inp_match').addClass('inp_adopt');

    }
    if( ! $('#jsmenulist li').hasClass('adopt') ){ <!--//! $('.inp_match').hasClass('inp_adopt') || -->
        //alert('jsmenulist不存在adopt');
        console.log($('.inp_adopt').parent().html());
        curmenuSearch();
        $('.inp_match').removeClass('.inp_adopt');
        $('.inp_match').val('');
        var maxScroll = $('ul.uldishes li').height()*($('ul.uldishes li').length - 1);
        $('ul.uldishes').scrollTop(maxScroll);    
        inputFocus();
    }else if($('#jsmenulist li').hasClass('adopt')){
        alert('jsmenulist存在adopt');
        //alert($('#jsmenulist').html());
        console.log('第二次存在inp_adopt:'+$('.inp_adopt').parent().html());
        //if( $('.inp_match').hasClass('inp_entered')){ $('.addmenu').find('i').text('该菜品已存在列表中!'); return false;} 
        var linow = $('#jsmenulist').find('li.adopt');
        console.log('aaaaa:'+$('#jsmenulist').html());
        var menuCode = linow.find('span').text();
        var ID = linow.attr('data-id');
        var name = linow.find('em').text();
        var price = linow.attr('data-key');
        /*查找是否有同编号的存在*/
        var dishes=dishesList();
        console.log('dishes:'+dishes);
        for(var i=0;i<dishes.length;i++){
            var num = dishes[i];
            if( num != ID){
                continue;
            }
            else{
                $('.addmenu').find('i').text('此菜品已存在!');
                $('ul.uldishes li').removeClass('already');
                $('ul.uldishes li').eq(i).addClass('already');
                var offsetY = $('ul.uldishes li').height()*i;
                console.log(offsetY);
                $('ul.uldishes').scrollTop(offsetY);
                $('.inp_match').removeClass('.inp_adopt');
                $('#jsmenulist li').removeClass('adopt');
                $('.inp_match').val('');
                $('div.addmenu').data('key',0);
                $('#jsmenulist li').remove();
                return;
            }
        }
        
        $('ul.uldishes').append('<li class=\'added\' data-id=\''+ID+'\'><div><span class=\'num\'>'+menuCode+'</span><span class=\'dishname\'>'+name+'</span><span class=\'per\'>'+price+'</span><span class=\'amount\'><a class=\'minus\' href="#">-</a><input class=\'inp inp_short\' value=\'1\' type=\'text\'><a class=\'add\' href=\'#\'>+</a></span><span class=\'price\'>'+price+'</span><span><a class=\'del\' href=\'#\'>删除</a></span></div><div class=\'msg\'><input class=\'inp inp_long inp_msg\' value=\'此处为菜品备注信息\' type=\'text\'></div></li>');
        //$('.inp_match').addClass('inp_entered');
        //flag_done = 0;
        mealFee();
        service();
        total();
        stripesTable();
        $('.inp_match').val('');
        var maxScroll = $('ul.uldishes li').height()*($('ul.uldishes li').length - 1);
        $('ul.uldishes').scrollTop(maxScroll);        
        inputFocus();
        $('div.addmenu').data('key',0);
        $('.inp_match').removeClass('.inp_adopt');
        $('#jsmenulist li').removeClass('adopt');
        $('#jsmenulist li').remove();
    }

})

/*iframe高度自适应*/
$("#edit").load(function(){
    var editheight = $(this).contents().find("body").height();
    $(this).height(editheight);
}); 




})


var m;
/*列表中存在的菜品*/
function dishesList(){
    var liNode = $('ul.uldishes li');
    var dishes= new Array();
    for(var i=0;i<liNode.length;i++){
        var key = $.trim(liNode.eq(i).attr('data-id'));
        dishes.push(key);
    }
    return dishes;
}




/*搜索框 ajax请求---搜索建议*/
function matchSearch(elem,keycode){
    //if( m ){clearTimeout(m);}
    var matchVal = $('.inp_match').val();
    if( $.trim(matchVal) == ''){ return; };
    //m = setTimeout('getData()',200);
    getData();
    
}

function getData(elem,keycode){
    $('div.addmenu').data('key',0);
    var matchVal = $('.inp_match').val();
    console.log('输入框的值:'+matchVal);
    $.ajax({
        type:'GET',
        url:'http://xuexia.redcms.com/order/edit.html',
        data:'act=select&sname='+matchVal+'',
        success: function(result){
            console.log('result:'+result);
            eval('var data='+ result);
            if( data.length == 0 ){ $('#jsmenulist').hide(); return;}
            $('#jsmenulist').show();
            $('#jsmenulist').html('<ul><li data-val=\''+matchVal+'\'></li></ul>');
            var menu_amount = data.length;
            for(var i=0;i<menu_amount;i++){
                $('#jsmenulist ul').append('<li data-id=\''+data[i].id+'\' data-key=\''+data[i].order_price+'\''+'><span>'+data[i].menu_code+'</span><em>'+data[i].menu_name+'</em></li>');
            };
            mouseOver();
        }
    });
}





var enter=0;

/*键盘上下键操作*/
function changekey(keycode){
    //alert('changekey');
    console.log('changekey'+$('div.addmenu').data('key'));
    var len = $('#jsmenulist ul li').length - 1;
    console.log('ajax加载的len:'+len);
    if( keycode == 38){/*向上*/
        $('#jsmenulist li').removeClass('now');
        var i = $('div.addmenu').data('key');
        if( i<0){
            i=len-1;
        }else{
            i--;
        }
        $('div.addmenu').data('key',i);
        
        var liNode = $('#jsmenulist ul li').eq(i);
        liNode.addClass('now');
        if( i==0 ){ 
            var defaultV = $('#jsmenulist ul li').eq(0).attr('data-val');
            $('.inp_match').val(defaultV);
        }else{
            $('.inp_match').val(liNode.find('span').text() +'  '+liNode.find('em').text());
        }
//        var text = liNode.find('span').text() +'  ' + liNode.find('em').text();
//        $('.inp_match').val();$(this).find('span').text() +'  '+$(this).find('em').text()
        //$('.inp_match').val(liNode.text());
    }
    if(keycode == 40){
        $('#jsmenulist li').removeClass('now');
        //$('#jsmenulist ul li').removeClass('now');
        var i = $('div.addmenu').data('key');
        if( i>len-1){
            i=0;
        }else{
            i++;
        }
        console.log('下:'+ i);
        var liNode = $('#jsmenulist ul li').eq(i);
        liNode.addClass('now');
        if( i==0 ){
            var defaultV = $('#jsmenulist ul li').eq(0).attr('data-val');
            $('.inp_match').val(defaultV);
        }
        else{
            $('.inp_match').val(liNode.find('span').text() +'  '+liNode.find('em').text());
        }
        $('div.addmenu').data('key',i);
    }
    if( keycode == 13 ){
        if( $('#jsmenulist li').hasClass('now') || $('#jsmenulist li').hasClass('now')){
          $('#jsmenulist li.now').addClass('adopt');
          $('.inp_match').addClass('inp_adopt');
          $('#jsmenulist').hide();
        }
         $("#js_addmenu").trigger("click");
          enter =1;
    }
    
    
}



/*鼠标划过*/
function mouseOver(){
    $('#jsmenulist li').hover(function(){
        $('#jsmenulist li').removeClass('now');
        var index = $(this).index();
        $('div.addmenu').data('key',index);
        $(this).addClass('now');
    },function(){
        $(this).removeClass('now');
    });
}




/*ajax请求 查找当前菜品*/
var timer;
function curmenuSearch(){
    var inpVal = $('.inp_match').val();
    var dishes=dishesList();
    console.log(dishes);
    $.ajax({
        type:'GET',
        url:'http://xuexia.redcms.com/order/edit.html',
        data:'act=select&sname='+inpVal+'',
        success:function(result){
            eval('var data='+ result);
            console.log('result:'+result);
            console.log('response数据:'+data.length);
            if( data.length == 0 ){ $('.addmenu').find('i').text('此菜品不存在!'); return;}
            if( data.length > 1){ $('.addmenu').find('i').text('需填写菜品编号!'); return;}
            if( data.length == 1){
                if( inpVal.toLowerCase() != data[0].menu_code.toLowerCase()){ $('.addmenu').find('i').text('此菜品不存在!'); return;}
                console.log($('.inp_adopt').html());
                for(var i=0;i<dishes.length;i++){
                    var num = dishes[i];
                    console.log(dishes[i]);
                    //console.log(data[i]);
                    if( num != data[0].id){
                        continue;
                    }
                    else{
                        $('.addmenu').find('i').text('此菜品已存在!');
                        $('.inp_match').removeClass('.inp_adopt');
                        $('.inp_match').val('');
                        console.log(i);
                        $('ul.uldishes li').removeClass('already');
                        $('ul.uldishes li').eq(i).addClass('already');
                        var offsetY = $('ul.uldishes li.already').height() *  i;
                        console.log(offsetY);
                        $('ul.uldishes').scrollTop(offsetY);
                        return;
                    }
                }
                console.log('append');
                $('ul.uldishes').append('<li class=\'added\' data-id=\''+data[0].id+'\'><div><span class=\'num\'>'+data[0].menu_code+'</span><span class=\'dishname\'>'+data[0].menu_name+'</span><span class=\'per\'>'+data[0].order_price+'</span><span class=\'amount\'><a class=\'minus\' href="#">-</a><input class=\'inp inp_short\' value=\'1\' type=\'text\'><a class=\'add\' href=\'#\'>+</a></span><span class=\'price\'>'+data[0].order_price+'</span><span><a class=\'del\' href=\'#\'>删除</a></span></div><div class=\'msg\'><input class=\'inp inp_long inp_msg\' value=\'此处为菜品备注信息\' type=\'text\'></div></li>');    
                stripesTable();        
            }

        }
    })

}




/*餐费*/
function mealFee(){
    var dish_amount = $('.uldishes li').length;
    var total_meal = 0;
    for(var i=0;i<dish_amount;i++){
        var per_meal = parseInt($('.uldishes li').eq(i).find('.price').text());
        total_meal += per_meal; 
    }
    $('.mealfee').find('strong').text(total_meal.toFixed(2));
}

/*补差金额*/
function addFee(){
    if( $('.supply').val() == '' ){ $('.supply').val(0);}
    var txt = $('.supply').val();
    var supply = parseInt(txt);
    $('.supplement').find('strong').text(supply.toFixed(2));
}

/*服务费*/
function service(){
    var mealfee = parseInt( $('.mealfee').find('strong').text());
    var supple = parseInt( $('.supplement').find('strong').text());
    var fee = (mealfee + supple)* 0.15;
    $('.service').find('strong').text(fee.toFixed(2));

}


function total(){
    var mealfee = parseInt( $('.mealfee').find('strong').text());
    var supple = parseInt( $('.supplement').find('strong').text());
    var service = parseInt( $('.service').find('strong').text());
    var fee = mealfee + supple + service;
    $('.total').find('strong').text(fee);
}

/*每个菜的总价*/
function totalPrice(elem){
    var liNode = elem.parents('li');
    var per = parseInt(liNode.find('.per').text());
    var num = parseInt( liNode.find('.amount .inp_short').val());
    var total = (parseInt(per) * parseInt(num)).toFixed(2);
    liNode.find('.price').text(total);
}




/*服务台 高度*/
function serviceHeight(){
    $('.maincon').css('height','auto !important');
    var screenY = $(window).height();
    $('.service').css('min-height',screenY-30);
}


/*判断有无订单号*/
var order_id;
function getOrder(){
    //alert('请求订单');
    $.ajax({
        type:'GET',
        url:'http://xuexia.redcms.com/order/edit.html',
        data:'act=get',
        success:function(msg){
            eval('var json='+msg);
            if( json == null || json == '' ){ alert('没订单了'); return;}    
            /*订单号存在*/
                $.fancybox({
                    'href':'#order_details',
                    'overlayOpacity':'0.5',
                    'showCloseButton':false
                });
                var start = new Date();
                countDown(start);
                $('.uldishes li').remove();
                $('.inp_match').removeClass('inp_adopt');
                /*将数据塞入*/
                order_id = json.res.id;
                $('.order_no strong').text(json.res.order_no);
                $('.create_time strong').text(json.res.create_time);
                $('.roominfo li.tel').find('strong').text(json.res.target_telephone);
                $('.roominfo li.address').find('.inp').val(json.res.target_address);
                //$('.roominfo li.deliver').find()
                $('.roominfo li.guest').find('strong').text(json.res.target_guest_name);
                $('.roominfo .address2').find('strong').text(json.res.target_address);
                for(var i=0;i<json.resinfo.length;i++){
                    var menuID = json.resinfo[i].menu_id;
                    var menuCode = json.resinfo[i].menu_code;
                    var menuName = json.resinfo[i].menu_name;
                    var count = json.resinfo[i].count;
                    var unitPrice = json.resinfo[i].unit_price;
                    var remark = json.resinfo[i].remark;
                    var price = unitPrice * count;
                    $('ul.uldishes').append('<li data-id=\''+menuID+'\'><div><span class=\'num\'>'+menuCode+'</span><span class=\'dishname\'>'+menuName+'</span><span class=\'per\'>'+unitPrice+'</span><span class=\'amount\'><a class=\'minus\' href="#">-</a><input class=\'inp inp_short\' value=\'1\' type=\'text\'><a class=\'add\' href=\'#\'>+</a></span><span class=\'price\'>'+price+'</span><span><a class=\'del\' href=\'#\'>删除</a></span></div><div class=\'msg\'><input class=\'inp inp_long inp_msg\' value=\'1\' type=\'text\'></div></li>');            
                }
                stripesTable();
                /*送餐时间段*/
                var timeBox = timeBuckets();
                console.log(timeBox);
                $('.deliver em').html(timeBox);
                /*合计费用*/
                $('.mealfee strong').text(json.res.money);
                $('.supplement strong').text(0);
                $('.service strong').text(0);
                $('.total strong').text(json.res.money);
                /*遮罩层*/
                $(self.parent.frames['header'].document.body).append("<div class='masklayer'></div>");
                $(self.parent.frames['subnav'].document.body).append("<div class='masklayer'></div>");
                  
            }
    })


}

/*送餐时间段*/
function timeBuckets(){
    var html = '<select name=\'time\'>';
    var arr =[]; //'00:00--00:30','00:30--01:00'
    /*列出所有的时间*/
    for(var i=0;i<24;i++){
        arr[2*i] = checknum(i)+':00--'+checknum(i)+':30';
        arr[2*i+1] = checknum(i)+':30--'+checknum(i+1)+':00';
//        html+='<option>'+arr[2*i]+'</option>'
//            + '<option>'+arr[2*i+1]+'</option>';        
    }
    /*现在的时间以后*/
    var today = new Date();
    var hours = today.getHours();
    var minutes = today.getMinutes();
    if(minutes==0){
        start = 2*hours +1;
    }else if(minutes<=30 && minutes > 0){
        start = 2*(hours + 1);
    }else{
         start = 2*(hours+1)+1;
    }
    for(var j=start;j<48;j++){
        html+='<option>'+arr[j]+'</option>'
    }
    return html;
}

function checknum(i){
    if(i<10){
        i = '0'+i;
    }
    return i;
}


/*隔行换色*/
function stripesTable(){
    $('ul.uldishes li').removeClass('c1 c2');
    $('ul.uldishes li:odd').addClass('c2');
    $('ul.uldishes li:even').addClass('c1');
    //$('ul.uldishes li:last').css('border','none');
}


/*遮罩层消失*/
function maskLayer(){
    $(self.parent.frames['header'].document.body).find('.masklayer').fadeOut('slow');
    $(self.parent.frames['subnav'].document.body).find('.masklayer').fadeOut('slow');
}

/*倒计时*/
var flag = 0;
function countDown(startTime){
    console.log(startTime);
    var endTime = startTime;
    endTime.setMinutes(endTime.getMinutes()+1);  //设置时间为5分钟后
    console.log(endTime);
    
    /*判断有无键盘鼠标操作*/
    $('#order_details').keydown(function(){
        /*重置计时*/
        if( flag == 0){ console.log('keydown');timerReset(); }
    });
    $('#order_details').mouseover(function(){
        /*重置计时*/
        if( flag == 0){ console.log('mouseover');timerReset(); }
    })
    $('#order_details').click(function(){
        /*重置计时*/
        if( flag == 0){ console.log('click'); timerReset(); }
    })

    /*在没有操作的情况下*/
    oddTime(endTime);
    /*每隔30秒发送重置计时请求*/
    resetRequest();
    
    
    
}

/*重置计时*/
function timerReset(){
    flag = 1;
    setTimeout(function(){
        flag = 0;
    },31000);
}

/*重置标志*/
var request;
function resetRequest(){
    request = setTimeout(function(){
        console.log('flag请求标志'+flag);
            $.ajax({
                type:'GET',
                url:'http://huangchen.redcms.com/order/edit.html',
                data:'act=check&oid='+order_id+'&flag='+flag+'',
                success:function(){
                    console.log('30秒发送一个请求标志');
                },
                error:function(){
                    console.log('不向服务器发送重置请求标志')
                }
            });        
        if( flag == 1){
            clearTimeout(movement);
            $('#minutes').text('4分');
            //$('#seconds').hide();
            var startTime = new Date();
            var endTime = startTime;
            endTime.setMinutes(endTime.getMinutes()+1);  //设置时间为5分钟后        
            oddTime(endTime);
        }
        resetRequest();
    },30000);
}



/*剩余时间*/
var movement;
function oddTime(endTime){
    if( movement){ clearTimeout(movement);}
    var current = new Date();
    if( new Date(endTime) <= current){
        alert(endTime); 
        alert(current);
        clearTimeout(movement);
        $.fancybox.close();
        $('.btn_order').attr('key',0);
        $('.btn_order').addClass('btn_start');
        $('.layer_center').removeClass('refuse');
        /*遮罩层消失*/
        maskLayer();
        setTimeout(function(){
            $.fancybox({
                'href':'#order_tips02',
                'onClosed' : function() { maskLayer();}
            });
            $('.btn_order').attr('request',0);
            $('.btn_order').removeClass('btn_stop');
            $('.btn_order').addClass('btn_start');
            $('.layer_center').removeClass('refuse');
            
        },500);
        return;
    }
    var oddTime = new Date(endTime) - current;
    var oddMinutes = parseInt(oddTime/1000/60);
    $('#minutes').text(oddMinutes + '分');
    //if( new Date(endTime) - current <= 60000){
        $('i#seconds').show();
        var oddSeconds = parseInt(oddTime/1000%60);
        $('#seconds').text(oddSeconds +'秒');
    //}
    movement = setTimeout("oddTime('"+endTime+"')" ,1000);

}

/*自动请求订单*/
function getotherOrder(){
    setTimeout(function(){
        $.fancybox.close();
        if( $('.btn_order').attr('request') == 1){
            alert('btn_order的状态');
            getOrder();
        }else{
            alert($('.btn_order').parent().html());
        }
    },2000)
}



$(window).resize(function(){
    serviceHeight();
});

 

posted @ 2014-07-11 21:40  Western Journey  阅读(498)  评论(0编辑  收藏  举报