var storybuilder_regex=/^https:\/\/secure\.nrdconline\.org\/site\/Stories.*/i;

if( storybuilder_regex.test(document.location.href) ) {
  jQuery(document).ready(function() {
    //All Cancel Buttons
    jQuery(".appArea input[value='Cancel']").click(function () { 
        window.location='http://www.nrdconline.org/site/PageNavigator/biogemsdefenders';
      });
  
    //Sign in page
    if (document.location.href.indexOf("theme=1181") >= 0) {
      var sign_in_text = '<p></p>If you&rsquo;re already a\
         BioGems Defender or you&rsquo;ve taken action on another\
         NRDC website, <strong><a href="https://secure.nrdconline.org/site/UserLogin?NEXTURL=https%3A%2F%2Fsecure.nrdconline.org%2Fsite%2FStories?theme=1181.">Sign In here</a></strong>.</p>\
         <p>If not, become a BioGems Defender now and then tell your story.</p>';
      jQuery(".appArea p").each(function (i) {
        if(this.innerHTML.indexOf("Thank you for your interest in submitting a story") >= 0) {
          this.innerHTML = sign_in_text;
        }
      });
      jQuery(".appArea h3.ObjTitle").html("Sign In and Tell Why You Defend BioGems");
      jQuery(".appArea .dividerHeading").css("display", "none");
      jQuery(".appArea #accept_email").css("display", "none");
      jQuery(".appArea label[for='accept_email']").css("display", "none");
      jQuery(".appArea #accept_email").parent().append('When you become a BioGems Defender we will keep you informed by sending you alerts and progress reports. You can unsubscribe at any time.');
    }
    
    //Introduction page
    if (document.location.href.indexOf("page=Introduction") >= 0 || document.location.href.indexOf("page=introduction") >= 0) {
      jQuery(".appArea h3.ObjTitle").html("Follow These Three Steps");
    }
    
    //Step 1 page
    if (document.location.href.indexOf("page=Article") >= 0 || document.location.href.indexOf("page=article") >= 0) {
      jQuery(".appArea h3.ObjTitle").html("Why Do You Defend BioGems?");
      jQuery(".appArea>br").remove();
      jQuery(".appArea td.Divider").parent().parent().parent().css("display", "none");
      jQuery("#articleEditor p").each(function (i) {
        if(this.innerHTML.indexOf("Please provide the details for your story such as a title") >= 0) {
          this.innerHTML = 'You can edit this text.';
        } else {
          this.style.marginBottom="0";
        }
      });
      jQuery("#articleEditor label[for='title']").html("<strong>1. Give Your Post a Title:</strong>");
      jQuery("#articleEditor label[for='subheading']").html("<strong>2. Enter Your State or Region:</strong>");
      jQuery("#articleEditor label[for='article']").html("<strong>3. Tell Why You Care:</strong>");
    }
    
    //Step 2 page
    if (document.location.href.indexOf("page=Photo") >= 0 || document.location.href.indexOf("page=photo") >= 0 ) {
      jQuery(".appArea td.Divider").parent().parent().parent().css("display", "none");
      jQuery(".appArea>br").remove();
      jQuery(".appArea .photocaption").css("display", "none");
      jQuery(".appArea input[value='Next Step']").css("font-size", "18px").css("font-weight", "bold");
    }
      
    //Step 3 page
    if (document.location.href.indexOf("page=Confirmation") >= 0 || document.location.href.indexOf("page=confirmation") >= 0) {
      jQuery(".appArea h3.ObjTitle").html("Preview");
      jQuery(".appArea td.Divider").parent().parent().parent().css("display", "none");
      jQuery(".appArea>br").remove();
      jQuery(".appArea p").each(function (i) {
        if(this.innerHTML.indexOf("Here is a preview of how your") >= 0) {
          this.style.display = "none";
        } else if(this.innerHTML.indexOf("Please review your story and click on the") >= 0) {
          this.innerHTML = "Click Submit if you're satisfied, or Previous Step to make changes. If you don't submit a photo, a BioGems avatar will be added to your post.";
        }
      });
      
      jQuery(".appArea .photocaption").css("display", "none");
      jQuery(".previewBox").css("min-height","250px");
      jQuery(".previewBox img").attr("src", jQuery(".previewBox img").attr("src").replace("medium","thumb"));
      jQuery(".previewBox img").css("float", "left");
      jQuery(".previewBox img").css("margin", "0 10px 5px 0");
      jQuery(".previewBox .subheading").css("margin-top", "-15px");
      if (jQuery(".appArea .byline").html())
        jQuery(".appArea .byline").html(jQuery(".appArea .byline").html().replace("By:", "Submitted By:"));
    }
    
    //Verification page
    if (document.location.href.indexOf("page=Verification") >= 0 || document.location.href.indexOf("page=verification") >= 0 ) {
      jQuery(".appArea h3.ObjTitle").html("Thanks for Telling Your Story");
      var verify_text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit,\
       sed do eiusmod tempor incididunt ut labore et dolore magna\
       aliqua. Ut enim ad minim veniam, quis nostrud exercitation\
       ullamco laboris nisi ut aliquip ex ea commodo consequat.\
       Duis aute irure dolor in reprehenderit in voluptate velit\
       esse cillum dolore eu fugiat nulla pariatur. Excepteur sint\
       occaecat cupidatat non proident, sunt in culpa qui officia\
       deserunt mollit anim id est laborum";
      jQuery(".appArea .dividerHeading").html(verify_text);
      jQuery(".appArea .dividerHeading").removeClass("dividerHeading");
      jQuery(".appArea input[value='Done']").click(function () { 
        window.location='http://www.nrdconline.org/site/PageNavigator/biogemsdefenders';
      });
    }
  });
}

//Listing Page
var storybuilderlist_regex=/^http:\/\/www\.nrdconline\.org\/site\/PageNavigator\/biogemsdefenders.*/i;
if( storybuilderlist_regex.test(document.location.href) ) {
  jQuery(document).ready(function() {
    jQuery("#content-area a").each(function (i) {
      if((this.href.indexOf("page=NewsArticle") >= 0 || this.href.indexOf("page=newsarticle") >= 0) && this.href.indexOf("search") < 0) {
        jQuery(this).addClass("lightwindow");
        jQuery(this).attr("params", "lightwindow_type=external,lightwindow_width=600,lightwindow_height=400,lightwindow_top=200");
        //jQuery(".action-page h2").css("padding", "0");
      }
    });
    jQuery("#content-area .byline").each(function (i) {
      html = jQuery(this).html();
      html = html.replace(/\s+[^\s]+\s*$/img, "");
      jQuery(this).html(html);
    });
    jQuery("#content-area table table p").each(function (i) {
      html = jQuery(this).html();
      html = html.replace(/<br>(\s*[^<>]+\s*)$/img, "<br>My Location: $1");
      jQuery(this).html(html);
    });
  });
}


var storybuilderdisplay_regex=/^http:\/\/www\.nrdconline\.org\/site\/News2.*/i;
if( storybuilderdisplay_regex.test(document.location.href) ) {
  jQuery(document).ready(function() {
    //Display Page
    if (document.location.href.indexOf("search") < 0 ) {
      jQuery("#NewsSearch").parent().css("display", "none");
      jQuery(".appArea .photocaption").css("display", "none");
      if (jQuery(".appArea .byline").html()) {
        jQuery(".appArea .byline").html(jQuery(".appArea .byline").html().replace("By:", "Submitted By:"));
        jQuery(".appArea .byline").html(jQuery(".appArea .byline").html().replace(/\n/im, ""));
        jQuery(".appArea .byline").html(jQuery(".appArea .byline").html().replace(/\s+[^\s]+[\s]*$/img, ""));
      }
      if (jQuery(".appArea img").attr("src"))
        jQuery(".appArea img").attr("src", jQuery(".appArea img").attr("src").replace("large","thumb"));
      jQuery(".appArea img").css("float", "left");
      jQuery(".appArea img").css("margin", "0 10px 5px 0");
      jQuery(".appArea .subheading").css("margin-top", "-15px").html('My Location: ' + jQuery(".appArea .subheading").html());
    } else {
    //Search page
      jQuery(".appArea h3.ObjTitle").html("BioGems Defender Story Search");
      jQuery(".appArea .dividerHeading").css("display", "none");
      jQuery(".appArea #intlistctrl").parent().parent().css("display", "none");
      jQuery(".appArea .lc_Heading").each(function (i) {
        if(this.innerHTML.indexOf("Headline") >= 0) {
          this.innerHTML = "<span>Title</span>";
        }
      });
      jQuery(".appArea .Hint").each(function (i) {
        if(this.innerHTML.indexOf("Headline") >= 0) {
          this.innerHTML = "Searches Title, Author and Keywords";
        }
      });
      jQuery(".appArea input#search").attr("value","Find Defender").attr("title","Find Defender");
      jQuery(".appArea #pubdate_date").css("margin", "0 0.5em").css("padding", "0 0.5em");
      jQuery(".appArea input[value='Done']").parent().parent().attr("action", "http://www.nrdconline.org/site/PageNavigator/biogemsdefenders");
      jQuery(".appArea input[value='Done']").attr("value", "Back to Defenders List");
      jQuery(".appArea .lc_Table td a").each(function (i) {
        elmnt = jQuery(this);
        elmnt.addClass("lightwindow");
        elmnt.attr("title", elmnt.html());
        elmnt.attr("params", "lightwindow_type=external,lightwindow_width=600,lightwindow_height=400,lightwindow_top=200");
        //jQuery(".action-page h2").css("padding", "0");
      });
      jQuery(".appArea #articlelib").append('<input type="hidden" value="1681" id="s_AffiliateSecCatId" name="s_AffiliateSecCatId">');
      jQuery(".appArea a[class=lc_PaginatorLinks]").each(function (i) {
        href = jQuery(this).attr("href");
        jQuery(this).attr("href", href + "&s_AffiliateSecCatId=1681");
      });
      jQuery(".appArea .lc_Table th map area").each(function (i) {
        href = jQuery(this).attr("href");
        jQuery(this).attr("href", href + "&s_AffiliateSecCatId=1681");
      });
    }
  });
}