var RP = {
    baseUrl: document.location.protocol + '//' + document.location.host + '/',
    templatesUrl: document.location.protocol + '//' + document.location.host + '/' + 'fileadmin/templates/',
    
    init: function() {
        var parentObj = this;
        var ts = new Date().getTime();
        
        $(document).ready(function() {
            $.getScript(RP.templatesUrl + 'js/RPNavigation.js?' + ts, function() {
                RPNavigation.init();
            });
            
            if ($('.rp-videolist').length > 0) {
                $.getScript(RP.templatesUrl + 'js/RPVideoplayer.js?' + ts, function() {
                    RPVideoplayer.init($('.rp-videolist'));
                });
            }
            
            parentObj.beautifyImages();
        });
    },
    
    // Replaces images inserted by Typo3 with nicely wrapped ones (polaroid effect)
    beautifyImages: function() {
        var templateImageBox = $('<div class="box-wrapper"><div class="box"><div class="inner clearfix"></div><div class="shadow-left"></div><div class="shadow-right"></div></div></div>');
        
        $('#content-container > .csc-frame.csc-frame-frame1').each(function() {
            var original = this;
            
            var boxTitle = '';
            var boxText = '';
            
            // Find title
            $(this).find('.csc-header').each(function() {
                boxTitle += $(this).text();
            });
            
            $(this).find('.bodytext').each(function() {
                boxText += $(this).html();
            });
            
            var newImageBox = templateImageBox.clone();                
            
            $(this).find('img').each(function() {
                var img = $(this);
                var imgWidth = img.width();
                
                newImageBox.find('.inner').append(img);
                $(original).replaceWith(newImageBox);
                
                var newImageBoxInnerWidth = newImageBox.innerWidth();
                
                if (newImageBoxInnerWidth < imgWidth) {
                    // If the image is bigger than the box, change image width to 100%
                    img.removeAttr('width');
                    img.removeAttr('height');
                    img.css('width', '100%');
                } else if (newImageBoxInnerWidth > imgWidth) {
                    // If the image is smaller than the box, change the box width accordingly
                    var boxInner = newImageBox.find('.box .inner');
                    var boxInnerPaddingRight = parseInt($(boxInner).css('padding-right'), 10);
                    var boxInnerPaddingLeft = parseInt($(boxInner).css('padding-left'), 10);
                    
                    newImageBox.find('.box').width(imgWidth + boxInnerPaddingRight + boxInnerPaddingLeft);
                }
                
                if (boxTitle != '') {
                    newImageBox.find('.inner').prepend('<h2>' + boxTitle + '</h2>');
                }

                if (boxText != '') {
                    newImageBox.find('.inner').append('<p>' + boxText + '</p>');
                }
            });
        });
    }
};

RP.init();