/* Set random ad image on home page. */
function setAdImage() {
	/* Generate a random between 1 and 3. */
	randNum = Math.floor(Math.random() * 3) + 1;
	$('#home #whats-new img').attr('src', 'assets/images/whatsnew/new-jungle-' + randNum.toString(10) + '.jpg');
}

/* Initialize all the slideshows. */
function initSlideshows() {
  // Homepage slideshow.
  if ($('#home #slides_container').length > 0) {
    $('#home #slides_container').anythingSlider({
      buildArrows: false,
      pauseOnHover: false,
      width: 620,
      hashTags: false,
      height: 410,
      autoPlayLocked  : true,
      delay     : 8000,
      resumeDelay     : 10000,
      theme: 'jag-home-slider',
      animationTime: 600
    });
  }

  // Work page slideshows
  if ($('body.work #slides_container').length > 0) {
    if ($('.work-eventdesign #slides_container').length > 0) {
      $('#slides_container').anythingSlider({
        buildArrows: false,
        pauseOnHover: false,
        autoPlay: true,
        autoPlayLocked  : true,
        delay     : 8000,
        resumeDelay     : 3000,
        startText: 'play',
        stopText: 'pause',
        animationTime: 500
      });
    } else {
      $('.work-panel').hide();
      $('#slides_container').anythingSlider({
        buildArrows: false,
        pauseOnHover: false,
        autoPlay: true,
        autoPlayLocked  : true,
        delay     : 10000,
        resumeDelay : 15000,
        theme: 'jag-work-slider',
        startText: 'play',
        stopText: 'pause',
        onBeforeInitialize: function() {
          // Sync the panel description to the current slideshow panel.
          if (!window.location.hash == '') {
            var panelNum = parseInt(window.location.hash.substring(window.location.hash.length - 1, window.location.hash.length));

            if (panelNum != 1) {
              // nth child selector is zero based.
              panelNum--;
            } else {
              panelNum = 0; 
            }
          } else {
            panelNum = 0; 
          }

          $('.work-panel').hide();
          $('.work-panel:nth(' + panelNum + ')').fadeIn();
        },
        onSlideComplete : function() {
          var current = $('#slides_container').data('AnythingSlider').currentPage;
          current--; // nth child selector is zero based.
          $('.work-panel').hide();
          $('.work-panel:nth(' + current + ')').fadeIn();
        },
        animationTime: 500
      });
    }
  }

  // About Us slideshow.
  if ($('#aboutus #slides_container').length > 0) {
  
    $('#aboutus #slides_container').anythingSlider({
      buildArrows: false,
      buildNavigation: false,
      height: 260,
      hashTags: false,
      width: 940,
      autoPlay: false,
      autoPlayLocked  : true,
      theme: 'jag-aboutus-slider',
      delay     : 8000,
      animationTime: 500
    });

    // HACK: Make other slides visible (initially hidden to address page load
    // problem.
    $('#aboutus #slides_container img').show();

    // slideshow controls
    $('img#prev-arrow').click(function() {
      $('#slides_container').data('AnythingSlider').goBack();
    });

    $('img#next-arrow').click(function() {
      $('#slides_container').data('AnythingSlider').goForward();
    });
  }

}

/* Initialize 'read more' behavior for long text. */
function initReadMore() {
  if ($('body#aboutus').length > 0) {
    $('.read-more').click(function(event) {
      $(this).hide();
      $(this).siblings('.more-text', this).fadeIn();
    });

    // TODO: un-hardcode this.
    $('#hide-text-linda').click(function(event) {
      $('#more-text-linda').fadeOut();
      $('#read-more-linda').show();
    });
    $('#hide-text-kristi').click(function(event) {
      $('#more-text-kristi').fadeOut();
      $('#read-more-kristi').show();
    });
    $('#hide-text-cory').click(function(event) {
      $('#more-text-cory').fadeOut();
      $('#read-more-cory').show();
    });
    $('#hide-text-terry').click(function(event) {
      $('#more-text-terry').fadeOut();
      $('#read-more-terry').show();
    });
    $('#hide-text-brendon').click(function(event) {
      $('#more-text-brendon').fadeOut();
      $('#read-more-brendon').show();
    });
  }
}

/* Initialize Flickr and Twitter plugins. */
function initSocialMedia() {
  if ($('.tweet').length > 0) {
    $(".tweet").tweet({
        username: "jaguardesign",
        //join_text: "auto",
        avatar_size: 32,
        count: 3,
        //auto_join_text_default: "we said,",
        //auto_join_text_ed: "we",
        //auto_join_text_ing: "we were",
        //auto_join_text_reply: "we replied to",
        //auto_join_text_url: "we were checking out",
        loading_text: "loading tweets..."
    });
  }

  if ($('#flickr-feed').length > 0) {
    $('#flickr-feed').jflickrfeed({
      limit: 6,
      qstrings: {
        // TODO: Set to Jaguar Flickr Page
        id: '67521607@N06'
      },
      itemTemplate: 
      '<li>' +
        '<a href="{{link}}"><img src="{{image_s}}" alt="{{title}}" /></a>' +
      '</li>'
    });
  }
}

/* Initialize lightbox style galleries. */
function initGalleries() {
  if ($('#vmware-captures').length > 0) {
		$("#vmware-captures").click(function() {
			$.fancybox([
				'../../assets/images/work/slides/web-vmware-large-home-alt.jpg',
				'../../assets/images/work/slides/web-vmware-large-partners.jpg',
			], {
				'padding'			: 0,
				'type'              : 'image'
			});
		});
	}

  // Mystic card of the day 

  if ($('#mystic-cards').length > 0) {
    // 13 cards in the deck; cycle back to card 1 on the 14th of each month.
    day = (new Date().getDate()) % 14;
    day = day == 0 ? 1 : day;

		$("#mystic-cards").click(function() {
			$.fancybox([
				'assets/images/mystic-cards/jag-tarotcards-' + day.toString(10) + '.jpg',
			], {
				'padding'			: 0,
				'type'              : 'image',
        'title': 'Mystic Marketing Card of the Day'
			});
		});
	}
}

$(function() {
  setAdImage();
  initSlideshows();
  initReadMore();
  initSocialMedia();
  initGalleries();
});

