    var cmscuruser;
    $(document).ready(function(){

        if( window.location.hash.indexOf('#admin') > -1 || $.cookie("cmsadminlogin") ){

           $('body').append('<div id="cmseditbuttons"><a href="" id="cmsbutsave">[UPDATE]</a> &nbsp; &nbsp; <a href="" id="cmsbutcancel">[CANCEL]</a></div><div id="cmsadmin">  <div id="cmsadmintop">    <div id="cmsloginform"><input type="text" name="cmslogin" value="tunnus" />    <input type="password" name="cmspasswd" value="" /> <input type="button" value="GO" id="cmsloginbutton" /></div>    <div id="cmstools">       <div id="cmstools1"><input type="button" id="cmseditonoff" value="Muokkaus ON/OFF" /></div>       <div id="cmstools2">Kirjautuneena; <span id="cmsloginname"></span> <input type="button" value="Logout" id="cmslogoutbutton" /></div>    </div>  </div>    <div id="cmsadminswitch">hide admin</div></div>');

           $('#cmsadmin').show();
           $.cookie("cmsadminlogin", "true");
//           $('body').css('margin-top', $('body').css('margin-top')+40);
           $('body').css('margin-top',  $('#cmsadmin').height());

           $('#cmsloginform input[name=cmslogin]').bind('click', function(){ if($(this).val() == 'tunnus') $(this).val('') });
           $('#cmslogoutbutton').bind('click', function(){
              $.ajax({ dataType: 'json', cache: false, url: "/cms_ajax/User::logout", success: function(res){
//                  console.log(res);
                  if(res.result == false){
                      alert( res.error[0] );
                      return false;
                  } else {    
                    $('#cmstools').hide();
                    $('#cmsloginform').show();
                    $.cookie("cmsadminlogin", null);
                  }
                }
              });
           });
           $('#cmsloginbutton').bind('click', function(){
              $.ajax({ type: 'post', dataType: 'json', cache: false, data: {'u':$('#cmsloginform input[name=cmslogin]').val(), 'p': $('#cmsloginform input[name=cmspasswd]').val()}, url: "/cms_ajax/User::login:p_u,p_p", success: function(res){
                  if(res.result == false){
                      alert( res.error[0] );
                      return false;
                  }
                  //onko oikeus adminoida
                  if(res.result.privilege && res.result.privilege.admin){
                     cmscuruser = res.result;
                     $('#cmsloginform').hide();
                    $('#cmstools #cmsloginname').html(cmscuruser.firstname+' '+cmscuruser.lastname);
                     $('#cmstools').show();
                  }
                }
              });
            });
            $.ajax({ dataType: 'json', cache: false, url: "/cms_ajax/User::getCurrent", success: function(res){
                  if(res.result == false){
                    $('#cmstools').hide();
                    $('#cmsloginform').show();
                  } else {
                    cmscuruser = res.result;
                    $('#cmstools #cmsloginname').html(cmscuruser.firstname+' '+cmscuruser.lastname);
                    $('#cmstools').show();
                    $('#cmsloginform').hide();
                  }
                }
            });
            $('#cmsadminswitch').toggle(function(){ $(this).html('show admin'); $('#cmsadmintop').hide('slow');  }, function(){ $(this).html('hide admin'); $('#cmsadmintop').show('slow');  });

            $('#cmseditonoff').toggle(function(){
                $('.cmsedit').each(function(){
                  $(this).addClass('cmseditactive');
                  $(this).bind('dblclick.cmsedit', bind_dblclick);
                  $.cookie("cmsadminediton", 1);
                });  
              }, function(){ 
                $('.cmsedit').each(function(){
                  $(this).removeClass('cmseditactive');
                  $(this).unbind('dblclick.cmsedit');
                  $.cookie("cmsadminediton", null);
                });
            });

           //jos ollaan muokkaustilassa niin enabloidaan se
           if( $.cookie("cmsadminediton") == 1 ){
              $('#cmseditonoff').click();
           }  

            
        } else { //jos ei #admin urlissa
           $('#cmsadmin').hide();
        }

/*NAPIT*/
    /*CLOSE*/
    $('#cmsbutcancel').bind('click', function(){
        var edit = $(cmscurrenteditable).attr('name').split(".");
        switch(edit[0]){
          case "page":
            switch(edit[1]){
              case "content":
                var tmp_val = $('textarea:first', cmscurrenteditable).val();
                $('textarea:first', cmscurrenteditable).ckeditor( function(){$(this).destroy()} );
                $(cmscurrenteditable).html(tmp_val);
                break;
              case "title":
                $(cmscurrenteditable).html(cmscurrenteditable_orig_content);
                break;
            }   
            break;
        }
        $('#cmseditbuttons').hide();        
        $('#cmseditonoff').attr('disabled', false);
        $('.cmsedit').bind('dblclick.cmsedit', bind_dblclick);
     });
     /*CLOSE END*/
    /*SAVE*/
    $('#cmsbutsave').bind('click', function(){
        var edit = $(cmscurrenteditable).attr('name').split(".");
        switch(edit[0]){
          case "page":
            switch(edit[1]){
              case "content":
                $.ckeditor.update();
                var tmp_val = $('textarea:first', cmscurrenteditable).val();
                $.ajax({ type:'post', data:{'i':edit[2],'k':edit[1],'v':tmp_val} ,dataType: 'json', cache: false, url: "/cms_ajax/Page::updatePage:p_i,p_k,p_v", success: function(res){
//                    console.log("content UPDATE ok");
//                    console.log(res);
                    if(res.result == false){
                      alert( res.error[0] );
                      return false;
                    } else {    
                      $('textarea:first', cmscurrenteditable).ckeditor( function(){$(this).destroy()} );
                      $(cmscurrenteditable).html(tmp_val);
                    }
                  }
                });
                break;
              case "title":
                var tmp_val = $('input:first', cmscurrenteditable).val();
                $.ajax({ type:'post', data:{'i':edit[2],'k':edit[1],'v':tmp_val} ,dataType: 'json', cache: false, url: "/cms_ajax/Page::updatePage:p_i,p_k,p_v", success: function(res){
//                    console.log("title UPDATE ok");
//                    console.log(res);
                    if(res.result == false){
                      alert( res.error[0] );
                      return false;
                    } else {   
//                    console.log(cmscurrenteditable); 
                      $(cmscurrenteditable).html(tmp_val);
                    }
                  },
                  error: function(a,b,c,d){
//                    console.log(a);
//                    console.log(b);
//                    console.log(c);
//                    console.log(d);
                  }
                });
                break;
            }   
            break;
        }
//        console.log("ok");
        $('#cmseditbuttons').hide();  
        $('#cmseditonoff').attr('disabled', false);
        $('.cmsedit').bind('dblclick.cmsedit', bind_dblclick);
        return false;
     });
     /*SAVE END*/
             
    });//docready

    CKEDITOR_BASEPATH = '/ckeditor/';
    var cmscurrenteditable;
    var cmscurrenteditable_orig_content;
    var bind_dblclick = function(){
        cmscurrenteditable = this;
        var tmp_height = $(this).height();
        var tmp_width = $(this).width();
        cmscurrenteditable_orig_content = $(this).html();
        var edit = $(this).attr('name').split(".");
//        console.log(edit);
        $('.cmsedit').unbind('dblclick.cmsedit');
        $('#cmseditbuttons').css({top: $(this).offset().top - $('#cmseditbuttons').height(), left: $(this).offset().left}).show();
        $('#cmseditonoff').attr('disabled', true);
        switch(edit[0]){
          case "page":
            switch(edit[1]){
              case "content":
                $(this).wrapInner('<textarea name="ckeditor" />');
                $('textarea:first',this).val(cmscurrenteditable_orig_content).css('width', tmp_width-2).css('height', tmp_height+50 );
                $('textarea', this).ckeditor( {'toolbar':'CMS1'} );
                break;
              case "title":
                $(this).html('<input type="text" class="cmseditinput" name="title" />')
                $('input', this).val(cmscurrenteditable_orig_content).css('width', tmp_width-2).css('height', tmp_height );
                break;
            }   
            break;
        }
    }
    


