function init(){
    $('#logform input').blur(function (){
        $(this).animate({
            'opacity': 0.75
        }, 'slow')
        });
    $('#logform input').focus(function (){
        $(this).animate({
            'opacity': 1
        }, 'slow')
        });

    $('span.form-button').click( function (){
        $('body').addClass('wait');
        var data=JSON.stringify($(this).prev().formToArray());
        var tosend='json={ "command":"'+$(this).attr('id')+'", "data":'+data+'}';
        $.ajax({
            url: "engine_core.php",
            type: "post",
            dataType: "json",
            data: tosend,
            success: function(res){
                if(res.response){ 
                    if(res.layout)
                        tb_show("Uzupełnij dane","popup-change.php?TB_iframe=true&height=300&width=375" ,"null");
                    page.process(res.page, res.layout);
                }else{
                    popup.error2(res.errno);
                    $('input#pass').val("");
                }	
                setTimeout("$('.wait').removeClass('wait'); ", 100); 
            },
            error: function (){ 
                setTimeout("$('.wait').removeClass('wait'); ", 100); 
                popup.error(0);  
            }	
        });
    });
    
    $('#go_login').click( function (){
        $('body').addClass('wait');
        var data=JSON.stringify($('#logform').formToArray());
        var tosend='json={ "command":"go_login", "data":'+data+'}';
        $.ajax({
            url: "engine_core.php",
            type: "post",
            dataType: "json",
            data: tosend,
            success: function(res){
                if(res.response){ 
                    if(res.layout)
                        tb_show("Uzupełnij dane","popup-change.php?TB_iframe=true&height=300&width=375" ,"null");
                    page.process(res.page, res.layout);
                }else{
                    popup.error2(res.errno);
                    $('input#pass').val("");
                }	
                setTimeout("$('.wait').removeClass('wait'); ", 100); 
            },
            error: function (){ 
                setTimeout("$('.wait').removeClass('wait'); ", 100); 
                popup.error(0);  
            }	
        });
    });
    
    $('#go_logout').click( function (){
        $('body').addClass('wait');
        var data=JSON.stringify($('#logform').formToArray());
        var tosend='json={ "command":"go_logout", "data":'+data+'}';
        $.ajax({
            url: "engine_core.php",
            type: "post",
            dataType: "json",
            data: tosend,
            success: function(res){
                if(res.response){ 
                    if(res.layout)
                        tb_show("Uzupełnij dane","popup-change.php?TB_iframe=true&height=300&width=375" ,"null");
                    page.process(res.page, res.layout);
                    FB.logout(function(response) {alert(response);});
                }else{
                    popup.error2(res.errno);
                    $('input#pass').val("");
                }	
                setTimeout("$('.wait').removeClass('wait'); ", 100); 
            },
            error: function (){ 
                setTimeout("$('.wait').removeClass('wait'); ", 100); 
                popup.error(0);  
            }	
        });
    });
}	
	
var popup={
    error:function(_1){
        tb_remove();
        setTimeout(function() {
            tb_show("Uwaga","popup-error.php?num="+_1+"&TB_iframe=true&height=300&width=375" , "null");
        }, 800); 
    },
    error2:function(_1){
        tb_remove();
        setTimeout(function() {
            tb_show("Uwaga","popup-error2.php?num="+_1+"&TB_iframe=true&height=300&width=375" , "null");
        }, 800); 
    },
    hide:function(_1,_2){
        tb_remove();
        var num=0;
        page.flag=true; 
        switch(_1){
            case 4:{
                
                eval(_2);
                break;
            }
            case 5:{
                
                page.get(_2.name,_2.page,_2.attr);
                break;
            }//hide and update
            case 6:{
                var json={
                    "command":_2.name, 
                    "data":_2.data
                    };
                page.send(json);
                break;
            }
            case 3:{
                
                user.get(_2);
                break;
            }
            case 2:{
                
                setTimeout(function() {
                    tb_show("Uwaga","popup-error.php?num="+_2+"&TB_iframe=true&height=300&width=375","null");
                }, 800);
                break;
            }
            case 7:{
                
                setTimeout(function() {
                    tb_show("Info","popup-info.php?num="+_2+"&TB_iframe=true&height=300&width=375","null");
                }, 800);
                break;
            }
            case 8:{
                setTimeout(function() {
                    tb_show("Odpowiedź","popup-newmail.php?nick="+_2+"&TB_iframe=true&height=340&width=380","null");
                }, 400);
                break;
            }
            default:
                break;
        }
			
        if(num) setTimeout(function() {
            tb_show(title,"popup-info.php?num="+_1+"var="+_2+"&TB_iframe=true&height=300&width=375","null");
        }, 800); 
    },
    show:function(_1,_2, _3, _4, _5){
        if(!_4) _4=250; 
        if(!_5) _5=375; 
        tb_remove();
        tb_show(_3,"popup-"+_1+".php?"+_2+"&TB_iframe=true&height="+_4+"&width="+_5,"null");
    },
    image:function(_1){
        tb_show("","popup-showphoto.php?"+_1+"&TB_iframe=true","null");
    }
}
var item={
    tab: new Array ('cmt','rlt'),
    up:function(_1, _2, _3){
        var json={
            'command':'mark_'+item.tab[_2], 
            'data':{
                'id': _1, 
                'val': 1, 
                'type': _3
            }
        };   
    page.send(json);
},
down:function(_1, _2, _3){
    var json={
        'command':'mark_'+item.tab[_2], 
        'data':{
            'id': _1, 
            'val': 0, 
            'type': _3
        }
    };   
page.send(json);
}
}

var page={
    history: ["recipes_mainpage;0;"],
    flag:true, 
    content: {
        'cbox0':'none',		
        'cbox1':'none',
        'cbox2':'none',
        'cbox3':'none',
        'cbox4':'none',
        'cbox5':'none',
        'lbox1':'none',
        'lbox2':'none',
        'lbox3':'recipes_features',
        'rbox1':'none',
        'rbox2':'none',
        'rbox3':'none', 
        'panel': ' ', 
        'tv_body_main': ' '
    },
    user:function(_1){
        this.get('user', 0, "'"+_1+"'");
    },
    req:function(_1, _2){
        if(this.flag){
            $('body').addClass('wait'); 
            this.flag=false;
            $.ajax({
                url: "engine_core.php",
                type: "post",
                dataType: "json",
                data: 'json='+JSON.stringify(_1),
                success: function(res) {
                    eval(res.response); 
                    page.flag=true;
                    setTimeout("$('.wait').removeClass('wait'); ", 100);
                }	
            });
        }
    },	
    send:function(_1, _2){	
        if(this.flag){
            $('body').addClass('wait'); 
            this.flag=false;
            $.ajax({
                url: "engine_functions.php",
                type: "post",
                dataType: "json",
                data: 'json='+encodeURIComponent(JSON.stringify(_1)),
                success: function(res) {
                    page.flag=true; 
                    if (res.response) eval(res.response); 
                    else if(res.page) page.process(res.page);
                    setTimeout("$('.wait').removeClass('wait'); ", 100); 
                },
                error: function (){ 
                    setTimeout("$('.wait').removeClass('wait'); ", 100); 
                    popup.error(0);
                    page.flag=true; 
                }	
            });
        }
    },
    get:function(_1,_2,_3){ 
        if(this.flag){ 	
            $('body').addClass('wait'); 
            this.flag=false;
            if(!_3) _3="default";
            tosend='json='+JSON.stringify({
                'id':_1, 
                'page':_2, 
                'attr':_3
            });
            $.ajax({
                url: "engine_content.php",
                type: "post",
                dataType: "json",
                data: tosend,
                beforeSend: function (req){
                    len=page.history.length;
                    if(len>5) page.history.shift();	
                    page.history.push(_1+";"+_2+";"+_3);
                },
                success: function(res) {
                    if(res.response){
                        if(page.history.length<2) $('#back').animate({
                            'opacity': 0.5
                        });
                        else $('#back').animate({
                            'opacity': 1
                        });
                        var len=res.page.length; 
                        page.process(res.page, res.layout);
                        setTimeout("$('.wait').removeClass('wait'); ", 100); 
                    }else{
                        setTimeout("$('.wait').removeClass('wait'); ", 100);
                        popup.error(res.errno); 
                    }
                    page.flag=true; 
                },
                error: function (){ 
                    setTimeout("$('.wait').removeClass('wait'); ", 100);
                    popup.error(0);
                    page.flag=true;  
                }	
            });
        }
    },
    process: function(_1, _2){
        var len=_1.length; 
        if(len){
            page.show(_1[0], _2);
            for(i=1; i<len; i++) page.show(_1[i]);
        }else page.show(_1);
    },			
    show:function(_1, _2){
        var ele=$(_1.html);
        var newid=ele.attr('id');
        if(page.content[_1.id] && newid!=page.content[_1.id]){
            $('div.'+_1.id).fadeOut('fast',function() {
                $('div.'+_1.id).html(_1.html);
                if(_2)$("#content").attr('class',_2);
                if(_1.js) eval(_1.js);
                $('div.'+_1.id).fadeIn('fast');  
            });
        }else if(_1.flag&1){
            if(_1.flag&4){
                $('div.'+_1.id).html(_1.html);
                eval(_1.js);
            }else{
                $('div.'+_1.id).fadeOut('fast',function(){
                    $('div.'+_1.id).html(_1.html);
                    if(_2) $("#content").attr('class', _2);
                    if(_1.js) eval(_1.js);
                    $('div.'+_1.id).fadeIn('fast'); 
                });
            }	
        }
        else if(_1.flag&2){
            eval(_1.js);
        }	
        page.content[_1.id]=newid;	
    },
    lookup: function (_1, _2) {
        if(_2.length == 0) $('#suggestions').hide();
        else {
            $.ajax({
                type: "post", 
                url:"engine_functions.php", 
                data:'json={ "command":"get_suggest", "data":{"name":"'+_1+'", "value":"'+_2+'"}}',
                success:function(res) {
                    if(res.response.length>0){
                        FLAG = true; 
                        $('#su_'+res.id).show();
                        $('#sl_'+res.id).html(res.response);
                    }else  $('#su_'+res.id).hide();
                },
                error:function(){
                    self.onError.apply(self,arguments);
                },
                dataType:"json"
            });
        }
    },
    fill:function(_1, _2) {
        $('#'+_1).val(_2);
        setTimeout("$('#suggestions').hide();", 200);
    },
    back: function(){
        if(page.history.length){
            page.history.pop();
            var ele=page.history.pop().split(";");
            page.get(ele[0], ele[1], ele[2]); 
        }
    },
    reload: function(){
        var ele=page.history[page.history.length-1].split(";");
        page.get(ele[0], ele[1], ele[2]); 
    }
}



var user={
    get: function(_1){
        page.get("core_user",0,_1);
    }
}

var fridge={
    add: function (_1){
        if(_1.attr('className')=='fridge_add'){
            if(_1.prev().val()){
                _1.after("<input type='text' class='fridge_ingd'/><img class='fridge_add' onclick='fridge.add($(this))' src='layout/graphic/lodowka_add.png'/>");
                _1.attr('src','layout/graphic/lodowka_delete.png').attr('className', 'fridge_delete');
            }else popup.show('info','num=12', 'Info'); 
        }else{
            _1.prev('input').remove();
            _1.remove();
        }
    }
}
