/**
 * @author jcrane
 */

google.load("feeds", "1");
      
// Our callback function, for when a feed is loaded.
function feedLoaded(result) {
  if (!result.error) {
    // console.log(result);
    // Grab the container we will put the results into
    var container = $('#posts');
	// var container = document.getElementById("posts");
    container.innerHTML = '';

    // Loop through the feeds, putting the titles onto the page.
    // Check out the result object for a list of properties returned in each entry.
    // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON
    for (var i = 0; i < result.feed.entries.length; i++) {
      var entry = result.feed.entries[i];
	  var link = entry.link;
	  var link = '<a href="' + link + '">';
	  var end_link = '</a>';
      var new_entry = link + entry.title + end_link;
	  var div = document.createElement("div");
      // div.appendChild(document.createTextNode(new_entry)); // entry.title
      $(div).html(new_entry);
	  container.append(div);
	  
	  // container.appendChild(div);
	  $(div).addClass('headline');
    }
  }
}

function babyFeed() {
  // Create a feed instance that will grab the feed.
  var feed = new google.feeds.Feed("http://www.kjcrane.com/topics/baby/feed/");
  feed.setNumEntries(5);
  // Calling load sends the request off.  It requires a callback function.
  feed.load(feedLoaded);
}
	    google.setOnLoadCallback(function() { 
		// newSlideShow(); 
		babyFeed()}
		);	

$(document).ready(function(){
jsonURL = 'http://picasaweb.google.com/data/feed/base/user/kjcrane.com/albumid/5387453358137016161?alt=json&kind=photo&authkey=Gv1sRgCN6D9PmW6NryQg&hl=en_US';
$.getJSON(jsonURL + "&callback=?",
  function(data) {
  	
    $.each(data.feed.entry, function(i,item) {
		// console.log(item);
		photo_url = item.media$group.media$thumbnail[0].url;
		photo_url = photo_url.replace('/s72/','/s800/');
      $("<img/>")
//.attr("src",item.media$group.media$content[0].url)
  .attr("src",photo_url)
  // .addClass('maya_photo')
  .appendTo("#photos")
  .wrap('<div class="maya_photo"></div>');
    });
	makeSlideShow();
});
	
		$('#email_subscribe').dialog({autoOpen: false, title: 'Subscribe by e-mail',
		buttons: {
			'Subscribe' : function() {
				email = $('#subscribed_email').val();
				window.open('http://feedburner.google.com/fb/a/mailverify?uri=kjcrane&email='+ email, 'popupwindow', 'scrollbars=yes,width=550,height=520');
				return true;
				dialogClose();
			}
		}
		// ,
		/* close: function() {
			$(this).dialog('close');
		} */
	});
		
	$('#newsletter_link').click(function() {
		$('#email_subscribe').dialog('open');
		return false;	
	});
 });
 function dialogClose() {
 	$('#email_subscribe').dialog('close');
 }
 function makeSlideShow(){
 	$('#photos').cycle({
 		fx: 'fade',
 		speed: 1000,
 		timeout: 5000,
		pause: 0
 	});
	$('#first_photo').remove();
 }
