// Initializing Shadowbox 
window.addEvent("domready", function() {
	var options = {
		resizeDuration:		0.3,
		fadeDuration:		0.2,
		animSequence:		"sync",
		flashBgColor:		"#ffffff"
		//skipSetup:			true  // because we're not going to mark up the anchor links with rel attributes
	};
	Shadowbox.init(options);
});


// Defining some wrapper functions

// Note: in this case, "gallery" means a list of images.  Each image is {url [, title]}
function setupGallery(options) {
	if (!options.className || !options.gallery) { return; }
	// there are also the optional options.skipMode and options.continuousMode
	
	var listOfImages = options.gallery.map(function(item) {
		var result = {};
		result.player = "img";
		result.title = item.title || "";
		result.content = item.url;
		
		return result;
	});

	var sbOptions = {};  // Shadowbox's options
	if (options.skipMode) { sbOptions.counterType = "skip"; }
	if (options.continuousMode) { sbOptions.continuous = true; }

	window.addEvent("domready", function() {
		$$("." + options.className).each(function(item, index) {
			item.addEvent("click", function() {
				Shadowbox.open(listOfImages, sbOptions);
				return false;  // disable the default click behaviour
			});
		});
	});
}


function setupImage(options) {
	if (!options.className || !options.url) { return; }
	
	window.addEvent("domready", function() {
		$$("." + options.className).each(function(item, index) {
			item.addEvent("click", function() {
			
				Shadowbox.open({
					player:     'img',
					title:      options.title || "",
					content:    options.url
				});
				return false;  // disable the default click behaviour
		
			});
		});
	});
}


function setupYouTube(options) {
	if (!options.className || !options.url) { return; }
	
	window.addEvent("domready", function() {
		$$("." + options.className).each(function(item, index) {
			item.addEvent("click", function() {

				Shadowbox.open({
					player:     'iframe',
					title:      options.title || "",
					content:    p_videoOnly(options.url),
					width:     options.width || 560,
					height:    options.height || 340  
				});
				return false;  // disable the default click behaviour

			});
		});
	});
}

// a function available to be called from within Flash
function openYouTube(options) {
	Shadowbox.open({
		player:     'iframe',
		title:      options.title || "",
		content:    p_videoOnly(options.url),
		width:     options.width || 560,
		height:    options.height || 340  
	});
}

function setupQuicktime(options) {
	if (!options.className || !options.url) { return; }
	
	window.addEvent("domready", function() {
		$$("." + options.className).each(function(item, index) {
			item.addEvent("click", function() {

				Shadowbox.open({
					player:     'qt',
					title:      options.title || "",
					content:    options.url,
					width:     options.width || 320,
					height:    options.height || 240  
				});
				return false;  // disable the default click behaviour

			});
		});
	});
}



// Private functions

// returns the URL to view the YouTube video only.  Note that it'll play the high-quality video by default.
function p_videoOnly(pageUrl) {
	return "http://www.youtube.com/v/" + p_parseVideoId(pageUrl) + "&autoplay=1&ap=%2526fmt%3D18";
}

// returns the value of the variable 'v' in the query string
function p_parseVideoId(url) {
	if (url.contains("?")) {
		var startOfQuery = url.lastIndexOf("?") + 1;
		var query = url.substr(startOfQuery);
		var variables = new Array();
		variables = query.split("&");
		
		var s = "v=";
		var videoId = "";
		for (var i=0; i < variables.length; i++) {
			if (variables[i].substr(0, s.length) === s) {
				videoId = variables[i].substr(s.length);
				break;
			}
		}
		return videoId;
	}
	return "";
}

/* Attaching the given onclick event handlers to either:
   - elements specified by the class name or 
   - an element specified by id
*/
/*function p_setupEventHandler(options, fn) {
	if (options.className) {
		$$("." + options.className).each(function(item, index) {
			item.addEvent("click", fn);
		});
	} else {
		$(options.id).addEvent("click", fn);
	}
}
*/