/**
 * Klasa obsługuje klip video.
 * @param _parent
 * @param _id
 * @param _title
 * @param _type
 * @param _movie
 * @param _width
 * @param _height
 * @returns {GalleryMovie}
 */
function Movie(_parent, _id, _title, _type, _movie, _width, _height) {
    this.parent = _parent;
    this.id = _id;
    this.title = _title;
    this.type = _type;
    this.movie = _movie;
    this.width = _width;
    this.height = _height;
    this.container = null;
}

Movie.prototype.setContainer = function(_container) {
    this.container = _container;
}

Movie.prototype.clearContainer = function() {
    while (this.container.childNodes.length > 0) {
        this.container.removeChild(this.container.childNodes[0]);
    }
}

Movie.prototype.getParent = function() {
    return this.parent;
}

Movie.prototype.playerSize = function() {
    var player = {};
    if (this.height == -1) {
        player.width = this.width;
        player.height = (this.width / 16) * 9;
    } else if (this.width == -1) {
        player.width = (this.height / 9) * 16;
        player.height = this.height
    }
    return player;
}

Movie.prototype.getYoutubePlayer = function() {
    var size = this.playerSize();
    var movieURL = "http://www.youtube.com/v/" + this.movie + "&hl=pl&fs=1&rel=0&color1=0xEEEEEE&color2=0xCCCCCC";
    var player = document.createElement("object");
    player.width = size.width;
    player.height = size.height;
    player.setAttribute("classid", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000");
    player.setAttribute("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0");
    var paramSize = document.createElement("param");
    paramSize.name = "allowFullScreen";
    paramSize.value = "true";
    var paramMovie = document.createElement("param");
    paramMovie.name = "movie";
    paramMovie.value = movieURL;
    player.appendChild(paramSize);
    player.appendChild(paramMovie);
    try {
        var embed = document.createElement("embed");
        embed.width = size.width;
        embed.height = size.height;
        embed.type = "application/x-shockwave-flash";
        embed.src = movieURL;
        embed.setAttribute("allowfullscreen", "true");
        player.appendChild(embed);
    } catch (e) {}
    return player;
};

Movie.prototype.getVimeoPlayer = function() {
    var size = this.playerSize();
    var movieURL = "http://player.vimeo.com/video/" + this.movie + "?title=0&amp;byline=0&amp;portrait=0&amp;color=0xEEEEEE";
    var player = document.createElement("iframe");
    player.width = size.width;
    player.height = size.height;
    player.src = movieURL;
    player.setAttribute("frameborder", "0");
    return player;
};

/**
 * Odrysowuje zdjęcie galerii.
 */
Movie.prototype.draw = function(parentContainer) {
    var player = null;
    if (this.type == 0) {
        player = this.getYoutubePlayer();
    } else {
        player = this.getVimeoPlayer();
    }
    player.className = "galleryImage";
    if (parentContainer != null) {
        if (parentContainer.childNodes.length > 0) {
            parentContainer.insertBefore(player, parentContainer.childNodes[0]);
        } else {
            parentContainer.appendChild(player);
        }
    }
};

Movie.prototype.drawLink = function(parentContainer) {
    var div = document.createElement("div");
    div.id = "link_movie_" + this.id;
    div.className = "menuItem";
    div.onclick = this.play;
    div.innerHTML = this.title;
    if (parentContainer) {
        parentContainer.appendChild(div);
    }
}

Movie.prototype.play = function() {
    var regex = /link_movie_(-?\d+)/;
    if (!this.id || !this.id.match(regex)) return;
    var match = regex.exec(this.id);
    var movie = null;
    if (match[1] == -1) {
        movie = this;
    } else {
        movie = Movies.getMovie(match[1]);
    }
    if (!movie) return;
    movie.clearContainer();
    movie.draw(movie.container);
}

Movie.prototype.getId = function() {
    return this.id;
}
