
function changeToMed(url) {
	// changes a src string to the -med image
	return url.replace(".jpg", "-med.jpg");
}

function pviewer(objs, windo) {
	first = 1;
	$each(objs, function(h) {

		var url = h.get("href");
		if (first == 1) {
			newImage(url, windo);
		}	
		h.set("href", "javascript:void(0)");
		h.addEvents({
			"click" : function() {
				newImage(url, windo);
			},
			"mouseover" : function() {
				
			}
		});
		first++;
	})
}

function pmeta(url, windo) {
	var req = new Request.JSON({url: 'http://www.stpiusri.org/js/getPhotoMeta.php?id='+url,
		method: 'get',
		onSuccess: function(response) {
			windo.theTitle.set('text', response.title);
			windo.theCaption.set('text', response.caption);
		}
	}).send();
}

function pcomments(url, windo) {
	var req = new Request.HTML({url: 'http://www.stpiusri.org/js/getPhotoComments.php',
		data: {
			"id" : url,
			"user_id" : ''
		},
		method: 'get',
		onSuccess: function(e, j, response) {
			$("pcomments").set('html', response)
		}
	}).send();
}

function newImage(url, windo) {
	var medurl = changeToMed(url);
	// create our image info object (only gets dimensions now, hence the name)
	var request = new Request.JSON({
		url : "http://www.stpiusri.org/js/getDimensions.php",
		data : {"url" : urlencode(medurl) },
		method : "get",
		onSuccess: function(dimensions) {
			var LoadImage = new Fx.Morph(windo.pframe, {
				'duration': 500,
				'link' : 'chain'
			});
			
			windo.pframe.fade('hide');
			LoadImage.loadit(url, medurl);
			LoadImage.start({
				'height' : dimensions.h,
				'width' : dimensions.w
			}).start({
				'opacity' : 100
			});
			pmeta(url, windo);
			pcomments(url, windo);
		}
	}).send();
	

}

window.addEvent("domready", function() {
	// definitions
	var objs = $$(".clickablephoto");
	var windo = $("pviewer");
	windo.photo = $("pphoto");
	windo.theTitle = $("ptitle");
	windo.theCaption = $("pcaption");
	
	// setup 
	var newel = new Element('img', {
		"id" : "pphoto-frame",
		"height" : 50,
		"width" : 50,
		"src" : "" // set later
	});
	windo.photo.set("text", "");
	windo.photo.adopt(newel);
	windo.pframe = newel;
	
	
	// link 
	var link = new Element('a', {
		"id" : "plink",
		"href" : "#"
	});
	link.wraps(windo.pframe);
	windo.plink = link;
	
	Fx.Morph.implement({
		loadit: function(url, medurl) {
			windo.pframe.set({
				"src" : medurl
			});
			windo.plink.set("href",url);
		}
	});	
	// initialize
	pviewer(objs, windo);

	// newImage(, newel);
	windo.photo.set("styles", {'display': 'block'});
	$('pviewer').set("class", 'pviewer-active');

})

function urlencode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
 
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };
 
    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();
 
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}