$(function(){
    var morePag = 0;
    // Hide get more bits
    $('#getMoreIdeas').hide();
    $('#getMoreIdeasNewest').hide();
    
    var firstRecordNewest = 5;

    // Attach ajax load to load more button for top ideas
    $(window).scroll(function(){
 //     alert($(this).scrollTop());
     if(morePag != 1 && $(this).scrollTop() == ($(document).height() - $(this).height())) {

          // Put up a loading icon
          $('#getMoreIdeas').css('background', 'url(../images/ajax-loader.gif) no-repeat 50% 50%');

          $.post(
              "/ideas/index",
              {'firstRecord' : firstRecordNewest},
              function(r) {
                  
                  var data = r.json;
                  morePag = r.num;
                  for (var i=0; i < data.length; i++) {
                                        
                      $('#getMoreIdeas')
                          .before([
                              '<div class="idea green" id="post_'+data[i].id+'">',
                              '<a href="/ideas/view/'+data[i].slug+'">',
                              '<img src="http://www.umbroindustries.com/uploads/ideas/120x120/'+data[i].image+'" alt="'+data[i].name+'" width="120"  height="120" />',
                              '</a>',
                              '<div class="text">',
                              '<h2><a href="http://www.umbroindustries.com/ideas/view/'+data[i].slug+'">'+data[i].name+'</a></h2>',
                              '<small>added by <a href="http://www.umbroindustries.com/users/profile/'+data[i].user_username+'">'+data[i].user_username+'</a> '+data[i].time_since+'</small>',
                              '<p>'+data[i].short_description+'</p>',
                              '<div class="meta">',
                              '<span class="rate">rate this idea:</span>',
                              data[i].star_rater,
                              '<span class="count">'+data[i].votes+' votes, '+data[i].comment_count+'</span>',
                              '</div>',
                              '<div class="tags">'+data[i].tags+'</div>',
                              '</div>',
                              '</div>'
                              ].join(''));
                  };
                
                  // Remove loading icon
                  $('#getMoreIdeas').css('background', 'none');
                
                  // Now bind all the voting stuff
                  var base_url = self.location.protocol+"//"+self.location.host+"/";
                  var star = $(".stars-wrapper");
                  star.each(function(i){
                      var id = this.id.split("_");
                      id = id[1];
                      var $caption = $("<span/>");
                      $caption.css({ 'font-size' : '10px', 'color' : '#949494', 'margin-left' : '10px', 'line-height' : '20px'});
                      $(this).stars({
                          inputType: "select",
                          oneVoteOnly: true,
                          captionEl: $caption, // point to our newly created element
                          callback: function(ui, type, value){
                              $.ajax({
                                  type:"POST",
                                  url: base_url + 'ideas/vote',
                                  data:{idea:id, vote:value},
                                  success: function(data){
                                      $caption.html(data).stop();
                                  }
                              });
                              $(this).stars("select", value);
                          }
                      });
                  });
              },
              'json'
          );
          
          $.post(
              "/ideas/getnewest",
              {'firstRecord' : firstRecordNewest},
              function(r) {
                var data = r.json;
                morePag = r.num;
                  for (var i=0; i < data.length; i++) {

                      $('#getMoreIdeasNewest')
                          .before([
                              '<div class="idea green" id="post_'+data[i].id+'">',
                              '<a href="/ideas/view/'+data[i].slug+'">',
                              '<img src="http://www.umbroindustries.com/uploads/ideas/120x120/'+data[i].image+'" alt="'+data[i].name+'" width="120"  height="120" />',
                              '</a>',
                              '<div class="text">',
                              '<h2><a href="http://www.umbroindustries.com/ideas/view/'+data[i].slug+'">'+data[i].name+'</a></h2>',
                              '<small>added by <a href="http://www.umbroindustries.com/users/profile/'+data[i].user_username+'">'+data[i].user_username+'</a> '+data[i].time_since+'</small>',
                              '<p>'+data[i].short_description+'</p>',
                              '<div class="meta">',
                              '<span class="rate">rate this idea:</span>',
                              data[i].star_rater,
                              '<span class="count">('+data[i].votes+' votes, '+data[i].comment_count+')</span>',
                              '</div>',
                              '<div class="tags">'+data[i].tags+'</div>',
                              '</div>',
                              '</div>'
                              ].join(''));
                  };

                  // Remove loading icon
                  $('#getMoreIdeasNewest').css('background', 'none');

                  // Now bind all the voting stuff
                  var base_url = self.location.protocol+"//"+self.location.host+"/";
                  var star = $(".stars-wrapper");
                  star.each(function(i){
                      var id = this.id.split("_");
                      id = id[1];
                      var $caption = $("<span/>");
                      $caption.css({ 'font-size' : '10px', 'color' : '#949494', 'margin-left' : '10px', 'line-height' : '20px'});
                      $(this).stars({
                          inputType: "select",
                          oneVoteOnly: true,
                          captionEl: $caption, // point to our newly created element
                          callback: function(ui, type, value){
                              $.ajax({
                                  type:"POST",
                                  url: base_url + 'ideas/vote',
                                  data:{idea:id, vote:value},
                                  success: function(data){
                                      $caption.html(data).stop();
                                  }
                              });
                              $(this).stars("select", value);
                          }
                      });
                  });
              },
              'json'
          );
          firstRecordNewest += 5;
        }
        return false;
    });

});
