var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9605322-3']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

document.writeln("<title>Schmidt's Garden Center</title>");

function StartBody(navSelected,subSelected)
{
  StartBodyCore(navSelected,subSelected,true);
}

function AdvertStartBody()
{
  StartBodyCore('none','none',false);
}

function StartBodyCore(navSelected,subSelected,showSignup)
{
  document.writeln('<body>');
  // Start of main body table
  document.writeln('<table width="100%">');
  // Start of only row in main body table
  document.writeln('<tr>');
  document.writeln('<td width="5%" valign="top">');
  WriteStdLeftColumn(navSelected,subSelected);
  if(showSignup)
    ShowEmailSignup();
  document.writeln('</td>');
  document.writeln('<td class="rightsidetd" width="*" valign="top">');
  document.writeln('<div class="rightsidediv">');
  WriteStdTopBanner();
  // Start of content area
  document.writeln('<div class="contentblock">');
}

function EndBody()
{
  document.writeln('</div>');
  // End of content area
  document.writeln('</div>');
  document.writeln('</td>');
  document.writeln('</tr>');
  // End of only row in main body table
  document.writeln('</table>');
  // End of main body table
  document.writeln('</body>');
}

function AdvertEndBody()
{
  EndBody();
}

function WriteStdTopBanner()
{
  document.writeln('<div class="bannerblock">');
  document.writeln('  <div class="bizname">Schmidt\'s Garden Center</div>');
  document.writeln('  <table width="100%">');
  document.writeln('    <tr>');
  document.writeln('      <td class="hoursloc">On The Corner of NW 29th &amp;<br>Grant in Corvallis Since 1959</td>');
  document.writeln('      <td class="hoursloc" align="right">Open 9am-6pm Mon-Sat &amp;<br>9am-5pm Sun, 541-757-3214</td>');
  document.writeln('    </tr>');
  document.writeln('  </table>');
  document.writeln('</div>');
}

function WriteStdLeftColumn(navSelected,subSelected)
{
  document.writeln('<div class="leftnav">');
  document.writeln('  <img src="images/store3.jpg">');
  document.writeln('  <br>');
  document.writeln('  <div class="leftnavoptions">');
  WriteStdLeftItem(navSelected,'home',   'index.html',   'Home On The Web',
      subSelected,[
      { selector:'index',       url:'index.html',       title:'Welcome To Schmidt\'s' },
      { selector:'blogs',       url:'blogs.html',       title:'Staff Blogs' },
      /*{ selector:'dailycoupon', url:'dailycoupon.html', title:'Daily Coupon' },*/
      { selector:'askschmidts', url:'askschmidts.html', title:'Ask Schmidt\'s' },
      { selector:'email',       url:'email.html',       title:'Email Newsletter' },
      { selector:'weather',     url:'weather.html',     title:'Local Weather Forecast' },
      /*{ selector:'club',      url:'club.html',        title:'Club Schmidt\'s Forum' },*/
      { selector:'facebook',    url:'facebook.html',    title:'Facebook And Twitter' },
      { selector:'history',     url:'history.html',     title:'Our History' }
    ]);
  WriteStdLeftItem(navSelected,'plants',  'plants.html',  'Plants &amp; Gardening');
  WriteStdLeftItem(navSelected,'birds',   'birds.html',   'Birds &amp; Bird Feeding',
    subSelected,[
      { selector:'birds',         url:'birds.html',         title:'Introduction' },
      { selector:'birdfeeding',   url:'birdfeeding.html',   title:'Bird Feeding' },
      { selector:'birdscorvallis',url:'birdscorvallis.html',title:'Birds Of Corvallis' },
      { selector:'ebird',         url:'ebird.html',         title:'EBird.org' }
    ]);
  WriteStdLeftItem(navSelected,'gifts',   'gifts.html',   'Gifts &amp; Garden Art');
  document.writeln('  </div>');
  document.writeln('</div>');
}

function WriteStdLeftItem(navSelector,navWritten,url,title,subSelected,subItems)
{
  var navMatched=(navSelector==navWritten);
  var navClass=navMatched?'leftnavlinksel':'leftnavlink';
  document.writeln('<a class="'+navClass+'" href="'+url+'">'+title+'</a>');
  if(subItems!=undefined&&navMatched)
  {
    document.writeln("<div class='leftsubnavblock'>");
    for(var i in subItems)
    {
      var subItem=subItems[i];
      if(subSelected==subItem.selector)
        subClass='leftsubnavlinksel';
      else
        subClass='leftsubnavlink';
      document.writeln('<a class="'+subClass+'" href="'+subItem.url+'">'+subItem.title+'</a>');
    }
    document.writeln("</div>");
  }
}

function AdvertEmailSignup(signupType)
{
  document.writeln('<p class="advertsignup">');
  document.writeln('<a class="advertsignuplink" '+
    'href="'+EmailSignupHref(signupType)+'">Click here to subscribe and get your coupon</a>');
  document.writeln('</p>');
}

function ShowEmailSignup()
{
  document.writeln('<div class="leftemaildiv">');
  document.writeln('<div class="leftemailhead">Save $5 On Your<br/>Next Purchase</div>');
  document.writeln('<div class="leftemailbody">Sign up for our email newsletter and get a <strong>coupon for $5 off</strong> your next purchase.</div>');
  document.writeln('<div class="leftemaillinkdiv"><a class="leftemaillinka" href="'+EmailSignupHref('leftnav')+'">Click Here To Sign Up</a></div>');
  document.writeln('<div class="leftemailbody">Nearly all our customers subscribe to our email newsletter, '+
    'because it\'s a mix of practical gardening know-how, '+
    'interesting information related to gardening, and <br/><strong>really good coupons.</strong></div>');
  document.writeln('</div>');
}

function EmailSignupHref(signupType)
{
  return 'http://flamingoemail.com/AddEmailAddressExternal.php?k=_KH_1&c=2&m=2&t=&a=&h=991f8e&st='+
    signupType;
}

// ================================================================================
// Asynchronously show the contents of an RSS/Atom feed inside an existing element
// ================================================================================

function ShowFeed(containerId,url,maxEntries)
{
  CreateFeedContainer(containerId);
  FillFeedResultContainer(containerId,url,maxEntries);
}

function CreateFeedContainer(containerId)
{
  $("#"+containerId).html(
    "<table class='EmbeddedFeedTopContainer'><tr><td><div class='EmbeddedFeedResultContainer'>"+
    "</div></td></tr></table>");
}

function FillFeedResultContainer(containerId,url,maxEntries)
{
  $.ajax({
    url: 'xmlhttpproxy.php',
    cache: false,
    data: { url:url },
    success: function(feedXml) { 
      var resultContainer=$("#"+containerId+" .EmbeddedFeedResultContainer");
      resultContainer.empty();
      var docElemName=feedXml.documentElement.tagName;
      if(docElemName=="rss")
      {
        BuildRSSFeed(resultContainer,feedXml,maxEntries);
      }
      else if(docElemName=="feed")
      {
        BuildAtomFeed(resultContainer,feedXml,maxEntries);
      }
      else
      {
        ShowFeedError(containerId,"Unrecognized feed top element: "+docElemName);
      }
    },
    error: function(jqXHR, textStatus, errorThrown) {
      ShowFeedError(containerId,"Error loading feed XML: "+errorThrown);
    },
    dataType: 'xml'
  });
}

function BuildRSSFeed(resultContainer,feedXml,maxEntries)
{
  var items=$(feedXml).find("channel item");
  var itemCount=0;
  items.each(
    function()
    {
      itemCount++;
      if(itemCount<=maxEntries)
      {
        var item=$(this);
        var title=item.find("title").text();
        var pubDate=item.find("pubDate").text();
        var localeDate=new Date(pubDate).toLocaleDateString();
        var author=item.find("author").text();
        var leftParen=author.indexOf('(');
        var rightParen=author.indexOf(')');
        if(leftParen>=0&&rightParen>leftParen)
          author=author.substring(leftParen+1,rightParen);
        var descr=item.find("atom\\:summary");
        if(descr.length==0)
          descr=item.find("description");
        BuildFeedItem(resultContainer,localeDate,title,descr.text(),author);
      }
    });
}

function BuildAtomFeed(resultContainer,feedXml,maxEntries)
{
}

function BuildFeedItem(resultContainer,date,title,descr,author)
{
  var item=
    "<div class='EmbeddedFeedDate'>"+date+"</div>"+
    "<div class='EmbeddedFeedBody'>"+
    "  <div class='EmbeddedFeedTitle'>"+title+"</div>"+
    "  <div class='EmbeddedFeedContent'>"+descr+"</div>"+
    "  <div class='EmbeddedFeedAuthor'>Posted By "+author+"<div>"+
    "</div>";
  resultContainer.append($(item));
}

function ShowFeedError(containerId,msg)
{
  $("#"+containerId+" .EmbeddedFeedResultContainer").html("<p class='EmbeddedFeedError'>"+msg+"</p>");
}

// ================================================================================
// Output coupons
// ================================================================================

function OutputTodaysCoupon(startDate,coupons)
{
  var now=new Date();
  var elapsedDays=Math.floor((now.valueOf()-startDate.valueOf())/86400000);
  var index=elapsedDays%coupons.length;
  coupons[index].output(now,false);
}

function OutputAllCoupons(coupons,summaryOnly)
{
  var i;
  var startDate=dailyCouponStart;
  for(i=0;i<coupons.length;i++)
  {
    var coupon=coupons[i];
    coupon.output(startDate,summaryOnly);
    startDate=new Date(startDate.valueOf()+86400000);
  }
}

function OutputSimpleCoupon(startDate,summaryOnly,headline,regularPrice,copy)
{
  if(summaryOnly)
  {
    OutputCouponSummary(headline,startDate);
  }
  else
  {
    OutputCouponStart();
    OutputCouponStartBorder();
    OutputCouponHeadline(headline);
    OutputCouponPrice(regularPrice);
    OutputCouponConditions(startDate);
    OutputCouponEndBorder();
    OutputCouponCopy(copy);
    OutputCouponEnd();
  }
}

function OutputCouponSummary(headline,startDate)
{
  document.writeln('<div class="couponsummary"><strong>'+headline+'</strong>,<br/>starts '+
    startDate.toLocaleDateString()+'</div>');
}

function OutputCouponStart()
{
  document.writeln('<div class="couponaligner">');
  document.writeln('<table class="couponcontainer"><tr><td>');
}

function OutputCouponStartBorder()
{
  document.writeln('<div class="couponborder">');
  document.writeln('<div class="coupontitle">Coupon</div>');
}

function OutputCouponEndBorder()
{
  document.writeln('<div class="couponnoprint">No need to print coupon - just mention it to our staff</div>');
  document.writeln('</div>');
}

function OutputCouponEnd()
{
  document.writeln('</td></tr></table>');
  document.writeln('</div>');
}

function OutputCouponHeadline(headline)
{
  document.writeln('<div class="couponheadline">'+headline+'</div>');
}

function OutputCouponPrice(regularPrice)
{
  if(regularPrice!=null)
    document.writeln('<div class="couponregularprice">'+regularPrice+'</div>');
}

function OutputCouponConditions(startDate)
{
  var endDate=new Date(startDate.valueOf()+86400000);
  var formattedEndDate=endDate.toLocaleDateString();
  document.writeln('<div class="couponconditions">'+
    'Offer good through <strong>'+formattedEndDate+'</strong><br/>'+
    'May not be combined with any other offer'+
    '</div>');
  document.writeln('<div class="couponaddress">'+
    'Schmidt\'s Garden Center<br/>1299 NW 29th Street<br/>Corvallis, OR 97330</div>');
}

function OutputCouponCopy(copy)
{
  document.writeln('<div class="couponcopy">'+copy+'</div>');
}

