$(document).ready(function() {
  var hash = location.hash;

  if (hash.indexOf("subscribe") != -1) {
    $('#subscribe').animate({'backgroundColor': '#ffff00'}, 500, function() {
      $('#subscribe').animate({'backgroundColor': '#fff'}, 500, function() {
        $('#subscribe').animate({'backgroundColor': '#ffff00'}, 500, function() {
          $('#subscribe').animate({'backgroundColor': '#fff'}, 500, function() {
            $('#subscribe').animate({'backgroundColor': '#ffff00'}, 500);
          });
        });
      });
    });
  }

  $('#id_honeypot').parents('tr').hide();

  $('input, textarea').each(function() {
    var hasPlaceholder = $(this).attr("data-placeholder") == undefined ? false : true;
    if (hasPlaceholder) {
      $(this).data("placeholder", $(this).attr("data-placeholder"));
      $(this).addClass("placeholder").val($(this).data("placeholder"));
      $(this).focus(function() {
        var currText = $(this).val();
        if (currText == $(this).data("placeholder")) {
          $(this).val('').removeClass("placeholder");
        }
      });
      $(this).blur(function() {
        var currText = $(this).val();
        if (currText == '') {
          $(this).val($(this).data("placeholder"));
          $(this).addClass("placeholder");
        }
      });
    }
  });
});

/*
var Issue = function(json) {
  this.id = json.id;
  this.title = json.title;
  this.summary = json.summary;  
}
*/

function renderIssues(data) {
  var html = tmpl("tmpl_issue_list", data);
//  console.log(html);
  $('.issueListContainer').html(html);
/*
  var hasNext = data.hasNext;
  var hasPrev = data.hasPrev;
  var issues = data.issues;
  var endNo = data.end_no;
  var pageSize = data.page_size;    
*/
}

$(document).ready(function() {
  $.getJSON("issue_list/", {}, function(data) {
    renderIssues(data);  
  });
  
  $('#searchForm').submit(function(e) {
    e.preventDefault();
    var searchInput = $('#issueSearch');
    var search_terms = searchInput.val();
    if (search_terms == searchInput.data("placeholder")) {
      return false;
    }
    doIssuesLoading();
    $.getJSON("issue_list/", {'search': search_terms}, function(data) {
      renderIssues(data);
    });
  });

});

function loadIssues(parentElem, page_no) {
  var search_terms = parentElem.data("terms");
  var params = {};
  params['page'] = page_no;
  //sets get var to search if search_terms is not empty string, otherwise sets to undefined.
  params['search'] = search_terms == '' ? undefined : search_terms;
//  console.log(start);
  doIssuesLoading();
  $.getJSON("issue_list/", params, function(data) {
    renderIssues(data);
  }); 
}

$('.nextBtn').live("click", function() {
//  console.log("clicked next");
  var parentElem = $(this).closest(".navBtns");
  var curr_page = parseInt(parentElem.data("page"));
  var next_page = curr_page + 1;
  loadIssues(parentElem, next_page);
});

$('.prevBtn').live("click", function() {
  var parentElem = $(this).closest(".navBtns");
  var curr_page = parseInt(parentElem.data("page"));
  var prev_page = curr_page - 1;
  loadIssues(parentElem, prev_page);
});

function doIssuesLoading() {
  $.noop();
}

$(document).ready(function() {
  $('#subscribeForm').submit(function(e) {
    e.preventDefault();
    $('.validation').slideUp("fast");
    var $email = $('#email');
    var email = $email.val();
    if (!isRFC822ValidEmail(email)) {
      $('<div />').addClass('validation').html("Invalid Email Address").hide().insertAfter($email).slideDown("slow");
//      $email.append(html);
      return false;
    }
    $('#subscribeBtn').attr("disabled", "disabled").text("Subscribing...");
    $.post("/erang/subscribe/", {
      'email': email
    }, function(response) {
      $('#subscribe').slideUp("slow", function() {
        $(this).addClass("response").html("<b>" + email + "</b>" + " has been subscribed to eRang. Thanks!");
        $(this).slideDown("slow");
      });
    });
  });
  $('#commentForm').submit(function(e) {
    e.preventDefault();
    $('#commentSubmit').attr("disabled", "disabled").text("Submitting...");
    $.post("/erang/postfeedback/", {
      'name': $('#commentName').val(),
      'email': $('#commentEmail').val(),
      'comment': $('#commentComment').val(),
      'issue': document.title
    }, function(response) {
      $('#commentWrapper').slideUp("slow", function() {
        $(this).addClass("response").html("Your feedback has been emailed to us at erang" + "@" + "theatreforum.in. Thanks!");
        $(this).slideDown("slow", function() { 
          $(document).scrollTop($(document).height());
        });
      });
    });    
  });
});  



