var actions = new Object();

actions['tooltip'] = function($parent){$('.tooltip, .optiBtn', $parent).tooltip({showURL: false})};
actions['gotoLink'] = function($parent){$('.gotoLink', $parent).click(function(){gotoLink($(this));});};

function doActions($parent)
{
    if (!$parent) $parent = $(document);
    for(var i in actions)
    {
        actions[i]($parent);
    }
}

/**
 * .. используя #id, .class-name
 ***/
function doActionsWith(parent)
{
    if (parent)
        parent = $(parent);

    doActions(parent);
}

$(document).ready(function(){
    doActions();
});

function getElementPosition(elem)
{
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
    var l = 0;
    var t = 0;
	
    while (elem){
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}

function gotoLink($parent)
{
    var href = $parent.find('a')[0].href;
    if (href) document.location = href;
}

var printCounter = 0;
function print_r(el, counter)
{
    if (printCounter >= counter) return false;
    printCounter++;
    var str = '';
    for (var i in el) str += i + ': '+el[i]+'; \n';
    //$('body').append('<div>'+str+'</div>');
    if (str) alert(str);
    else alert(el);
    return true;
}

function length(el) {
    var result = 0;
    for (var i in el) result++;
    return result
}

function hasPropertys(obj) {
    var result = 0;
    for (var i in obj)
        return true;
    return false;
}

function setCookie(name, value)
{
    var expDate = new Date();
    expDate.setDate(expDate.getDate() + 3600);
    document.cookie = name+'='+value+'; expires='+expDate.toUTCString();
    return true;
}

function serialize( mixed_val ) {    // Generates a storable representation of a value
    //
    // +   original by: Ates Goral (http://magnetiq.com)
    // +   adapted for IE: Ilia Kantor (http://javascript.ru)

    switch (typeof(mixed_val))
    {
        case "number":
            if (isNaN(mixed_val) || !isFinite(mixed_val)) return false;
            else return (Math.floor(mixed_val) == mixed_val ? "i" : "d") + ":" + mixed_val + ";";
        case "string":
            return "s:" + mixed_val.length + ":\"" + mixed_val + "\";";
        case "boolean":
            return "b:" + (mixed_val ? "1" : "0") + ";";
        case "object":
            if (mixed_val == null) return "N;";
            else if (mixed_val instanceof Array)
            {
                var idxobj = { idx: -1 };
                var map = []
                for(var i=0; i<mixed_val.length;i++)
                {
                    idxobj.idx++;
                    var ser = serialize(mixed_val[i]);

                    if (ser) map.push(serialize(idxobj.idx) + ser);
                }

                return "a:" + mixed_val.length + ":{" + map.join("") + "}"
            }
            else
            {
                var class_name = get_class(mixed_val);

                if (class_name == undefined){
                    return false;
                }

                var props = new Array();
                for (var prop in mixed_val)
                {
                    var ser = serialize(mixed_val[prop]);
                    if (ser) props.push(serialize(prop) + ser);
                }
                return "O:" + class_name.length + ":\"" + class_name + "\":" + props.length + ":{" + props.join("") + "}";
            }
        case "undefined":
            return "N;";
    }

    return false;
}

var $infoBlock;
function createInfoBlock()
{
    var block = '<div id="infoBlock" class="infoBlock"></div>';
    $('body').append(block);
    $infoBlock = $('#infoBlock').css({opacity:0.9});
}
function showInfo(text, time)
{
    if (!$infoBlock) createInfoBlock();
    var timeOut = $infoBlock.attr('timeOut');
    if (timeOut) {
        clearTimeout(timeOut);
        $infoBlock.attr({'timeOut': null});
    }
    $infoBlock.fadeOut('fast').html(text).fadeIn('fast');
    if (time)
    {
        timeOut = setTimeout('hideInfo()', time);
        $infoBlock.attr({'timeOut': timeOut});
    }
}
function hideInfo()
{
    var timeOut = $infoBlock.attr('timeOut');
    if (timeOut) {
        clearTimeout(timeOut);
        $infoBlock.attr({'timeOut': null});
    }
    $infoBlock.fadeOut('fast');
}

function getCaptcha(elementId, element) {
  var obj = elementId ? $("#"+elementId) : $(element);
      obj.attr("src", "/libs/Captcha/captcha.php?"+Math.random(10000));
}