sds.articleSearch = {
    init:function() {
        var location = window.location.toString();
        tag = location.substring(location.indexOf("/tags/") + 6);
        tag = tag.substring(tag.indexOf(0, /\?#&/));
        if ($('#wikiBox').length > 0) {
            this.initWiki(tag);
        }
        if ($('#flickrBox').length > 0) {
            this.initFlickr(tag.replace("+","%20"));
        }
        if ($('#relatedTagsBox').length > 0) {
            this.getRelatedTags(tag);
        }
    },
    initWiki:function(tag) {
        var url = "http://sv.wikipedia.org/w/api.php?action=query&titles=" + tag + "&prop=revisions&rvprop=content&rvsection=0&format=json&callback=?";
        $.getJSON(url, function(data) {

            for (var i in data.query.pages) {
                if (i == "-1" || typeof(data.query.pages[i]) == 'function') {
                    continue;
                }

                var page = data.query.pages[i].revisions[0];
                if (typeof(page['*']) == 'undefined') {
                    break;
                }

                if (page['*'].indexOf('#OMDIRIGERING') != -1 || page['*'].indexOf('#omdirigering') != -1 || page['*'].indexOf('#REDIRECT') != -1 || page['*'].indexOf('#redirect') != -1) {
                    sds.articleSearch.initWiki(page['*'].match(/\[\[.+?\]\]/)[0].replace(/[\[\]]/g, ''));
                } else {
                    page['*'] = page['*'].replace(new RegExp('\\n', 'g'), '<br />');
                    page['*'] = page['*'].replace(/{{Audio[^}]+\|/g, '');
                    var max = 5;
                    while (page['*'].search(/{{[^{]+?}}/) != -1 && max > 0) {
                        page['*'] = page['*'].replace(/{{[^{]+?}}/g, '');
                        max -= 1;
                    }
                    max = 5;
                    while (page['*'].search(/{[^{]+?}/) != -1 && max > 0) {
                        page['*'] = page['*'].replace(/{[^{]+?}/g, '');
                        max -= 1;
                    }
                    page['*'] = page['*'].replace(/}}/g, '');
                    var result = page['*'];
                    if (result.indexOf(':') != -1 && result.indexOf(':') < 8) {
                        result = result.substring(result.indexOf('<br />', result.indexOf(':')) + 6);
                    }
                    result = result.replace(/<ref.+?<\/ref>/g, '');
                    result = result.replace(/\[\[(Fil|Bild):.+?\]\]<br \/>/g, '');
                    result = result.replace(/\[[^\]]+?\|/g, '');
                    result = result.replace(/[\[\]']/g, '');
                    if (result.length > 500 && result.lastIndexOf('<br />') > 500) {
                        result = result.substring(0, result.indexOf('<br />', 500));
                    }
                    while (result.indexOf('<br />') < 3 && result.indexOf('<br />') != -1) {
                        result = result.replace("<br />", '');
                    }
                    $('#wikiBox').append($('<p />').append(result));
                    $('#wikiBox').append($("<a />").attr('href', 'http://sv.wikipedia.org/wiki/' + tag).html('L&auml;s mer'));
                    $('#wikiBox').show();
                    $('#wikiBox').next('p.info').show();
                }
                break;
            }
        });
    },
    initFlickr:function(tag) {
        $.get('/template/ver2-0/component/infoBlock/flickr.jsp?saploTags=' + tag.replace(/%20/g, ','), function(data) {
            if (data.length > 0) {
                $("#flickrBox").append(data);
                $("#flickrBox").show();
                $('#flickrBox').next('p.info').show();
            }
        });
    },
    getRelatedTags:function(tag) {
        var searchHelper = new SearchHelper('/template/ver2-0/component/gsa/proxy.jsp');
        searchHelper.query=tag;
        getSortedTags('saploTags', 10000, searchHelper, function(searchHelper, tags) {
            for (var i = 0; i <= 5 && i < tags.length; ++i) {
                if (encodeURIComponent(tags[i].value) != tag) {
                    $('#relatedTagsBox').append($('<a />').attr('href', '/tags/'+encodeURIComponent(tags[i].value).replace('%20','+')).html(tags[i].value)).append('&nbsp;');
                }
            }
            $('#relatedTagsBox').show();
        });
    }
};
