function showAlert(m) {
  alert(m)
  }
function showMsg(m) {
  $('#msg').html(m).absCenter().show();
  setTimeout(function(){
    $('#msg').fadeOut('slow')
    },2500);
}
function isset(x)
{ 
  return typeof(x)!='undefined';
}

atip={
  on:true,
  id:''
}
/* mouse events */
$(document).mouseover(function(e){
  $targ=$(e.target);
  if($targ.attr('rollover')) {
    var bp=$targ.css('backgroundPosition');
    if(!isset(bp)) bp='0 0';
    var bp2=bp.split(' ');
    $targ.data('bp',bp).css('backgroundPosition',bp2[0]+' -'+$targ.attr('rollover')+'px');
  }
  if($targ.attr('rollover2')) {
    var bp=$targ.css('backgroundPosition');
    if(!isset(bp)) bp='0 0';
    $targ.data('bp',bp).css('backgroundPosition','-'+$targ.attr('rollover2')+'px 0');
  }
  if($targ.attr('tip') && atip.on) {
    $targ.triggerHandler('tip');
    if(e.target.id=='') $targ.attr('id','rndid#'+Math.random())
    $('#tip').html($targ.attr('tip')).stop(1,1).removeClass().css({
      left:0,
      top:0
    });
    if($targ.attr('ctip')) $('#tip').addClass($targ.attr('ctip'));
    var xy=$targ.offset();
    var th=$('#tip').outerHeight(), tw=$('#tip').outerWidth();
    xy.left-=Math.round(tw/2-$targ.outerWidth()/2);
    if(xy.top-th<0) xy.top+=$targ.outerHeight();
    else xy.top-=th;
    if(xy.left<0) xy.left=0;
    if(xy.left+tw>786)xy.left=786-tw;
    $('#tip').css(xy);
    atip.id=e.target.id;
    setTimeout('showTip("'+atip.id+'")',200);
  }
  if($targ.attr('wmap')) map.highlightMap($targ.attr('wmap'));
});
$(document).mouseout(function(e){
  $targ=$(e.target);
  if($targ.attr('rollover')) $targ.css('backgroundPosition',$targ.data('bp'));
  if($targ.attr('rollover2')) $targ.css('backgroundPosition',$targ.data('bp'));
  if($targ.attr('tip')) hideTip();
});
$(document).mousemove(function(e){
  });
$(document).click(function(e){
  if($(e.target).attr('tip')) hideTip();
});

/* tips helper functions */
function showTip(id) { 
  if(id==atip.id) $('#tip').fadeIn('fast');
}
function hideTip() { 
  $('#tip').stop(1,1).fadeOut('fast'); atip.id='';
}

(function($) {
  $.each({
    /* centers any absolute positioned element
         */
    absCenter: function() {
      var $$ = $(this);
      $$.css({
        left:Math.round($(window).scrollLeft()+$(window).width()/2-$$.width()/2),
        top:Math.round($(window).scrollTop()+$(window).height()/2-$$.height()/2)
        });
    },
    /* insert text at current cursor position in textarea
         original: http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript
        */
    insertAtCaret: function(myValue){
      if (document.selection) {
        this.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        this.focus();
      }
      else if (this.selectionStart || this.selectionStart == '0') {
        var startPos = this.selectionStart;
        var endPos = this.selectionEnd;
        var scrollTop = this.scrollTop;
        this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
        this.focus();
        this.selectionStart = startPos + myValue.length;
        this.selectionEnd = startPos + myValue.length;
        this.scrollTop = scrollTop;
      } else {
        this.value += myValue;
        this.focus();
      }
    },
    log: function (msg) {
      console.log("%s: %o", msg, this);
      return this;
    },
    rot13: function() {
      $(this).each(function() {
        $(this).text($(this).text().replace(/[a-z0-9]/ig, function(chr) {
          var cc = chr.charCodeAt(0);
          if (cc >= 65 && cc <= 90) cc = 65 + ((cc - 52) % 26);
          else if (cc >= 97 && cc <= 122) cc = 97 + ((cc - 84) % 26);
          //else if (cc >= 48 && cc <= 57) cc = 48 + ((cc - 43) % 10);
          return String.fromCharCode(cc);
        }));
      });
      return this;
    }
  }, function(name, fn){
    jQuery.fn[ name ] = function(){
      return this.each( fn, arguments )
      };
  });
})(jQuery);


function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
