/*
Copyright (c) 2008, Mustafa OZGUR
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of mit.core.js nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

var $ = document;
function $g(x) {return $.getElementById(x);}
function $c(x) {return $.createElement(x);}

var isIE = (navigator.appName.indexOf("Microsoft")!=-1) ? 1 : 0;

var XHTTP_CHARSET = '' != '' ? '' : 'UTF-8';

var MIT = {};

(MIT.CORE = {
  version:'1.3',
  debug:false,
  genUID:function(iLen)
  {
    sChrs = '_qwertyuiopasdfghjklzxcvbnm';
    sRnd  = '';
    for (i = 0; i < iLen; i++)
    {
      randomPoz = Math.floor(Math.random() * sChrs.length);
      sRnd += sChrs.substring(randomPoz,randomPoz+1);
    }
    return sRnd;
  },
  FixDATA:function(sData)
  {
    addSlash = {
      '%':0,
      '+':1,
      '\'':0,
      '"':0,
      '<':0,
      '>':0,
      ' ':0,
      '&':0
    };
    for (xChr in addSlash)
    {
      xRegExp = new RegExp((addSlash[xChr] ? '\\' : '') + xChr, 'g');
      sData = sData.replace(xRegExp, '%' + xChr.charCodeAt(0).toString(16).toUpperCase());
    }
    return sData;
  },
  getElementInForm:function(formName)
  {
    var allowedType = new Array();
    var formObj     = document.getElementById(formName);
    if(formObj.tagName.toLowerCase() == 'form')
    {
      allowedType['text']     = 1;
      allowedType['password'] = 1;
      allowedType['checkbox'] = 1;
      allowedType['radio']    = 1;
      allowedType['hidden']   = 1;

      x_inputs    = new Array();
      arrtextarea = formObj.getElementsByTagName("textarea");
      for(xPuts in arrtextarea)
      {
        if(typeof(arrtextarea[xPuts].tagName) != 'undefined')
        {
          if(arrtextarea[xPuts].tagName.toString().toLowerCase() == "textarea")
          {
            x_inputs.push(arrtextarea[xPuts].name + "=" + escape(arrtextarea[xPuts].value));
          }
        }
      }
      arrInputs       = formObj.getElementsByTagName("input");
      for(xPuts in arrInputs)
      {
        if(typeof(arrInputs[xPuts].type) != 'undefined')
        {
          if(allowedType[arrInputs[xPuts].type.toString().toLowerCase()] == 1)
          {
            if (arrInputs[xPuts].type.toString().toLowerCase() == 'radio')
            {
              if(arrInputs[xPuts].checked == true)
              {
                x_inputs.push(arrInputs[xPuts].name + "=" + FixDATA(arrInputs[xPuts].value));
              }
            }
            else
            {
              x_inputs.push(arrInputs[xPuts].name + "=" + FixDATA(arrInputs[xPuts].value));
            }
          }
        }
      }
      return x_inputs.join("&");
    }
  }
}
);

(MIT.FESA = {
  version:'1.2.0',
  setForm:function(xObj)
  {
    xForm             = document.getElementById(xObj.form);
    var retVal        = '';

    if(typeof(xObj.action) == 'string')
    {
      xForm.setAttribute('action', xObj.action);
    }
    if(typeof(xObj.onLoad) == 'string')
    {
      xForm.x_onLoad  = new Function('a', 'b', xObj.onLoad + '(a, b)');
    }
    else
    {
      xForm.x_onLoad  = 'void(0)';
    }

    xForm.x_onError   = new Function('a', 'b', 'this.innerHTML="Hata:"+a+"="+b;');
    xForm.x_onStart   = new Function('this.innerHTML="Yukleniyor...";');

    if(typeof(xObj.onSubmit) == 'string')
    {
      retVal = xObj.onSubmit;
    }

    if(isIE == 1)
    {
      xForm.onsubmit = new Function((retVal != '' ? 'if(' + retVal + '()){' : '') + 'MIT.FESA.doReq({method:"' + xForm.method.toString().toLowerCase() + '",url:"' + xForm.action.toString().toLowerCase() + '",form:"' + xObj.form + '",onError:this.x_onError,onLoad:this.x_onLoad,onStart:this.x_onStart});' + (retVal != '' ? '}' : '') + 'return false;');
    }
    else
    {
      xForm.setAttribute('onsubmit', (retVal != '' ? 'if(' + retVal + '()){' : '') + 'MIT.FESA.doReq({method:"' + xForm.method.toString().toLowerCase() + '",url:"' + xForm.action.toString().toLowerCase() + '",form:"' + xObj.form + '",onError:this.x_onError,onLoad:this.x_onLoad,onStart:this.x_onStart});' + (retVal != '' ? '}' : '') + 'return false;');
    }
  },
  doReq:function(urlObj)
  {
    if(MIT.CORE.debug)
    {
      alert(urlObj);
    }

    if(typeof(urlObj.method) == 'undefined')
    {
      urlObj.method = '';
    }

    if(typeof(urlObj.onLoad) != 'function')
    {
      urlObj.method = 'h';
    }

    if(typeof(urlObj.url) == 'undefined')
    {
      if(typeof(urlObj.onError) == 'function')
      {
        urlObj.onError('-100', 'URL yok');
      }
    }
    else
    {
      var req = this.getXMLHttpObj();

      req.onreadystatechange = function()
      {
        if (req.readyState == 4)
        {
          if (req.status < 400)
          {
            if(typeof(urlObj.onLoad) == 'function')
            {
              if(urlObj.method == 'h')
              {
                if(typeof(urlObj.onLoadParam) != 'undefined')
                {
                  urlObj.onLoad(urlObj.onLoadParam, req.getResponseHeader('Content-Length'), req.getResponseHeader('Content-Type'));
                }
                else
                {
                  urlObj.onLoad(req.getResponseHeader('Content-Length'), req.getResponseHeader('Content-Type'));
                }
              }
              else
              {
                if(typeof(urlObj.onLoadParam) != 'undefined')
                {
                  urlObj.onLoad(urlObj.onLoadParam, req.responseText, req.status);
                }
                else
                {
                  urlObj.onLoad(req.responseText, req.status);
                }
              }
            }
          }
          else
          {
            if(typeof(urlObj.onError) == 'function')
            {
              urlObj.onError(req.status, req.statusText);
            }
          }
        }
      }

      if(typeof(urlObj.form) != 'undefined')
      {
        if(urlObj.form != '')
        {
          urlObj.data = MIT.CORE.getElementInForm(urlObj.form);
        }
      }

      if(urlObj.url.indexOf('?') > 0)
      {
        delimChar = '&';
      }
      else
      {
        delimChar = '?';
      }

      if(typeof(urlObj.onStart) == 'function')
      {
        if(typeof(urlObj.onLoadParam) != 'undefined')
        {
          urlObj.onStart(urlObj.onLoadParam);
        }
        else
        {
          urlObj.onStart();
        }
      }

      urlObj.url = urlObj.url + delimChar + MIT.CORE.genUID(7) + '=' + MIT.CORE.genUID(7);
      urlObj.url = urlObj.url + '&__FESA=OK';
      urlObj.method = urlObj.method.toLowerCase();

      if(urlObj.method == '') urlObj.method = 'g';

      if (urlObj.method == 'post' || urlObj.method == 'p')
      {
        req.open('POST', urlObj.url, true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded' + (XHTTP_CHARSET != '' ? '; charset='+XHTTP_CHARSET : ''));
        if(XHTTP_CHARSET != '') req.setRequestHeader('charset',XHTTP_CHARSET);
        req.send(urlObj.data);
      }
      else if(urlObj.method == 'head' || urlObj.method == 'h')
      {
        req.open('HEAD', urlObj.url, true);
        req.setRequestHeader('Content-Type', 'text/html' + (XHTTP_CHARSET != '' ? '; charset='+XHTTP_CHARSET : ''));
        if(XHTTP_CHARSET != '') req.setRequestHeader('charset',XHTTP_CHARSET);
        req.send(null);
      }
      else if(urlObj.method == 'get' || urlObj.method == 'g')
      {
        req.open('GET', urlObj.url, true);
        req.setRequestHeader('Content-Type', 'text/html' + (XHTTP_CHARSET != '' ? '; charset='+XHTTP_CHARSET : ''));
        if(XHTTP_CHARSET != '') req.setRequestHeader('charset',XHTTP_CHARSET);
        req.send(null);
      }
    }
    return false;
  },
  getXMLHttpObj:function()
  {
    var _XH_ACTIVE_X_IDENTS =
    [
      'MSXML2.XMLHTTP.6.0',
      "MSXML2.XMLHTTP.5.0",
      "MSXML2.XMLHTTP.4.0",
      "MSXML2.XMLHTTP.3.0",
      "MSXML2.XMLHTTP.2.0",
      "MSXML2.XMLHTTP.1.0",
      "MSXML2.XMLHTTP",
      "MICROSOFT.XMLHTTP.2.0",
      "MICROSOFT.XMLHTTP.1.0",
      "MICROSOFT.XMLHTTP.1",
      "MICROSOFT.XMLHTTP",
      'MSXML2.DOMDocument.6.0',
      'MSXML2.DOMDocument.5.0'
    ], _xh_ieProgId = undefined;
    if (typeof(XMLHttpRequest) == "undefined" && typeof(ActiveXObject) != "undefined")
    {
      for(var a=0;a<_XH_ACTIVE_X_IDENTS.length;a++)
      {
        var b=_XH_ACTIVE_X_IDENTS[a];
        try
        {
          new ActiveXObject(b);
          _xh_ieProgId=b;
          break;
        } catch(c) {}
      }
    }
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version>=5)
    try {
      xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      } catch(E) {
        xmlhttp=false;
      }
    }
    @end @*/
    if (_xh_ieProgId != undefined)
    {
      xmlhttp = new ActiveXObject(_xh_ieProgId);
    }
    if (!xmlhttp&&typeof(XMLHttpRequest)!='undefined')
    {
      try
      {
        xmlhttp=new XMLHttpRequest();
      }
      catch (e)
      {
        xmlhttp = false;
      }
    }
    if (!xmlhttp&&window.createRequest)
    {
      try
      {
        xmlhttp=window.createRequest();
      }
      catch(e)
      {
        xmlhttp=false;
      }
    }
    return xmlhttp;
  }
});

(MIT.EFCT = {
  version:'1.2',
  init:function(divname, imgPath, imgS1, imgS2, imgDelim, imgStep)
  {
    this.imgPath  = imgPath ? imgPath : '';
    this.imgS1    = imgS1 ? imgS1 : 0;
    this.imgS2    = imgS2 ? imgS2 : 0;
    this.imgDelim = imgDelim ? imgDelim : '-';
    this.imgStep  = imgStep ? imgStep : '-';
    objs = $g(divname).getElementsByTagName("img");
    for (obj in objs)
    {
      if(typeof(objs[obj].tagName) != 'undefined')
      {
        if(objs[obj].tagName.toString().toLowerCase() == "img")
        {
          MIT.EFCT.doMac(objs[obj].getAttribute('id'));
        }
      }
    }
  },
  doMac:function(imgID)
  {
    var i            = $g(imgID);
    imgTitle         = i.getAttribute('alt');
    imgUrl           = i.getAttribute('efct');
    i.src            = this.imgPath + imgUrl + this.imgDelim + this.imgS1 + '.png';
    i.alt            = imgTitle;
    i.border         = '0';
    i.height         = this.imgS1;
    i.width          = this.imgS1;
    i.style.position = 'relative';
    i.style['bottom']= '0px';
    i.style['left']  = '0px';
    i.onmouseover    = function(){return MIT.EFCT.setTimedScale(imgID, 20, MIT.EFCT.imgS2, MIT.EFCT.imgS1, 1, 1);}
    i.onmouseout     = function(){return MIT.EFCT.setTimedScale(imgID, 20, MIT.EFCT.imgS1, MIT.EFCT.imgS2, 0, 1);}
    $c('img').src    = this.imgPath + imgUrl + this.imgDelim + MIT.EFCT.imgS2 + '.png';
  },
  setTimedScale:function(iID, timeInt, from, to, ittr, ne)
  {
    var i     = $g(iID);
    imgTitle  = i.getAttribute('alt');
    imgUrl    = i.getAttribute('efct');
    g = 1;
    if(ittr == 1)
    {
      to += this.imgStep;
      if(to > from)
      {
        to      = from;
        i.src   = this.imgPath + imgUrl + this.imgDelim + to + '.png';
        g       = 0;
      }
    }
    else if(ittr == 0)
    {
      to -= this.imgStep;
      if(to < from)
      {
        to      = from;
        i.src   = this.imgPath + imgUrl + this.imgDelim + to + '.png';
        g       = 0;
      }
    }

    i.style['bottom']= (Math.floor(Math.abs(MIT.EFCT.imgS2 - MIT.EFCT.imgS1) / 2) - Math.floor(Math.abs(to - MIT.EFCT.imgS2) / 2)) + 'px';
    i.width          = to;
    i.height         = to;
    if(g)
    {
      window.setTimeout('MIT.EFCT.setTimedScale("' + iID + '", ' + timeInt + ', ' + from + ', ' + to + ', ' + ittr + ', 0)', timeInt);
    }
  }
});

if(typeof window.MIT == 'undefined' || !window.MIT)
{
  window.MIT = MIT;
}

if(typeof document.MIT == 'undefined' || !document.MIT)
{
  document.MIT = MIT;
}
