

gfPageLoaded = 0;
gf_loadingImg = '';


// On Ready, On Load, ...
$(document).ready(function() { gfGetPictures(gfDate); });
$(window).load(function () { gfPageLoaded = 1; });




function gfGetPictures(gfDate) {
	//$("#gfAttachedPic").hide();
	//$("#gfAttachedPicNavigation").hide();
	//$("#gfDayNavigation").hide();

	$("#gfLoading").html(gf_loadingImg + 'Loading gallery...');
	
	gfReq = $.ajax({
				   
		type: "GET",
		url: gfWpdir + "ajax-attached.php",
		data: "gf_reqdate=" + gfDate,
		success: function(data) {
	
			xmlResult = data;
			
			// Say picture count	
			var gfSayPicCount = $(xmlResult).find('thumbCount').eq(0).text();
			$("#gfDayNavigation > li.picCount").text(gfSayPicCount);
			$("#gfDayNavigation > li.prev").html($(xmlResult).find('prevDayButton').eq(0).text());
			$("#gfDayNavigation > li.next").html($(xmlResult).find('nextDayButton').eq(0).text());
			
			//$("#gfDayNavigation div").fadeIn("slow");
		
			// Load text & stuff here
			$("#gfAttachedPicNavigation > li.titleBlock").html($(xmlResult).find('postTitle').eq(0).text());
		
			// Load thumbs
			$("#gfLoading").html(gf_loadingImg + 'Loading thumbnails...');
			
			var preloadThumbs = new Array();
			for (var n = 0; n < xmlResult.getElementsByTagName('relPath').length; n++) {
				preloadThumbs[n] = new Image();
				if (n == xmlResult.getElementsByTagName('relPath').length - 1) {
					$(preloadThumbs[n]).load(function () {
						gfThumbsLoaded();
					});		
				}
				preloadThumbs[n].src = xmlResult.getElementsByTagName('relPath')[n].firstChild.data + '&w=56&h=42&zc=1&q=30';
			}	
	
			gfReq = null;
	
		},
		error:function(xhr,err){
			alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
			alert("responseText: "+xhr.responseText);
		}

	});
	
}


function gfThumbsLoaded() {
	$("#gfAttachedPicNavigation > li.prev").html("&nbsp;");
	
	var gfTest = $(xmlResult).find('relPath').length;
	if (gfTest > 1) {

		var gfNextPicPath = $(xmlResult).find('relPath').eq(1).text();
		var gfThumbTitle = $(xmlResult).find('thumbTitle').eq(1).text();
		var gfNextPic = $('<a href="javascript:gfGetPic(1)" title="' + gfThumbTitle + '"><img src="' + gfNextPicPath + '&w=56&h=42&zc=1&q=30" alt="" /><br />next &raquo;</a>');
	} else {
		var gfNextPic = '&nbsp;';
	}


	$("#gfAttachedPicNavigation > li.next").html(gfNextPic);
	//$("#gfAttachedPicNavigation").slideDown("slow");

	$("#gfLoading").html(gf_loadingImg + 'Loading first picture...');

	var gfBigPicPath = $(xmlResult).find('relPath').eq(0).text();
	var gfBigAlt = $(xmlResult).find('alttitle').eq(0).text();
	var gfBigGuid = $(xmlResult).find('targetPost').eq(0).text();
	var gfBigPic = $('<a href="' + gfBigGuid + '" title="' + gfBigAlt + '"><img src="' + gfBigPicPath + '&w=480&aoe=1&fltr=wmi|../watermark.png|BR|100|10|3&q=85" alt="' + gfBigAlt + '" /></a>');
	var image = new Image();

	$(image).load(function () {
		$("#gfAttachedPic div").html(gfBigPic);
		$("#gfAttachedPic div").fadeIn("slow");
		$("#gfLoading").html('&nbsp;');
		gfPreloadPicWait(1);
	});
	
	image.src = gfBigPicPath + '&w=480&aoe=1&fltr=wmi|../watermark.png|BR|100|10|3&q=85';
}



function gfPreloadPicWait(picnum) {
	$("#gfLoading").html(gf_loadingImg + 'Waiting for complete page load...');
	if (gfPageLoaded == 1) {
		gfPreloadPic(picnum)
	} else {
		$(window).load(function () {
			gfPreloadPic(picnum);
		});
	}
}
function gfPreloadPic(picnum) {

	if (picnum < xmlResult.getElementsByTagName('relPath').length) {
		$("#gfLoading").html(gf_loadingImg + ' Preloading picture ' + (picnum+1));
		var gfBigPicPath = $(xmlResult).find('relPath').eq(picnum).text();
		var gfBigPic = $('<a href="javascript:void(0)"><img src="' + gfBigPicPath + '&w=480&aoe=1&fltr=wmi|../watermark.png|BR|100|10|3&q=85" alt="" /></a>');
		var image = new Image();
		$(image).load(function () {
			gfPreloadPic(picnum+1);
		});		
		image.src = gfBigPicPath + '&w=480&aoe=1&fltr=wmi|../watermark.png|BR|100|10|3&q=85';
	} else {
		$("#gfLoading").html('&nbsp;');
	}
		
}



function gfGetPic(picKey) {
	prevPic = picKey - 1;
	var bigPic = picKey;
	picKey = picKey + 1;

	var totalPic = xmlResult.getElementsByTagName('relPath').length;

	if (picKey < totalPic) {
		var gfNextPicPath = $(xmlResult).find('relPath').eq(picKey).text();
		var gfThumbTitle = $(xmlResult).find('thumbTitle').eq(picKey).text();
		var gfNextPic = $('<a href="javascript:gfGetPic(' + picKey + ')" title="' + gfThumbTitle + '"><img src="' + gfNextPicPath + '&w=56&h=42&zc=1&q=30" alt="" /><br />next &raquo;</a>');
		$("#gfAttachedPicNavigation > li.next").html(gfNextPic);
	} else {
		$("#gfAttachedPicNavigation > li.next").html("&nbsp;");
	}

	if (prevPic >= 0) {
		var gfPrevPicPath = $(xmlResult).find('relPath').eq(prevPic).text();
		var gfThumbTitle = $(xmlResult).find('thumbTitle').eq(prevPic).text();
		var gfPrevPic = $('<a href="javascript:gfGetPic(' + prevPic + ')" title="' + gfThumbTitle + '"><img src="' + gfPrevPicPath + '&w=56&h=42&zc=1&q=30" alt="" /><br />&laquo; back</a>');
		$("#gfAttachedPicNavigation > li.prev").html(gfPrevPic);
	} else {
		$("#gfAttachedPicNavigation > li.prev").html("&nbsp;");
	}

	$("#gfAttachedPic div").fadeOut("slow", function () {
													
		$("#gfAttachedPicNavigation > li.titleBlock").html($(xmlResult).find('postTitle').eq(bigPic).text());

		var gfBigPicPath = $(xmlResult).find('relPath').eq(bigPic).text();
		var gfBigAlt = $(xmlResult).find('alttitle').eq(bigPic).text();
		var gfBigGuid = $(xmlResult).find('targetPost').eq(bigPic).text();
		var gfBigPic = $('<a href="' + gfBigGuid + '" title="' + gfBigAlt + '"><img src="' + gfBigPicPath + '&w=480&aoe=1&fltr=wmi|../watermark.png|BR|100|10|3&q=85" alt="' + gfBigAlt + '" /></a>');
		//var gfBigPic = $('<a href="' + gfBigGuid + '" title="' + gfBigAlt + '"><img src="' + gfBigPicPath + '&w=480&aoe=1&fltr=wmt|Test|2|BR|ffffff||80|||||&q=85" alt="' + gfBigAlt + '" /></a>');
		var image = new Image();
	
		$(image).load(function () {
			$("#gfAttachedPic div").html(gfBigPic);
			$("#gfAttachedPic div").fadeIn("slow");
		});		
	
		image.src = gfBigPicPath + '&w=480&aoe=1&fltr=wmi|../watermark.png|BR|100|10|3&q=85';
	});
}



function gfGetGallery(gfDate) {
	//$("#gfDayNavigation").slideUp("slow", function () {
	//	$("#gfAttachedPicNavigation").slideUp("slow", function () {
			$("#gfAttachedPic div").fadeOut("slow", function () {
				gfGetPictures(gfDate);
			});
	//	});
	//});
}



