/*
 *
 *	Girl Scouts jQuery functions
 *		Stuart Swope
 */
 
/* showToolbar enables the toolbar functionality if the user has javascript */ 
jQuery.showActions = function () {
   var actions = '<ul id="actions">\n\
                     <li>\n\
                     <a class="print" href="#" onclick="window.print(); return false;">Print</a>\n\
                     </li>\n\
                     <li>\n\
                     <!-- AddThis Button BEGIN -->\n\
                     <script type="text/javascript">var addthis_pub="lkfgshom";<\/script><a class="send" href="http://www.addthis.com/bookmark.php?v=20" onmouseout="addthis_close()" onclick="return addthis_sendto()" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')">Share</a>\n\
                     <script type="text/javascript" src="https://s7.addthis.com/js/200/addthis_widget.js"><\/script>\n\
                     </li>\n\
                  </ul>\n';
   jQuery('#content').after(actions);
};

/* Enables scroller functionality on the home and stories pages. */
jQuery.enableScroller = function (location) {

// Do not enable the scroller for ie6.  The ajax request fails.
if (window.XMLHttpRequest) {
   $("<img>").attr("src", 'graphics/loading.gif'); //preload loading.gif

   var curr_id = "";
	
	// Insert scroller nav arrows into the dom.
   $('.scrollable').before('<img class="prev browse scroller-nav" src="graphics/carousel-left.png" alt="Previous" title="Previous" />');
         $('.scrollable').after('<img class="next browse scroller-nav" src="graphics/carousel-right.png" alt="Next" title="Next" />');
         
   // Initialize scrollable      
   $("#infinite").scrollable({
      size: 7,
      item: 'a',
      loop: false
   }).circular();
   
   // Get the clipping for a person in the featured person clipping group and populate via ajax.
   var $getPerson = function (id) {

      if (curr_id != id)
      {
         // Show loading gif until images are loaded.
         if (location == 'stories') {
            $('#stories-full-body').html('&nbsp;');
            $('#stories-info').html('<img style="margin: 100px 0 0 285px" src="graphics/loading.gif" alt=" " /> ');
         }
         else {
            $('#slogan').html('<img style="margin: 110px 0 0 330px" src="graphics/loading.gif" alt=" " />');
            $('#featured-person').html(' ');
         }
         
         // Ajax call returns a json object
         $.getJSON('includes/ajax_clippings.php?' + (new Date().getTime()), {'clipping' : id }, function(data) {
            var clippings = data;
               
            if (location == 'home') {
               $("#featured-info").html(clippings['home_info']);
            }
            else {
               $('.facebook-share').attr ('href', "http://www.facebook.com/sharer.php?u=http://www.gshom.org/stories/" + clippings['name'] + "&t=Girl Scouts Heart of Michigan: Meet " + clippings['name'].charAt(0).toUpperCase() + clippings['name'].slice(1));
               $('.email-share').attr('href', "mailto:?subject=Girl%20Scouts%20Heart%20Of%20Michigan%20Website&amp;body=Hi%2C%0A%0AI%20thought%20you%27d%20like%20to%20see%20this%20page%20from%20the%20Girl%20Scouts%20Heart%20Of%20Michigan%20Website%3A%0A%0Ahttp%3A%2F%2Fwww.gshom.org/stories/" + clippings['name']);
            }
            
            // IE 6/7/8 can't do nice fades so only fade for good browsers; firefox, chrome, safari.
            if (!($.browser.msie) ) {
               if (location == 'home') {
                  $('#featured-person').fadeOut(300, function () {
                     $(this).html(clippings['home_image']);
                  })
                  .fadeIn(400);
                  
                  $('#slogan').fadeOut(300, function () {
                     $(this).html(clippings['slogan']);
                  })
                  .fadeIn(400);
               } else {
                  $('#stories-full-body').fadeOut(300, function () {
                     $(this).html(clippings['featured_image']);
                  })
                  .fadeIn(400);
                  
                  $('#stories-info').fadeOut(300, function () {
                     $(this).html(clippings['featured_info']);
                  })
                  .fadeIn(400);
               }
            }
            else
            {
               if (location == 'home') {
                  $('#featured-person').html(clippings['home_image']);
                  $('#slogan').html(clippings['slogan']);
               }
               else {         
                  $('#stories-full-body').html(clippings['featured_image']);
                  $('#stories-info').html(clippings['featured_info']);
               }
            }
         });
         
         curr_id = id;
      }
   };
   
   // Get the clicked person and load data via ajax
   $(".items a").click(function() {
      var id = $(this).attr("href").match(/featured=([0-9]+)/); // Parse out the clipping id
      $getPerson(id[1]);
      return false;
   });
} // End ie6 conditional
};

/* Enable home tab interface*/
jQuery.enableHomeTabs = function () {

      // Preload tab images
      $("<img>").attr("src", 'graphics/bg-tab-news.png');
      $("<img>").attr("src", 'graphics/bg-tab-events.png'); 
      $("<img>").attr("src", 'graphics/bg-tab-cookie.png'); 

   $('#tabs li a').click(function() {
      if ($(this).attr('class') == "news-tab") {
         $('#tabs').css("background-image", "url(graphics/bg-tab-news.png)");
         $('#home-events').hide();
         $('#home-cookie').hide();         
         $('#home-news').show();
      }
      if ($(this).attr('class') == "events-tab") {
         $('#tabs').css("background-image", "url(graphics/bg-tab-events.png)");
         $('#home-news').hide();
         $('#home-cookie').hide();         
         $('#home-events').show();
      }
      if ($(this).attr('class') == "cookie-tab") {
         $('#tabs').css("background-image", "url(graphics/bg-tab-cookie.png)");
         $('#home-events').hide();
         $('#home-news').hide();         
         $('#home-cookie').show();
      }
      return false;
   });
};