﻿var TrainerInfo = new Class({
    Implements: Options,
    options: {
        dataUrl: 'data/videos.aspx',
        player: 'profile_player'
    },

    initialize: function() {
        if (window.location.search && window.location.search.length > 1)
            this.currentTrainer = parseInt(window.location.search.substring(1,window.location.search.length).parseQueryString().id);
        this.currentTab = $('bio_content');
        this.categories = $('categories');
        this.workouts = $('workouts');
        this.workoutsContainer = $('workouts_container');
        this.programs = $('programs');
        this.featured = $('featured');

        this.loadCategories();
        this.loadPrograms();
        this.loadFeatured();
    },
    
    showVideo: function(url, id) {
        $(this.options.player).empty();
        var s1 = new SWFObject('swf/player-licensed.swf','player','250','218','9');
        s1.addParam('allowfullscreen','true');
        s1.addParam('allowscriptaccess','always');
        s1.addParam('wmode', 'transparent');
        s1.addParam('autostart', 'false');
        s1.addParam('flashvars','autostart=false&file=' + url + '&image=photo.aspx%3Ft=tp%26id=' + id);
        s1.write(this.options.player);
    },
    
    setTab: function(name) {
        var el = $(name + '_content');
        if (el) {
            this.currentTab.setStyle('display', 'none');
            el.setStyle('display', 'block');
            this.currentTab = el;
        }
    },
    
    loadCategories: function() {
        var url = this.options.dataUrl + '?type=trainer-categories&trainer=' + this.currentTrainer;
        new Request.JSON({ 'url': url, 'method': 'get',
            'onSuccess': function(result) {
                if (result) {
                    this.categories.empty();

                    $('container-bodypart').setStyle('display', result.length == 0 ? 'none' : 'block');
                    result.each(function(item) {
                        new Element('img', { 'src': 'photo.aspx?t=ct&id=' + item.ID, 'rel': item.Name, 'events': {
                            'click': function() {
                                this.currentCategoryID = item.ID;
                                this.currentWorkout = null;
                                this.loadWorkouts();
                            }.bind(this)
                        }}).inject(this.categories);
                    }.bind(this));
                    
                    new Top10Tooltip($$('div#categories img'));
                }
            }.bind(this)}).send();
    },
    
    loadWorkouts: function() {
        if (!this.currentCategoryID || !this.currentTrainer) {
            this.workouts.empty();
            this.workoutsContainer.setStyle('display', 'none');
            return;
        }
        
        var url = this.options.dataUrl + '?type=workouts&category=' + this.currentCategoryID + '&trainer=' + this.currentTrainer;
        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.addAndCheckout(item.ID); window.location.href = './checkout/'; }
                            }}).inject(detail);
                        }
                    }.bind(this));
                    }
            }.bind(this)}).send();
    },
    
    loadPrograms: function() {
        if (!this.currentTrainer) {
            this.programs.empty();
            return;
        }
        
        var url = this.options.dataUrl + '?type=programs&trainer=' + this.currentTrainer + '&max=5';
        new Request.JSON({ 'url': url, 'method': 'get',
            'onSuccess': function(result) {
                if (result) {
                    this.programs.empty();

                    $('container-workouts').setStyle('display', result.length == 0 ? 'none' : 'block');
                    result.each(function(item) {
                        if (item.Name != 'Unallocated Videos') {
                            new Element('img', { 'src': 'photo.aspx?t=vc&id=' + item.ID, 'rel': item.Name, 'events': {
                                'click': function() {
                                    this.currentCategoryID = item.CategoryID;
                                    this.currentWorkout = null;
                                    this.loadWorkouts();
                                }.bind(this)
                            }}).inject(this.programs);
                        }
                    }.bind(this));
                    new Top10Tooltip($$('div#programs img'));
                }
            }.bind(this)}).send();
    },

    loadFeatured: function() {
        if (!this.currentTrainer) {
            this.featured.empty();
            return;
        }
        
        var url = this.options.dataUrl + '?type=featured&trainer=' + this.currentTrainer;
        new Request.JSON({ 'url': url, 'method': 'get',
            'onSuccess': function(result) {
                if (result) {
                    this.featured.empty();

                    result.each(function(item) {
                        if (item.Name != 'Unallocated Videos') {
                            var wrapper = new Element('div', { 'class': 'homeresult' }).inject(this.featured);
                            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.addAndCheckout(item.ID); window.location.href = './checkout/'; }
                                }}).inject(detail);
                                new Element('img', { 'src': 'images/results/button_buynow.png', 'class': 'rollover', 'events': {
                                    'click': function() { VideoCart.current.addAndCheckout(item.ID); window.location.href = './checkout/'; }
                                }}).inject(detail);
                            }
                        }
                    }.bind(this));
                }
            }.bind(this)}).send();
    }
});

var trainer;
window.addEvent('domready', function() {
    trainer = new TrainerInfo();
});