/* idTip ~ Duels Version */
(function(){
var version = "1.0",
    dep = {"jQuery":"http://"+STATIC_SERVER+"/js/jquery-1.2.2.pack.js",
           "jQuery.dimensions":"http://"+STATIC_SERVER+"/js/jquery.dimensions.js"},
    attachToBody = false;

var init = function(){


(function($){ 

  // Load
  if($.idTip && $.idTip.version*1 >= version*1) return; //already loaded
  $.idTip = {version:version};

  // Defaults 
  $.idTip.defaults = {
    show:{ fn:null, event:"!mouseover", effect:"~show", speed:0 },
    hide:{ fn:null, event:"!mouseout", effect:"~hide", speed:0 },
    html:'<div class="idTooltip"><div class="inner"></div></div>',
    title:"",
    offset:10,
    delay:100,
	sticky:null,
	zoomi:null
  };

  // Main
  $.fn.idTip = function(){
    if(!this.length) return this; //nothing to do

    // Loop Arguments matching options 
    var e,f,t,p,s={show:{},hide:{}};
    $.extend(true,s,$.idTip.defaults);
    for(var i=0; i<arguments.length; ++i) { 
      var a=arguments[i]; 
      switch(a.constructor){ 
        case Boolean: $.idTip.remove.call(this); if(!a) return this; break;
        case Object: $.extend(true,s,a); break; 
        case Number: s[(p=!p)?'offset':'delay'] = a; break;
        case Function: s[(f=!f)?'show':'hide'].fn = a; break;
        case String: switch(a.charAt(0)){
          case "!": s[(e=!e)?'show':'hide'].event = a; break;
          case "~": a = a.match(/~|[A-Za-z]+|\d+/g);
            for(var k=0; k<a.length; ++k) {
			  if(/~/.test(a[k])) t=!t;
			  else if(/\d+/.test(a[k])) s[t?'show':'hide'].speed = a[k];
			  else s[t?'show':'hide'].effect = "~"+a[k];
			} break;
		  case "<": if(s.indexOf("</>")==0) { s.html = a.substr(3); break; }
          default: s.title = a.replace(/^ */,''); break;
        } break;
      }
    }

    // Bind events
    this.bind("mousemove",$.idTip.move);
    if(s.show.event) this.bind(s.show.event.substr(1),$.idTip.show);
    if(s.hide.event) this.bind(s.hide.event.substr(1),$.idTip.hide);

    // Save settings (defaults < options < metadata < title)
    this.each(function(){
      var n = $.extend({},s);
      var o = $.data(this,'idTip');
      if($.metadata) $.extend(true,n,$(this).metadata());
      $.data(this,'idTip',n);
      if(o && o.e) { //currently active
        n.x = o.x;
        n.y = o.y;
        o.e.hide();
		if(s.show.event) $(this).trigger(s.show.event.substr(1));
      }
    });

    return this; //chainable
  }

  // Show
  $.idTip.show = function(m, offsetDimensions){
    var s = $.data(this,'idTip');
    if(!s) return false;

    var e = true;
    if(s.show.fn) e = s.show.fn.call(this);
    if(e===true) {
      e = this.title || s.title;
      if(!e && s.zoomi && /img/i.test(this.tagName) && this.src) e=this.src;
	  if($.browser.msie) { s.title = e; this.title = ""; }
    }
    if(!e) return false;

    if(s.timer) s.timer = clearTimeout(s.timer);
    if(e.constructor == String) {
      if(e.charAt(0)=='#') e = $(e);
      else {
        var id = "idTip"+$.data(this);
        $('#'+id).remove();
        if(/^\S*\.(jpe?g|gif|png|bmp)$/.test(e))
          e=$('<img/>').attr({src:e}).load(function(){ $.idTip.move.call($.data(s.e[0],'idTip'),m); });
        e=$('<div/>').html(e);
		if(s.html) e.wrapInner(s.html);
		e=e.find('>').attr({id:id}).hide().appendTo("body");
        if(s.zoomi) e.addClass("zoom2");
        if(s.sticky) e.addClass("sticky");
      }
    } else e=$(e);
    if(s.e && s.e==e) console.log(s.e);
    if(s.e) s.e.hide();
    s.e = e; //save reference
    $.data(s.e[0],'idTip',this);
	// e.css("position","fixed"); // Not supported by IE6... Gah.
    e.css("position","absolute");
	if(s.show.event) e.bind(s.show.event.substr(1),$.idTip.stick);
    if(s.hide.event) e.bind(s.hide.event.substr(1),$.idTip.unstick);
    $.idTip.move.call(this,m);
    if(s.show.effect.contructor==Function) s.show.effect.call(s.e);
    else s.e[s.show.effect.substr(1)](s.show.speed);
    return false;
  }

  // Stick
  $.idTip.stick = function(){
    var s = $.data(this,'idTip');
    if(!s) return;
    s = $.data(s,'idTip');
    if(!s || !s.timer) return;
    if(s.sticky || s.zoomi) s.timer = clearTimeout(s.timer);
  }

  // Unstick
  $.idTip.unstick = function(){
    var s = $.data(this,'idTip');
    if(s) $.idTip.hide.apply(s,arguments);
  }

  // Hide
  $.idTip.hide = function(){
    var self = this;
    var s = $.data(this,'idTip');
    if(!s || !s.e) return false;

    var f = function(){
      if(!s || !s.e) return;
      s.timer = clearTimeout(s.timer);
      if(self != $.data(s.e[0],'idTip')) return;
      if(s.hide.fn && !s.hide.fn.call(self)) return false;
      s.e.unbind(s.hide.event.substr(1),$.idTip.hide);
      if(s.hide.effect.contructor==Function) s.hide.effect.call(s.e);
      else s.e[s.hide.effect.substr(1)](s.hide.speed);
      s.e = null;
	  if($.browser.msie) self.title = s.title;
    }
    s.timer = setTimeout(f,s.delay); //don't instantly hide
    return false;
  }

  // Move
  $.idTip.move = function(m){
    var s = $.data(this,'idTip');
    if(!s || !s.e) return false;
    s.x = m && m.clientX || s.x;
    s.y = m && m.clientY || s.y;
    var self = $(this);
    var x,y,l,t,w,h,tip = s.e;
    var tw = tip.width();
    var th = tip.height();
    // Center
    if(s.sticky || s.zoomi) {
      var o = self.offset();
      l = o.left;
      t = o.top;
      w = self.outerWidth()/2;
      h = self.outerHeight()/2;
      x = l + w;
      y = t + h;
      if(s.zoomi) {
        x -= tw/2;
        y -= th/2;
      }
	  test = self;
    } else {
      w = s.offset;
      h = s.offset;
      x = $().scrollLeft() + s.x;
      y = $().scrollTop() + s.y;
    }
    // Adjust Corner
    if(!s.zoomi) {
      x = (x + w + tw > $(window).width())? x - w - tw : x + w;
      y = (y - h - th < $(window).scrollTop())? y + h : y - h - th;
    }
	// Adjust coordinates
	/*if(attachToBody) {
		y += 100;
	}*/
    tip.css({top:y||-8000,left:x||-8000});
    return false;
  }

  // Unbind
  $.idTip.remove = function(){
    return this.each(function(){
      var s = $.data(this,'idTip');
      if(!s) return;
      var self = $(this);
      self.unbind("mousemove",$.idTip.move);
      if(s.show.event) self.unbind(s.show.event.substr(1),$.idTip.show);
      if(s.hide.event) self.unbind(s.hide.event.substr(1),$.idTip.hide);
      if(s.e) s.e.hide();
      $.removeData(this,'idTip');
    });
  }

  // Auto-run
  $(function(){
    $(".idTip, .shop_icon").idTip();
    $(".idTips").idTip({sticky:true});
    $(".zoomi").idTip({zoomi:true});
	show_info = function(e,i,ab) {
	  e = ($.browser.msie)?event.srcElement:e.target;
	if (ab) {
		attachToBody = true;
		// Attach tooltip to document body
		$('.info_over')
		.css({'z-index': 4000})
		.prependTo(document.body);
		// $(e).prependTo(document.body);
	}
	  $(e).idTip(true,'#'+i,{show:{event:false},hide:{event:false}});
	  $.idTip.show.call(e);
	}
	hide_info = function(i){ $('#'+i).hide(); }
  }); 

})(jQuery);



} //init

// Check Dependencies
var check = function(o,s){
  s = s.split('.');
  while(o && s.length) o = o[s.shift()];
  return o;
}

// Add Script
var head = document.getElementsByTagName("head")[0];
var add = function(url){
  var s = document.createElement("script");
  s.type = "text/javascript"; s.src = url;
  head.appendChild(s);
}

// Load Dependencies
var ok=true;
for(d in dep) {
  if(check(this,d)) continue;
  ok=false;
  add(dep[d]);
} if(ok) return init();

// Reload Self
var s = document.getElementsByTagName('script');
s = s[s.length-1];
add(s.src);

})();

