function popup(url) {
  newwindow=window.open(url,'name','height=400,width=600');
  if (window.focus) newwindow.focus();
  return false;
}
function collapse(id) { jQuery('#'+id).slideToggle(); }
function fade(id,value) { if(value>0) jQuery('#'+id).hide().fadeIn('slow'); else jQuery('#'+id).show().fadeOut('slow'); }
function ajax(u,s,t) {
  var query="";
  for(i=0; i<s.length; i++) { 
     if(i>0) query=query+"&";
     query=query+encodeURIComponent(s[i])+"="+encodeURIComponent(document.getElementById(s[i]).value);
  }
  jQuery.ajax({type: "POST", url: u, data: query, success: function(msg) { if(t==':eval') eval(msg); else document.getElementById(t).innerHTML=msg; } });  
}
String.prototype.reverse = function () { return this.split('').reverse().join('');};
function web2py_ajax_init() {
  jQuery('.hidden').hide();
  // jQuery('.error').hide().slideDown('slow');
  jQuery('.flash').click(function() { jQuery(this).fadeOut('slow'); return false; });
  jQuery('input.integer').keyup(function(){this.value=this.value.reverse().replace(/[^0-9\-]|\-(?=.)/g,'').reverse();});
  jQuery('input.double').keyup(function(){this.value=this.value.reverse().replace(/[^0-9\-\.]|[\-](?=.)|[\.](?=[0-9]*[\.])/g,'').reverse();});
  jQuery("input[type='checkbox'].delete").each(function(){jQuery(this).click(function() { if(this.checked) if(!confirm("{{=T('Sure you want to delete this object?')}}")) this.checked=false; });});
  try {jQuery("input.date").focus( function() {Calendar.setup({
     inputField:this.id, ifFormat:"{{=T('%Y-%m-%d')}}", showsTime:false
  }); }); } catch(e) {};
  try { jQuery("input.datetime").focus( function() {Calendar.setup({
     inputField:this.id, ifFormat:"{{=T('%Y-%m-%d %H:%M:%S')}}", showsTime: true,timeFormat: "24"
  }); }); } catch(e) {};
  try { jQuery("input.time").clockpick({
      starthour:0, endhour:23, showminutes:true, military:true
  }); } catch(e) {};
};
jQuery(document).ready(function(){
   jQuery('.flash').hide();
   if(jQuery('.flash').html()!='') jQuery('.flash').slideDown();
   web2py_ajax_init();});

function web2py_trap_form(action,target) {
   jQuery('#'+target+' form').each(function(i){
      var form=jQuery(this);
      jQuery(':submit,.submit',this).click(function(){
         jQuery('.flash').hide().html('');
         web2py_ajax_page('post',action,form.serialize(),target);
         return false;
      });
   });
}
function web2py_ajax_page(method,action,data,target) {
  jQuery.ajax({'type':method,'url':action,'data':data,
    'beforeSend':function(xhr){
      xhr.setRequestHeader('web2py-component-location',document.location);
      xhr.setRequestHeader('web2py-component-element',target);},
    'complete':function(xhr,text){
      command=xhr.getResponseHeader('web2py-component-command');
      if(command) eval(command);
      flash=xhr.getResponseHeader('web2py-component-flash');
      if(flash) jQuery('.flash').html(flash).slideDown();
      },
    'success': function(text) {
      jQuery('#'+target).html(text);
      web2py_trap_form(action,target);
      web2py_ajax_init();
      }
    });
}
function web2py_component(action,target) {
    jQuery(document).ready(function(){ web2py_ajax_page('get',action,null,target); });
}

// flash a message in the top right corner
flash = function() {
  var flash = $('#flash')
  // if args are empty, hide message bubble
  if(arguments.length == 0)
    flash.fadeOut()
  else { // otherwise, slide in the new message
    message = $.makeArray(arguments).join(' ')
    if(flash.is(':visible'))
      flash.fadeOut('normal', function() {
        $(this).html(message).slideDown()
      })
    else
      flash.html(message).slideDown()
  }
}

// fix buggy js eval
reval = function(t) { return eval('(' + t + ')') }

/* TODO move these css rules to stylesheet */
$(document).ready(function() {
  if($.browser.msie)
    $('body')
      .append($('<div></div>')
                .css({ height: '100%',
                       width: '100%',
                       position: 'fixed',
                       top: 0,
                       left: 0,
                       backgroundColor: '#000',
                       opacity: 0.5 }))
      .append($('<div></div>')
                .append('<h2>Unsupported Browser</h2><br/>')
                .append('<p>Sorry, but Internet Explorer is not supported by LakeBase at this time.</p><br/>')
                .append('<p>Please try again using a different browser.</p>')
                .css({
                  border: '5px solid #304085',
                  background: '#fff',
                  padding: '0 20px 20px',
                  width: 400,
                  position: 'fixed',
                  top: '50%',
                  left: '50%',
                  marginTop: -100,
                  marginLeft: -220 }));
})

// default options for jquery ui dialog boxes
dialog_options = { show: 'fade', hide: 'fade', autoOpen: false }

$(document).ready(function() {
  var menu = $('ul.web2py-menu ul.web2py-menu-vertical').hide()
  $('ul.web2py-menu > li.web2py-menu-expand:first-child > a').hover(function() {
    $(this).addClass('hover')
  }, function() {
    if(!menu.is(':visible'))
      $(this).removeClass('hover')
  }).click(function() {
    if(menu.is(':visible')) {
      $(this).removeClass('hover')
      menu.hide()
    } else {
      $(this).addClass('hover')
      menu.show()
    }
    return false
  })
})

