﻿var VideoCart = new Class({
    Implements: Options,
    options: {
        dataUrl: 'data/videos.aspx',
        cookieName: 'playlists',
        videoRoot: 'http://d3r33qgzufzbhd.cloudfront.net/',
        price: 20
    },

    initialize: function() {
        this.playerOptions = $('player_options');
        this.playlistDisplay = $('playlistDisplay');
        
        //if ($('player')) new Element('img', { 'src': 'images/player_bg1.png' }).inject($('player'));
        
        this.loadState();
        this.visible = $('preview') != null;
    },
    
    preview: function(video_id, url, collection_id, no_playlist) {
        if (collection_id != 0)
            new PreviewWindow(collection_id);
    },

    previewCollection: function(id, name, url) {
        new PreviewWindow(id);
    },
    
    add: function(id) {
        if (id) {
            if (!this.playlists) this.playlists = [];
            if (!this.isOnPlaylist(id)) {
                this.playlists[this.playlists.length] = id;
                this.saveState();
            }
        }
    },
    
    addAndViewCart: function(id) {
        this.add(id);
        window.location.href = 'playlist.aspx';
    },

    addAndCheckout: function(id) {
        this.add(id);
        window.location.href = './checkout/';
    },

    isOnPlaylist: function(video_id) {
        var found = false;
        if (this.playlists) {
            this.playlists.each(function(id) {
                found = found || (id == video_id);
            });
        }
        return found;
    },
    
    remove: function(id) {
        if (this.playlists) {
            this.playlists.erase(id);
            this.saveState();
        }
    },
    
    loadState: function() {
        this.playlists = [];
        var state = Cookie.read(this.options.cookieName);
        if (state) {
            state.split(',').each(function(s) {
                var id = parseInt(s);
                if (!isNaN(id)) this.playlists[this.playlists.length] = id;
            }.bind(this));
        }
        this.displayPlaylists();
    },
    
    saveState: function() {
        var state = '';
        if (this.playlists) {
            for (var i=0; i<this.playlists.length; i++) {
                if (i>0) state += ',';
                state += this.playlists[i].toString();
            }
        }
        Cookie.write(this.options.cookieName, state, { duration: 365 });
        this.displayPlaylists();
    },
    
    displayPlaylists: function() {
        if (!this.playlistDisplay) return;
        
        if (this.playlists && this.playlists.length > 0) {
            var url = this.options.dataUrl + '?type=playlists&list=';
            for (var i=0; i<this.playlists.length; i++) {
                if (i>0) url += ',';
                url += this.playlists[i].toString();
            }
            new Request.JSON({ 'url': url, 'method': 'get',
                'onSuccess': function(result) {
                    if (result) {
                        this.playlistDisplay.empty();
                        var amount = 0;

                        result.each(function(item) {
                            var wrapper = new Element('div', { 'class': 'item' }).inject(this.playlistDisplay);
                            new Element('img', { 'class': 'remove rollover', 'src': 'images/workout_videos/button_remove.jpg', 'events': {
                                'click': function() { this.remove(item.ID); }.bind(this)
                            }}).inject(wrapper);
                            var text = new Element('div', { 'class': 'text' }).set('text', item.Name).inject(wrapper);
                            new Element('div', { 'class': 'name' }).set('text', 'abc').inject(text);
                            new Element('input').set('value', '$20.00').inject(text);
                            new Element('img', { 'class': 'play rollover', 'src': 'images/workout_videos/play_video.jpg', 'events': {
                                'click': function() { new PreviewWindow(item); }.bind(this)
                            }}).inject(wrapper);
                            new Element('div', { 'class': 'clear' }).inject(wrapper);
                            amount += this.options.price;
                        }.bind(this));
                        
                        if (amount > 0) {
                            var total = new Element('div', { 'class': 'total' }).set('text', 'TOTAL').inject(this.playlistDisplay);
                            new Element('br').inject(total);
                            new Element('input').set('value', '$' + amount.toFixed(2)).inject(total);
                            new Element('img', { 'class': 'buynow rollover', 'src': 'images/workout_videos/button_buynow.jpg', 'events': {
                                'click': function() { window.location.href = './checkout/'; }
                            }}).inject(this.playlistDisplay);
                            new Element('div', { 'class': 'clear' }).inject(this.playlistDisplay);
                        }
                        
                        if (rollovers) rollovers.update();
                    }
                }.bind(this), 'onFailure': function(result) {
                    //alert('An error occurred retrieving data from the server');
            }.bind(this)}).send();
        }
        else {
            this.playlistDisplay.empty();
        }
    }
});
VideoCart.current;

window.addEvent('domready', function() {
    VideoCart.current = new VideoCart();
});