function FixAllTemplateProblems() {
        var ifr;
        try {
            ifr = $('#ctl00_FullRegion_PreviewFrame_InnerControl_PreviewFrame', parent.document.body);
        } catch (ex) {
            ifr = '';
        }

        var fixAll = function() {
            templateFixBody();
            templateRemoveDuplicatedTeaserSeparators();
            templateRemoveTeaserSeparators();
            templateFixColumn4Teasers();
        };

        if (ifr.length == 1) {
            ifr.load(function() {
                setTimeout(fixAll, 2000);
            });
        }
        else {
            fixAll();
        }
};

function templateFixBody() {

    //Fix for too much space in IE in bottom pagination     
    if (!$('.article-actions.bottom .paging a.action-link').length) {
        $('.article-actions.bottom').hide();
    }    
    //Fix for wide article - article text is covered by the banner, so text is underneath the banner. 
    //This script is duplicated in #phWideArticleCorrectionScript in Article.aspx. But it didn't properly fix the situation in admin. That's why we added it here too.
    //So maybe it's possible to remove that script in Article.aspx.
    var wideAdvertisementDivHeight=0;
    wideAdvertisementDivHeight = $('.wideAdvertisementDiv').height();
    var wideheightFix = 50;
    $('.article-wrapper-banner').height(wideAdvertisementDivHeight + wideheightFix);
    var wideAdvertisementDivWidth = 0;    
    wideAdvertisementDivWidth = $('.wideAdvertisementDiv').width();
    var widewidthFix = 155;
    $('.article-wrapper-banner').width(wideAdvertisementDivWidth - widewidthFix);      
    
    var maxheight = 0;
    var wideRegionHeight = 0;
    var extraWideRegionHeight = 0;
    maxHeight = $('#content-column1-region-wrapper').height();
    
    wideRegionHeight = $('.wide-region-content').height();
    if (maxHeight != null && maxHeight != 0) {
        var column2Height = maxHeight - wideRegionHeight;
        var wideRegionHeight = maxHeight - extraWideRegionHeight;

        var heightFix = 0;
        if ($('#column3-region').find('.panel-nav').length == 1) {
            heightFix += 150; // display:none div in this panel can contain more text than it is expected
        }

        $('#column1-region').height(maxHeight + heightFix);
        $('#wide-region-wrap').height(wideRegionHeight + heightFix);
        $('#column2-region').height(column2Height + heightFix);

        if ($.browser.msie && $.browser.version < 7) {
            $('#wide-region-wrap').css('padding-bottom', 0);
        }
    }
}

function templateResizeFix(diff) {
    $('#column1-region').height($('#column1-region').height() + diff);
    $('#wide-region-wrap').height($('#wide-region-wrap').height() + diff);
    $('#column2-region').height($('#column2-region').height() + diff);

    if ($.browser.msie && $.browser.version < 7) {
        $('#wide-region-wrap').css('padding-bottom', 0);
    }
}

function templateRemoveTeaserSeparators() {
    $("div.teaser + .teaserSeparator.hr").each(function(index) {
        var objToHide = $(this).prev();
        var objToDel = $(this);
        var domObj = $(this).prev().get();

        for (var i = 0; i < domObj.length; i++) {
            if (domObj[i].innerHTML.indexOf('empty.gif') != -1) {
                objToHide.hide();
                objToDel.remove();
                break;
            }
        }
    });
}

function templateFixColumn4Teasers() {
    var img = $("#column4-region div.teaser #banner img[width='1'][height='1']");
    if (img.length > 0) {
        img.parents(".teaser").next().remove();
        img.parents(".teaser").remove();
    }
}

//CR 914
function templateRemoveDuplicatedTeaserSeparators() {
    //remove duplicated separators
    var duplicatedSeparators = $("div.hr + div.hr").filter('.teaserSeparator');
    duplicatedSeparators.remove();

    //remove first separator if nothing above it. Possible issue in IE due to http://dev.jquery.com/ticket/2847
    var first = $(".teaserSeparator:first-child");
    if (first != undefined) {
        first.remove();
    }

}
