/**
 * ASNU.net
 *
 * Real estate social network
 *
 * @package ASNU.net
 * @author Wizardry, Golubev Sergii
 * @copyright Copyright (c) 2009, Wizardry
 * @link http://www.wizardry.ua
 * @version 0.1
 */

/**
 * JS helpers
 *
 * @package ASNU.net
 * @subpackage JS
 * @category JS
 */

var TIME_OUT = 4000, TIME_OUT_AFTER = 500, REDIRECT_TIME_OUT = 2500;

//position element relatively to other
function do_position(positioned, fixed)
{
    var pos = $(fixed).offset();
    var left = (pos.left - ($(fixed).hasClass('input_text') ? 9 : 0)) + "px";
    var top = (pos.top + $(fixed).height() + 2) + "px";
    $(positioned).css( {
        left: left,
        top: top
    } );
}

//make elements having same width
function same_width(etalon, el)
{
    $(el).width($(etalon).width() + ($(etalon).hasClass('input_text') ? 14 : 0));
}

//hide element
function hide(n)
{
    $(n).hide('fast');
}

//show element
function show(n)
{
    $(n).show('fast');
}

//show element for some time
function show_timeout(n, time)
{
    _visibility_timeout(n, time, true);
}

//hide element for some time
function hide_timeout(n, time)
{
    _visibility_timeout(n, time, false);
}

//set visibility for some time
function _visibility_timeout(n, time, show)
{
    if (time == undefined)
    {
        time = TIME_OUT;
    }
    if (show)
    {
        $(n).show('fast');
        setTimeout("hide('" + n + "');", time);
    }
    else
    {
        $(n).hide('fast');
        setTimeout("show('" + n + "');", time);
    }
}

//set visibility after some time
function _visibility_after_timeout(n, time, show)
{
    if (time == undefined)
    {
        time = TIME_OUT_AFTER;
    }
    if (show)
    {
        setTimeout("show('" + n + "');", time);
    }
    else
    {
        setTimeout("hide('" + n + "');", time);
    }
}

//show element after some time
function show_after_timeout(n, time)
{
    _visibility_after_timeout(n, time, true);
}

//hide element after some time
function hide_after_timeout(n, time)
{
    _visibility_after_timeout(n, time, false);
}

//next are going phpjs implementations for string functions
function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}

function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

function strrpos (haystack, needle, offset) {
    var i = (haystack+'').lastIndexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}

function substr (f_string, f_start, f_length) {
    f_string += '';

    if (f_start < 0) {
        f_start += f_string.length;
    }

    if (f_length == undefined) {
        f_length = f_string.length;
    } else if (f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }

    if (f_length < f_start) {
        f_length = f_start;
    }

    return f_string.substring(f_start, f_length);
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}

function is_numeric (mixed_var) {
    if (mixed_var === '') {
        return false;
    }
    return !isNaN(mixed_var * 1);
}

var timeout_handler;

//reset visibility timeouts
function reset_visibility(id) {
    clearTimeout(timeout_handler);
    timeout_handler = setTimeout("$('" + id + ":visible').hide()", TIME_OUT);
}

//do redirect
function redirect(url)
{
    document.location = url;
}

//do redirect
function reload()
{
    location.reload();
}

//redirect after timeout
function redirect_time_out(url, time)
{
    if (time == undefined)
    {
        time = REDIRECT_TIME_OUT;
    }
    setTimeout("redirect('" + url + "')", time);
}

/* End of file helpers.js */
/* Location: ./application/view_files/js/helpers.js */
