<%

var title = request.getParameter('q');
var title = encodeURI(title); // encode search term for rottentomatoes search        
var apikey = "e9r83r3qcp3hdghg8qpu5kf3";
   var moviesJSON = get("http://api.rottentomatoes.com/api/public/v1.0/movies.json", { apikey : "e9r83r3qcp3hdghg8qpu5kf3", q : title, page_limit : "1" },"json");

    var i=0;
	
 // var client = new APPClient();

 // Creating an instance of AtomFeed
 var feed= new Feed();
 if (moviesJSON.data.movies[0] != null && moviesJSON.data.movies[0] != 'undefined') {
 var videotitle = encodeURI(moviesJSON.data.movies[0].title);  //encode the full movie title for youtube search
 // Retrieving and online feed
 var url = "http://gdata.youtube.com/feeds/api/videos?q="+videotitle+"video&start-index=1&max-results=6&v=2";
 feed.getFeed(url);
 // Getting an array of individual entries from the feed
 var entries = feed.entries;
%>
        <div class="results" id="results" >

    <div id="poster">
        <img src="<%=moviesJSON.data.movies[0].posters.detailed%>" alt="" />
    </div>

    <div class="content-about">
    <h1 id="title"><%=moviesJSON.data.movies[0].title%></h1><span class="year">(<%=moviesJSON.data.movies[0].year%>)</span>
        <div style="clear:both;"></div>
    <div id="rating">Rating : <%=moviesJSON.data.movies[0].ratings.critics_score%>% </div> <div id="icon"><!--<img src="images/thumbsup.gif" alt="">--></div>
        
        <div id="Starring">Starring : <%

            for ( var j=0; j < moviesJSON.data.movies[0].abridged_cast.length; j++) {
                print(moviesJSON.data.movies[0].abridged_cast[j].name+", ");
            }

            %></div>
<%
           if (moviesJSON.data.movies[0].critics_consensus != null) {
        %>
        <div id="consensus">
            Overall : <%=moviesJSON.data.movies[0].critics_consensus%> </div>
      <%   }
           if (moviesJSON.data.movies[0].synopsis != "") {
        %>
        <h3>Plot</h3>
        <div id="synopsis">
           <%=moviesJSON.data.movies[0].synopsis%>
        </div>
        <%
        }
        %>
        </div>

      <div class="trailers">
          <h3>Related Videos</h3>
      <ul>

          <%
 for(var i=0;i < entries.length; i++) {
 var entry = entries[i];
    entry.id = entry.id.slice(27);
%>
 <li>
    <iframe width="300" height="150" src="http://www.youtube.com/embed/<%=entry.id%>" frameborder="0" allowfullscreen></iframe>
</li>
 <%
      }

        %>
      </ul>
      </div>


      </div>

 <%
        } else {

        print("<h2>Not Found, Sorry. Try a proper full movie title</h2>");
                 }
        %>


