$(document).ready(function() {
		prepare_slideshow();
		homepage_slideshow();
		prepare_links();
});

function prepare_slideshow() {
	var slideshow = $('#feature');

	if (!slideshow) {
		return;
	}

	slideshow.append('<p id="controls"><span id="ctrl_previous" title="Previous">&lt;</span> <span id="ctrl_play" title="Pause" class="active">Pause</span> <span id="ctrl_next" title="Next">&gt;</span></p>');
	
	var ctrl_play = $('#ctrl_play');
	ctrl_play.click(function() {
		e = $(this);
		if (e.hasClass('active')) {
			e.html('Play');
			e.attr('title', 'Play');
			e.removeClass('active');
			$('#feature').cycle('pause');
		}
		else {
			e.attr('title', 'Pause');
			e.html('Pause');
			e.addClass('active');
			$('#feature').cycle('resume');
		}
	});
	
	slideshow.cycle({prev: '#ctrl_previous', next: '#ctrl_next', slideExpr: 'img'});
}

function prepare_links() {
  // order matters for priority
  // add pdf icon to left of pdf links and open in a new window
  $('a[href$=".pdf"]').filter(function(){var t = $(this).text(); return t && $.trim(t) != "";}).addClass("icon_pdf").click(function() {
      window.open($(this).attr('href'), 'pdfWindow');

      return false;
  });

  // add external icon to any link that are not on this domain and open in a new window
  $('a[href^="http://"], a[href^="https://"]').filter(function(){var link = $(this); return !(/https?:\/\/(www\.)?limestonebuilders\./).test(link.attr('href')) && link.attr('class').indexOf('icon') == -1 && link.attr('rel') != 'external';}).addClass("icon_external").click(function() {
      window.open($(this).attr('href'), 'externalWindow');

      return false;
  });
	
	$('a[rel="external"]').click(function() {
		window.open($(this).attr('href'), 'externalWindow');

    return false;
	});
}

function homepage_slideshow() {
	var slideshow = $('#image_slider #images');

	if (!slideshow) {
		return;
	}
	// get height of images
	var tallest = 0;
	
	$('#image_slider #images img').each(function() {
		var current = $(this).height();
		if (current > tallest) {
			tallest = current;
		}
	});
	
	slideshow.css('height', tallest);
	
	
	var container = $('#image_slider');
	
	container.prepend('<img id="ss_previous" alt="Previous" src="/wp-content/themes/limestone/res/images/viewerBack_btn.gif">');
	container.append('<img id="ss_next" alt="Next" src="/wp-content/themes/limestone/res/images/viewerNext_btn.gif">');
	
	
	
	slideshow.cycle({prev: '#ss_previous', next: '#ss_next', slideExpr: 'img'});
	slideshow.cycle('pause');
}

