﻿var PreviewWindow = new Class({
    Implements: Options,
    options: {
        dataUrl: 'data/videos.aspx',
        videoRoot: 'http://d3r33qgzufzbhd.cloudfront.net/'
    },

    initialize: function(item) {
        this.item = item;
        PreviewWindow.hide();
        
        var container = new Element('div', { 'id': 'infobox', 'class': 'infobox' }).inject($(document.body));
        new Element('span', { 'events': { 'click': function() { PreviewWindow.hide(); }.bind(this)}}).set('text', 'X').inject(
            new Element('div', { 'class': 'top' }).inject(container));
        
        var col = new Element('div', { 'class': 'col1' }).inject(container);
        new Element('div', { 'class': 'name' }).set('text', item.Name).inject(col);
        this.videos = new Element('ul', { 'id': 'videolist' }).inject(col);
        
        col = new Element('div', { 'class': 'col2' }).inject(container);
        new Element('div', { 'id': 'infoplayer' }).inject(col);

        var btns = new Element('div', { 'class': 'buttons' }).inject(col);
        new Element('img', { 'src': 'images/preview/button_profile.png' }).inject(
            new Element('a', { 'href': 'trainer.aspx?id=' + item.TrainerID }).inject(btns));
        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(btns));
        }
        else {
            new Element('img', { 'src': 'images/preview/button_add.png', 'events': {
                'click': function() { VideoCart.current.addAndViewCart(item.ID); }
            }}).inject(btns);
            new Element('img', { 'src': 'images/preview/button_buynow.png', 'events': {
                'click': function() { VideoCart.current.addAndCheckout(item.ID); }
            }}).inject(btns);
        }
        new Element('div', { 'styles': { 'margin-top': '6px', 'text-align': 'center', 'font-size': '10px' }}).set('text', 'Preview Only. Purchase To View Full Video.').inject(col);

        col = new Element('div', { 'class': 'col3' }).inject(container);
        new Element('div', { 'class': 'trainer' }).set('text', item.TrainerName).inject(col);
        this.videoName = new Element('div', { 'class': 'name' }).inject(col);
        this.videoDescription = new Element('div', { 'class': 'description' }).inject(col);
        PreviewWindow.dialog = new StickyWin.Modal({ content: container });
        
        var position = PreviewWindow.dialog.element.getPosition();
        var size = container.getSize();
        PreviewWindow.dialog.element.setStyles({
            'left': (position.x - (size.x / 2)) + 'px',
            'top': (position.y - size.y) + 'px'
        });
        
        if (rollovers) rollovers.update();

        this.loadVideos();
    },
    
    loadVideos: function() {
        var url = this.options.dataUrl + '?type=videos&workout=' + this.item.ID;
        new Request.JSON({ 'url': url, 'method': 'get',
            'onSuccess': function(result) {
                if (result) {
                    result.each(function(item) {
                        if (item.SDPath) {
                            new Element('a', { 'rel': item.ID, 'events': {
                                'click': function() {
                                    this.loadVideo(item);
                                }.bind(this)
                            }}).set('text', item.Name).inject(new Element('li').inject(this.videos));
                        }
                    }.bind(this));
                    
                    // show first video
                    if (result.length > 0)
                        this.loadVideo(result[0]);
                }
            }.bind(this)}).send();
    },
    
    loadVideo: function(item) {
        // show new video
        var s1 = new SWFObject('swf/player-licensed.swf','player','300','225','9');
        s1.addParam('allowfullscreen','true');
        s1.addParam('allowscriptaccess','always');
        s1.addParam('wmode', 'transparent');
        
        var clip = '';
        if (item.HasPreviewTime && !is_member)
            clip = '&start=' + item.PreviewStart + '&duration=' + (item.PreviewEnd - item.PreviewStart);
        s1.addParam('flashvars','autostart=true&file=' + this.options.videoRoot + item.SDPath + '&image=photo.aspx%3Ft=vt%26id=' + item.ID + clip);
        s1.write('infoplayer');
        
        // update active link
        this.videos.getElements('a').each(function(el) {
            if (el.getProperty('rel') == item.ID)
                el.addClass('active');
            else
                el.removeClass('active');
        });
        
        // display video information
        if (this.videoName) this.videoName.set('text', item.Name);
        if (this.videoDescription) this.videoDescription.set('html', item.Description);
    }
});

PreviewWindow.hide = function() {
    if (PreviewWindow.dialog) PreviewWindow.dialog.hide();
    var el = $('infobox');
    if (el) el.destroy();
};

PreviewWindow.load = function(id) {
    var url = 'data/videos.aspx?type=collection&video=' + id;
    new Request.JSON({ 'url': url, 'method': 'get',
        'onSuccess': function(result) {
            if (result)
                new PreviewWindow(result);
        }.bind(this)}).send();
};