(function ($) {

    $(document).ready(function() {
        $('#login_username').focus();
        // Open formview
          $("#form_link").click(function(){
            
            $("#form_link").slideUp(500,function(){
                $("#form_fill").slideDown(500);
                $('[name=text]').focus();
            });
            return false;
          });
        
        //popup
        $('.edit_help').click(function(){
            var marker = this;
            $('<div id="dim_background" style="display: none;"></div>').appendTo('body');
            $('#dim_background')
            .css("height", $(document).height())
            .show()
            .css({
                'position' : 'fixed',
                'top' : '0px',
                'left' : '0px',
                'border' : '2px solid #333333',
                'background' : '#cccccc',
                'opacity' : '0.4',
                'width' : '100%',
                'z-index' : '100'
    
            });
            //console.log("window");
            $(window).bind("resize", function(){  
              $("#dim_background").css("height", $(window).height());  
            }); 
            //console.log("window done");
            $('<div id="help_edit_box" style="display: none;"></div>').appendTo('body');
            $('#help_edit_box')
            .show()
            .css({'position' : 'fixed',
                'border' : '2px solid #333333',
                'height' : '200px',
                'width'  : '300px',
                'background' : '#cccccc',
                'z-index' : '200',
                'top' : '50%',
                'left' : '50%',
                'margin-top' : '-100px',
                'margin-left' : '-150px',
                'padding' : '10px'
              });
              //console.log("ajax.php?module=Jb-HelpText&view=edit&name=" + this.id);
            $('#help_edit_box').load("pages/helptext/edit.php?name=" + $(this).attr('name'), function(){
              $('#help_submit').click(function(){
                //console.log('Data: ' + $('#help_name').val() + $('#help_text').val());
                //console.log("ajax.php?module=Jb-HelpText&view=save&name=" + $('#help_name').val() + "&text=" + $('#help_text').val());
                var newText = $('#help_text').val();
                $.get("pages/helptext/save.php?name=" + $('#help_name').val() + "&text=" + $('#help_text').val());
                $('#dim_background').remove();
                $('#help_edit_box').remove();
              });
              $(document).keypress(function(e){
                  if (e.keyCode == 27) {
                      $('#dim_background').remove();
                      $('#help_edit_box').remove();
                  }
              });
            });
        });


        var closetimer = 0;    
        $('[tooltip_old]').hover(function(e){
//          console.log('Mouse over' + $(this).attr('tooltip'));
            var current = this;
            closetimer = window.setTimeout(function(){
                    $("#pop").html($(current).attr('tooltip'));
                    $("#pop").css("top",($(current).offset().top+20)+"px");
                    $("#pop").css("left",$(current).offset().left+"px");
                    $("#pop").show();
                    $("#pop").mouseleave(function(){
                      $("#pop").hide();
                    });
            }, 500);
        }, function(){
          $("#pop").hide();
          window.clearTimeout(closetimer); closetimer = null;
        });

        $('[tooltip]').tooltip({ 
            bodyHandler: function() { 
                return $(this).attr('tooltip'); 
            }, 
            showURL: false,
            fade: 500,
            opacity: 1.00
        });
        
    });

    $(window).load(function(){
    });
    $(document).ready(function() {
      $('<div id="pop" class="tooltip">Text here </div>').appendTo("body"); //.hide().fadeIn("slow");
      $("#pop").hide();
      //console.log('Adding popupdiv...');
      
    });


  $(document).ready(function() {
//  $(window).load(function(){
      //return true;
//    $('<div id="submenu" style="position: absolute;"><div id="inner_submenu"></div></div>').appendTo("body"); //.hide().fadeIn("slow");
//    $("#submenu").hide();
    //$(".mainmenu").css('background','yellow');
      var menuLoaded = false;
      var timeout    = 50;
      var closetimer = 0;
      var ddmenuitem = 0;
      var mainShown = false;
      var subShown = false;
      var menuItem = 0;
      //$('#page').mouseover(function(){
      
      if (menuLoaded) {
        return false;
      }
      menuLoaded = true;
      //$('#hidden_menues').load('ajax/load_menu.php', '',
      //function() {
          $("#menu .mainmenu a").click(function(){
            //return false;
          });
          $(".mainmenu").mouseenter(function(e) {
            if (menuItem != $(this).attr('id')){
              $(".visible_submenu").hide().attr('class','hidden_submenu');
            }
            //console.log('am In: ' + menuItem);
            menuItem = $(this).attr('id');
            if ($("#submenu_" + menuItem).html() != '') {
              if(closetimer) {  window.clearTimeout(closetimer); closetimer = null;}
              if (!mainShown) {
                //console.log('m In: ' + menuItem);
                $("#submenu_" + menuItem)
                .css({'position':'absolute', 'z-index':'2' 
                    }).attr('class','visible_submenu');
                $("#submenu_" + menuItem).css("top",($(this).offset().top+23)+"px");
                $("#submenu_" + menuItem).css("left",($(this).offset().left-10)+"px");
                $("#submenu_" + menuItem).show();
                mainShown = true;
              }
            }
          });
          $(".hidden_submenu").mouseenter(function() {
              //console.log('s In: ' + menuItem);
            subShown = true;
            mainShown = false;
          });
          $(".hidden_submenu").mouseleave(function() {
              //console.log('s Out: ' + menuItem);
            subShown = false;
            closetimer = window.setTimeout(function(){
              if (!subShown && !mainShown) {
                $("#submenu_" + menuItem).hide().attr('class','hidden_submenu');
              }
            }, timeout);
          });
          $(".mainmenu").mouseleave(function(){
              //console.log('m Out: ' + menuItem);
            mainShown = false;
            closetimer = window.setTimeout(function(){
              if (!subShown && !mainShown) {
                $("#submenu_" + menuItem).hide().attr('class','hidden_submenu');
              }
            }, timeout);
          });
      //});
      //});
  });

  $('document').ready(function(){
    $('cite').each(function(){
      $(this).attr('height', $(this).height());
    });
    $('cite').css({'overflow': 'hidden'});
    $('cite').animate({height: '14px'}, 500);
    $('cite').hover(function(){
      //$('cite').css('height', '');
      var height = $(this).attr('height')
      $(this).stop().animate({height: height+'px'}, 500);
    },
    function(){
      $(this).stop().animate({height: '10'}, 500);
    });
  });


  $('document').ready(function(){
    $('#vip_activate').change(function(){
      var n = $(this).val()
      if (n > 0) {
        $('#vip_club').show();
      } else {
        $('#vip_club').hide();
      }
    });
  });
  
  
  // calendar.js...
  $('document').ready(function(){

      if (!$('[name=pay]').attr('checked')) {
          $('#event_cost').hide();
      }
      $('[name=pay]').click(function(){
        if (!$('[name=pay]').attr('checked')) {
            $('#event_cost').hide();
        } else {
            $('#event_cost').show();
        }
      });


    $('.language_key').click(function(){
        $('<div id="dim_background" style="display: none;"></div>').appendTo('body');
        $('#dim_background')
        .css("height", $(document).height())
        .show()
        .css({
            'position' : 'fixed',
            'top' : '0px',
            'left' : '0px',
            'border' : '2px solid #333333',
            'background' : '#cccccc',
            'opacity' : '0.4',
            'width' : '100%',
            'z-index' : '100'
  
        });
        //console.log("window");
        $(window).bind("resize", function(){  
          $("#dim_background").css("height", $(window).height());  
        }); 
        //console.log("window done");
        $('<div id="language_box" style="display: none;">' 
          + $(this).attr('id')
          + ' - ' + $(this).attr('lang') 
          + ' <textarea id="language_translation"></textarea><input type="submit" id="translation_save" value="Spara"></div>').appendTo('body');
        $('#language_translation')
        .css({'width'  : '270px',
              'height' : '150px'});
        $('#language_box')
        .show()
        .css({'position' : 'fixed',
            'border' : '2px solid #333333',
            'height' : '200px',
            'width'  : '300px',
            'background' : '#cccccc',
            'z-index' : '200',
            'top' : '50%',
            'left' : '50%',
            'margin-top' : '-100px',
            'margin-left' : '-150px',
            'padding' : '10px'
          });
    });

  });

})(jQuery);
