String.prototype.truncate = function(limit, rep, append)
{
    var string = this.toString();
    var limitTemp = limit;
    append = append != null ? append : '...';

    if(!limit || string.length < limit)
    {
        return string;
    }

    if(rep)
    {
        limit--;
        last = string.substr(limit, 1);

        while(last != " ")
        {
            limit += (rep == 'after') ? 1 : -1;
            last = string.substr(limit, 1);
        }

        if(limit > 0 && limit < string.length-1)
        {
            return string.substr(0, limit) + append;
        }
    }

    return string.substr(0, limitTemp) + append;
}

/*Array.prototype.inArray = function (value)
{
    for (var i=0; i < this.length; i++)
    {
        if (this[i] === value)
        {
            return true;
        }
    }

    return false;
};*/

String.prototype.format = function(args)
{
    var text = this.toString();

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;

    if(args.constructor == Array)
    {
        tokenCount = args.length-1;
    }
    else
    {
        args = arguments;
    }

    for( var token = 0; token <= tokenCount; token++ )
    {
        text = text.replace( new RegExp("\\{" + token + "\\}", "gi" ), args[ token ]);
    }

    return text;

};

$.fn.setFocus = function()
{
    var elem = $(this).offset();

    if($.browser.safari)
        document.body.scrollTop = elem.top;
    else
        document.documentElement.scrollTop = elem.top;
}

$.fn.imgReload = function()
{
    var force = '?refresh=' + (Math.random().toString().replace(/[^0-9]+/, ''));
    
    $(this).each(function()
    {
        $(this).attr('src', $(this).attr('src') + force);
    });
}


$.fn.fixPosition = function()
{
    $(this).each(function()
    {
        var elm = this;

        if($.browser.msie && $.browser.version <= 6)
        {          
            var offset =
            {
                offset: $(elm).offset(),
                top: $(elm).css('top'),
                right: $(elm).css('right'),
                bottom: $(elm).css('bottom'),
                left: $(elm).css('left'),
                width: $(elm).width(),
                height: this.offsetHeight
            };

            var reffer = $('<div />').hide().insertBefore(elm);

            $(elm).appendTo('body').css({'position': 'absolute', top: 0});
            
            var reposition = function()
            {
                var screen = {};

                if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
                 {
                       screen.width = document.documentElement.clientWidth,
                       screen.height = document.documentElement.clientHeight
                 }
                 else
                 {
                       screen.width = document.getElementsByTagName('body')[0].clientWidth,
                       screen.height = document.getElementsByTagName('body')[0].clientHeight
                 }

                var scrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
                var scrollHeight = document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;

                if(offset.top.replace(/px$/i, '') == "0")
                {
                    $(elm).css('top', parseInt(scrollTop));
                }
                else if(offset.bottom.replace(/px$/i, '') == "0")
                {
                    $(elm).css('top', parseInt(screen.height) + parseInt(scrollTop) - parseInt(offset.height));
                }
                else
                {
                    $(elm).css('top', offset.offset.top-(Math.round((offset.offset.top/parseInt(screen.height)))*parseInt(screen.height)) + parseInt(scrollTop));
                }                
            };

            $(window).bind('scroll resize', reposition);

            $(elm).bind('unfixPositon', function()
            {
                $(window).unbind('scroll resize', reposition);
                $(reffer).replaceWith(elm);

            });

            reposition();
        }
        else
        {
            $(this).css('position', 'fixed');
        }
    });

    return this;
};

function extend(subclass, baseclass)
{
    subclass.prototype = new baseclass;
    subclass.prototype.parent = baseclass;
}

$.fn.unfixPosition = function()
{
    $(this).each(function()
    {
        if($.browser.msie && $.browser.version <= 6)
        {
            $(this).trigger('unfixPositon');
        }

        $(this).css('position', 'static');
    });

    return this;
};

function hasPopupBlock()
{
    var test = window.open('about:blank','','width=1,height=1,left=0,top=0,scrollbars=no');

    if($.browser.opera && !test.opera) return true;

    if(!test || test.closed)
    {
        return true;
    }
    else
    {
        test.close();
        return false;
    }
}


function popup(url, name, params, center)
{
    var defaults = {height:400, width: 400, status: false, toolbar: false, menubar: false, location: false, resizable: true };




    for(var i in params)
    {
        if(params[i] === true)
            params[i] = 'true';
        else if(params[i] === false)
            params[i] = 'false';

        defaults[i] = params[i];
    }

    defaults.left = defaults.left == 'center' ? (screen.width-defaults.width)/2 : defaults.left;
    defaults.top = defaults.top == 'center' ? (screen.height-defaults.height)/2 : defaults.top;
    
    var param = '';
    
    for(var d in defaults)
    {
        param += d + '=' + defaults[d] + ',';
    }

    var popup = window.open(url, name || 'newwindow', param);

    if (popup && window.focus)
        popup.focus();

    return popup;    
}

$.fn.roundedCorners = function(o, width)
{
    var width = (typeof(width) == 'undefined') ? 3 : width;

    var sides = {tr: 0, br: 0, bl: 0, tl: 0};

    var opts = {
                    tl:  /top|tl|left/.test(o),       tr:  /top|tr|right/.test(o),
                    bl:  /bottom|bl|left/.test(o),    br:  /bottom|br|right/.test(o),
                    
                    behavior: '/site/js/border-radius.htc'
            };

    if(typeof(o) == 'object')
    {
        sides = $.extend(sides, o);
    }
    else if(typeof(o) == 'string' && width > 1)
    {
        width = o > 1 ? o : width;

        for(var s in opts)
        {
            if(opts[s])
            {
                sides[s] = width;
            }
        }
    }
    else
    {
        width = o > 1 ? o : width;

        for(var s in opts)
        {
            sides[s] = width;
        }
    }

    $(this).css(
    {
        'border-top-right-radius': sides.tr,
        'border-bottom-right-radius': sides.br,
        'border-bottom-left-radius': sides.bl,
        'border-top-left-radius': sides.tl,

        '-moz-border-radius-topright': sides.tr,
        '-moz-border-radius-bottomright': sides.br,
        '-moz-border-radius-bottomleft': sides.bl,
        '-moz-border-radius-topleft': sides.tl,

        '-webkit-border-top-right-radius': sides.tr,
        '-webkit-border-bottom-right-radius': sides.br,
        '-webkit-border-bottom-left-radius': sides.bl,
        '-webkit-border-top-left-radius': sides.tl,
               
        
        'border-top-right-radius': sides.tr, 
        'border-bottom-right-radius': sides.br,
        'border-bottom-left-radius': sides.bl,
        'border-top-left-radius': sides.tl,
        
        'border-radius': sides.tr, 
        'behavior': 'url(' + opts.behavior + ')'
    });

    return this;
};