var cssTpl = 
    '<link rel="stylesheet" type="text/css" href="#STYLE_SHEET#" />';
var formTpl =
    '<div class="rambler-search-page" id="begun_rambler_searchform-out">' +
    '<div id="begun_rambler_sform">' +
    '<form name="ramlerSbegun" class="ramlerSbegun" action="#SEARCH_URL#" method="get" onSubmit="return SEARCH_FORM.search()">' +
    '    <table class="ramblersbox">' +
    '        <tr class="row-0">' +
    '            <td class="left">' +
    '                <label for="q">#LABLE_TEXT#,&nbsp;</label>' +
    '            </td>' +
    '            <td class="center">' +
    '               <input type="hidden" name="query" value=""/>' +
    '               <input type="text" id="q" name="q" value="" style="width:100%;padding-left:20px;" class="search" />' +
    '                <div class="spacer"><!-- --></div>' +
    '            </td>' +
    '            <td class="right">' +
    '                <input type="submit" #STYLE# value="#SEARCH_TEXT#!">' +
    '            </td>' +
    '        </tr>' +
    '    </table>' +
    '</form>' +
    '</div>' +
    '</div>';
if (typeof begun_config == 'object') {
    var SEARCH_FORM = new SEARCH_FORM( begun_config );
    SEARCH_FORM.start();
}

//--------------------------------------------------------------------------
// SEARCH_FORM Object
//--------------------------------------------------------------------------
function SEARCH_FORM(config) {
    //--------------------------------------------------------------------------
    // Init
    //--------------------------------------------------------------------------
    this.parameters     = {
        formDivName         : 'searchform',
        styleSheetUrl       : 'http://rs.begun.ru/search.css',
        searchPageUrl       : null
    }
    this.formDiv        = null;
    //--------------------------------------------------------------------------
    // Config
    //--------------------------------------------------------------------------
    if (config.hypersearch_form_div) {
        this.parameters.formDivName = config.hypersearch_form_div;
    }

    if (config.search_page_url) {
        this.parameters.searchPageUrl = config.search_page_url;
    }

    //--------------------------------------------------------------------------
    // Start
    //--------------------------------------------------------------------------
    this.start = function() {
        if (this.parameters.searchPageUrl) {
            this.getFormDiv();
            this.showForm();
        }
    }

    //--------------------------------------------------------------------------
    // Get from div
    //--------------------------------------------------------------------------
    this.getFormDiv = function() {
        if (!this.formDiv) {
            this.formDiv = document.getElementById(this.parameters.formDivName);
        }
        return this.formDiv;
    }

    //--------------------------------------------------------------------------
    // Show form
    //--------------------------------------------------------------------------
    this.showForm = function() {
        var search_text = decodeURIComponent('%D0%B8%D1%89%D0%B8');
        var lable_text = decodeURIComponent('%D0%A0%D0%B0%D0%BC%D0%B1%D0%BB%D0%B5%D1%80');
        
        var cssTemplate = new BEGUN_Template(cssTpl);
        cssTemplate.assign('STYLE_SHEET', this.parameters.styleSheetUrl);
        cssTemplate.display();
        
        if (window.opera && ("BackCompat" == document.compatMode)) {
            var style = 'style="position:relative; margin-left: -24px;"';
            
        } else {
            var style = '';
        }
        var formTemplate = new BEGUN_Template(formTpl);
        formTemplate.assign('SEARCH_URL', this.parameters.searchPageUrl);
        formTemplate.assign('LABLE_TEXT', lable_text);
        formTemplate.assign('STYLE', style);
        formTemplate.assign('RAMBLER_IMG', this.ramblerImageSmall);
        formTemplate.assign('SEARCH_TEXT', search_text);
        this.formDiv.innerHTML += formTemplate.tpl;
        
               
    }
    //--------------------------------------------------------------------------
    // On submit escape query string
    //--------------------------------------------------------------------------
    this.search = function() {
        
        var form = window.document.forms[ 'ramlerSbegun' ];
        if ( form ) {
            var paramQuery = form.elements.q;
            form.elements.query.value = escape(paramQuery.value);
        }
        return true;
    }
}
//---------------------------------------------------
// Templates
//---------------------------------------------------
function BEGUN_Template(tpl) {
    
    this.tpl    = tpl;
    this.vars   = new Array();
    
    this.assign = function(variable, value) {
        if(this.tpl != null) {
            var pattern = new RegExp('#' + variable + '#', 'g');
            this.tpl = this.tpl.replace(pattern, value);
        }
    }
    
    this.assignEncoded = function(variable, value) {
        if(this.tpl != null) {
            var pattern = new RegExp('#' + variable + '#', 'g');
            this.tpl = this.tpl.replace(pattern, decodeURIComponent(value));
        }
    }
    
    this.add = function(variable, value) {
        if(typeof this.vars[variable] == 'undefined') {
            this.vars[variable] = '';
        }
        this.vars[variable] += value; 
    }
    
    this.parse = function(variable) {
        if(this.tpl != null && typeof this.vars != 'undefined') {
            var pattern = new RegExp('#' + variable + '#', 'g');
            this.tpl = this.tpl.replace(pattern, this.vars[variable]);
        }     
    }
     
    this.display = function() {
        if(this.tpl != null) {
            document.write(this.tpl);
        }
    }
}