﻿// jQuery plug-ins
// 3/6/2010, E.Taylor (http://www.aceoftstudios.com)
// Not for redistribution.

jQuery.fn.fadeToggle = function(speed) {
	return this.each(function() {
		if (jQuery(this).is(':visible')) {
			jQuery(this).fadeOut(speed);
		}
		else {
			jQuery(this).fadeIn(speed);
		}
	});
};
jQuery.fadeSwitch = function(selector1, selector2, speed) {
	jQuery(selector1).fadeToggle(speed);
	jQuery(selector2).fadeToggle(speed);
};

// URLEncode/Decode functions
// http://plugins.jquery.com/project/URLEncode
jQuery.extend({ URLEncode: function(c) {
	var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
	while (x < c.length) {
		var m = r.exec(c.substr(x));
		if (m != null && m.length > 1 && m[1] != '') {
			o += m[1]; x += m[1].length;
		} else {
			if (c[x] == ' ') o += '+'; else {
				var d = c.charCodeAt(x); var h = d.toString(16);
				o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
			} x++;
		} 
	} return o;
},
	URLDecode: function(s) {
		var o = s; var binVal, t; var r = /(%[^%]{2})/;
		while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
			b = parseInt(m[1].substr(1), 16);
			t = String.fromCharCode(b); o = o.replace(m[1], t);
		} return o;
	}
});

// Gallery
function addPhotoToGallery(photoId, selectorPanelId, rootUrl) {
	
	// (int photoid, [int galleryid], [string name], [string description])
	var url = rootUrl + 'TNLogHomes/scripts/async/AddPhotoToGallery.aspx?photoid=' + photoId;
	var panel = jQuery('#' + selectorPanelId);
	
	if (panel.find('.existingGallery').is(':visible')) {
		// Existing
		var galleryId = panel.find('.existingGallery select').val();
		url += '&galleryid=' + galleryId;
	}
	else {
		// New
		var galleryName = panel.find('.newGallery .txt').val();
		if (galleryName == '') {
			alert('Please enter a name for the new gallery.');
			panel.find('.newGallery .txt')[0].focus();
			return;
		}
		url += '&name=' + jQuery.URLEncode(galleryName);
	}

	// Show 'wait' panel
	panel.find('.existingGallery').fadeOut();
	panel.find('.newGallery').fadeOut();
	panel.find('.wait').fadeIn(300, function() {
		// Execute ajax call
		jQuery.post(url, function(data) {
			// Show results
			panel.find('.wait').fadeOut();
			if (data.substring(0, 1) == '1') {
				// Success
				panel.slideUp();
				panel.prev().attr('onclick', '').find('p').text('Added!');
			}
			else {
				panel.find('.results').fadeIn().find('p').text('Error: ' + data);
			}
		});
	});
}

// Plan
function addPlanToLodge(planId, buttonId, rootUrl) {
	
	var url = rootUrl + 'TNLogHomes/scripts/async/AddPlanToLodge.aspx?planid=' + planId;

	// Show 'wait' message
	jQuery('#' + buttonId).children('p').text('Please wait...');

	// Execute ajax call
	jQuery.post(url, function(data) {
		if (data.substring(0, 1) == '1') {
			// Success
			jQuery('#' + buttonId).attr('onclick', '').children('p').text('Added!');
		}
		else {
			jQuery('#' + buttonId).text('Error');
		}
	});
}

function removePlanFromLodge(planId, buttonId, rootUrl) {
	
	var url = rootUrl + 'TNLogHomes/scripts/async/RemovePlanFromLodge.aspx?planid=' + planId;

	// Show 'wait' message
	jQuery('#' + buttonId).children('p').text('Please wait...');

	// Execute ajax call
	jQuery.post(url, function(data) {
		if (data.substring(0, 1) == '1') {
			// Success
			jQuery('#' + buttonId).attr('onclick', '').children('p').text('Removed');
		}
		else {
			jQuery('#' + buttonId).text('Error');
		}
	});
}
