var gtwm_image_number = 0;

function next_slide() {
  var numSlideshowItems = $('.slideshow_item').size();
  if (numSlideshowItems < 2) {
  	// The first time we're called, load the slideshow items in
  	$.get('../static_features/slideshow_content.htm', function(data) {
      $('#slideshow').append(data);
    });
  } else {
  	  //rotate slideshow items
	  var imgSrc = $($('.slideshow_item').get(gtwm_image_number+1)).find("img").attr("src");
	  $('#slideshow').css('background','url(' + imgSrc + ')');
	  $('.slideshow_item').fadeOut(2000);
	  $($('.slideshow_item').get(gtwm_image_number+1)).fadeIn(2000);
	  if (gtwm_image_number >= ($('.slideshow_item').size() - 2)) {
	  	gtwm_image_number = -1;
	  } else {
	  	gtwm_image_number++;
	  }
  }
}

function fShowCatalogues() {
  setTimeout("fShowCataloguesWork(true);",2500);
}

function fShowCataloguesWork(appendToBottom) {
  var ajax_cf = new ContentFlow('ajax_cf', {
    useAddons: "slideshow"
  });
  var catalogueRows = abJson; // loaded by script in html head
  //var tableId = "skqbhhqtqo532wf02";
  //var catalogueFieldId = "qyoennzxzulxokx1b";
  for ( i=0; i < catalogueRows.length; i++ ) {
    var catalogueRow = catalogueRows[i];
    var rowId = catalogueRow["rowId"];
    var catalogue = catalogueRow["qyoennzxzulxokx1b"];
    var cover = catalogueRow["t0kllmfrkrl4mq1j0"];
    var cataloguePath = "http://appserver.gtportalbase.com/agileBase/uploads/skqbhhqtqo532wf02/qyoennzxzulxokx1b/" + rowId + "/" + catalogue;
    var coverPath = "http://appserver.gtportalbase.com/agileBase/uploads/skqbhhqtqo532wf02/t0kllmfrkrl4mq1j0/" + rowId + "/" + cover;
    var catalogueName = catalogueRow["fml8psb0kb1vbai4w"];
    var itemHtml = $("<a href='" + cataloguePath + "'></a>").addClass("item");
    var itemImage = $("<img src='" + coverPath + "' />").addClass("content").attr("title", catalogueName);
    itemHtml = itemHtml.append(itemImage);
    ajax_cf.addItem(itemHtml[0], 'last');
    if (appendToBottom) {
      var bottomHtml = $("<a href='" + cataloguePath + "'></a>");
      var bottomImage = $("<img src='" + coverPath + "' />").attr("alt",catalogueName).css("width","99px");
      bottomHtml = bottomHtml.append(bottomImage).append("<br />").append(catalogueName);
      $("#bottom_half").append(bottomHtml);
      $("#bottom_half").append("<br><br>");
    }
  }
}

/* Is this an obsolete function? */
function show_all_news() {
  jQuery(".newsheadline").css('height','');
  jQuery(".newsheadline").show("fast");
  jQuery("#all_news").fadeOut("normal");
}

function formatPage() {
  var jqFirstArticle = $(".newsheadline:eq(0)");
  var jqSecondArticle = $(".newsheadline:eq(1)");
  // replace top left image with the image in the first article
  var jqSlideshowImage = jqFirstArticle.find("img");
  if (jqSlideshowImage.size() > 0) {
    $("#slideshow_image").replaceWith(jqSlideshowImage).addClass("rounded").attr("id","slideshow_image");
  }
  // set text over top left image
  $("#slideshow_caption").html(jqFirstArticle);
  // move second article to top right, unless there's an RSS feed already there
  if(jQuery("#video").find("#feedControl").size() == 0) {
    $("#video").html(jqSecondArticle);
  }
  // tidy up link separators
  $("img.section_separator").last().remove();
  // bottom section: remove columns if necessary
  if($("#bottom_half").find("iframe").size() > 0) {
    $("#bottom_half").css("column-count","1").css("-moz-column-count","1").css("-webkit-column-count","1");
  }
}

function fYouTube() {
	$('a').each(
			function() {
				var sHref = this.getAttribute('href');
				if ((sHref.indexOf('youtube.com') > -1)
						|| (sHref.indexOf('vimeo.com') > -1)) {
					var oContainer = document.createElement('div');
					oContainer = $(oContainer);
					oContainer.addClass('gtpb_youtube');
					$(this).replaceWith(oContainer);
					oContainer.oembed(sHref, 
					  {
					  	youtube: { maxWidth: 390 }
					  }
					);
				}
				// oContainer.find('embed').attr('rel',0);
			});
}

// pre-submit callback 
function ajaxFormValidate(formData, jqForm, options) { 
    if (jQuery("#enter_email").val().indexOf("@") == -1) {
      alert("Please enter an email address to sign up for special offers");
      return false;
    } else {
      return true;
    }
} 
 
// post-submit callback 
function ajaxFormShowResponse(responseText, statusText, xhr, $form)  { 
	alert("Thank you, your email address has been received");
}

