﻿
// ***********************
// START video-player.js
// ***********************

// VideoPlayer Class
//   Usage: new VideoPlayer(element);
//      or: new VideoPlayer(element, options);
var VideoPlayer = new Class({

    //--- Interfaces ---

    Implements: [Options],

    //--- Private members ---

    container: null,
    currentPage: null,
    data: null,
    description: null,
    menu: null,
    noOfPages: null,
    options: { // Options with default values
        autoplayOnClick: 1,
        autoplayOnLoad: 0,
        noOfThumbs: 3,
        scrollerLeftArrowHtml: '&lt;',
        scrollerRightArrowHtml: '&gt;'
    },
    player: null,
    scroller: null,
    thumbs: null,

    //--- Methods ---

    // Constructor
    initialize: function(container, options) {
        this.container = $(container);
        if (!this.container) { return; }

        this.setOptions(options);

        this.data = this.getMenuData();
        if (!this.data) { return; }
        this.disposeChildren(this.container);

        this.player = new Element('div', { 'class': 'player' }).inject(this.container);
        this.menu = new Element('div', { 'class': 'menu' }).inject(this.container);
        this.description = new Element('div', { 'class': 'description', 'html': '&nbsp;' }).inject(this.menu);

        // If only one video, don't create menu
        if (this.data.length > 1) {
            this.thumbs = new Element('div', { 'class': 'thumbs' }).inject(this.menu, 'top');
            this.scroller = new Element('div', { 'class': 'scroller' }).inject(this.menu, 'top');
            this.createScroller();
            this.createThumbs();
            this.setPage(1);
        }
        this.selectVideo(0, { 'autoplay': this.options.autoplayOnLoad });
    },

    // Disposes all children of an element
    disposeChildren: function(parent) {
        if ($type(parent) == 'element') {
            parent.getChildren().each(function(child) {
                child.dispose();
            });
        }
    },

    // Sets a page in the thumb scroller menu
    setPage: function(pageIndex) {
        this.currentPage = pageIndex;
        this.setScroller();
        this.setThumbs();
    },

    // Go to previous page in the thumb scroller menu
    prevPage: function() {
        if (this.currentPage - 1 < 1) {
            this.setPage(this.noOfPages);
        }
        else {
            this.setPage(this.currentPage - 1);
        }
    },

    // Go to next page in the thumb scroller menu
    nextPage: function() {
        if (this.currentPage + 1 > this.noOfPages) {
            this.setPage(1);
        }
        else {
            this.setPage(this.currentPage + 1);
        }
    },

    // Retrieves and decodes the data
    getMenuData: function() {
        var dataElement = this.container.getElement('.data');
        var data = null;
        if (dataElement && dataElement.get('text')) {
            var text = dataElement.get('text').clean();
            data = JSON.decode(text);
        }
        dataElement.dispose();
        return data;
    },

    // Creates the thumb scroller menu markup
    createScroller: function() {
        this.currentPage = 1;
        this.noOfPages = (this.data.length / this.options.noOfThumbs).ceil();
        // If more the one page, create scroll menu items
        if (this.noOfPages <= 1) { return; }

        var list = new Element('ul', { 'class': 'menu horizontal' });

        // Numbers
        var listItem = null, index = null;
        this.noOfPages.times(function(index) {
            listItem = new Element('li', { 'text': (index + 1).toString() });
            listItem.inject(list);
        });
        list.getElements('li').each(function(item) {
            item.addEvent('click', function() {
                this.setPage(item.get('text'));
            } .bind(this));
        }, this);

        // Prev Page
        new Element('li', {
            'html': this.options.scrollerLeftArrowHtml,
            'events': {
                'click': function() {
                    this.prevPage();
                } .bind(this)
            }
        }).inject(list, 'top');

        // Next Page
        new Element('li', {
            'html': this.options.scrollerRightArrowHtml,
            'events': {
                'click': function() {
                    this.nextPage();
                } .bind(this)
            }
        }).inject(list);
        list.inject(this.scroller);
    },

    // Sets the current state on thumb scroller menu
    setScroller: function() {
        if (this.noOfPages <= 1) { return; }
        this.scroller.getElements('li').each(function(item) {
            item.removeClass('current');
        });
        this.scroller.getElement('[text=' + this.currentPage + ']').addClass('current');
    },

    // Creates the thumb box markup
    createThumbs: function() {
        var box = null, boxClass = null, videoIndex;
        this.options.noOfThumbs.times(function(index) {
            if (index < this.data.length) {
                boxClass = (index < (this.options.noOfThumbs - 1)) ? 'box' : 'box last';
                box = new Element('div', { 'class': boxClass });
                new Element('div', { 'class': 'image' }).inject(box);
                new Element('div', { 'class': 'name' }).inject(box);
                new Element('div', { 'class': 'time' }).inject(box);
                box.inject(this.thumbs);
            }
        }, this);
        this.thumbs.getElements('.box').each(function(item) {
            item.addEvent('click', function() {
                this.selectVideo(item.retrieve('videoIndex'), { 'autoplay': this.options.autoplayOnClick });
            } .bind(this));
        }, this);
    },

    // Sets new data to the thumb boxes
    setThumbs: function() {
        var startIndex = 0;
        if (this.data.length > this.options.noOfThumbs) {
            startIndex = (this.currentPage * this.options.noOfThumbs) - this.options.noOfThumbs;
            if ((startIndex + 2) >= this.data.length) {
                startIndex = startIndex - (this.options.noOfThumbs - (this.data.length % this.options.noOfThumbs));
            }
        }
        this.thumbs.getElements('.box').each(function(item, index) {
            videoIndex = startIndex + index;
            //console.log(videoIndex, startIndex, index);
            item.getElement('.image').setStyle('background-image', "url('" + this.data[videoIndex].image + "')");
            item.getElement('.name').set('text', this.data[videoIndex].name);
            item.getElement('.time').set('text', this.data[videoIndex].time);
            item.store('videoIndex', videoIndex);
        }, this);
    },

    // Changes the video to a new one
    setVideo: function(item/*, options*/) {
        var options = (arguments.length > 1) ? new Hash(arguments[1]) : null;
        var url = options ? this.data[item].url + '&amp;' + options.toQueryString() : this.data[item].url;
        var swiff = new Swiff(url, {
            'container': this.player,
            'height': '265',
            'id': 'invFlashVideoPlayer',
            'params': {
                'allowScriptAccess': true
            },
            'width': '320'
        });
    },

    // What to do when a video is selected
    selectVideo: function(item, videoOptions) {
        this.setVideo(item, videoOptions);
        this.setDescripton(this.data[item].description);
    },

    // Changes the video to a new one
    setDescripton: function(html) {
        this.description.set('html', html);
    }
});
// ********** END ************

// ***************************
// Start Toggler.js
// ***************************

// Toggler Class
//   Usage: new Toggler(element);
//      or: new Toggler(element, options);
var Toggler = new Class({

    //--- Interfaces ---

    Implements: [Options],

    options: { // Options with default values
        containerClass: 'hidden',
        triggerClass: 'hidden'
    },

    //--- Private members ---

    trigger: null,
    container: null,

    //--- Methods ---

    // Constructor
    initialize: function(trigger, container, options) {
        this.setOptions(options);
        this.trigger = $(trigger);
        this.container = $(container);
        if (!this.trigger || !this.container) { return; }
        this.togglify();
    },

    // Find child link and linkify the container
    togglify: function() {
        this.trigger.addEvent('click', function() {
            this.container.toggleClass(this.options.containerClass);
            this.trigger.toggleClass(this.options.triggerClass);
        } .bind(this));
    }
});

// ********** END ************


// *********************************
// Start link-container.js
// *********************************

// LinkContainer Class
//   Usage: new LinkContainer(containerElement);
//      or: new LinkContainer(containerElement, options);
var LinkContainer = new Class({

    //--- Interfaces ---

    Implements: [Options],

    //--- Private members ---

    container: null,
    link: null,
    options: { // Options with default values
        linkSelector: 'a',
        setCursor: true,
        setTitle: true
    },

    //--- Methods ---

    // Constructor
    initialize: function(container, options) {
        this.setOptions(options);
        this.container = $(container);
        if (!this.container) { return; }
        this.linkify();
    },

    // Find child link and linkify the container
    linkify: function() {
        this.link = this.container.getElement(this.options.linkSelector);
        if (!this.link || !this.link.get('href')) { return; }
        this.container.store('href', this.link.get('href'));
        if (this.options.setCursor) { this.container.setStyle('cursor', 'pointer'); }
        if (this.options.setTitle) { this.container.set('title', this.link.get('title')); }
        this.container.addEvent('click', function() {
            window.location = this.retrieve('href');
        });
    }
});
// ********** END ************


// **************************************
// Start Implementation.js
// **************************************

// To do when DOM is loaded and ready
window.addEvent('domready', function() {
    // Create video player
    $$('.contentBox.video .body').each(function(item) {
        new VideoPlayer(item);
    });
    // Linkify hilights
    $$('.highlightsWrapperLeft .highlight').each(function(item) {
        new LinkContainer(item);
    });
    // Togglify article boxes
    $$('.contentBox').each(function(item) {
        var trigger = item.getElement('.title .toggler');
        var container = item.getElement('.body');
        new Toggler(trigger, container);
    });
});
// ********** END ************

// **************************************
// functions Added by kjeld Poulsen
// **************************************
function ShowArticle(pid, did) {
    var to = document.getElementById(pid);
    var from = document.getElementById(did);
    if (from && to) {
        var save = to.innerHTML;
        to.innerHTML = from.innerHTML;
        from.innerHTML = save;
    }
}

function MoveContentTo(fromid, toid) {
    var to = document.getElementById(toid);
    var from = document.getElementById(fromid);
    if (from && to) {
        to.innerHTML = from.innerHTML;
    }
}


function ShowHide(pid) {
    var elm = document.getElementById(pid);
    if (elm) {
        if (elm.style.display == 'none') {
            elm.style.display = 'block';
        } else {
        elm.style.display = 'none';
        }
    }
}

var currentFormplace = null;
var currentComment = '';

function PostComment(pid, blogid) {
    if (currentFormplace == null) {
        currentFormplace = document.getElementById('InitialFormPlace');
    }
    CancelComment();
    var elm = document.getElementById(pid);

    if (elm != currentFormplace) {
        elm.innerHTML = currentFormplace.innerHTML;
    }
    elm.style.display = 'block';

    if (elm != currentFormplace) {
        currentFormplace.innerHTML = '';
    }
    currentFormplace = elm;
    currentComment = pid;

    var hf = document.getElementById("commentblogid");
    hf.value = blogid;
}


function CancelComment() {
    if (currentComment != '') {
        var elm = document.getElementById(currentComment);
        elm.style.display = 'none';
    }
}

function SubmitComment() {
    var xname = document.getElementById('commentname');
    if (xname.value == '') {
        alert('Please fill in your name');
        xname.focus();
        return;
    }
    var mail = document.getElementById('commentemail');
    if (mail.value == '') {
        alert('Please fill in your email');
        mail.focus();
        return;
    }
    var text = document.getElementById('commenttext');
    if (text.value == '') {
        alert('Please fill in the comment');
        text.focus();
        return;
    }
    document.forms[0].submit();
}


function ToggleRegions() {
    var reg = document.getElementById('regions');
    if (reg) {
        if (reg.style.display == 'none') {
            reg.style.display = 'block';
        } else {
            reg.style.display = 'none';
        }
    }
}