(function () { // shamefully stolen from somewhere and then lost the link
var methods = {
    defaultValueActsAsHint: function (element) {
        element = $(element);
        element._default = element.value;
        return element.observe('focus', function () {
            if (element._default != element.value) {
                return;
            }
            element.removeClassName('hint').value = '';
            element.setStyle({
                color: '#333'
            });
        }).observe('blur', function () {
            if (element.value.strip() !== '') {
                return;
            }
            element.addClassName('hint').value = element._default;
            element.setStyle({
                color: '#888'
            });
        }).addClassName('hint');
    }
};
$w('input textarea').each(function (tag) {
    Element.addMethods(tag, methods);
});
})();

var CSSdev = {
    enable_tiles: function () {
        $(document.body).insert({
            bottom: '<div style="background: transparent url(/assets/img/tile.png) repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; position: absolute; top: 0pt; left: 0pt; right: 0pt; height: 9000px;"></div>'
        });
    }
};

Math.nearest = function (a, b, c) {
    //returns the nearest to a of b and c
    if (Math.abs(a - b) > Math.abs(a - c)) {
        return c;
    } else {
        return b;
    }
};

var Layout = {
    baseline: 24,
    containerSelector: 'div#alpha-inner',
    containerWidth: 0,
    setContainerWidth: function () {
        Layout.containerWidth = $('alpha-inner').getWidth();
    },
    maxImageWidth: 288,
    maxImageHeight: 336
};

var Images = (function () {
    
    var resize = function (img, isLandscape) {
        var width, newWidth, height, newHeight, imgWidth = img.width, imgHeight = img.height;

        img.store('originalWidth', img.width);
        img.store('originalHeight', img.height);
        
        if (!isLandscape || img.match('img.artwork')) {
            if (imgWidth > Layout.maxImageWidth) {
                newWidth = Layout.maxImageWidth;
            } else {
                newWidth = Math.nearest(imgWidth, Math.floor(imgWidth / Layout.baseline) * Layout.baseline, Math.ceil(imgWidth / Layout.baseline) * Layout.baseline);
            }
            height = Math.floor(newWidth / imgWidth * imgHeight);
            var newHeight = Math.nearest(height, Math.floor(height / Layout.baseline) * Layout.baseline, Math.ceil(height / Layout.baseline) * Layout.baseline);
        } else {
            height = imgHeight;
            if ( height > Layout.maxImageHeight) {
                height = Layout.maxImageHeight;
            }
            width = height / imgHeight * imgWidth;
            newHeight = Math.nearest(height, Math.floor(height / Layout.baseline) * Layout.baseline, Math.ceil(height / Layout.baseline) * Layout.baseline);
            newWidth = Math.nearest(width, Math.floor(width / Layout.baseline) * Layout.baseline, Math.ceil(width / Layout.baseline) * Layout.baseline);
            if (img.up('p')) {
                img.up('p').setStyle({
                    position: 'relative',
                                     width: '100%',
                                     height: newHeight + 'px'
                });
            }
            
        }
        img.setStyle({
            width: newWidth + 'px',
                     height: newHeight + 'px'
        });
    };
    
    var center = function (img) {
        img.setStyle({
            display: 'block',
                     marginTop: '0',
                     marginBottom: '0',
                     marginLeft: 'auto',
                     marginRight: 'auto'
        });
    };
    
    var align = function (img, side) {
        img.setStyle({
            display: 'block',
                     float: side,
                     marginTop: '0',
                     marginRight: side === 'left' ? Layout.baseline + 'px' : '0',
                     marginBottom: Layout.baseline + 'px',
                     marginLeft: side === 'right' ? Layout.baseline + 'px' : '0'
        });
    };
    
    var isLandscape = function (img) {
        if (img.getWidth() > img.getHeight()) {
            return true;
        } else {
            return false;
        }
    };

    var popupImage = function (evt) {
        evt.stop();
        widthRule = parseInt(this.retrieve('originalWidth')) > (document.viewport.getWidth() * 90 / 100) ? 'width: 90%;' : '';
        $(document.body).insert('<div id="overlay" class="clickableImage" style="background: black; position: fixed; top: 0; bottom: 0; left: 0; right: 0;"><img class="clickableImage" style="' + widthRule + 'margin: 15px 0 0 0;" src="' + this.src + '"</div>');
        /*$(document.body).setStyle({
            overflow: 'hidden'
        });*/
        $('overlay').observe('click', function () {
            /*$(document.body).setStyle({
                overflow: 'visible'
            });*/
            $('overlay').remove();
        });
    }
    
    return {
        checkAndResize: function () {
            var helper = function (img) {
                if (isLandscape(img) && !img.match('img.artwork')) {
                    center(img);
                    resize(img, true);
                } else {
                    if (!img.match('img.artwork')) {
                        align(img, 'right');
                    }
                    resize(img, false);
                }
                var par = img.up('p');
                if (par && parseInt(img.retrieve('originalWidth')) > Layout.maxImageHeight + 10) {
                    img.absolutize();
                    img.addClassName('clickableImage');
                    img.observe('click', popupImage.bind(img));
                }
            };
            $$(Layout.containerSelector + ' img').each(function (img) {
                if (!img.match('ul.ratings img') && !img.match('.user-pic img') && !img.match('img.icon')) {
                    if (img.complete) {
                        helper(img);
                    } else {
                        img.onload = helper.bind(window, img);
                    }
                }
            });
        }
    };
    
})();

var Popups = (function () {
    
    var makeRatingPopups = function () {
        $$('ul.ratings .label').each(function (label) {
            var cont = label.up('ul.ratings'),
            image = label.next('img');
            label.clonePosition(cont, {
                offsetTop: -24,
                                setHeight: false,
                                setWidth: false
            });
            image.observe('mouseover', ratingHoverAction.bindAsEventListener(null, true));
            image.observe('mouseout', ratingHoverAction.bindAsEventListener(null, false));
        });
    };
    
    var ratingHoverAction = function (evt, inside) {
        var container = evt.element().up('ul.ratings'),
        label = evt.element().previous();
        if (inside) {
            label.show();
        } else {
            label.hide();
        }
    };
    
    var makeOPEDPopups = function () {
        $$('.youtube_oped').each(function (block) {
            block.getElementsBySelector('a').each(function (anchor) {
                if (anchor.readAttribute('href')) {
                    anchor.observe('click', function (evt) {
                        evt.stop();
                        window.open(anchor.readAttribute('href'), anchor.readAttribute('class'), 'toolbar=no,width=800,height=600');
                    });
                };
            })
        });
    }
    
    
    
    return {
        init: function () {
            makeRatingPopups();
            makeOPEDPopups();
        }
    };
    
})();

var EntryList = (function () {
    
    var showContent = function (evt) {
        var elm = evt.element().next('div');
        if (elm) {
            last_h3.setStyle({
                color: 'black',
                             borderBottom: 'none'
            });
            last_h3 = elm.previous();
            last_h3.setStyle({
                color: '#c00',
                             borderBottom: '1px solid #c00'
            });
            content.innerHTML = elm.innerHTML;
        }
    };
    
    return {
        init: function () {
            root = $('masterlist');
            if (!root) {
                return
            };
            
            root.setStyle({
                float: 'left'
            });
            root.insert({
                after: '<div id="list-content"></div>'
            });
            content = $('list-content');
            
            root.getElementsBySelector('li div.all-entries').invoke('hide');
            root.childElements().invoke('setStyle', {
                float: 'left'
            });
            root.getElementsBySelector('div.entry-container').invoke('setStyle', {
                float: 'left'
            });
            
            last_h3 = root.down('h3:not(.empty)');
            showContent({
                element: function () {
                    return last_h3
                }
            });
            
            root.observe('click', showContent);
        }
    }
    
})();

var Search = function () {
    var activate = function (widget) {
        var txtarea = widget.down('input.search');
        if (!txtarea.value) {
            txtarea.value = "Search";
        }
        txtarea.style.color = '#888888';
        txtarea.defaultValueActsAsHint();
        widget.down('img.search_wipe').observe('click', function () {
            this.previous('.search').value = '';
            this.previous('.search').focus();
        });
    };
    
    var init = function () {
        $$('.header-search').each(activate);
    };
    
    return {
        init: init
    };
} ()

var init = function () {
    Layout.setContainerWidth();
    Search.init();
    if (!$('masterlist')) {
        Images.checkAndResize();
        Popups.init();
    } else {
        EntryList.init();
    }
}

document.observe('dom:loaded', init);
