/**
 * Utility to implement seach
 *
 * @author: Arnab Chakraborty
 * Copyright (c) 2009, Arnab Chakraborty
 * Licensed under MIT licenses
 *
 * Date : 11 November 2009
 */
( function ( ) {
    var ID = 's';
    var defaultValue = 'Search my blog';

    var Search = function ( el ) {
        this._init( el );
    }

    Search.prototype._init = function ( el ) {
        el.value = el.value || defaultValue;

        this.on( el, 'focus', this._focus );
        this.on( el, 'blur', this._blur );
    }

    Search.prototype.on = function ( elem, type, handle ) {
        // bind event using proper DOM means
        if ( elem.addEventListener ) {
            elem.addEventListener(type, this._bind(handle), false);
        }
        // use the Internet Explorer API
        else if ( elem.attachEvent ) {
            elem.attachEvent("on" + type, this._bind(handle) );
        }
    }

    Search.prototype._bind = function ( fn ) {
        var that = this;
        return function (  ) {
            fn.apply( that, [].slice.call( arguments ) );
        }
    }


    Search.prototype._focus = function ( e ) {
        var t = e.target || e.srcElement;

        var v = t.value || '';

        if ( v == defaultValue ) {
            t.value = '';
        }
    }

    Search.prototype._blur = function ( e ) {
        var t = e.target || e.srcElement;

        var v = t.value || '';

        if ( v == '' ) {
            t.value = defaultValue;
        }
    }

    Search.prototype.toString = function () {
        return '[Search instance]';
    }




    // check for the element presence
    var timeout = setInterval( function () {
        try {
            var el = document.getElementById( ID );
            if ( el ) {
                clearInterval( timeout );
                new Search( el );
            }
        }
        catch (e) {
        }
    }, 20 );

} )();