/* --------------------------------------------------- */ /* -------------- DEFAULT AUTHORS SCRIPT-------------- */ /* --------------------------------------------------- */ var AUTH_POSTS_PER_PAGE = 20; var AWAY_MAX = 8; var total_pages_author = 0; var local_author_year_data = []; $(document).ready(function() { initAuthorArchives(); }); function initAuthorArchives(){ //BUILDS DIRECTORY PATH OF CURRENT LOCATION var dir = location.pathname.substring(0,location.pathname.lastIndexOf('/')+1); var archives_dir = dir + "archives/"; //REQUESTS ALL AVAILABLE JSON YEARLY ARCHIVES //AND BUILDS MENU BASED ON THAT FOR CURRENT DIRECTORY/AUTHOR /*$.getJSON("http://dyn.realclearpolitics.com/authors/genAuthorArchiveMenu.php?jsoncallback=?&ldir="+archives_dir, function(data) { $.each(data.items, function(i,item){ //LOOPS THROUGH EACH ITEM //CREATES MENU ITEM'S JSON FILE LOCATION var year_archive_loc = location.hostname+archives_dir+item.year+'.js'; if(i==0){ //PARSES JSON FILE FOR FIRST ITEM loadAuthorJSON_file(year_archive_loc); } //BUILDS OPTION var year_option = ''; $('select#form_year_select').append(year_option); //APPENDS OPTION }); });*/ // LOOP THROUGH CURRENT YEAR ALL THE WAY BACK TO FIRST POSSIBLE YEAR (2008) // LOOKING FOR YEARLY ARCHIVE FILES, THE BUILD MENU var this_year = new Date().getFullYear(); var first_run = true; for(var y = this_year; y >= 2008; y--) { var year_archive_loc = archives_dir+y+'.js'; if(doesFileExist(year_archive_loc)) { var year_option = ''; $('select#form_year_select').append(year_option); if(first_run) { loadAuthorJSON_file(year_archive_loc); first_run = false; } } } } function doesFileExist(file_loc) { var http = new XMLHttpRequest(); http.open('HEAD', file_loc, false); http.send(); return http.status!=404 && http.status!=403; } function loadAuthorJSON_file(file_loc){ $(".author-archive .posts ul").css("opacity","0.3"); $(".processing_div").css("display","block"); $.getJSON(file_loc, function(data) { data.sort(mycomparator); //SORTS JSON RESULTS BY DATE local_author_year_data = data; show_author_posts(data); }); } function show_author_posts(data, active_page) { if(typeof active_page === 'undefined') { active_page = 1; } $('.author-archive .posts ul').html(''); var article_items = []; var total_items = data.length; total_pages_author = Math.ceil(total_items / AUTH_POSTS_PER_PAGE); for(var i = 0; i < total_items; i++) { //BUILDS ITEM REQUESTED CONTAINER if(i >= ((active_page - 1) * AUTH_POSTS_PER_PAGE) + AUTH_POSTS_PER_PAGE ) { // } else if(i >= (active_page - 1) * AUTH_POSTS_PER_PAGE) { article_items.push(buildAuthor_items(data[i])); //BUILDS HTML FOR EACH ITEM } } if(total_items > AUTH_POSTS_PER_PAGE) { create_author_pagination(total_pages_author, active_page); } else { $('.author-archive .pagination_container').remove(); } //INSERTS INTO TO UL LISTING $('.author-archive .posts ul').html(article_items.join('')); //INSERTS INTO HTML $(".author-archive .posts ul").css("opacity","1"); $(".processing_div").css("display","none"); } function create_author_pagination(total_pages, active_page) { if(typeof active_page === 'undefined') { active_page = 1; } else { active_page = parseInt(active_page); } var html = ''; html += '
'; html += ''; html += '
'; $('.author-archive .pagination_container').remove(); $('.author-archive .posts').after(html); $('.author-archive .pagination_container .pagination ul li a').click(function(e) { e.preventDefault(); var divPosition = $('.author-top').offset(); $('html, body').animate({scrollTop: divPosition.top}, "slow"); var active_page = $(this).attr('data-page'); show_author_posts(local_author_year_data, active_page); }); } function buildAuthor_items(item){ var art_item = '
  • '; if(item.img_url!=""){ if(parseInt(item.type) == 700){ art_item += ''; art_item += '
    '; art_item += ''; art_item += ''; art_item += '
    '; art_item += '
    '; }else{ art_item += ''; art_item += '
    '; art_item += '
    '; } } art_item += '
    '+dateFromTimeStamp(item.art_date)+'
    '; art_item += '
    '+item.title+'
    '; if(typeof item.source !== 'undefined'){ art_item += '
    '+item.source+'
    '; } art_item += '
    '+item.excerpt+'
    '; art_item += '
  • '; return art_item; } //SORT JSON RESULTS // a and be are object elements of your array function mycomparator(a,b) { return parseInt(b.art_date) - parseInt(a.art_date); } function dateFromTimeStamp(t) { var monthnames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var date = new Date(t*1000); return monthnames[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear(); } //checks for testing query string and reloads the page if on www1 (function(Utils, window){ if(typeof Utils !== 'undefined'){ var testing = Utils.get_query_param('testing'); var preview = Utils.get_query_param('preview'); if( (testing == null || testing.length == 0) || (preview == null || preview.length == 0) ){ var url = window.location.host; var url_parts = url.split('.'); if(url_parts[0] == 'www1'){ var location = window.location.href; location = location.replace('www1', 'www'); window.location = location; } } } })(Utils, window);