// jquery.photopane.js by Derek Moseley, DerekM07@Gmail.com

// Resize Delay -- Moved to common.js
/*var delay = (function() {
	var timers = {};
	return function(callback, ms, uniqueId) {
		if (timers[uniqueId]) {
			clearTimeout (timers[uniqueId]);
		}
		timers[uniqueId] = setTimeout(callback, ms);
	};
})();*/

// Vertical centering for captions
function adjustCaptions() {
	$("p#ppw_captiontext").css({ "margin-top": "-" + (parseInt( $("p#ppw_captiontext").outerHeight() ) / 2) + "px", "top": "50%" });
};

$(document).ready(function() {
	
	// Default settings
	var maxpanes = 10; // Max Supported Panes
	var cpr = 4; // Captions Per Row
	
	// Resize settings
	$(window).resize(function() {
		delay(function() {
			adjustCaptions();
		}, 500, "photopane");
	});
	
	$.get('js/photopane.xml', function(xmldata) {
		var imgcount = $('img', xmldata).length;
		
		$('img', xmldata).text(function() {
			var img = $(this);
			
			if ( $("div#photopane").data("paneid") == "0" ) {
				
				// Display first image
				$("div#photopane img:first").attr( "src", $(img).attr("url") );
				$("div#photopane").data( "paneid", $(img).find("paneid").text() );
				
				$('div#photopane > div[data-paneid="' + $("div#photopane").data("paneid") + '"]') // Display first pane contents
					.css({ "display": "block", "visibility": "visible" });
				
				$("div#ppw_photocredit") // Display first photo credit
					.html( $(img).find("showcredit").text() ? $(img).find("credit").text() : "" );
				
				// Display photo captions
				$('img', xmldata).each(function() {
					var img = $(this);
					
					if ( parseInt($(img).find("paneid").text()) <= maxpanes ) {
						var caption = $("<span />").appendTo("p#ppw_captiontext");
						$(caption)
							.html( $(img).find("caption").text() )
							.data( "paneid", $(img).find("paneid").text() )
							.css({
								"color": "rgba(255,255,255,1)",
								"cursor": $(caption).data("paneid") == $("div#photopane").data("paneid") ? "default" : "pointer",
								"text-decoration": $(caption).data("paneid") == $("div#photopane").data("paneid") ? "underline" : "none"
							});
						if ( parseInt($(img).find("paneid").text()) < imgcount ) {
							$(caption).after( $(img).find("paneid").text() % cpr === 0 ? "<br />" : " &nbsp; | &nbsp; ");
						}
					}
					
					$(caption).delegate($(this), "click", function(swap) {
						if ( $(this).data("paneid") != $("div#photopane").data("paneid") ) {
							
							if ( swap.hasOwnProperty('originalEvent') ) { // User Click
								clearInterval(swappane);
							}
							
							$("div#photopane")
								.data( "paneid", $(this).data("paneid") );
							
							$(this) // Stylize photo captions
								.css({ "cursor": "default", "text-decoration": "underline" })
								.siblings("span").css({ "cursor": "pointer", "text-decoration": "none" });
							
							$("div#photopane > div") // Hide last pane contents and photo credit
								.not("#ppw_captionbox")
								.css({ "display": "none", "visibility": "hidden" });
							
							$("div#photopane img:first").fadeOut(function() { // Hide last photo
								$("div#photopane img:first").attr( "src", $(img).attr("url") ).load(function() {
									$("div#photopane img:first").fadeIn(function() { // Show new photo
										$('div#photopane > div[data-paneid="' + $("div#photopane").data("paneid") + '"]') // Show new pane contents
											.css({ "display": "block", "visibility": "visible" });
										$("div#ppw_photocredit") // Show new photo credit
											.html( $(img).find("showcredit").text() ? $(img).find("credit").text() : "" )
											.css({ "display": "block", "visibility": "visible" });
									});
								});
							});
						}
						return false;
					});
					
					$("div#photopane form, div#photopane input, div#photopane textarea").delegate($(this), "click", function() {
						clearInterval(swappane);
					});
				});
				adjustCaptions();
			}
		});
		
		// Photopane automatic content swaps
		var nextpane = parseInt($("div#photopane").data("paneid"));
		var swappane = setInterval(function() {
			nextpane = parseInt($("div#photopane").data("paneid")) + 1;
			if (nextpane > imgcount) {
				nextpane = 1;
				clearInterval(swappane);
			}
			if (nextpane <= imgcount) {
				$("p#ppw_captiontext span").each(function() {
					if ( $(this).data("paneid") == nextpane ) {
						$(this).click();
					}
				});
			}
		}, 12500);
		
	});
});
