var party = {
	sRoot: ((window.location.href.toUpperCase().indexOf('WWW.') != -1) ? 'http://www.partyanimalsphotos.co.uk/' : 'http://partyanimalsphotos.co.uk/'),
	iRoot: ((window.location.href.toUpperCase().indexOf('WWW.') != -1) ? 'http://www.partyanimalsphotos.co.uk/images/' : 'http://partyanimalsphotos.co.uk/images/'),
	gRoot: ((window.location.href.toUpperCase().indexOf('WWW.') != -1) ? 'http://www.partyanimalsphotos.co.uk/images/galleries/' : 'http://partyanimalsphotos.co.uk/images/galleries/'),
	galleries: null,
	arrMonths: ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
};

// ==============================

var p_loadInterface = function() {
	// add top galleries to list
	$('#ins-recent-images').empty();
	$(party.galleries).filter(':lt(14)').each(function(i) {
		var fname = party.gRoot + $(this).attr("loc") + "/" + $(this).attr("image");
		$('#ins-recent-images').append('<img id="thumb-' + $(this).attr("loc") + '" src="' + fname + '" alt="" />');
		$('#ins-recent-images').append('<div class="thumb-info" id="info-' + $(this).attr("loc") + '">' + $(this).attr("title") + ' - ' + $(this).attr("desc") + '</div>');
	});
	// add random gallery and click event
	//$('#random-images').empty();
	//var rndgallery = $(party.galleries[Math.round(Math.random() * (party.galleries.length - 1))]);
	//$('#random-images').append('<img id="thumb-' + $(rndgallery).attr("loc") + '" src="' + party.gRoot + $(rndgallery).attr("loc") + "/" + $(rndgallery).attr("image") + '"alt="' + $(rndgallery).attr("title") + '" />');
	//$('#random-images').append('<p>' + $(rndgallery).attr("title") + '</p>');
	//$('#random-images img').each(function() {
		//var gName = $(this).attr("id").substr(6);
		//$(this).click(function() { p_openGalleryNamed(gName); });
	//});
	// add click events to recent galleries
	$('#ins-recent-images img').each(function() {
		var gName = $(this).attr("id").substr(6);
		$(this).click(function() { p_openGalleryNamed(gName); });
	});
	// add info boxes for recent galleries
	$('#ins-recent-images img').each(function() {
		var gName = $(this).attr("id").substr(6);
		$(this).bind("mousemove", function(e) {
			$('#info-' + gName).css("top", parseInt(e.pageY + 4) + 'px');
			$('#info-' + gName).css("left", parseInt(e.pageX + 4) + 'px');
		});
		$(this).bind("mouseout", function(e) {
			$('#info-' + gName).hide();
		});
		$(this).bind("mouseenter", function(e) {
			$('#info-' + gName).show();
		});
		$('#info-' + gName).bind("mousemove", function(e) {
			$('#info-' + gName).css("top", parseInt(e.pageY + 4) + 'px');
			$('#info-' + gName).css("left", parseInt(e.pageX + 4) + 'px');
		});
	});
	
	// set up change event for keyword search
	$('#find-keyword').keyup(function() {
		var searchTerm = $('#find-keyword').val();
		if(searchTerm.length > 1) {
			$('#find-results').fadeIn("fast");
			p_search(searchTerm);
		} else {
			$('#find-results').fadeOut("fast");
		}
	});
	$('#find-keyword').focus(function() {
		if($('#find-keyword').val().length > 1) {
			$('#find-results').fadeIn("fast");
		}
	});
	$('#find-keyword').click(function(event) { event.stopPropagation(); });
	$('#find-results').click(function(event) { event.stopPropagation(); });
	// $('#find-keyword').blur(function() {
	//	$('#find-results').fadeOut("fast");
	// });
	$('body').click(function() { $('#find-results').fadeOut("fast"); });
	// prev, back, share, close buttons
	$('#prevBtn').click(function() { s_prevSlide(); });
	$('#nxtBtn').click(function() { s_nextSlide(); });
	$('#shareBtn').click(function() { $('#g-share').fadeIn("fast"); });
	$('#shareCloseBtn').click(function() { $('#g-share').fadeOut("fast"); });
	// recent, search choosers
	$('#searchBtn').click(function() { $('#recent').fadeOut("fast", function() { $('#find').fadeIn("fast", function() { $('#find-keyword').focus(); }); }) });
	$('#recentBtn').click(function() { $('#find').fadeOut("fast", function() { $('#recent').fadeIn("fast"); }) });
	
	// clear search keywords
	$('#find-keyword').val("");
	
	// functionality for opening/closing mms/email info
	$('#closeSendBtn').click(function() { $('#send-box').fadeOut("fast"); });
	$('#sendBtn').click(function() { $('#send-box').fadeIn("fast"); });
	
	// gallery total
	$('.galleries-total').append(' (' + party.galleries.length + ')');
};

var p_search = function(keywords) {
	var resultsFound = 0;
	
	$('#find-results').empty();
	
	$(party.galleries).each(function(i) {
		var thisTitle = $(this).attr("title");
		var thisDesc = $(this).attr("desc");
		var thisCaption = $(this).attr("caption");
		if( (thisCaption.toUpperCase().indexOf(keywords.toUpperCase()) != -1) || (thisDesc.toUpperCase().indexOf(keywords.toUpperCase()) != -1) || (thisTitle.toUpperCase().indexOf(keywords.toUpperCase()) != -1) ) {
			var resultHTML = '<div class="result-container" id="result-' + $(this).attr("loc") + '">';
			resultHTML += '<div class="pic-container"><img src="' + party.gRoot + $(this).attr("loc") + '/' + $(this).attr("image") + '" alt="' + $(this).attr("title") + '" /></div>';
			resultHTML += '<div class="txt-container"><p><strong>' + $(this).attr("title") + ' (' + p_dateAsReadable($(this).attr("date")) + ')</strong> - ' + $(this).attr("desc") + '</p></div></div>';
			$('#find-results').append(resultHTML);
			resultsFound++;
		}
	});
	
	if(resultsFound == 0) { 
		$('#find-results').append('<p class="title">No galleries found.</p>');
	}
	
	$('#find-results div.result-container').each(function() {
		var gName = $(this).attr("id").substr(7);
		$(this).click(function() { 
			p_openGalleryNamed(gName);
			$('#find-results').fadeOut("fast");
			$('#find-keyword').val("");
		});
		$(this).bind("mouseenter", function() { $(this).css("background-color", "#1F5389"); });
		$(this).bind("mouseleave", function() { $(this).css("background-color", "#666666"); });
	});
};

var p_gallery_load_callback = function(loc) {
	$('#g-buttons').show();
	$('#share-link').val(party.sRoot + '?gallery=' + loc);
	$('#facebook-link').attr('href', 'http://www.facebook.com/sharer.php?u=' + party.sRoot + '?gallery=' + loc);
	$('#stumble-link').attr('href', 'http://www.stumbleupon.com/submit?url=' + party.sRoot + '?gallery=' + loc);
	$('#digg-link').attr('href', 'http://digg.com/submit?url=' + party.sRoot + '?gallery=' + loc);
	$('#g-share').fadeOut('fast');
	pageTracker._trackEvent('Galleries', 'Load', loc);
};

var p_openGalleryNamed = function(s) {
	$('#g-buttons').hide();
	var matched = $(party.galleries).filter('[loc=' + s + ']');
	s_loadGallery($(matched)[0], p_gallery_load_callback);
};

var p_openGallery = function(n) {
	$('#g-buttons').hide();
	s_loadGallery($(party.galleries)[n], p_gallery_load_callback);
};

var p_dateAsReadable = function(dateStr) {
	return dateStr.substr(6, 2) + " " + party.arrMonths[parseInt(dateStr.substr(4, 2), 10)] + " " + dateStr.substr(0, 4);
};

// on load =============================================

$(document).ready(function() {
	// set up gallery browser
	s_setup({
		sRoot: party.sRoot,
		iRoot: party.iRoot,
		gRoot: party.gRoot,
		slideContainer: 'g-slides',
		slideClass: 'slide',
		captionContainer: 'g-caption',
		titleContainer: 'g-title',
		infoContainer: 'g-info'
	});
	
	// get galleries data
	$.ajax({
		  url: party.sRoot + 'galleries.xml',
		  type: "GET",
		  dataType: "xml",
		  cache: false,
		  success: function(data, txtStatus) {
		  	party.galleries = $(data).find("gallery");
			if($(party.galleries).length > 0) {
				var wlh = window.location.href;
				var requested = "";
				if(wlh.indexOf("?") != -1)
				{
					if( (wlh.indexOf("<") == -1) && (wlh.indexOf(">") == -1) && (wlh.indexOf("%3C") == -1) && (wlh.indexOf("%3E") == -1) ) {
						requested = window.location.href.split("?")[1].split("=")[1];
						p_openGalleryNamed(requested);
					}
				} else {
					p_openGallery(0);
				}
				p_loadInterface();
			} else {
				// no galleries
			}
		  },
		  error: function(req, txtStatus, errThrown) {
			// problem loading site
		  }
	});
});

// ============================================