var intImages = 0;	//	this will contain the total number of images
var galleryIndex = 1;
var strMainGalleryImgSelector = ".imgPhotoGalleryMain";

$(function() {
  $('#galleryThumbs a').click(function(){  
    var src = $(this).children('img').attr('src');
    var alt = $(this).children('img').attr('alt');
    var imgID = $(this).parent('li').attr('jcarouselindex'); 
    $('.galleryTitle').html($('#title'+imgID).html());
    $('.galleryDescription').html($('#description'+imgID).html());
    $(strMainGalleryImgSelector).attr('src',src);
    $(strMainGalleryImgSelector).attr("alt", alt);
    $(this).parent().siblings("li").removeClass("cssActive");
    $(this).parent().addClass("cssActive");
    var imageID = $('#imgID'+imgID).html();    

    var prodLinks = $.ajax({
      url: "/getGalleryProdLinks.aspx?img="+imageID + "&now=" + (new Date()).toString(),
      async: false
     }).responseText;
    //alert(prodLinks); 
    $('#galleryProdLinks').html(prodLinks);
    if(prodLinks=="not found")
    {    
      $('.prodsUsedTitle').hide();
      $('#galleryProdLinks').hide();      
    }
    else
    {
      $('.prodsUsedTitle').show();
      $('#galleryProdLinks').show();      
    }
    galleryIndex = imgID;
    return false;
  }); 
  
  $("#galleryThumbs li").each(function() {
  	intImages++;
  });     
  
  if(intImages != 0){
    jQuery("#galleryThumbs").jcarousel({		
      visible: 5,
      buttonNextHTML: "<img src=\"/img/buttons/btn_galleryRight.png\" class=\"btnJCarouselRight pngFix\" />",
      buttonPrevHTML: "<img src=\"/img/buttons/btn_galleryLeft.png\" class=\"btnJCarouselLeft pngFix\" />",
      scroll: 1,
      itemFirstInCallback: fCarouselFirstInCallback,
      itemLastInCallback: fCarouselLastInCallback,
      initCallback: fCarouselInitCallback
    });
  }
	
  
  
  $('#content').css("height","auto").css("padding-bottom","20px");
});

/*
	Hide the previous button if there are no images prior to the first one displaying in the carousel.
	@param oCarousel a reference to the jcarousel object
	@param oLi a reference to the <li> object
	@param intLiIndex the index of the <li> object within the list
	@param strAction the state of the carousel action (prev, next or init)
*/
function fCarouselFirstInCallback(oCarousel, oLi, intLiIndex, strAction) {
//alert("oCarousel = " + oCarousel + "\noLi = " + oLi + "\nintLiIndex = " + intLiIndex + "\nstrAction = " + strAction);
	if(intLiIndex == 1) {
		$(".jcarousel-prev").hide();
	} else {
		$(".jcarousel-prev").show();
	}
}

/*
	Hide the next button if there are no images following the last one displaying in the carousel.
	@param oCarousel a reference to the jcarousel object
	@param oLi a reference to the <li> object
	@param intLiIndex the index of the <li> object within the list
	@param strAction the state of the carousel action (prev, next or init)
*/
function fCarouselLastInCallback(oCarousel, oLi, intLiIndex, strAction) {
//alert("oCarousel = " + oCarousel + "\noLi = " + oLi + "\nintLiIndex = " + intLiIndex + "\nstrAction = " + strAction);
	if(intLiIndex == intImages) {
		$(".jcarousel-next").hide();
	} else {
		$(".jcarousel-next").show();
	}
}

/**
 * This is the callback function which receives notification
 * about the state of the next button.
 */
function fCarouselbuttonNextCallback(carousel, button, enabled) {
    alert("next");
    return;
};

/**
 * This is the callback function which receives notification
 * about the state of the prev button.
 */
function fCarouselbuttonPrevCallback(carousel, button, enabled) {
    alert("prev");
    return;
};



function fCarouselInitCallback(carousel, state) {
  if (state == 'init'){
    
    $('#galleryThumbs li.cssActive a').click();
    $(".jcarousel-prev").click(function(){
      var newidx = parseInt(galleryIndex) -1;
      var selector = "galleryImage"+newidx+" a";
      //alert(selector);
      $('#'+selector ).click();
      galleryIndex = newidx;
    });
    $(".jcarousel-next").click(function(){
      var newidx = parseInt(galleryIndex) + 1;
      var selector = "galleryImage"+newidx+" a";
      //alert(selector);
      $('#'+selector ).click();
      galleryIndex = newidx;
    });
    
  }    
};