﻿var VideoPage = new Class({
    Implements: Options,
    options: {
        dataUrl: 'data/videos.aspx',
        category: null,
        trainer: null,
        workout: null
    },

    initialize: function(options) {
        this.setOptions(options);
        
        this.categories = $('categories');
        this.trainers = $('trainers');
        this.workouts = $('workouts');
        
        this.trainersContainer = $('trainers_container');
        this.workoutsContainer = $('workouts_container');

        this.loaded = this.options.workout ? false : true;
        this.loadCategories();
        this.addFromQuerystring();
    },
    
    addFromQuerystring: function() {
        if (window.location.search && window.location.search.length > 1) {
            var add = parseInt(window.location.search.substring(1, window.location.search.length).parseQueryString().add);
            if (!isNaN(add))
                VideoCart.current.add(add);
        }
    },
    
    loadCategories: function() {
        var url = this.options.dataUrl + '?type=categories';
        new Request.JSON({ 'url': url, 'method': 'get',
            'onSuccess': function(result) {
                if (result) {
                    this.categories.empty();

                    result.each(function(item) {
                        new Element('img', { 'src': 'photo.aspx?t=ct&id=' + item.ID, 'rel': item.Name, 'events': {
                            'click': function() {
                                this.currentCategory = item;
                                this.currentTrainer = null;
                                this.currentWorkout = null;
                                this.loadTrainers();
                                this.loadWorkouts();
                            }.bind(this)
                        }}).inject(this.categories);
                        
                        if (this.options.category && !this.loaded && item.ID == this.options.category) {
                            this.currentCategory = item;
                            this.loadTrainers();
                        }
                    }.bind(this));
                    
                    new Top10Tooltip($$('div#categories img'));
                }
            }.bind(this), 'onFailure': function(result) {
        }.bind(this)}).send();
    },
    
    loadTrainers: function() {
        if (!this.currentCategory) {
            this.trainers.empty();
            this.trainersContainer.setStyle('display', 'none');
            return;
        }
        
        var url = this.options.dataUrl + '?type=trainers&category=' + this.currentCategory.ID;
        new Request.JSON({ 'url': url, 'method': 'get',
            'onSuccess': function(result) {
                if (result) {
                    this.trainersContainer.setStyle('display', 'block');
                    this.trainers.empty();

                    result.each(function(item) {
                        new Element('img', { 'src': 'photo.aspx?t=tt&id=' + item.ID, 'rel': item.Fullname, 'events': {
                            'click': function() {
                                this.currentTrainer = item;
                                this.currentWorkout = null;
                                this.loadWorkouts();
                            }.bind(this)
                        }}).inject(this.trainers);
                        
                        if (this.options.trainer && !this.loaded && item.ID == this.options.trainer) {
                            this.currentTrainer = item;
                            this.loadWorkouts();
                        }
                    }.bind(this));
                    
                    new Top10Tooltip($$('div#trainers img'));
                }
            }.bind(this), 'onFailure': function(result) {
        }.bind(this)}).send();
    },
    
    loadWorkouts: function() {
        if (!this.currentCategory || !this.currentTrainer) {
            this.workouts.empty();
            this.workoutsContainer.setStyle('display', 'none');
            return;
        }
        
        var url = this.options.dataUrl + '?type=workouts&category=' + this.currentCategory.ID + '&trainer=' + this.currentTrainer.ID;
        new Request.JSON({ 'url': url, 'method': 'get',
            'onSuccess': function(result) {
                if (result) {
                    this.workouts.empty();
                    this.workoutsContainer.setStyle('display', 'block');

                    result.each(function(item) {
                        var wrapper = new Element('div', { 'class': 'homeresult' }).inject(this.workouts);
                        new Element('img', { 'class': 'thumb', 'src': 'photo.aspx?t=vc&id=' + item.ID }).inject(wrapper);
                        var detail = new Element('div', { 'class': 'detail' }).inject(wrapper);
                        new Element('div', { 'class': 'news_search' }).set('text', item.Name).inject(detail);
                        new Element('div', { 'class': 'news_search_text' }).set('text', item.CategoryName).inject(detail);
                        new Element('img', { 'src': 'images/results/button_preview.png', 'class': 'rollover', 'events': {
                            'click': function() { new PreviewWindow(item); }.bind(this)
                        }}).inject(detail);
                        if (is_member) {
                            new Element('img', { 'src': 'images/button_addtoaccount.jpg', 'class': 'rollover' }).inject(
                                new Element('a', { 'href': 'add.aspx?id=' + item.ID + '&from=' + escape(window.location.pathname + window.location.search) }).inject(detail));
                        }
                        else {
                            new Element('img', { 'src': 'images/results/button_add.png', 'class': 'rollover', 'events': {
                                'click': function() { VideoCart.current.addAndViewCart(item.ID); }.bind(this)
                            }}).inject(detail);
                            new Element('img', { 'src': 'images/results/button_buynow.png', 'class': 'rollover', 'events': {
                                'click': function() { VideoCart.current.add(item.ID); window.location.href = './checkout/'; }
                            }}).inject(detail);
                        }
                    }.bind(this));

                    new Top10Tooltip($$('div#workouts img'));
                }
            }.bind(this), 'onFailure': function(result) {
        }.bind(this)}).send();
    }
});

