﻿var SearchForm = new Class({
    Implements: Options,
    options: {
        videoRoot: 'http://d3r33qgzufzbhd.cloudfront.net/'
    },

    initialize: function() {
        this.textbox = $('q');
        this.searchBtn = $('qs');
        this.advancedBtn = $('qa');
        
        if (this.textbox && this.searchBtn && this.advancedBtn)
        {
            this.textbox.addEvent('keydown', function(event) {
                if (event.key == 'enter') {
                    event.stop();
                    this.search();
                }
            }.bind(this));
            
            this.searchBtn.addEvent('click', function() {
                this.search();
            }.bind(this));
            
            this.advancedBtn.addEvent('click', function() {
                this.advancedSearch();
            }.bind(this));
        }
    },
    
    search: function() {
        var q = this.textbox.value.trim();
        if (q.length == 0) {
            alert('Please enter your search criteria');
            this.textbox.focus();
            return;
        }

        window.location.href = 'search.aspx?q=' + escape(q);        
    },
    
    advancedSearch: function() {
        window.location.href = 'search.aspx';
    },

    showInfo: function(id, name, description, trainer_id, trainer_name, video) {
        this.hideInfo();
        
        var container = new Element('div', { 'id': 'infobox', 'class': 'infobox' }).inject($(document.body));
        new Element('span', { 'events': { 'click': function() { this.hideInfo(); }.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', { 'id': 'infoplayer' }).inject(col);

        new Element('div', { 'class': 'trainer' }).set('text', trainer_name).inject(col);
        var btns = new Element('div', { 'class': 'buttons' }).inject(col);
        new Element('img', { 'src': 'images/moreinfo_profile.png' }).inject(
            new Element('a', { 'href': 'trainer.aspx?id=' + trainer_id }).inject(btns));
        new Element('img', { 'src': 'images/moreinfo_add.png' }).inject(
            new Element('a', { 'href': 'videos.aspx?add=' + id }).inject(btns));
        
        col = new Element('div', { 'class': 'col2' }).inject(container);
        new Element('div', { 'class': 'name' }).set('text', name).inject(col);
        new Element('div', { 'class': 'description' }).set('html', description).inject(col);
        
        this.dialog = new StickyWin.Modal({ content: container });
        
        var position = this.dialog.element.getPosition();
        var size = container.getSize();
        this.dialog.element.setStyles({
            'left': (position.x - (size.x / 2)) + 'px',
            'top': (position.y - size.y) + 'px'
        });
        
        if (rollovers) rollovers.update();

        var s1 = new SWFObject('swf/player-licensed.swf','player','300','225','9');
        s1.addParam('allowfullscreen','true');
        s1.addParam('allowscriptaccess','always');
        s1.addParam('wmode', 'transparent');
        s1.addParam('flashvars','file=' + this.options.videoRoot + video + '&image=photo.aspx%3Ft=vc%26id=' + id);
        s1.write('infoplayer');
    },

    hideInfo: function() {
        if (this.dialog) this.dialog.hide();
        
        var el = $('infobox');
        if (el) el.destroy();
    }
});

var searchform;
window.addEvent('domready', function() {
    searchform = new SearchForm();
});
