(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];


var moneySlotStd = { 0 : {img: '/i/i/coin1.png', w: 50, h: 50, allowCButtons: false, bg: 'c', tooltip:'<b>Золотые Монеты</b>'},
                     1 : {img: '/i/i/money_diamond.png',   w: 50, h: 50, allowCButtons: false, bg: 'c', tooltip:'<b>Кристаллы</b>'},
                     2 : {img: '/i/i/coin2.png', w: 50, h: 50, allowCButtons: false, bg: 'c', tooltip:'<b>Темные Монеты</b>'},
                     3 : {img: '/i/i/coin3.png', w: 50, h: 50, allowCButtons: false, bg: 'c', tooltip:'<b>Светлые Монеты</b>'}}

/*@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");
    }
}



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  = params.noSizeCheck ? " display: none; " : "width: " + iw + "px; height: " + ih + "px; ";
   var onlCheck    = params.noSizeCheck ? "onload=\"SlotImageOnload(this, " + params.w + " , " + params.h + " )\"" : "";
   
   
   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) + ";";
   var dsi         = params.displayType ? (" display: " + params.displayType + ";") : "";

   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")@*/  + " " + onlCheck + "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 class=\"slot-cover\"></span>"+
                     "</span>");
}

function refill(obj, fill)
{
   for(var i in fill)
   {
      if (!(i in obj))
         obj[i] = fill[i];
   }
}

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]);
   }

   if (params.stype == 'money')
   {
      var mt = moneySlotStd[params.itemId];
      refill(params, mt);
   }


   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);}
           },
           //buildAction
   'realEstate': {'funcUse' : function(slot){funcBuildActionUse(slot.itemId, slot.useAction);},
                   'quote'   : function(slot){},
                   'info'    : function(slot){},
                   'drop'    : function(slot){}
           }
};


Sl.actions['itemBase'] = Sl.actions['item'];
Sl.actions['unitBase'] = Sl.actions['unit'];

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

Sl.cleanData = function(slotId)
{
   delete slotsParamsStorage[slotId];
}


Sl.remove = function(slotId)
{
   var slot = gs(slotId);
   if (slot)
   {
      if (slot.parentNode)
      {
         slot.parentNode.removeChild(slot);
         Sl.cleanData(slotId);
      }
      
   }
}

Sl.getStorage = function()
{
   return slotsParamsStorage;
}

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))
   {
           if (!params.noSizeCheck)
     {
      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;
     }
     else
     {
        slot.childNodes[0].src = params.img;
     }
   }

   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;
         
      slotParams.cnt = params.cnt;
   }

   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));

}
function slotTooltipMh(e)
{
    TipShow(this._slotTooltip, e || window.event);
}


function slotTooltipShow(element, event, slot)
{
    element._slotTooltip = slot.tooltip;
    element.onmousemove = slotTooltipMh;
    slotTooltipMh(event);
}

function slotTooltipHide(element)
{
    element.onmousemove = null;
    TipHide();
}

Sl.evtHandler = function(event, element)
{
try{
   var slot = slotsParamsStorage[element.id];

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


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

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

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

}catch(e){}
};

this.SetSlotCount = Sl.SetCount = function(id, count)
{

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


this.SlotImageOnload = function(img, w, h)
{
  if (img.width >= w)
  {
     img.style.width = w;
  }
  else
  {
     img.style.marginLeft = ((w - img.width) / 2) + "px";
  }
  
  if (img.height >= h)
  {
     img.style.width = h;
  }
  else
  {
     img.style.marginTop = ((h - img.height) / 2) + "px";
  }
  img.style.display = "block";


}


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);
}
@*/


var MSL = this.MoneySlot = function(slotId, params, _tmpl)
{
   var tmpl = _tmpl || params.tmpl;
   
   tmpl = tmpl && tmpl in MSL.tmpl ? tmpl : 'light';

   params.type  = !!params.type && params.type in MSL.moneyTypeClassList ? params.type : '#none';
   params.cnt   = Number(params.cnt) || '?';
   
   return MSL.tmpl[tmpl](slotId || params.slotId, params);
}

MSL.moneyTypeClassList = {
   
   '0' : 'money-gold',
   '1' : 'money-diamond',
   '2' : 'money-dark',
   '3' : 'money-light'
};

MSL.moneyTypeTipList = {
   
   '0' : '<b>Золотые Монеты</b>',
   '1' : '<b>Кристаллы</b>',
   '2' : '<b>Тёмные Монеты</b>',
   '3' : '<b>Светлые Монеты</b>'
};


MSL.tmpl = {
   "light" : function (params, slotId)
   {

      
      var html = ['<span id="' + slotId + '" class="money-slot money-slot-light ' + MSL.moneyTypeClassList[params.itemId] + '">',
                  '<span class="money-slot-icon"></span><span id="' + slotId + '_value" class="money-slot-value">' + params.cnt + ' </span></span>']
      
      return html.join("");
   }
};


MSL.set = function(_slot, params)
{
   var slotId = "";
   var slot   = (typeof _slot == "string") ? g(_slot) : _slot;
   
   if (!slot)
      return;

   slotId = slot.id;

   if (params.cnt != undefined)
   {
      var sc = g(slotId + "_value");
      
      if (sc)
      {
         sc.innerHTML = Number(params.cnt) || '?';
      }
   }   
};


MSL.SetCount = function (_slot, _count)
{
   MSL.set(_slot, {cnt: _count});
};

MSL.remove = Sl.remove;

this.createMoneySlot = function(params, _slotId)
{
};


this.drawMoneySlot = function(params, _slotId)
{
};

})();
