var defaultSearch = "Search this website";
var gm_search_term = "";
$(document).ready(function() {

  if ( $("#salutation").text().indexOf("[firstname,fallback=]") > -1 ) {
    $("#salutation").text("");
  }
	
  // add wrapper divs to blockquotes for closing quote tags
  $("#content blockquote p").prepend("&#8220;").append("&#8221;");
  //$("#content blockquote").wrap('<div class="outerQuote"><div class="centerQuote"></div></div>');

  // zebra striping
  $(".package-list tr:even, .news-list tr:even").addClass("dark");

  /* Fix PNG transparency in IE 6 */
  $('img[src$=.png], #logo a').ifixpng();
	
  /* open new links in a popup window */
  $('a[rel=new]').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
	
  //hide the gallery drop down if only one item
  if ( $('.sets ul').children("li").length == 1 ) {
    $('.sets > a').addClass("hide");
  }

  /* setup the main menu */
  $('#navigation.sf-menu ul').superfish({
    autoArrows:		false,
    dropShadows:	false,
    disableHI:		true
  });
	
  $('#navigation ul:first > li:lt(5)').addClass('main'); // add class to important nav items

  /* showcase image switcher */	
  $('#controls .sets a, a.gallerylink').click(function() {
    $('#controls .pause').click(); // swap back to paused state
    var selectedGallery = $(this).attr('href');
    // update the current gallery title
    $('#controls .sets a').each(function(){
      if ( selectedGallery == $(this).attr('href') ) {
        $('#controls .sets .title').html($(this).html()); 
      }
    });
    $('#images').cycle('stop').html('').addClass('loading'); // stop the slideshow, empty the images, display loader
		
    // get the images array string
    var galleryID = $(this).attr('href');
    var galleryImagesStr = $("#g"+galleryID).text();
    
    $.ajax({
      type: 'POST',
      url: "/modules/showcase_gallery/new_load.php",
      data: {
        gallery_id: galleryID,
        gallery_images: galleryImagesStr
      },
      success: function(data){
        $('#images').html(data).removeClass('loading');

        // get a random image to start on
        var num_imgs = $('#images').children().length;
        var random_img = Math.floor(Math.random()*(num_imgs-1));

        // not random start for homepage
        if ( $("body.home") ) {
          random_img = 0;
        }

        var action = "pause";
        if ( $("#showcase-gallery_87 #images, #showcase-gallery_97 #images, #showcase-gallery_105 #images, #showcase-gallery_30 #images").length > 0 ) {
          $('a.play').hide().siblings('a.pause').show();
          action = "resume";
        }

        $('#images').cycle({
          fx:     	'fade', 
          speed:  	1200, 
          timeout: 	4000, 
          next:   	'.next', 
          prev:   	'.previous' ,
          startingSlide: random_img
        })
        .cycle(action); // pause the slideshow 

        $('#controls .play').click(function() {
          $('#images').cycle('resume');
          $(this).hide().siblings('a.pause').show();

          return false;
        });

        $('#controls .pause').click(function() {
          $('#images').cycle('pause');
          $(this).hide().siblings('a.play').show();

          return false;
        });
      }
    });
    
    return false;
  });
	
  /* activate the first set */
  $('#controls .sets ul a:first').click();
	
  /* search box */
  if ( gm_search_term != defaultSearch ) {
    $('#searchQuery').val(gm_search_term);
  }
  $('#searchQuery').focus(function() {
    if ($(this).val() == defaultSearch) $(this).val('');
  }).blur(function() {
    if ($(this).val() == '') $(this).val(defaultSearch);
  });
	
  /* tell a friend */
  var tellfriendlink = $('.tell-a-friend a');
  $('#friend-modal').jqm({
    ajax: tellfriendlink.attr('href'), 
    trigger: tellfriendlink, 
    onLoad: function() {
    }
  });
	
/* subscribe*/
var subscribelink = $('.subscribe a');
  $('#subscribe-modal').jqm({
    ajax: subscribelink.attr('href'), 
    trigger: subscribelink, 
    onLoad: function() {
    }
  });
	
});
