function Picture(){
	AjaxModule.apply(this, arguments); // superclass constructor
	this.emitters.imgID = new CallbackList();
	this.emitters.srcID = new CallbackList();
	this.rankHandler = '/mod/v3/pict/pictureRank.asp';
	this.uploadHandler = '/mod/v3/pict/pictUplo.asp';
	this.minRankInterval = 24; //hours
	this.slideShowMode = 0;
	this.slideShowInterval = 8000;
	this.slideShowTurnedOff = 0;
	this.slideShowTimeoutRef = "";
}
function Picture_load() {
	AjaxModule.prototype.load.apply(this, arguments);
}
function Picture_doOnRequestsComplete(request) {
	if (this._xmlDoc) {
		this.imgID = this.xsltParams['selImgID'];
	}
	AjaxModule.prototype.doOnRequestsComplete.apply(this, arguments);
	if (this._xmlDoc) {
		this.emitters.imgID.invoke(this.xsltParams['selImgID'], this);
		this.emitters.srcID.invoke(this.imgID, this);
	}
	this.acquireRankTools();
}
function Picture_rank(ranking) {
	if (!this._rankSubmitting) {
		new Ajax.Request(
			this.rankHandler,
			{
				method: 'get',
				parameters: 'img_id=' + this.imgID + '&ranking=' + ranking,
				onComplete: this.doOnRanked.bind(this, ranking),
				onFailure: this.doOnFailure.bind(this),
				onException: this.doOnException.bind(this)
			});
		this._rankSubmitting = true;
	}
}
function Picture_doOnRanked(ranking, request) {
	this._rankSubmitting = false;
	if (request.responseXML) {
		var node = request.responseXML.documentElement;
		if (node.getAttribute("success")) {
			var expire = new Date();
			expire.setTime(expire.getTime() + Math.round(this.minRankInterval * 60 * 60 * 1000));
			setCookie("pictureRank" + this.imgID, ranking, expire, "/");
			this.showUserRanking(ranking);
		}
	}
}
function Picture_acquireRankTools() {
	var contEls = Selector.findChildElements(this.htmlContEl, $A(['.rankTool']));
	this._rankTools = contEls.collect(function(contEl) {
		var rankTool = {
			contEl: contEl,
			rankIt: {
				contEl: Selector.findChildElements(contEl, $A(['.rankIt']))[0],
				buttons: []
			},
			ranked: {contEl: Selector.findChildElements(contEl, $A(['.ranked']))[0]},
			noCookies: {contEl: Selector.findChildElements(contEl, $A(['.noCookies']))[0]},
			hlSrc: Selector.findChildElements(contEl, $A(['img.rk0']))[0].src
		};
		for (var i = 0; i < 5; i++) {
			rankTool.rankIt.buttons[i] = {img: Selector.findChildElements(rankTool.rankIt.contEl, $A(['img.rk' + (i + 1)]))[0]};
			rankTool.rankIt.buttons[i].origSrc = rankTool.rankIt.buttons[i].img.src;
		}
		return rankTool;
	});
	var ranking = getCookie("imgRank" + this.imgID);
	if (ranking) this.showUserRanking(parseInt(ranking));
	else if (!navigator.cookieEnabled) this.showUserRanking(-1);
	else this._rankTools.each(function(rankTool){
		rankTool.rankIt.contEl.style.display = '';
		rankTool.ranked.contEl.style.display = 'none';
		rankTool.noCookies.contEl.style.display = 'none';
	});
}
function Picture_showUserRanking(ranking) {
	if (ranking >= 0) this._rankTools.each(function(rankTool){
		for (var i = 0; i < ranking; i++) {
			Selector.findChildElements(rankTool.ranked.contEl, $A(['img.rk' + (i + 1)]))[0].src = rankTool.hlSrc;
		}
		rankTool.rankIt.contEl.style.display = 'none';
		rankTool.ranked.contEl.style.display = '';
		rankTool.noCookies.contEl.style.display = 'none';
	});
	else this._rankTools.each(function(rankTool){
		rankTool.rankIt.contEl.style.display = 'none';
		rankTool.ranked.contEl.style.display = 'none';
		rankTool.noCookies.contEl.style.display = '';
	});

}
function Picture_rankToolHighlight(ranking) {
	this._rankTools.each(function(rankTool){
		rankTool.rankIt.buttons.each(function(button, i){
			if (i < ranking) button.img.src = rankTool.hlSrc;
			else button.img.src = button.origSrc;
		});
	});
}
function Picture_formSubmit(form) {
	if (!this._formSubmitting) {
		var formValues = Form.serialize(form, true);
		var errEl;
		var errorOccured = false;

		errEl = Selector.findChildElements(form, $A(['.author_name .error']))[0]
		if (formValues['author_name'].match(/^\W*$/)) {
			if (errEl) errEl.style.display = '';
			errorOccured = true;
		}
		else if (errEl) errEl.style.display = 'none';

		errEl = Selector.findChildElements(form, $A(['.author_email .error']))[0]
		if (!formValues['author_email'].match(/^[\w\.-]+@[\w\.-]+\.\w{2,4} *$/)) {
			if (errEl) errEl.style.display = '';
			errorOccured = true;
		}
		else if (errEl) errEl.style.display = 'none';

		errEl = Selector.findChildElements(form, $A(['.text .error']))[0]
		if (formValues['text'].match(/^\W*$/)) {
			if (errEl) errEl.style.display = '';
			errorOccured = true;
		}
		else if (errEl) errEl.style.display = 'none';

		if (errorOccured) return false;

		new Ajax.Request(
			this.formHandler,
			{
				method: 'post',
				parameters: this.formHandlerParams.merge(formValues),
				onComplete: this.doOnFormSubmitted.bind(this),
				onFailure: this.doOnFailure.bind(this),
				onException: this.doOnException.bind(this)
			});
		this._formSubmitting = true;
	}
	return false;
}
function Picture_doOnFormSubmitted(request) {
	this._formSubmitting = false;
	if (request.responseXML) {
		this._allItems.unshift(cloneDomNode(this._xmlDoc, request.responseXML.documentElement));
		this.load(false, false);
		var el = $(this.uid + '_' + request.responseXML.documentElement.getAttribute('id'));
		if (el) Element.scrollTo(el);
	}
}
function Picture_changeSelectedPicture(selImgId) {
	this.xsltParams["selImgID"] = selImgId;
	this.load(false,false);
}
function Picture_toggleSlideShow(moduleInstance) {
	if (this.slideShowTurnedOff == 1) {
		this.slideShowTimeoutRef = window.setTimeout("if ("+moduleInstance+".slideShowTurnedOff==0) {"+moduleInstance+".load(false,false);};",this.slideShowInterval);	
		this.slideShowTurnedOff = 0;
		this.xsltParams["selImgID"] = this.slideShowCurrentImageID;
		this.xsltParams["slideShowTurnedOff"] = 0;
		this.load(false,false);
	}
	else {
		clearTimeout(this.slideShowTimeoutRef);
		this.slideShowTimeoutRef = "";
		this.slideShowTurnedOff = 1	;
		this.xsltParams["selImgID"] = this.slideShowCurrentImageID;
		this.xsltParams["slideShowTurnedOff"] = 1;
		this.load(false,false);
	}
}
Object.extend(Picture.prototype, AjaxModule.prototype);
Picture.prototype.load = Picture_load;
Picture.prototype.doOnRequestsComplete = Picture_doOnRequestsComplete;
Picture.prototype.rank = Picture_rank;
Picture.prototype.doOnRanked = Picture_doOnRanked;
Picture.prototype.acquireRankTools = Picture_acquireRankTools;
Picture.prototype.rankToolHighlight = Picture_rankToolHighlight;
Picture.prototype.showUserRanking = Picture_showUserRanking;
Picture.prototype.doOnFormSubmitted = Picture_doOnFormSubmitted;
Picture.prototype.formSubmit = Picture_formSubmit;
Picture.prototype.changeSelectedPicture = Picture_changeSelectedPicture;
Picture.prototype.toggleSlideShow = Picture_toggleSlideShow;


function PictureStatic() {
	Picture.apply(this, arguments); // superclass constructor
	AjaxModuleStatic.apply(this, arguments); // superclass constructor
}
function PictureStatic_doOnRequestsComplete() {
	AjaxModuleStatic.prototype.doOnRequestsComplete.apply(this, arguments);
	this.acquireRankTools();
}
function PictureStatic_processStaticContent() {
	AjaxModuleStatic.prototype.processStaticContent.apply(this, arguments);
	this.acquireRankTools();
}
Object.extend(PictureStatic.prototype, Picture.prototype);
Object.extend(PictureStatic.prototype, AjaxModuleStatic.prototype);
PictureStatic.prototype.doOnRequestsComplete = PictureStatic_doOnRequestsComplete
PictureStatic.prototype.processStaticContent = PictureStatic_processStaticContent;

