Advertisement
franzkev

kevin js

Aug 29th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    var config;
  2.     var baseUrl = 'http://api.themoviedb.org/3/';
  3.     var api_Key = '676e50341c60a74423a05f38e25b23c6';
  4.  
  5.  
  6.     function initialize(callback) {
  7.         $.get(baseUrl + 'configuration', {
  8.             api_key: '676e50341c60a74423a05f38e25b23c6'
  9.         },function(res) {
  10.             config = res;
  11.             console.log(config);
  12.             callback(config);
  13.         });
  14.     }
  15.  
  16.    function setEventHandlers(config) {
  17.             $('#form-search').submit(function() {
  18.                 var query = $('.input-query').val();
  19.                 searchMovie(query);
  20.                
  21.             });
  22.  
  23.             $('.show').click(function() {
  24.                 loadNowShowing();
  25.  
  26.             });
  27.  
  28.             $('.upcoming').click(function() {
  29.                 loadUpComing();
  30.                
  31.             });
  32.  
  33.             $('.popular').click(function() {
  34.                 loadPopular();
  35.                
  36.             });
  37.  
  38.  
  39.             $('.toprated').click(function() {
  40.                 loadTopRated();
  41.        
  42.             });
  43.  
  44.             loadNowShowing();
  45.     }
  46.     function searchMovie(query) {
  47.         var searchUrl = baseUrl + 'search/movie';
  48.         $('.movies-list').html('');
  49.         $.get(searchUrl, {
  50.             query: query,
  51.             api_key: apiKey
  52.         }, function(response) {
  53.             displayMovies(response);
  54.         });
  55.     }
  56.     function displayMovies(data) {
  57.         data.results.forEach(function(movie) {
  58.             var imageSrc = config.images.base_url + config.images.poster_sizes[3] + movie.poster_path;
  59.             var htmlStr = [
  60.                             '<div class="col-md-4 portfolio-item">',
  61.                                 '<a href="/view/'+movie.id+'">',
  62.                                     '<img class="img-responsive" style="height:500px;width:350px"src="' + imageSrc + '" alt="">',
  63.                                 '</a>',
  64.                                 '<h3><center><font face="Maiandra GD">',
  65.                                     '<a href="/view/'+movie.id+'" style="color:black">' + movie.title +'</a>',
  66.                                 '</font></center></h3>',
  67.                             '</div>'
  68.                             ];
  69.             $('.movies-list').append($(htmlStr.join('')));
  70.         });
  71.     }
  72.  
  73.     function loadNowShowing() {
  74.         var nowShowingUrl = baseUrl + 'movie/now_playing';
  75.         $('.movies-list').html('');
  76.         $.get(nowShowingUrl, {
  77.             api_key: apiKey
  78.         }, function(response) {
  79.             displayMovies(response);
  80.         });
  81.     }
  82.    
  83.  
  84.     function loadUpComing() {
  85.         var upcomingUrl = baseUrl + 'movie/upcoming';
  86.         $('.movies-list').html('');
  87.         $.get(upcomingUrl, {
  88.             api_key: apiKey
  89.         }, function(response) {
  90.             displayMovies(response);
  91.         });
  92.     }
  93.    
  94.  
  95.     function loadPopular() {
  96.         var popularUrl = baseUrl + 'movie/popular';
  97.         $('.movies-list').html('');
  98.         $.get(popularUrl, {
  99.             api_key: apiKey
  100.         }, function(response) {
  101.             displayMovies(response);
  102.         });
  103.     }
  104.  
  105.     function loadTopRated() {
  106.         var topratedUrl = baseUrl + 'movie/top-rated';
  107.         $('.movies-list').html('');
  108.         $.get(topratedUrl, {
  109.             api_key: apiKey
  110.         }, function(response) {
  111.             displayMovies(response);
  112.         });
  113.     }
  114.    
  115.    
  116.  
  117.     function viewMovie(id){
  118.     $(".movie-list").hide();
  119.     console.log(id);
  120.     url = baseUrl + "movie/"+id;
  121.     reqParam = {api_key:apiKey};
  122.     $.get(url,reqParam,function(response){
  123.         $("#title").html(response.original_title);
  124.         $("#overview").html(response.overview);
  125.  
  126.         url = baseUrl + "movie/"+id+"/videos";
  127.         $.get(url,reqParam,function(response){
  128.             var html = '<embed width="600" height="400" src="https://www.youtube.com/v/'+response.results[0].key+'" type="application/x-shockwave-flash">'
  129.             $("#trailer").html(html);
  130.         });
  131.  
  132.         url = baseUrl + "movie/"+id+"/credits";
  133.         $.get(url,reqParam,function(response){
  134.             var casts = "";
  135.             for(var i=0;i<response.cast.length;i++){
  136.                 casts+= (i!=response.cast.length-1)?response.cast[i].name+", "
  137.                     : " and "+response.cast[i].name;
  138.             }
  139.             $("#casts").html(casts);
  140.         });
  141.  
  142.         url = baseUrl + "movie/"+id+"/similar";
  143.         $.get(url,reqParam,function(response){
  144.             var movies = response.results;
  145.             var allMovies = "";
  146.             for(var i=0;i<movies.length;i++){
  147.                 allMovies += (i==movies.length-1)? '<a href="/movie/'+movies[i].id+'">'+movies[i].title+'</a>, '
  148.                     : '<a href="/movie/'+movies[i].id+'">'+movies[i].title+'</a>';
  149.             }
  150.             $("#similar").html(allMovies);
  151.         });
  152.  
  153.     });
  154. }
  155. $(document).ready(function(){
  156.  
  157.     $(".btn-top-rated, .btn-popular, .btn-upcoming, .btn-now-showing").click(function(){
  158.         $(".movie-view").hide();
  159.         $(".movies-list").show();
  160.     });
  161.     initialize(setEventHandlers);
  162. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement