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/voices';
      });
  
    //Sign in page
    if (document.location.href.indexOf("theme=1041") >= 0) {
      var sign_in_text = '<p><strong><a href="https://secure.nrdconline.org/site/UserLogin?NEXTURL=https%3A%2F%2Fsecure.nrdconline.org%2Fsite%2FStories?theme=1041.">Sign in here</a></strong> if you&rsquo;re already a\
         BioGems Defender or you&rsquo;ve taken action on another\
         NRDC website. <i>(You\'ll need your username - usually your email address - and password. If you don\'t know them, it\'ll just take a few seconds to have them emailed to you.)</i></p>\
         <p>If you\'re not already a BioGems Defender, sign up below and then tell your story.</p>';
      jQuery(".appArea p").each(function (i) {
        element = jQuery(this);
        if(element.html().indexOf("Thank you for your interest in submitting a story") >= 0) {
          element.html(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 action alerts and updates. 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("Here's how you submit your story:");
      jQuery(".appArea .Hint").html("We may edit the title of your story and your state/region for length and consistency (for example changing ny to New York).  We reserve the right not to post stories or photos we deem inappropriate.");
      jQuery(".appArea table td p").each(function (i) {
        element = jQuery(this);
        if(element.html().indexOf("Tell us your story") >= 0) {
          element.html("Tell us why you're part of this movement - why you care so deeply about saving wildlife and wild places. Please include a title for your story and tell us - and other readers - what state or region you live in (include the country if not in the U.S.). Your first and last name as they appear in our records will be displayed.");
        } else if(element.html().indexOf("Illustrate your story with a photo") >= 0) {
          element.html("Post a photo of yourself in jpeg format. It will be sized down to about an inch and a half wide, so close-ups work best. No photo? We'll add a BioGems wildlife photo to your story.");
        } else if(element.html().indexOf("Preview your story to see how") >= 0) {
          element.html("Preview your story and edit it to your satisfaction. After making your final changes, submit it for approval. We'll send you an email when your story is posted.");
        }
      });
    }
    
    //Step 1 page
    if (document.location.href.indexOf("page=Article") >= 0 || document.location.href.indexOf("page=article") >= 0) {
      jQuery(".appArea h3.ObjTitle").html("Write Your Story");
      jQuery(".appArea>br").remove();
      jQuery(".appArea td.Divider").parent().parent().parent().css("display", "none");
      jQuery("#articleEditor p").each(function (i) {
        element = jQuery(this);
        if(element.html().indexOf("Please provide the details for your story such as a title") >= 0) {
          element.html('&nbsp;');
        } else {
          element.css("marginBottom", "0");
        }
      });
      jQuery("#articleEditor label[for='title']").html("<strong>1. Give Your Story a Title (e.g. Camping in the Rockies):</strong>");
      jQuery("#articleEditor label[for='subheading']").html("<strong>2. Enter Your State or Region:</strong>");
      jQuery("#articleEditor label[for='article']").html("<strong>3. Tell Us Why You are a BioGems Defender:</strong>");
      jQuery("#articleEditor textarea").addClass("step1-textarea");
    }
    
    //Step 2 page
    if (document.location.href.indexOf("page=Photo") >= 0 || document.location.href.indexOf("page=photo") >= 0 ) {
      jQuery(".appArea h3.ObjTitle").html("Upload a Photo (Optional)");
      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");
      jQuery(".appArea form p").each(function (i) {
        element = jQuery(this);
        element.css('margin', '0.3em 0');
        if(element.html().indexOf("Upload a photo from your own computer to display in this article.") >= 0) {
          element.html("Upload a photo of yourself to accompany your story. It will be sized down to about an inch and a half wide, so close-ups work best. If you don't have a photo to post, click Skip This Step. We'll add a BioGems wildlife photo to your story before posting it.");
        }
      });
      jQuery(".appArea form label").parent().css('margin', '0.7em 0 0 0');
    }
      
    //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) {
        element = jQuery(this);
        if(element.html().indexOf("Here is a preview of how your") >= 0) {
          element.html("Here is a preview of how your story will look when it is posted.");
        } else if(element.html().indexOf("Please review your story and click on the") >= 0) {
          element.html("Click Submit if you're satisfied, or Previous Step to make changes. If you don't submit a photo, a BioGems wildlife photo will be added to your post.");
        }
      });
      jQuery(".appArea .Hint").each(function (i) {
        element = jQuery(this);
        if(element.html().indexOf("Please note that we reserve the right") >= 0) {
          element.html("We may edit the title of your story and your state/region for length and consistency (for example changing ny to New York).  We reserve the right not to post stories or photos we deem inappropriate.");
        }
      });
      jQuery(".previewBox .headline").parent().css("margin", "0 0 -5px");
      jQuery(".previewBox .photocaption").css("display", "none");
      jQuery(".previewBox").css("min-height","250px");
      if(jQuery(".previewBox img").length > 0)
        jQuery(".previewBox img").attr("src", jQuery(".previewBox img").attr("src").replace("medium","thumb"));
      jQuery(".previewBox img").css("float", "left");
      jQuery(".previewBox img").css("margin", "15px 10px 5px 0");
      jQuery(".previewBox .subheading").css("margin", "0").html("My Location: " + jQuery(".previewBox .subheading").html());
      if (jQuery(".appArea .byline").html())
        jQuery(".appArea .byline").html(jQuery(".appArea .byline").html().replace("By:", "Submitted By:"));
        jQuery(".appArea .byline").before(jQuery(".appArea .subheading"));
    }
    
    //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 = "Thank you so much for being  a BioGems Defender and joining our Voices for the Wild. We'll send you an email when your story is posted.";
      jQuery(".appArea .dividerHeading").html(verify_text);
      jQuery(".appArea .dividerHeading").removeClass("dividerHeading");
      jQuery(".appArea input[value='Done']").attr("value", "Read Other Defenders' Stories").click(function () { 
        window.location='http://www.nrdconline.org/site/PageNavigator/voices';
      });
      jQuery(".appArea .Hint").css("display", "none");
    }
    
    //All steps
    jQuery(".appArea img[alt='Go to next step']").each(function (i) {
      jQuery(this).addClass("right-arrow");
    });
    jQuery("form input[alt='Go to next step']").each(function (i) {
      jQuery(this).addClass("right-arrow");
    });
  });
}

//Listing Page
var storybuilderlist_regex=/^http:\/\/www\.nrdconline\.org\/site\/PageNavigator\/voices.*/i;
if( storybuilderlist_regex.test(document.location.href) ) {
  jQuery(document).ready(function() {
    jQuery("#content-area a").each(function (i) {
      href = jQuery(this).attr("href");
      if((href.indexOf("page=NewsArticle") >= 0 || href.indexOf("page=newsarticle") >= 0) && 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 table table p").each(function (i) {
      html_byline = jQuery(this).html();
      pattern_location = /<br>\s*[^<>]+\s*$/img;
      pattern_location.lastIndex = 0;
      pattern_result_location = pattern_location.exec(html_byline)+"";
    
      html_byline = jQuery(".byline", jQuery(this)).html();
      pattern_submitted_by = /<br.*$/img;
      pattern_submitted_by.lastIndex = 0;
      pattern_result_submitted_by = pattern_submitted_by.exec(html_byline)+"";
      
      jQuery("a:first", jQuery(this)).append(pattern_result_submitted_by);
      jQuery(".byline", jQuery(this)).html(jQuery(".byline", jQuery(this)).html().replace(pattern_result_submitted_by,'')).prepend("My Location: " + pattern_result_location.replace("<br>","")+"<br>");
    });
    
    jQuery("#content-area table table p").each(function (i) {
      html = jQuery(this).html();
      html = html.replace(/<br>\s*[^<>]+\s*$/img, "");
      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 ) {
		  if(jQuery(".appArea .photocaption").length > 0)
        jQuery(".appArea .headline").parent().css("margin", "0 0 -10px");
      jQuery("#NewsSearch").parent().css("display", "none");
      jQuery(".appArea .photocaption").css("display", "none");
      jQuery("#catselect").parent().parent().css("display", "none");
      jQuery(".appArea .subheading").css("margin", "0").css('font-family','Arial,Helvetica,Verdana,sans-serif').html('My Location: ' + jQuery(".appArea .subheading").html());
      if (jQuery(".appArea .byline").html()) {
        byline_temp = jQuery(".appArea .byline").css('font-style','normal').html(jQuery(".appArea .byline").html().replace("By:", "Submitted By:")).html();
        pattern = /Submitted By:.*/;
        pattern_result = pattern.exec(byline_temp);
        byline_temp = byline_temp.replace(pattern, jQuery(".appArea .subheading").html());
        jQuery(".appArea .byline").html(byline_temp);
        jQuery(".appArea .byline").before(jQuery(".appArea .subheading"));
        jQuery(".appArea .subheading").html((pattern_result+""));
      }
      jQuery(".appArea img").each(function (i) {
        if (jQuery(this).attr("src"))
          jQuery(this).attr("src", jQuery(this).attr("src").replace("large","thumb"));
      });
      jQuery(".appArea img").css("float", "left");
      jQuery(".appArea img").css("margin", "20px 10px 5px 0");
    } 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) {
        element = jQuery(this);
        if(element.html().indexOf("Headline") >= 0) {
          element.html("<span>Title</span>");
        }
      });
      jQuery(".appArea .Hint").each(function (i) {
        element = jQuery(this);
        if(element.html().indexOf("Headline") >= 0) {
          element.html("Search by first or last name");
        }
      });
      jQuery(".appArea input#search").attr("value","Search").attr("title","Search");
      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/voices");
      jQuery(".appArea input[value='Done']").attr("value", "Back to Defenders List").parent().parent().parent().removeClass('appArea');
      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");
      });
    }
  });
}

var userlogin_regex=/^https:\/\/secure\.nrdconline\.org\/site\/UserLogin.*/i;

if( userlogin_regex.test(document.location.href) ) {
  jQuery(document).ready(function() {
    jQuery(".appArea").addClass("no-border");
  });
}
