/**
 * Mogul Link Icons jQuery Plugin
 * @version: 0.0.1
 * @author Emil Löfquist
 * Dependencies: jQeury v. >= 1.3.2 (not tested with any versions below)
 */
(function($) {
    $.fn.linkicons = function(settings) {

        settings = $.extend({
            fileTypes: {
                doc: 'page_white_word.gif',
                xls: 'page_white_excel.gif',
                pdf: 'page_white_acrobat.gif',
                ppt: 'page_white_powerpoint.gif',
                docx: 'page_white_word.gif',
                xlsx: 'page_white_excel.gif',
                pptx: 'page_white_powerpoint.gif'
            },
            linkCss: {
                paddingLeft: '25px',
                backgroundPosition: '0 2px'
            },
            cssClasses: {
                external: 'externalLink',
                internal: 'internalLink',
                email: 'emailLink'
            },
            iconPath: '/cui/img/icons/'
        }, settings);



        var hostname = window.location.hostname.replace('www.', '').toLowerCase();
        function isExternal(url) {
            url = url.toLowerCase();
            return (url.indexOf('http://') !== -1 && url.indexOf(hostname) === -1) ? true : false;
        };

        function isEmail(url) {
            return (url.indexOf('mailto:') !== -1) ? true : false;
        };

        function init($this) {
            if ($this.attr('name') !== '') {
				return;
			}
            if ($this.attr('class') == "noIcon") {
				return;
			}
            var href = $this.attr('href');
			if(typeof href === 'undefined') return;
            var hrefArr = href.split('.');			
            var ext = hrefArr[hrefArr.length - 1];
            var img = settings.fileTypes[ext];

            if (isExternal(href) && !(img)) {
                var img2 = $('<img>').attr('src', settings.iconPath + 'external-link.gif').css({
                    right: '-20px',
                    bottom: '-2px',
                    position: 'absolute'
                });
                $this.css({
                    position: 'relative',
                    display: 'inline-block'
                }).append(img2);

            }
            else if (isEmail(href)) {
                $this.addClass(settings.cssClasses.email);
            }
            else {
                if (!(ext == 'pdf' || ext == 'doc' || ext == 'xls' || ext == 'ppt' || ext == 'docx' || ext == 'pptx' || ext == 'xlsx')) {
                    $this.addClass(settings.cssClasses.internal);
                }
            }

            if (isExternal(href) && ($this.attr('target') === '')) {
                $this.attr('target', '_blank');
            }

            if (img) {
                var $img = $('<img>').attr('src', settings.iconPath + img).css({
                    right: '-20px',
                    bottom: '-1px',
                    position: 'absolute'
                });
                $this.css({
                    position: 'relative',
                    display: 'inline-block'
                }).append($img);
            }
        };

        return this.each(function() {
            init($(this));
        });
    };
})(jQuery);
