﻿var imgList;
var imageList;
var j=0;

// -----------------------------------------------------------------------
// replaces the images with thumbnail.
// -----------------------------------------------------------------------
$("#photo-source img").each(function(i) { 
	var capid = $("#caption-source span").eq(i).attr('id');
	
	var x1 = this.src.indexOf('rteimg.gif');
	var x2 = this.src.indexOf('CMSRemoveImage.GIF');
	if (x1 != -1) { 
	} else if (x2 != -1) { 
	} else {
		var thissrc = $(this).attr("src");
		if (thissrc) {
			var suburl = this.src.lastIndexOf('/');
			imgLink = this.src.substring(0, suburl+1) + 'Forms/DispForm.aspx?ID=' + capid + '&Source=' + window.location;
			if(!imageList) { 
				imageList = imgLink + ','; 
			} else {
				imageList = imageList + imgLink  + ','; 
			};
			$(this).attr({ alt: $("#caption-source span").eq(i).html() });
			//$(this).attr({ src: "/_layouts/getthumbnail.aspx?ImageUrl=" + this.src  });
		};
	};
});	

// -----------------------------------------------------------------------
// this gets all the images in the #photo-source div block into a comma list.
// -----------------------------------------------------------------------
$("#photo-source img").each(function(i){ 
	var x1 = this.src.indexOf('rteimg.gif');
	var x2 = this.src.indexOf('CMSRemoveImage.GIF');
	if (x1 != -1) { 
	} else if (x2 != -1) { 
	} else {
		var thissrc = $(this).attr("src");
		if(thissrc) {
			if(!imgList) {
				imgList = this.src + ',';
			} else {
				imgList = imgList + this.src + ',';
			};
		};
	};
});


// -----------------------------------------------------------------------
// makes said list into array
// -----------------------------------------------------------------------
var itemList = new Array();
itemList = imgList.split(',');
var oImgUrlList = new Array();
oImgUrlList = imageList.split(',');


// -----------------------------------------------------------------------
// Now we get captions
// -----------------------------------------------------------------------
var cList;
$("#caption-source span").each(function(i){
	var jQ = $(this);
	var c = jQ.html(); 
	if(c) { 
		if(!cList) {
			cList = c + '||';
		} else {
			cList = cList + c + '||';
		};
	};
});
var capList = new Array();
capList = cList.split('||');


// -----------------------------------------------------------------------
// checking for an undefined last item.
// -----------------------------------------------------------------------
var x = itemList.length;
if (itemList[x] == undefined) {
	itemList.pop();
};


// -----------------------------------------------------------------------
// Load the actual carousel with the items from our arrays.
// -----------------------------------------------------------------------
function loadItemHandler(carousel, start, last, available) {
    if (!available) {
        for (var i = start; i <= last; i++) {
			if (i > itemList.length) {
				break;
			};
			carousel.add(i, getItemHTML(itemList[i - 1],itemList[i - 1],capList[i - 1], oImgUrlList[i-1]) + "<p>" + capList[i - 1] + "</p>");
        };
    };
    // Trigger loaded
    carousel.loaded();
};


// -----------------------------------------------------------------------
// This creates the links and the images
// You see src and url are the same, but are treated as a number and text.
// that's why it's duplicated.
// ----------------------------------------------------------------------- 
function getItemHTML(src, url, alt, link) {
	var indexQ = src.lastIndexOf('?');
	var startParam1Index = indexQ + "?ImageUrl=".length; // WAS: + 10;
	var indexAmp = src.indexOf('&');
	if (indexAmp < 0)
		indexAmp = src.length;
	var endIndexImgParamUrl = indexAmp;// - indexQ;
	var imgParamUrl = src.substring(startParam1Index , endIndexImgParamUrl );
	
	var indexDotMil = src.indexOf(".mil");
	var endIndexPrefix = indexDotMil + ".mil".length;
	var prefix = src.substring(0, endIndexPrefix);
	var newsrc = prefix + imgParamUrl;
	
    return '<a href="/_layouts/imagemeta.aspx?image=' + newsrc + '"><img src="' + url + '" alt="' + alt + '" width="250" /></a>'; };


// -----------------------------------------------------------------------
// Next-Button handling...
// -----------------------------------------------------------------------
var nextOver = function() {
    $(this).attr("src", "/_assets/img/next-over.gif");
};
var nextOut = function() {
    $(this).attr("src", "/_assets/img/next.gif");
};
var nextDown = function() {
    $(this).attr("src", "/_assets/img/next-down.gif");
    var jch = $("ul.jcarousel-list").height();
	$(".jcarousel-clip").height(jch);

};
function nextButtonStateHandler(carousel, button, enabling) {
	if (enabling) {
		$(button).attr("src", "/_assets/img/next.gif")
			.bind("mouseover", nextOver)
			.bind("mouseout", nextOut)
			.bind("mousedown", nextDown);
	} else {
		$(button).attr("src", "/_assets/img/next-disabled.gif")
			.unbind("unmouseover", nextOver)
			.unbind("unmouseout", nextOut)
			.unbind("unmousedown", nextDown);
	};
};


// -----------------------------------------------------------------------
// Prev-Button handling
// -----------------------------------------------------------------------
var prevOver = function() {
    $(this).attr("src", "/_assets/img/prev-over.gif");
};
var prevOut = function() {
    $(this).attr("src", "/_assets/img/prev.gif");
};
var prevDown = function() {
    $(this).attr("src", "/_assets/img/prev-down.gif");
    var jch = $("ul.jcarousel-list").height();
	$(".jcarousel-clip").height(jch);
};
function prevButtonStateHandler(carousel, button, enabling) {
	if (enabling) {
		$(button).attr("src", "/_assets/img/prev.gif")
			.bind("mouseover", prevOver)
			.bind("mouseout", prevOut)
			.bind("mousedown", prevDown);
	} else {
		$(button).attr("src", "/_assets/img/prev-disabled.gif")
			.unbind("unmouseover", prevOver)
			.unbind("unmouseout", prevOut)
			.unbind("unmousedown", prevDown);
	};
};

// this figures out which item is showing, does height 
function mm_itemFirstInHandler(carousel, item, idx, state) {
    var jci = '.jcarousel-item-' + idx;
    var jcih = $(jci).height();
	$(".jcarousel-clip").height(jcih);
};


// -----------------------------------------------------------------------
// The on-ready page load items.
// -----------------------------------------------------------------------
$(document).ready(function() {
	$("#mycarousel").jcarousel({
		itemVisible: 1,
		itemScroll: 1,
		wrap: true,
		loadItemHandler: loadItemHandler,
		nextButtonStateHandler: nextButtonStateHandler,
		prevButtonStateHandler: prevButtonStateHandler,
		itemFirstInHandler: mm_itemFirstInHandler
	});
	
	var jch = $("ul.jcarousel-list li").height();
	$(".jcarousel-clip").height(jch);
	
	// Start of figuring out maximum height.
	var maxh = 0;
	$(".jcarousel-list li img").each(function(i) { 
		if ($(this).height() > maxh) 
			maxh = $(this).height(); 
	});	
	
	var imgHeight = $(".jcarousel-list li img").height();
	$(".jcarousel-next").css("top", imgHeight);
	$(".jcarousel-prev").css("top", imgHeight);
	
	var imgWidth = $(".jcarousel-list li img").width();
	imgWidth = imgWidth + 2;
	$(".jcarousel-clip").width(imgWidth);
	$(".jcarousel-list li").width(imgWidth);
});
