if (typeof(tracker) == 'undefined') {
    tracker = false;
}


var AdSwap = Class.create({

    createMovieSizes: function(item) {
        item.addClassName('adtrigger');

        var id = item.up('div').identify().replace(/ad-/, '');
        var div = document.createElement('div');

        if (this.movieSizeNav) {
            var movieSizeNav = this.movieSizeNav.cloneNode(true);
            var links = movieSizeNav.select('a');
            links.each(function(link, i) {
                var href = link.getAttribute('href').replace(/.*#/, '#'+id+'-');
                link.href = href;
            });
        }

        var large = div.cloneNode(true);
        large.id = id+'-large';
        large.className = 'size large';
        item.parentNode.appendChild(large);
        var movie = div.cloneNode(true);
        movie.className = 'movie';
        movie.innerHTML += '<a class="movieLink" href="'+item.href.toString().replace('848x480', '640x360')+'?width=640&amp;height=360"></a>';
        if (this.endState) {
            movie.appendChild(this.endState.cloneNode(true));
        }
        if (movieSizeNav) {
            movie.appendChild(movieSizeNav.cloneNode(true));
        }
        large.appendChild(movie);

        var hd = div.cloneNode(true);
        hd.id = id+'-hd';
        hd.className = 'size hd';
        item.parentNode.appendChild(hd);
        var movie = div.cloneNode(true);
        movie.className = 'movie';
        movie.innerHTML += '<a class="movieLink" href="'+item.href+'?width=848&amp;height=480"></a>';
        if (this.endState) {
            movie.appendChild(this.endState.cloneNode(true));
        }
        if (movieSizeNav) {
            movie.appendChild(movieSizeNav.cloneNode(true));
        }
        hd.appendChild(movie);

        var small = div.cloneNode(true);
        small.id = id+'-small';
        small.className = 'size small';
        item.parentNode.appendChild(small);
        var movie = div.cloneNode(true);
        movie.className = 'movie';
        movie.innerHTML += '<a class="movieLink" href="'+item.href.toString().replace('848x480', '320x180')+'?width=320&amp;height=180"></a>';
        if (this.endState) {
            movie.appendChild(this.endState.cloneNode(true));
        }
        if (movieSizeNav) {
            movie.appendChild(movieSizeNav.cloneNode(true));
        }
        small.appendChild(movie);

        var medium = div.cloneNode(true);
        medium.id = id+'-medium';
        medium.className = 'size medium';
        item.parentNode.appendChild(medium);
        var movie = div.cloneNode(true);
        movie.className = 'movie';
        movie.innerHTML += '<a class="movieLink" href="'+item.href.toString().replace('848x480', '480x272')+'?width=480&amp;height=272"></a>';
        if (this.endState) {
            movie.appendChild(this.endState.cloneNode(true));
        }
        if (movieSizeNav) {
            movie.appendChild(movieSizeNav.cloneNode(true));
        }
        medium.appendChild(movie);

        item.href = '#'+id+'-large';
    },

    initialize: function() {
        this.wrapper = $('display').up();
        this.endState = this.wrapper.down('.endState');
        this.movieSizeNav = this.wrapper.down('.sizenav');

        this.ads = $$('#adsnav div a');

        this.ads.each(this.createMovieSizes.bind(this));
        this.swapView = new AC.ViewMaster.Viewer(null, 'display', 'adtrigger', { animationDuration:.4 });
        this.swapView.setDelegate(this);

        if (document.location.hash) {
            var id = document.location.hash.replace(/#/, '');
            var content = $(id);
            if (content) {
                var section = this.swapView.addSection(content);
                this.swapView.show(section);
            }
        }

        if (!this.swapView.currentSection) {
            var section = this.swapView.addSection($$('#adsnav div.size')[0]);
            this.swapView.show(section);
        } else {
            this.swapView.didAppendContent(this.swapView);
        }
    },

    willShow: function(view, outgoing, incoming) {
        this.currentId = incoming.id;

        var size = incoming.id.replace(/.*-/, '');
        view.options.shouldAnimateContentChange = !(this.currentSize == size);
        this.currentSize = size;
    },

    didAppendContent: function(view) {
        var incoming = view.currentSection;
        var container = view.view.view();

        if (incoming && container) {
            var newDimensions = incoming.content.getDimensions();
            var currentDimensions = view.view.view().getDimensions();

            if (view.options.shouldAnimateContentChange) {
                new Effect.Parallel([
                    new Effect.Scale(container, ((newDimensions.width / currentDimensions.width) * 100), { scaleY:false, scaleContent:false, sync:true }),
                    new Effect.Scale(container, ((newDimensions.height / currentDimensions.height) * 100), { scaleX:false, scaleContent:false, sync:true })
                ], { duration:view.options.animationDuration });
            } else {
                container.style.width = newDimensions.width+'px';
                container.style.height = newDimensions.height+'px';
            }
        }
    },

    didShow: function(view, outgoing, incoming) {
        if (!this.currentId || !this.currentSize) {
            this.currentId = incoming.id;
            this.currentSize = incoming.id.replace(/.*-/, '');
        }

        this.setSize(incoming.id);

        view.options.shouldAnimateContentChange = false;
    },

    setSize: function(view, outgoing, incoming) {
        var id = this.currentId;
        var size = this.currentSize;

        $$('#adsnav div > a.'+this.swapView.triggerClassName).each(function(item) {
            var movie = item.href.replace(/.*#(.*)-.*/, '$1');
            item.href = '#'+movie+'-'+size;
        });
    }

});


Event.observe(window, 'load', function() {
    if (!tracker) {
        tracker = new AC.ViewMaster.Tracker('click');
    }

    var container = $('adsnav');

    if (AC.Detector.isMobile()) {

        container.addClassName('mobile');
        container.select('a').each(function(ad) {
            ad.href = ad.href.replace(ad.href.substr(ad.href.length-11, 7), 'r320-9cie')
        });

    } else {

        container.addClassName('standard');
        new AdSwap();

    }

});
