/* JS for RSS feed widget */
function ReadyFeedWidget(feedURL,itemCount){
$.jGFeed(feedURL,
function(feeds){
  // Check for errors
  if(!feeds){
    // there was an error
    return false;
  }
  // do whatever you want with feeds here
  for(var i=0; i<feeds.entries.length; i++){
    var entry = feeds.entries[i];

    $("#newsatjama").append("<h4 class=\"title pbreak\"><a href=\"" + entry.link + "\">" + entry.title + "</a></h4>");
  }
}, itemCount);
}

/* JS for case quiz feature */
function ReadyCaseQuiz(){
        $('#casequiz > .quizwrapper > .answers > ul > li > h3').siblings().hide();
	fixColHeights(-1);
	$('#casequiz > .quizwrapper > .answers > ul > li > h3').bind("click", function(){
		if ($(this).hasClass('feed_selected')){
			$(this).removeClass('feed_selected');
			$(this).siblings().hide();
		} else {
			$('#casequiz > .quizwrapper > .answers > ul > li > h3').removeClass('feed_selected');
			$('#casequiz > .quizwrapper > .answers > ul > li > h3').siblings().hide();
			$(this).addClass('feed_selected');
			$('.feed_block').hide();
			$(this).siblings().show();
		}
		fixColHeights(-1);
		return(false);
	});

	$('#references > h3').siblings().hide();
	$('#references > h3').html("[+] References");
	$('#references > h3').bind("click", function(){
		$(this).siblings().toggle();
		if ($(this).text() == "[+] References"){$(this).html("[-] References");} else {$(this).html("[+] References");}
		fixColHeights(-1);
	});
}

function ReadyRSS(){
/* $(document).ready(function(){ */
	$('.feed_block').hide();
	$('.sub_feeds').hide();
	$('#feeds > li > h3').bind("click", function(){
		$('#feeds > li > h3').removeClass('feed_selected');
		$(this).addClass('feed_selected');
		$('.feed_block').hide();
		$(this).siblings('.feed_block').show();
		return(false);
	});

$.ajax({
  url: "sections.txt",
  cache: false,
  dataType: 'text',
  success: function(html){
    var newSelect = '<option value="">Choose a feed from the <em>JAMA</em> Sections</option>';
/*    $('#sections_temp').append(html);
    $('#sections_temp a:not(:has(img))').each(function(){
	newSelect += '<option value="http://jama.ama-assn.org' + $(this).attr('href') + '">' + $(this).html() + '</option>\n';
    }); */
    newSelect += html;
    $('#section_select').html(newSelect);
  }
});

$.ajax({
  url: "/rss/include/collections.dtl",
  cache: false,
  dataType: 'text',
  success: function(html){
    var newSelect = '<option value="">Choose a feed from the <em>JAMA</em> Collections</option>';
    $('#collections_temp').append(html);
    $('#collections_temp a:not(:has(img))').each(function(){
	newSelect += '<option value="http://jama.ama-assn.org' + $(this).attr('href') + '">' + $(this).html() + '</option>\n';
    });
    $('#collection_select').html(newSelect);
  }
});
	$('#feeds > li > h3:first').click();

	$('.onchangeselect').bind("change", function(){

		var bloglines = 'http://www.bloglines.com/sub/';
		var google = 'http://fusion.google.com/add?source=atgs&feedurl=';
		var yahoo = 'http://add.my.yahoo.com/content?.intl=us&url=';
		var newsgator = 'http://www.newsgator.com/ngs/subscriber/subext.aspx?url=';
		var msn = 'http://my.msn.com/addtomymsn.armx?id=rss&ut=';
		if ($(this).val() != ""){
			$(this).siblings('.sub_feeds').find('input').val($(this).val());

			$(this).siblings('.sub_feeds').find('.add_google').attr('href',google+$(this).siblings('.sub_feeds').find('input').val());
			$(this).siblings('.sub_feeds').find('.add_msn').attr('href',msn+$(this).siblings('.sub_feeds').find('input').val());
			$(this).siblings('.sub_feeds').find('.add_newsgator').attr('href',newsgator+$(this).siblings('.sub_feeds').find('input').val());
			$(this).siblings('.sub_feeds').find('.add_bloglines').attr('href',bloglines+$(this).siblings('.sub_feeds').find('input').val());
			$(this).siblings('.sub_feeds').find('.add_yahoo').attr('href',yahoo+$(this).siblings('.sub_feeds').find('input').val());

			$(this).siblings('.sub_feeds').show();
		}
	});

// bloglines: http://www.bloglines.com/sub/RSS_URL_HERE
// google: http://fusion.google.com/add?source=atgs&feedurl=RSS_URL_HERE
// yahoo: http://add.my.yahoo.com/content?.intl=us&url=RSS_URL_HERE
// newsgator: http://www.newsgator.com/ngs/subscriber/subext.aspx?url=RSS_URL_HERE
// msn: http://my.msn.com/addtomymsn.armx?id=rss&ut=RSS_URL_HERE

}

$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return 0; }
return results[1] || 0;}


$(document).ready(function(){
if($.urlParam('iPhoneApp') == "true") {
$('#header').hide();
$('#footer').hide();
$('#col-2').hide();
$('#col-3').hide();
$('#bottom-features-controls').hide();
$('#bottom-features-container').hide();
$('#content-toggle').hide();
$('.social-bookmarking').hide();
$('#cited-by').hide();
$('#content-block').css('width','100%');
$('#content-block').css('font-size','1.4em');
$('a[href!=#]').removeAttr('href');
}
});
