(function(){
var IE = false /*@cc_on || true @*/;
var IE6 = false /*@cc_on || @_jscript_version < 5.7@*/;
var TRANSPARENT_IMAGE = "/i/empty.gif";
var DEF_BG = 'c';
var DEF_DISPLAY_TYPE = "block";
var SLOT_C = 0;

var slotsParamsStorage = {};
var emptySlotPm        = {iw: 1, ih: 1, bg:'n', img:'/i/empty.gif', cnt:''};

var menuElement        = document.createElement("div");
menuElement.innerHTML  = "<div class=\"SlotControlButtonContainer\" ><div class=\"SlotControlButtonCContainer\"><div class=\"SlotControlButton_quote\"></div><div class=\"SlotControlButton_info\"></div><div class=\"SlotControlButton_drop\"></div></div></div>";
menuElement            = menuElement.firstChild;
menuElement.bQuoteItem = menuElement.firstChild.childNodes[0];
menuElement.bInfoItem  = menuElement.firstChild.childNodes[1];
menuElement.bDropItem  = menuElement.firstChild.childNodes[2];

/*@cc_on
try{
function injVml()
{
   var _d = document, ns = _d.namespaces;
   if (ns && !ns['v']){ ns.add('v', 'urn:schemas-microsoft-com:vml');}
   var st = _d.createElement('style');
   (_d.getElementsByTagName('head')[0]).appendChild(st);
   var cs = st.styleSheet;
   cs.addRule('v\\:*', '{behavior:url(#default#VML); display:inline-block;}');
}
}catch(e){}
@*/

function g(id)
{
   return document.getElementById(id);
}


function cloneObj(obj)
{
   var obj2 = {};
   for(var i in obj)
      obj2[i] = obj[i];
   return obj2;
}


function extObj(obj, obj2)
{
   for(var i in obj2)
      obj[i] = obj2[i];

   return obj;
}




function isParentOf(c,p,i)
{
   i = i || 999999;

   while (c.parentNode != null && i > 0)
   {
      if (c.parentNode == p)
         return true;
      c = c.parentNode;
      i--;
   }
   return false;
}

if (!IE)
{
var txtShadow = function (str)
{
   var html = [];

   html.push("<div class=\"f-s f-s-c\"><div class=\"f-s f-s-t\">", str, "</div><div class=\"f-s f-s-1\">", str, "</div><div class=\"f-s f-s-2\">", str, "</div><div class=\"f-s f-s-3\">", str, "</div><div class=\"f-s f-s-4\">", str, "</div><div class=\"f-s f-s-5\">", str, "</div><div class=\"f-s f-s-6\">", str, "</div><div class=\"f-s f-s-7\">", str, "</div><div class=\"f-s f-s-8\">", str, "</div><div class=\"f-s f-s-9\">", str, "</div></div>");

   return html.join("");
}
}else
{
var txtShadow = function (str)
    {
       return createTextStroke(str, "#000000", "#ffffff");
    }
}


if (IE)
{
   
}

function createSource(slotId, params)
{
   (params = cloneObj(params)).empty ? extObj(params, emptySlotPm) : null;

   var iw         = (!!params.iw || params.iw == 0) && params.iw < params.w ? params.iw : params.w;
   var ih         = (!!params.ih || params.ih == 0) && params.ih < params.h ? params.ih : params.h;
   var sizeKey    = params.w + "_" + params.h;
   var cnt        = (params.cnt || 0)

   cnt = cnt <= 1 ? "" : cnt;


   var dSizeStyle  = "width: " + params.w + "px; height: " + params.h + "px; ";
   var iSizeStyle  = "width: " + iw + "px; height: " + ih + "px; ";
   var pdnStyle    =  ((iw < params.w) ? 'margin-left:' + ((params.w - iw) / 2)+ 'px;': '') + ((ih < params.h) ? 'margin-top:' + ((params.h - ih) / 2)+ 'px;': '');
   var backStyle   = "background: url('/i/slots/bg_" + (params.bg || DEF_BG) + "_" + sizeKey + ".gif'); ";
   var dsi         = " display: " + (params.displayType || DEF_DISPLAY_TYPE) + ";";

   return new String("<span onClick=\"Slot.evtHandler(event, this)\" onMouseOver=\"Slot.evtHandler(event, this)\" onMouseOut=\"Slot.evtHandler(event, this)\" id=\"" + slotId + "\" class=\"slot\" style=\"" + dsi  + dSizeStyle + backStyle +"\">" +
                     /*@cc_on (!IE6 ?@*/ "<img"/*@cc_on : "<v:image")@*/  + " src=\"" + params.img + "\" style=\"" + (ih <= 0 || iw <= 0 ? 'display: none; ' : '') + "position:absolute;" + iSizeStyle + pdnStyle + "\"/>" + /*@cc_on (IE6 ? "</v:image>" : "")+@*/
                     /*@cc_on (!IE6 ?@*/ "<img"/*@cc_on : "<v:image")@*/  + " src=\"/i/slots/brd_" + sizeKey + ".png\" style=\"" + dsi + "left:0;top:0; width: 100%; height: 100%; position:absolute;\"/>" + /*@cc_on (IE6 ? "</v:image>" : "")+@*/
                     "<span class=\"slot-cnt\">" + txtShadow(cnt) + "</span>" +
                     "</span>");
}
var Sl = this.Slot = function(params, _slotId)
{
   if (arguments.length > 2 && typeof arguments[2] == "array")
   {
      var p = arguments[2];
      if (p.length == 1)
         extObj(params, p[0]);
      else
         for (var i = 0; i < p.length; i++)
         extObj(params, p[i]);
   }



   var slotId = params.slotId  ||  _slotId || "slotUU_id" + (SLOT_C++);
   var skey   = params.w + "x" + params.h;

   params.slotId = slotId;

   if (!params.bodyOnly)
      params.allowCButtons = skey in Sl.allowedSizes ? Sl.allowedSizes[skey].allowCButtons : false;


   if (params.bodyOnly || !params.readOnlySLot)
   {
      slotsParamsStorage[slotId] = params;
   }

   return createSource(slotId, params);
};


Sl.allowedSizes =
{
   "50x50": {allowCButtons: true},
   "75x75": {allowCButtons: true},
   "25x25": {allowCButtons: false},
   "50x25": {allowCButtons: false}
};


Sl.actions =
{
   'item': {'funcUse' : function(slot){funcUse(slot.itemId, slot.useAction, slot.personal, slot.iname);},
            'quote'   : function(slot){funcQuote(slot.itemId);},
            'info'    : function(slot){funcInfo(slot.itemId);},
            'drop'    : function(slot){funcDrop(slot.itemId);}
           },
   'unit': {'funcUse' : function(slot){funcUnitUse(slot.itemId, slot.useAction);},
            'quote'   : function(slot){funcUnitQuote(slot.itemId);},
            'info'    : function(slot){funcUnitInfo(slot.itemId);},
            'drop'    : function(slot){funcUnitDrop(slot.itemId);}
           }
};

Sl.gb = function()
{
   for (var i in slotsParamsStorage)
   {
      if (!g(i))
         delete slotsParamsStorage[i];
   }
}


Sl.set = function(slot, params)
{
   var slotId = "";

   if (typeof slot == "string")
   {
      slotId = slot;
      slot   = g(slot);
   }
   else
   {
      slotId = slot.id;
   }

   if (!slot || !(slotId in slotsParamsStorage))
      return;


   var slotParams = slotsParamsStorage[slotId];
   var newSize    = false;

   (params = cloneObj(params)).empty ? extObj(params, emptySlotPm) : null;

   if (params.w && params.h && !(params.w == slotParams.w && params.h == slotParams.h))
   {
      newSize = true;

      slotParams.w = params.w;
      slotParams.h = params.h;
   }

   var sizeKey = slotParams.w + "_" + slotParams.h;

   if (newSize || (!!params.img && params.img != slotParams.img))
   {
      var _img   =  document.createElement("img");
      var iStyle =   _img.style

      var iw     = (!!params.iw || params.iw == 0) && params.iw < slotParams.w ? params.iw : slotParams.w;
      var ih     = (!!params.ih || params.ih == 0) && params.ih < slotParams.h ? params.ih : slotParams.h;


      if (iw == 0 || ih == 0)
          iStyle.display = "none";
      else
          iStyle.display = "";


      iStyle.position   = "absolute";
      iStyle.top        = 0;
      iStyle.left       = 0;
      iStyle.width      = iw + 'px';
      iStyle.height     = ih + 'px';
      iStyle.marginLeft = iw < slotParams.w ? ((slotParams.w - iw) / 2) + 'px' : 0;
      iStyle.marginTop  = ih < slotParams.h ? ((slotParams.h - iw) / 2) + 'px' : 0;

      if (!!params.img)
      {
         slotParams.img = params.img;
         _img.src       = slotParams.img;
      }

      slot.replaceChild(_img, slot.childNodes[0]);

      if (!!params.iw)
         slotParams.iw = params.iw;

      if (!!params.ih)
         slotParams.ih = params.ih;
   }

   if (params.cnt != undefined)
   {
      var ct    = slot.childNodes[2].firstChild;
      var count = params.cnt < 2 ? "" : params.cnt;

      for (var i = 0; i < ct.childNodes.length; i++)
         ct.childNodes[i].innerHTML = count;
   }

   if (newSize || (!!params.bg && params.bg != slotParams.bg))
   {
      if (!!params.bg)
         slotParams.bg = params.bg;

      slot.style.backgroundImage = "url('/i/slots/bg_" + (slotParams.bg || DEF_BG) + "_" + sizeKey + ".gif')";
   }


   if (newSize)
   {
      var brd = slot.childNodes[1];

      slot.style.width  = slotParams.w + 'px';
      slot.style.height = slotParams.h + 'px';

      brd.src = "/i/slots/brd_" + sizeKey + ".png";
   }

   slotParams.empty = !!params.empty;
};

//----------------
function showMenu(element, slot)
{
   var bQuote = false, bInfo = false, bDrop = false;

   if (slot.allowCButtons)
   {
      bQuote = !!slot.bQuote;
      bInfo  = !!slot.bInfo;
      bDrop  = !!slot.bDrop;
   }


   menuElement.bQuoteItem.style.display = bQuote ? '' : 'none';
   menuElement.bInfoItem.style.display  = bInfo  ? '' : 'none';
   menuElement.bDropItem.style.display  = bDrop  ? '' : 'none';

   element.appendChild(menuElement);
}

function hideMenu(element)
{
   element.removeChild(menuElement);
}


function processClick(event, slot)
{
   var _target = event.originalTarget || event.srcElement;
   var stype   = slot.stype || 'item';
   switch (_target.className)
   {
      case 'SlotControlButton_quote':
         Slot.actions[stype]["quote"](slot);
         break;

      case 'SlotControlButton_info':
         Slot.actions[stype]["info"](slot);
         break;

      case 'SlotControlButton_drop':
         Slot.actions[stype]["drop"](slot);
         break;

      default:
         if (slot.bUse)
         {
            Slot.actions[stype]["funcUse"](slot);
         }
   }
}


function mouseCheck(event, element)
{
   var related = event.relatedTarget || (event.type == 'mouseover' ? event.fromElement : event.toElement);

   if (related == undefined) return true;
   if (related == false) return false;

   return (related != element && related.prefix != 'xul' && !isParentOf(related, element, 5));

}

Sl.evtHandler = function(event, element)
{
try{

   var slot = slotsParamsStorage[element.id];

   if (!slot || slot.empty) return;

   switch (event.type)
   {
      case 'click':
         processClick(event, slot);
         break;

      case 'mouseover':
         if (mouseCheck(event, element))
            showMenu(element, slot);
         break;

      case 'mouseout':
         if (mouseCheck(event, element))
            hideMenu(element, slot);
         break;
   }

}catch(e){}
};

this.SetSlotCount = function(id, count)
{

   Sl.set(id,  {cnt: count});
}

this.CreateSlot = function (params, _slotId)
{
   return Sl(params, _slotId);
}
this.DrawSlot = function (params, _slotId)
{
  document.write(Sl(params, _slotId));
}

/*@cc_on
if (IE6)
{
   injVml();
   document.execCommand("BackgroundImageCache", false, true);
}
@*/
})();
