// PNG IE6 Fix (#include suilib_lite.js)
// Created in Shogo.Ru

var msie6 = !!(suilib.client.msie && navigator.appVersion && !navigator.appVersion.match(/MSIE 7/));
function fixBgPNG(el) {
  var src = el.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
  if(src && msie6) {
    src = src[1];
    el.runtimeStyle.backgroundImage = "none";
    el.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='crop')"; }
}

suilib.ready(function() {
  // PNG IE Fix
  if(msie6) {
    var tmp = false, imgs = $(suilib.body).filter('img',null,null,true);
    for(var i=0; i<imgs.length; i++) {
      tmp = imgs[i].getAttribute('src');
      if(tmp.search && tmp.search(/\.png$/i)>=0) {
        imgs[i].src = 'i/sp.gif';
        imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + tmp + "',sizingMethod='crop')"; }
    }
  }
});

// Object Functions
var objTools = {
  $specialChars: {
    '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'
  },
  $replaceChars: function(chr) {
    return this.$specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16);
  },
  parse: function(hash) {
    var self = this;
    switch(typeof(hash)) {
      case 'string':
        return "'" + hash.replace(/[\x00-\x1f\\"]/g, function(chr){ return self.$replaceChars.apply(self, [chr]); }) + "'";
      case 'number':
        return isFinite(hash) ? String(hash) : 'null';
      case 'object':
        if(hash===null) return String(hash);
        var string = [];
        if(hash.length) {
          for(var i=0,l=hash.length; i<l; i++) {
            if(typeof hash[i]=='undefined') continue;
            string.push(this.parse(hash[i]));
          }
          return '[' + String(string) + ']';
          break;
        }
        for(var i in hash) {
          var json = this.parse(hash[i]);
          if(json) string.push(this.parse(i) + ':' + json);
        }
        return '{' + string + '}';
      default: return String(hash);
    }
    return null;
  },
  length: function(hash) {
    if(!hash || typeof(hash)!=='object') return null;
    var j = false;
    for(var i in hash) j++;
    return j;
  },
  clone: function(hash) {
    if(!hash || typeof(hash)!=='object') return hash;
    var newHash = hash.constructor();
    for(var i in hash) {
      newHash[i] = this.clone(hash[i]);
    }
    return newHash;
  }
}

// Functions from SUILib_lite
function $HAR(resp) { // (handle ajax response) стандартный вывод сообщений об ошибках
  var result = false, defmsg = 'Произошла неизвестная ошибка!';
  if(resp && resp.responseJS) {
    switch(resp.responseJS.status) {
      case 'ok': result = true;
      break;
      case 'error': alert(resp.responseJS.message || defmsg);
      break;
      default: alert(defmsg);
    }
    if(resp.responseJS.evaluate) eval(resp.responseJS.evaluate);
    if(resp.responseJS.debug) alert('[debug]:\n'+resp.responseJS.debug);
  }
  return result;
}

function check_required(required)
{
  for( var i = 0; i < required.length; i++ )
  {
    if( !$(required[i]['id']).value.trim().length )
    {
      alert(required[i]['name']+' не может быть пустым!');
      $(required[i]['id']).focus();
      return false;
    }
    if( required[i]['email'] && !$(required[i]['id']).value.match(/\S+@\S+\.\S+/) )
    {
      alert(required[i]['name']+' содержит некорректное значение!');
      $(required[i]['id']).focus();
      return false;
    }
    if( required[i]['numeric'] && !$(required[i]['id']).value.match(/[\d]+/) )
    {
      alert(required[i]['name']+' содержит некорректное значение!');
      $(required[i]['id']).focus();
      return false;
    }
    if( required[i]['group_1_2_3'] && !$(required[i]['id']).value.match(/(1|2|3)(,(1|2|3))*/) )
    {
      alert(required[i]['name']+' содержит некорректное значение!');
      $(required[i]['id']).focus();
      return false;
    }
  }
  return true;
}

function objLength(hash) {
  if(!hash || typeof(hash)!=='object') return null;
  var result = false;
  for(var i in hash) result++;
  return result;
}

function array2hash(obj) {
  if( !obj )
    return obj;

  if( !(obj instanceof Array) )
    return obj;

  if( obj.length == 0 )
    return {};

  var new_obj = {};
  for( var i = 0; i < obj.length; i++ )
    new_obj[i] = obj[i];

  return new_obj;
}

var data_cacher = function()
{
  var data = {};

  return {
    'cache': function(url)
    {
      if( url in data )
        return false;
      else
      {
        data[url] = new Image();
        data[url].src = url;
        return true;
      }
    }
  };
}();

