/***************************************
    MAIN CODE - CALL THEN PAGE LOADED
****************************************/
       
// binding action to event onload page
$(document).ready(
    function()
    {
		setupGlobal();
        setupSearchBox();     
        // this file
    }         
);

function setupGlobal()
{
    // blur focus then user click on <a> element
    $("a").focus(function(){$(this).blur();});
    // the same for object with id searchBoxBtn
    $("#searchBoxBtn").focus(function(){$(this).blur();}); 
}

/**************************
    SEARCH BOX CODE
**************************/

// if set to true search box auto play is run
var g_searchBoxAutoPlayAllowed = true;
// display animated text true/false
var g_serachBoxPlayText = true;
// text wich will be displayed by auto play function
var g_searchBoxDummyText = "Ingresa tu búsqueda...";
// current end position of text display by auto play function
var g_searchBoxTextPos = 0;
// handle of auto play search box timer
var g_searchBoxTimer = null;

// Auto play serach box function, it task is display dummy text in some time period
function searchBoxAutoPlay()
{
    // if auto play is turn off, function set timer
    // to 16 seconds and return control 
    if(false == g_searchBoxAutoPlayAllowed)
    {
        // chcek status in 16 seconds
        g_searchBoxTimer = setTimeout(searchBoxAutoPlay, 16000);
        return;
    }
    
    // get number of characters in dummy text
    var length = g_searchBoxDummyText.length;
    // move to next character
    g_searchBoxTextPos++;
    // if end of string, clean search box and return to begin 
    if(g_searchBoxTextPos > length)
    {
       $("#searchBox").val("");
       g_searchBoxTextPos = 0;
    }
    
    // time period between character displaying in miliseconds
    var timeOut = 50;
    // if character is the last character in string,
    // we set next call function in 20 seconds, becouse we want
    // to keep all dummy text on screen longer time
    if(g_searchBoxTextPos == length)
    {
       timeOut = 20000;
    }
    // extract substring from dummy text
    var sub = g_searchBoxDummyText.substr(0, g_searchBoxTextPos);
    // set substring for search box
    $("#searchBox").val(sub);
    // set next function call in timeOut miliseconds
    g_searchBoxTimer = setTimeout(searchBoxAutoPlay, timeOut);
} // end of function searchBoxAutoPlay

// This function setup search box, and is called only one time when page is loaded
function setupSearchBox()
{
    // set action for focus on searchbox
    $("#searchBox").focus(
        function(){
            g_searchBoxAutoPlayAllowed = false;
            $("#searchBox").val("");
            $(this).css("color", "#000");
            clearTimeout(g_searchBoxTimer);
            });
    
    // set action for blur on searchbox
    $("#searchBox").blur(
        function(){
            $(this).val("");
            $(this).css("color", "#6a6a6a");
            g_searchBoxTextPos = 0;
            clearTimeout(g_searchBoxTimer); 
            g_searchBoxAutoPlayAllowed = true;    
            g_searchBoxTimer = setTimeout(searchBoxAutoPlay, 5000);
    });
            
    // clean searchbox
    $("#searchBox").val("");
    // set auto playing
    if(g_serachBoxPlayText == true)
    {
        g_searchBoxTimer = setTimeout(searchBoxAutoPlay, 1000); 
    }
} // end of function setupSearchBox