Source: MoviePage.js

require ('./iView.js');
/**
* @param {Telll} t the telll object
* @author Monsenhor filipo at kobkob.org
* @constructor
*/
function MoviePage(t, data, embed){
	if (embed) this.embed = true;
    this.t = t;
    this.imdbid = data.imdbid;
    this._init(t);
}

MoviePage.prototype = Object.create(iView.prototype);

/**
* @param t {} 
* @return bool
*/
MoviePage.prototype._init = function(t){
    this.state = "init";
    var me =this;
    this.loadImdbData(this.imdbid, function(){
       me._showWidget(t.store);
    });
    return null;
}

/**
* @param t {} 
* @return bool
*/
MoviePage.prototype.loadImdbData = function(id, cb){
        // TODO change to themoviedb.org
	var url = "http://www.omdbapi.com/?i="+id+"&plot=fulll&r=json";
        var me = this;

        var defaultData = 
	{
	Actors:      "",
	Awards:      "",
	Country:     "",
	Director:    "",
	Genre:       "",
	Language:    "",
	Metascore:   "",
	Plot:        this.t.movie.description,
	Poster:      this.t.movie.image,
	Rated:       "",
	Released:    "",
	Response:    "",
	Runtime:     "",
	Title:       this.t.movie.title,
	Type:        "",
	Writer:      "",
	Year:        "",
	imdbID:       id,
	imdbRating:  "",
	imdbVotes:   ""
	};
	if (id != "mock") {
	$.getJSON( url, function( data ) {
             if (data.Response == "False") {
                   telllDialog(data.Error + ": " + id, 3000);
             } else {
                 // do some clean check in the data response
                 for (var prop in defaultData){
                      if (!data[prop] || data[prop] == "N/A") data[prop] = defaultData[prop];
                 }
		 // TODO fix it, IMDb deny inject their images here ...
		 data.Poster = defaultData.Poster;
                 me.t.store.imdb = data;
                 if (cb) cb(data);
             }
	});
	} else {
             me.t.store.imdb = defaultData;
             if (cb) cb(defaultData);
	}
        return null;
}


/**
* @param data {} 
* @return bool
*/
MoviePage.prototype._showWidget = function(data){
    // Cleaning title and description plugins
    var tmpl = require('./templates/movie_page.mtjs');
    var html = Mustache.render(tmpl.html, data);
    if (tmpl.css)
    $('<style id="movie-page-css">'+tmpl.css+'</style>').appendTo('head');
    $(html).appendTo('body');
    var telll = this.t;
    var me = this;
    this.state = "open";
    $(".movie-page-widget .close").on( "click", function( ) {
	me.detach();
    });
    $("span.telll-bum_1, img.telll-img-poster").on( "click", function() {
	me.emit("playnow", me.t.movie);
	me.detach();
	me.t.moviesListView.detach();
    });

    // The popup
    if (! me.embed){
        me.popup = telllPopup($("#movie-page-widget"), data.imdb.Title);
    }
    return true;
};

/**
* @return null
*/
MoviePage.prototype.detach = function(){
    //$('.movie-page-widget').detach();
    $('#'+this.popup).detach();
    //$('#'+this.popup+"-overlay").detach();
    this.state = "detached";
};

/**
* @return null
*/
MoviePage.prototype.attach = function(){
    this._showWidget(this.t.store);
};


module.exports = {MoviePage:MoviePage};