(function () {
	var oSMPhotoViewer = new PhotoViewer(),
		aImg = [],
		aA = [],
		iToolbarDockIndex; 
	
	oSMPhotoViewer.email = function () {
		var photo = this.photos[this.index];
		var doc = this.win.document;
		
		var imgBaseName = new RegExp("thumb\/").test(photo.src) ? photo.src.substr(photo.src.indexOf("px-") + 3) : photo.src.substr(photo.src.lastIndexOf("/") + 1);
		var photoCaption = imgBaseName.replace(/\.jpg$/, "");
		var title = (photo.title != undefined ? photo.title : "SuperMemo 15 Screenshot Tour: Image #" + parseInt(this.index + 1) + " - " + photoCaption.replace(/_/g, " "));
		var emailAddress = this.emailAddress !== undefined ? this.emailAddress: "";
		var mailtoLink = "mailto:" + emailAddress + "?subject=" + title + "&body=" + getPhotoURL(photo.src);
		
		oSMPhotoViewer.slideShowStop();
		
		doc.location.href = mailtoLink;
	}
	
	oSMPhotoViewer.permalink = function () {
		var oImg = oSMPhotoViewer.photos[oSMPhotoViewer.index],
			sImgSrc;
		
		if (new RegExp("thumb\/").test(oImg.src)) {
			sImgSrc = oImg.src.replace(/thumb\//, "");
			sImgSrc = sImgSrc.substring(0, sImgSrc.lastIndexOf("/"));
		} else {
			sImgSrc = oImg.src;
		}
		
		oSMPhotoViewer.slideShowStop();
		
		window.open(sImgSrc);
	};
	
	oSMPhotoViewer.enableAutoPlay();
	oSMPhotoViewer.enableLoop();
	
	oSMPhotoViewer.setBorderWidth(2);

	oSMPhotoViewer.setOnClickEvent(function () {
		var oImg = oSMPhotoViewer.photos[oSMPhotoViewer.index],
			iImgIndex;
		
		if (iImgIndex = array_search(oImg.src, aImg)) {
			oSMPhotoViewer.close();
			window.location = "#" + aA[iImgIndex];				
		}  
	});
	
	$("div#bodyContent img").each(function () {
		var leadingSlash = (location.hostname.search(/\.org$/i) > -1) ? "/" : "";
		
		aImg[aImg.length] = this.src.substr(this.src.indexOf(leadingSlash + "images/"), this.src.length);
		if (aImg[aImg.length - 1] == leadingSlash + "images/f/ff/Toolbar_dock.jpg") {
			iToolbarDockIndex = aImg.length - 1;
		}
		oSMPhotoViewer.add(aImg[aImg.length - 1]);
	});
	
	$("div#contentSub").after("<div><a href=\"#\" id=\"playSlideshow\">Play slideshow</a></div>");
	$("a#playSlideshow").click(function (e) {
		oSMPhotoViewer.show(0);
		e.preventDefault();
	});
	
	$("div.center").each(function (i) {
		var j,
			image,
			paragraph;
		
		if ($(this).prev()[0].nodeName.toUpperCase() == "P") {
			j = (i > iToolbarDockIndex) ? i + 2 : i;
			$(this).prepend("<a href=\"#\" id=\"slide" + j + "\" class=\"playSlideshow\">Play slideshow</a>");			
			image = $(this).find("img");
			paragraph = $(this).prev();
			image.each(function () {
				var imgBaseName,
					toPrepend;
				
				imgBaseName = new RegExp("thumb\/").test(this.src) ? this.src.substr(this.src.indexOf("px-") + 3) : this.src.substr(this.src.lastIndexOf("/") + 1);
				toPrepend  = "<a name=\"" + imgBaseName.replace(/\.jpg$/, "").toLowerCase() + "\"></a>";
				paragraph.prepend(toPrepend);
			});
		}
	});
	
	$("a[name]").each(function () {
		aA[aA.length] = this.name;
		if (this.name == "toolbar_dock") {
			aA.splice(aA.length, 0, "toolbar_dock", "toolbar_dock", "toolbar_dock");
		}
	});
	
	$("div#bodyContent").click(function (e) {
		var target = e.target,
			hashPos = target.href.length - 1;
		
		while (target.id != "bodyContent") {
			if (((target.nodeName.toUpperCase() == "A") && (target.href.substr(hashPos) == "#")) && 
				(target.id != "playSlideshow")) {
				oSMPhotoViewer.show(parseInt(target.id.replace(/^slide/, "")));
				e.preventDefault();
				break;
			} else {
				target = target.parentNode;
			}
		}
	});
	
	function array_search (needle, haystack, argStrict) {
	    // Searches the array for a given value and returns the corresponding key if successful  
	    // 
	    // version: 908.406
	    // discuss at: http://phpjs.org/functions/array_search
	    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +      input by: Brett Zamir (http://brett-zamir.me)
	    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // *     example 1: array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'});
	    // *     returns 1: 'surname'
	
	    var strict = !!argStrict;
	    var key = '';
	
	    for (key in haystack) {
	        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
	            return key;
	        }
	    }
	
	    return false;
	}
})();
