if (typeof(window.NigmaForm)=="undefined")
{
    window.NigmaForm= new Object();
}

NigmaForm.Proto = new Object();

NigmaForm.Proto.defaultConfig = {
    'logo_img': '',
    'logo_link': '',
    'site_url': '',
    'search_type': 'web',
    'ui': '',
    'site_default': 'false',
    'site_enable': 'false',
    'width': '220px',
    'default_sword': '',
    'encoding': 'utf-8'
};

NigmaForm.Proto.proto_url = 'http://nigma.ru/themes/nigma/SearhForms/Proto.js';
NigmaForm.Proto.cur_url = 'http://nigma.ru/themes/nigma/SearhForms/Proto.js';
NigmaForm.Proto.search_page = 'http://nigma.ru';
NigmaForm.Proto.img_host       = 'http://nigma.ru';
NigmaForm.Proto.id = '';
NigmaForm.Proto.css = '';
NigmaForm.Proto.html = '';
NigmaForm.Proto.object_name = 'NigmaForm.Proto';

NigmaForm.Proto.js_tpl = '<script src="#proto_url#"  type="text/javascript"  charset="utf-8"></script>'+"\n"+'\
    <script src="#cur_url#"  type="text/javascript"  charset="utf-8"></script>'+"\n"+'\
    <script  type="text/javascript" >#object_name#.Show({#config#});</script>';


//**********************************************************************
NigmaForm.Proto.Copy= function ()
{
    var child = new Object();
    for (var key in this)
    {
        child[key] = this[key];
    }
    return child;
}

//**********************************************************************
NigmaForm.Proto.makeConfig = function (config)
{
    var config_new = Object();
    
    for (var key in this.defaultConfig)
    {
        var value = this.defaultConfig[key];
        if (typeof(config[key])=="undefined"){ config_new[key]=value; }
        else { config_new[key] = config[key]; }
        
        config_new[key] = new String(config_new[key]);
        config_new[key] = config_new[key].replace(   new RegExp("[\"']{1,}",'gi'),  '');      
    }
    return config_new;
}

//**********************************************************************
NigmaForm.Proto.makeUid = function()
{
    var date = new Date();
    return (date.getTime());
}

//**********************************************************************
NigmaForm.Proto.makeTpls = function(config)
{
     var uid = this.makeUid();
     var new_config = this.makeConfig(config);
     
     //make css
     var css_vars = new Object();
     css_vars['uid'] = uid;
     css_vars['width'] = new_config.width;

     //make html
     var html_vars = new Object();
     html_vars['uid'] = uid;
     html_vars['search_type'] = new_config.search_type;    
     html_vars['ui'] = new_config.ui;     
     html_vars['site_url'] = new_config.site_url;     
     html_vars['form_id'] = this.id;
     html_vars['logo_img'] = new_config.logo_img;     
     html_vars['logo_link'] = new_config.logo_link;     
     html_vars['default_sword'] = new_config.default_sword;   
     html_vars['default_sword'] = new_config.default_sword;
     html_vars['site_url'] = new_config.site_url;
     html_vars['search_page'] = this.search_page;
     html_vars['encoding'] = new_config.encoding;  
     html_vars['img_host'] = this.img_host;         
           
           
     (new_config.ui!="")               ?  (html_vars['ui_enabled']='')              : (html_vars['ui_enabled']='disabled="disabled"');
     (new_config.site_url!="")       ?  (html_vars['site_url_enabled']='')     : (html_vars['site_url_enabled']='disabled="disabled"');
     (new_config.logo_img!="")     ?  (html_vars['logo_img_enabled']='')   : (html_vars['logo_img_enabled']='disabled="disabled"');
     (new_config.logo_link!="")     ?  (html_vars['logo_link_enabled']='')    : (html_vars['logo_link_enabled']='disabled="disabled"');
     (new_config.site_url!="")     ?  (html_vars['site_url_enabled']='')       : (html_vars['site_url_enabled']='disabled="disabled"');
     
     if (new_config.site_enable=="true")
     {
         html_vars['site_enable']='';
         (new_config.site_default=="true")     ?  (html_vars['site_checked']='checked="checked"')       : (html_vars['site_checked']='');
         (new_config.site_default!="true")     ?  (html_vars['internet_checked']='checked="checked"')       : (html_vars['internet_checked']='');        
     }
     else
     {
           html_vars['site_enable']='display:none;';
           html_vars['site_checked'] = 'disabled="disabled"';
           html_vars['internet_checked'] = 'disabled="disabled"';           
     }
     
     var css = this.parse_obj(this.css, css_vars);  
     var html = this.parse_obj(this.html, html_vars);
                                
     return {'css':css, 'html':html};    
}

//**********************************************************************
NigmaForm.Proto.getHtml = function(config)
{
    var tpl = this.makeTpls(config);
    tpl.css    = tpl.css.replace(   new RegExp("[\t]{1,}",'gi'),  '');               
    tpl.css    = tpl.css.replace(   new RegExp("[ ]{2,}",'gi'),  ' ');         
    tpl.html   = tpl.html.replace(   new RegExp("[\t]{1,}",'gi'),  ' ');               
    tpl.html   = tpl.html.replace(   new RegExp("[ ]{2,}",'gi'),  ' ');       
    return tpl.css+"\n\n"+tpl.html;
}

//**********************************************************************
NigmaForm.Proto.getJS= function(config)
{
    var new_config = this.makeConfig(config);
    var conf_tpl = Array();
    for (var key in new_config)
    {
        var value = new_config[key];
        if (value==""){continue;}
        if (value==this.defaultConfig[key]){continue;}
        conf_tpl[conf_tpl.length] = "'"+new String(key)+"':"+"'"+new String(value)+"'";
    }
    conf_tpl = conf_tpl.join(', ');
    
    var out_vars = new Object();
    out_vars['config'] = conf_tpl;
    out_vars['proto_url'] = this.proto_url;
    out_vars['cur_url'] = this.cur_url;
    out_vars['object_name'] = this.object_name;
    
    var out_tpl = this.parse_obj(this.js_tpl, out_vars);         
    out_tpl = out_tpl.replace(   new RegExp("[\t]{1,}",'gi'),  '');               
    out_tpl = out_tpl.replace(   new RegExp("[ ]{2,}",'gi'),  ' ');    
    return out_tpl;
}

//**********************************************************************
NigmaForm.Proto.getSource= function(config)
{
    var tpl = this.makeTpls(config);
    return '<hr style="display:none;"/>'+tpl.css+tpl.html;
}

//**********************************************************************
NigmaForm.Proto.Show= function(config)
{ 
    document.write( this.getSource(config) );
}

//**********************************************************************
NigmaForm.Proto.parse_obj = function (source, vars)
{
    for (var key in vars)
    {
        source = this.str_replace('#'+key+'#', vars[key], source);
    }
    return source;
}

//**********************************************************************
NigmaForm.Proto.str_replace = function(search,replace, source)
{
   return source.replace(new RegExp(search,'gi'), replace);
}
