/*
 * Logging for IE {{{ */
window.addEvent(
    'domready', function(){
        if(Browser.Engine.trident){
            $('mainContainer').grab(
                new Element('div' , {"id": "log"}).setStyle('visibility', 'hidden'), 'top'
            );
        }
    });

var log = function(){
    var args = Array.prototype.slice.call(arguments);
    if ($('log')){
        $('log')
            .setStyle('visibility', 'visible')
            .grab(
                new Element('p', {"text": args.join('')}),
                'bottom'
            );
    } else {
        console.log.apply(console, args);
    }
};

/* }}}
 * setupFlashHouse - for the frontpage {{{ **/

var setupFlashHouse = function(){
    var flashHouse = new Swiff(
        '/userfiles/house/hrvhouse.swf',
        {
            'container': $('flashHouse'),
            'width': 793,
            'height': 340
        });
};

window.addEvent('domready', setupFlashHouse);

/* }}}
 * Paging for Videos {{{ */
window.addEvent(
    'domready', function(){
        if ($('Videos')){
            var pages = $$('#Videos .page');
            var prev = $$('#Videos .previous');
            var next = $$('#Videos .next');
            var numPages = pages.length;
            var currentPage = 1;

            function lastPage(){
                return currentPage >= numPages;
            }

            function firstPage(){
                return currentPage <= 1;
            }

            function showPage(num){
                pages.setStyle('display', 'none');
                pages[num - 1].setStyle('display', 'block');
                showHideButtons(num);
            }

            function showHideButtons(num){
                if (firstPage()) prev.addClass("disabled");
                else prev.removeClass("disabled");

                if (lastPage()) next.addClass("disabled");
                else next.removeClass("disabled");
            }

            function showPrevious(){
                if (!firstPage()){
                    currentPage -= 1;
                    showPage(currentPage);
                }
            }

            function showNext(){
                if (!lastPage()){
                    currentPage += 1;
                    showPage(currentPage);
                }
            }

            prev.addEvent('click', showPrevious);
            next.addEvent('click', showNext);
            showPage(currentPage);
        }
    });

/*
 repl.enter(content.wrappedJSObject);

 $$('.thumbLink').each(
 function(item, index, array){
 item.grab(
 new Element('div', {"class": "frame"}), 'top');
 });
 */

window.addEvent(
    'domready', function(){
        var hash = window.location.hash;
        if(hash.length !== 0 && $$(hash)) $$(hash).addClass('highlight');
    });

Array.implement({
    "collect": function collect (fn, bind){
        /* fn.calls that throw an exception are
         * excluded and the index is not incremented
         * so that it's like they were never there.
         * can be used instead of a.map( ... ).filter( ... )
         * --------------------------------------------------
         * Throwing is quite slow though, so you'd only really
         * want to use it when fn is already throwing errors IMO */
        var results = [];
        var j = 0;
        for (var i = 0, l = this.length; i < l; i++ ){
            try {
                results[j] = fn.call(bind, this[i], j, this);
                j++;
            } catch (err) {}
        }
        return results;
    }
});

function $throw (error) {
    throw ( error || new Error("Unknown"));
};

/**
 * Handle swfbanners
 */

function flashReplace (element, url, width, height) {
    (new Swiff( url, {
        'width': width,
        'height': height
    })).replaces(element);
}

window.addEvent('domready', function(){
    $$('div.swfBanner').each(
        function(item, index, array){
            try {
                var href = item.getElement('a.flashReplacement').get('href');
                var dim = item.getElement('img').getSize();

                flashReplace(
                    item, href,
                    dim.x, dim.y
                );
            } catch (err){
                // remember each time
                // you write an empty catch block
                // I die a little
                // -- #! /bin/basho
            }

        });
});
