// if logged in, link-vote class and ajax post
$('.link-to-vote').live('click', function(){
  var $this = $(this);
  var box = $(this).parent();
  var vote_count = box.children('h4').children('.vote-count');
  var vote_desc  = box.children('h4').children('sup');
  var article_id = $this.attr('id').split('-')[1];
  $.ajax({
    url: ('/api/vote/' + article_id),
    type: 'POST',
    dataType: 'json',
    data: {
      'vote_type': "up",
    },
    success: function(data){
      if (data.success) {
        vote_count.html( parseInt(vote_count.html())+1  );
      }
      if (parseInt(vote_count.html()) == 1) { 
        vote_desc.html("Vote");
      } else {
        vote_desc.html("Votes");
      }
      box.addClass("voted");
      $this.html("Voted");
    }
  });
  return false;
});

// if non logged in, link-login class and redirect to login page
$('.link-to-login').live('click', function(){
	var return_to = encodeURIComponent(window.location.pathname);
	window.location = '/login?return_to='+return_to;
});

$('ul.tabs li').live('click', function () {
  var tab_type = $(this).children().html().toLowerCase();
  $('ul.tabs li').removeAttr("id");
  $(this).attr("id", "tab-selected");
  $('#articles-popular-container ol').hide();
  $('#articles-popular-' + tab_type).show();
  return false;
});

// 
// <script>
// 		function redirect(url){
// 			parent.location = 'http://localhost:3000/'+url;
// 		};
// 		
// 		function ajaxCall(id) {
// 			var xmlHttpReq = false;
// 	    var self = this;
// 			var urlString = 'http://localhost:3000/api/vote/'+id;
// 	    // Mozilla/Safari
// 	    if (window.XMLHttpRequest) {
// 	        self.xmlHttpReq = new XMLHttpRequest();
// 	    }
// 	    // IE
// 	    else if (window.ActiveXObject) {
// 	        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
// 	    }
// 	    self.xmlHttpReq.open('POST', urlString, true);
// 	    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/json');
// 			self.xmlHttpReq.setRequestHeader("Connection", "close");
// 			self.xmlHttpReq.send('');
// 	    self.xmlHttpReq.onreadystatechange = function() {
// 	        if (self.xmlHttpReq.readyState == 4) {
// 							var JSONData = eval("(" + self.xmlHttpReq.responseText + ")"); // convert returned result (JSONText) to JSON
// 							if (JSONData['success'] == true) {
// 								document.getElementById('count').innerHTML = parseInt(document.getElementById('count').innerHTML) + 1;
// 							}
// 	        }
// 	    }
// 		};
// </script>