Lina.requireOnce("common/lina/Events.js");

var selectedCell = null;
var doAfterAlertClose = null;

var Bestellung = new function(){

	var ordered = {};
	var alertSend = false;
	var count = 0;

	var formats = {
		PRINT_9: {
				price: 0.11,
				minDimGood: [525, 700],
				minDimOpt: [1050, 1400],
				nr: 0,
				discount: {
					100: 0.09,
					200: 0.08,
					400: 0.07
				}
		},
		PRINT_10: {
				price: 0.14,
				minDimGood: [600, 800],
				minDimOpt: [1200, 1600],
				nr: 1,
				discount: {
					100: 0.11,
					200: 0.10,
					400: 0.09
				}
		},
		PRINT_13: {
				price: 0.21,
				minDimGood: [750, 1000],
				minDimOpt: [1500, 2000],
				nr: 2,
				discount: {
					100: 0.19,
					200: 0.18,
					400: 0.17
				}
		},
		PRINT_20: {
				price: 0.15,
				minDimGood: [1200, 1600],
				minDimOpt: [2400, 3200],
				nr: 3
		},
		PRINT_30: {
				price: 3.99,
				minDimGood: [1200, 1600],
				minDimOpt: [2400, 3200],
				nr: 4,
				poster: true
		},
		PRINT_40: {
				price: 6.99,
				minDimGood: [1600, 2100],
				minDimOpt: [3200, 4200],
				nr: 5,
				poster: true
		},
		PRINT_50: {
				price: 8.99,
				minDimGood: [2000, 2700],
				minDimOpt: [4000, 5400],
				nr: 6,
				poster: true
		}

	};

	var pflichtFelder = ["firstname", "lastname", "street", "zip", "city", "email"];

	var bearbeitungGebuehr = 0.99;
	var versandGebuehr1 = 1.68;
	var versandGebuehr2 = 2.71;

	var defaultItem = {
		q1: 1,
		f1: "PRINT_10",
		q2: 0,
		f2: "PRINT_13"
	};

	return {

		captchaNr: 0,

		serialize: function(){

			var ret = "";
			for(var ind in ordered){
				ret += list[ind][0]+"y"+ordered[ind].q1+"y"+ordered[ind].f1+"y"+ordered[ind].q2+"y"+ordered[ind].f2+"x";
			}
			ret = ret.substr(0, ret.length - 1);
			return ret;

		},

		toggleItem: function(ind){
			if( this.isOrdered(ind) ){
				this._removeItem(ind);
			}else{
				this._addItem(ind);
			}
		},

		_addItem: function(ind){
			if(!alertSend){
				Lina.get("maske").style.display = "block";
				Lina.get("alert").style.display = "block";
				doAfterAlertClose = Lina.Utils.Function.bind(this.toggleItem, this, ind);
				alertSend = true;
			}else{
				ordered[ind] = Lina.Utils.Object.extend({}, defaultItem);;
				count++;
				this._setCellCheckbox(ind);
				this._addEditRow(ind);
				this._setSettingUebernahme();
			}
			this.updateCounterLabel();
		},

		isQuantityPositive: function(ind){
			if( ordered[ind].q1 > 0 || ordered[ind].q2 > 0  ){
				return true;
			}
			this._removeItem(ind);
			return false;
		},

		checkQuantityForEach: function(){
			for(var ind in ordered){
				this.isQuantityPositive(ind);
			}
		},

		hasItem: function(){
			return (count > 0);
		},

		updateItem: function( index, eigenschaft, value ){
			ordered[index][eigenschaft] = value;
			this.isQuantityPositive(index);
		},

		_checkQuality: function(ind, format){

			var width = list[ind][3][0];
			var height = list[ind][3][1];

			var ret = "green";

			if( width < formats[format].minDimOpt[0]
					||
				height < formats[format].minDimOpt[1] ) {
					ret = "yellow";
					if( width < formats[format].minDimGood[0]
							||
						height < formats[format].minDimGood[1]
							) {
							ret = "red";
					}
			}

			return ret;
		},

		_getPrice: function(format, quantity){
			var price = formats[format].price;
			if(formats[format].discount){
				var masterQuantity = 1;
				for(var qnt in formats[format].discount){
					if(qnt <= quantity && masterQuantity < qnt){
						price = formats[format].discount[qnt]
					}
				}
			}
			return price;
		},

		_addEditRow: function(ind){
//			var trToCopy = Lina.get("orderEditTable").tBodies[0].rows[0];
//			if(!trToCopy){
				var tr = Lina.get("orderEditTable").tBodies[0].insertRow(0);
				tr.id = "fotoInd" + ind;
				tr.fotoInd = ind;

				var td = tr.insertCell(0);
				td.className = "bs0";
				td.innerHTML = "<a onclick='return false;' class='remI' href='#'></a>";
				td.rowSpan = 3;
				td = tr.insertCell(1);
				td.className = "bs1";
				var dim = (list[ind][3][0] > list[ind][3][1])?"width":"height";
				td.innerHTML = '<img style="' + dim + ':50px;" src="' + getLinkTo(ind, true) + '" />';
				td.rowSpan = 3;

/*
				td = tr.insertCell(2);
				td.innerHTML = 'Anzahl<br /><input id="qnt1_' + list[ind][0] + '" value="' + ordered[ind].q1 + '" /><br /><input id="qnt2_' + list[ind][0] + '" value="' + ordered[ind].q2 + '" />';
				td = tr.insertCell(3);
				td.innerHTML = 'Format<br /><select id="frm1_' + list[ind][0] + '"></select><br /><select id="frm2_' + list[ind][0] + '"></select>';
				td = tr.insertCell(4);
				var color1 = this._checkQuality(ind, ordered[ind].f1);
				var title1 = Lina.translate("myPictureGallery.qulaitaetampel." + color1, null, "myPictureGallery");
				var color2 = this._checkQuality(ind, ordered[ind].f2);
				var title2 = Lina.translate("myPictureGallery.qulaitaetampel." + color2, null, "myPictureGallery");
				td.innerHTML = '&nbsp; <br /><img title="' + title1 + '" id="qntImg1_' + list[ind][0] + '" src="/myPictureGallery/img/icon_qual_' + color1 + '.gif " /><br /><img id="qntImg2_' + list[ind][0] + '" title="' + title1 + '" src="/myPictureGallery/img/icon_qual_' + color2 + '.gif" />';
*/

				td = tr.insertCell(2);
				td.innerHTML = Lina.translate("myPictureGallery.count", null, "myPictureGallery");
				td = tr.insertCell(3);
				td.innerHTML = Lina.translate("myPictureGallery.format", null, "myPictureGallery");
				td = tr.insertCell(4);

				td = tr.insertCell(5);

				tr = Lina.get("orderEditTable").tBodies[0].insertRow(1);
				td = tr.insertCell(0);
				td.innerHTML = '<input id="qnt1_' + list[ind][0] + '" value="' + ordered[ind].q1 + '" />';
				td = tr.insertCell(1);
				td.innerHTML = '<select id="frm1_' + list[ind][0] + '"></select>';
				td = tr.insertCell(2);
				var color1 = this._checkQuality(ind, ordered[ind].f1);
				var title1 = Lina.translate("myPictureGallery.qulaitaetampel." + color1, null, "myPictureGallery");
				td.innerHTML = '<img title="' + title1 + '" id="qntImg1_' + list[ind][0] + '" src="/myPictureGallery/img/icon_qual_' + color1 + '.gif " />';

				td = tr.insertCell(3);
				td.rowSpan = 2;
				td.className = "settingsOrder";

				tr = Lina.get("orderEditTable").tBodies[0].insertRow(2);
				td = tr.insertCell(0);
				td.innerHTML = '<input id="qnt2_' + list[ind][0] + '" value="' + ordered[ind].q2 + '" />';
				td = tr.insertCell(1);
				td.innerHTML = '<select id="frm2_' + list[ind][0] + '"></select>';
				td = tr.insertCell(2);
				var color2 = this._checkQuality(ind, ordered[ind].f2);
				var title2 = Lina.translate("myPictureGallery.qulaitaetampel." + color2, null, "myPictureGallery");
				td.innerHTML = '<img title="' + title2 + '" id="qntImg2_' + list[ind][0] + '" src="/myPictureGallery/img/icon_qual_' + color2 + '.gif " />';

				var c = 0;
				for(var format in formats){
					var fmtText = Lina.translate("myPictureGallery.formatText." + format, null, "myPictureGallery");

					Lina.get("frm1_" + list[ind][0]).options[c] =
						new Option(fmtText, format, false, ordered[ind].f1 == format);
					Lina.get("frm2_" + list[ind][0]).options[c] =
						new Option(fmtText, format, false, ordered[ind].f2 == format);
					c++;
				}

				Lina.get("frm1_" + list[ind][0]).selectedIndex = formats[ordered[ind].f1].nr;
				Lina.get("frm2_" + list[ind][0]).selectedIndex = formats[ordered[ind].f2].nr;

				var f1 = function(){
					var fotoId = this.id.substr(5);
					var nr = this.id.substr(3, 1);
					var ind =   this.parentNode.parentNode.previousSibling.fotoInd;
					if(nr == 2){
						ind = this.parentNode.parentNode.previousSibling.previousSibling.fotoInd;
					}

					var formatValue = this.options[this.selectedIndex].value;
					Bestellung.updateItem(ind, "f" + nr, formatValue);
					Bestellung._updateQualityImage(ind, this.id.substr(3, 1));
				};

				Lina.get("frm1_" + list[ind][0]).onchange = f1;
				Lina.get("frm2_" + list[ind][0]).onchange = f1;

				var f2 = function(){
					var qntVal = parseInt(this.value);
					if( isNaN(qntVal) ){
						qntVal = 0;
					}
					qntVal = Math.max(0, qntVal);

					var nr = this.id.substr(3, 1);
					var ind =   this.parentNode.parentNode.previousSibling.fotoInd;
					if(nr == 2){
						ind = this.parentNode.parentNode.previousSibling.previousSibling.fotoInd;
					}
					this.value = qntVal;
					Bestellung.updateItem(ind, "q" + nr, qntVal);
				}

				Lina.get("qnt1_" + list[ind][0]).onchange = f2;
				Lina.get("qnt2_" + list[ind][0]).onchange = f2;

		},

		_updateQualityImage: function(ind, nr){
			var fotoId = list[ind][0];
			var color = this._checkQuality(ind, ordered[ind]["f" + nr]);
			Lina.get("qntImg" + nr + "_" + fotoId).src = "/myPictureGallery/img/icon_qual_" + color + ".gif";
			var title = Lina.translate("myPictureGallery.qulaitaetampel." + color, null, "myPictureGallery");
			Lina.get("qntImg" + nr + "_" + fotoId).setAttribute("title", title);
		},

		_removeItem: function(ind){
			count--;
			delete(ordered[ind]);
			this._setCellCheckbox(ind);
			var tr = Lina.get("fotoInd" + ind);
			Lina.Utils.Element.remove(tr.nextSibling);
			Lina.Utils.Element.remove(tr.nextSibling);
			Lina.Utils.Element.remove(tr);
			this._setSettingUebernahme();
			this.updateCounterLabel();
		},

		isOrdered: function(ind){
			return (ordered[ind])?true:false;
		},

		_setCellCheckbox: function(ind){
			var cell = Lina.get("cell" + ind);
			if(cell){
				setCellImg(cell);
			}
		},

		_areAllOrdered: function(){
			return (count == list.length);

		},

		updateCounterLabel: function(){
			Lina.get("orderedCount").innerHTML = count;
			Lina.get("allFotosImg").src = (this._areAllOrdered())?"/myPictureGallery/img/form_checkbox_checked.gif":"/myPictureGallery/img/form_checkbox.gif";
		},

		toggleAll: function(){
			if(this._areAllOrdered()){
				this._removeAll();
			}else{
				this._addAll();
			}
			for(var i = curPage * thumbsNumber; i < (curPage + 1) * thumbsNumber; i++){
				this._setCellCheckbox(i);
			}
		},

		_removeAll: function(){
		 	ordered = {};
			count = 0;
			Lina.Utils.Element.remove(Lina.get("orderEditTable").tBodies[0], true);
			Lina.get("orderEditTable").appendChild( document.createElement("tbody") );
			this.updateCounterLabel();
		},

		_addAll: function(){

			if(!alertSend){
				Lina.get("maske").style.display = "block";
				Lina.get("alert").style.display = "block";
				doAfterAlertClose = Lina.Utils.Function.bind(this.toggleAll, this);
				alertSend = true;
			}else{
				for(var ind = 0; ind < list.length; ind++){
					if(!this.isOrdered(ind)){
						ordered[ind] = Lina.Utils.Object.extend({}, defaultItem);
						this._addEditRow(ind);
					}
				}
				count = list.length;
				this._setSettingUebernahme();
				this.updateCounterLabel();
			}

		},

		_settingUebernahme: function(){
			var firstInd = Lina.get("linkSetting").parentNode.parentNode.previousSibling.fotoInd;

			for(var ind in ordered){
				if(firstInd != ind){
					Lina.Utils.Object.extend(ordered[ind], ordered[firstInd]);
					Lina.get("qnt1_" + list[ind][0]).value = ordered[firstInd].q1;
					Lina.get("qnt2_" + list[ind][0]).value = ordered[firstInd].q2;
					Lina.get("frm1_" + list[ind][0]).selectedIndex = formats[ordered[firstInd].f1].nr;
					Lina.get("frm2_" + list[ind][0]).selectedIndex = formats[ordered[firstInd].f2].nr;
					Bestellung._updateQualityImage(ind, 1);
					Bestellung._updateQualityImage(ind, 2);
				}
			}

			return false;
		},

		_setSettingUebernahme: function(){
			var link = Lina.get("linkSetting");
			if( link ){
				link = link.parentNode.removeChild(link);
			}else if(count > 1){
				link = document.createElement("a");
				link.id = "linkSetting";
				link.href = "#";
				link.innerHTML = Lina.translate("myPictureGallery.settings", null, "myPictureGallery");
				link.onclick = this._settingUebernahme;
			}
			if(count > 1){
				Lina.get("orderEditTable").rows[1].cells[3].appendChild(link);
			}

		},

		calculate: function(){

			Lina.Utils.Element.remove(Lina.get("summeTable").tBodies[0], true);
			Lina.get("summeTable").insertBefore( document.createElement("tbody"), Lina.get("summeTable").tBodies[0] );

			var formatsDif = {};
			var gesammtAnzahl = 0;

			var orderedFormatsSorted = [];

			var versandGebuehr = versandGebuehr1;

			for(var ind in ordered){
				if(this.isQuantityPositive(ind)){
					var q1 = ordered[ind].q1;
					if(q1){
						if(!formatsDif[ordered[ind].f1]){
							orderedFormatsSorted.push(ordered[ind].f1);
							formatsDif[ordered[ind].f1] = 0;
						}
						formatsDif[ordered[ind].f1] += q1;
						gesammtAnzahl += q1;
					}
					var q2 = ordered[ind].q2;
					if(q2){
						if(!formatsDif[ordered[ind].f2]){
							orderedFormatsSorted.push(ordered[ind].f2);
							formatsDif[ordered[ind].f2] = 0;
						}
						formatsDif[ordered[ind].f2] += q2;
						gesammtAnzahl += q2;
					}
				}
			}




			if(gesammtAnzahl > 70){
				versandGebuehr = versandGebuehr2;
			}

			orderedFormatsSorted.sort(function(a,b){ return (formats[b].nr - formats[a].nr)});

			var gesammt = bearbeitungGebuehr;

			for(var i=0; i < orderedFormatsSorted.length; i++){
				var format = orderedFormatsSorted[i];
				var sum = formatsDif[format] * this._getPrice( format, formatsDif[format] );
				var tr = Lina.get("summeTable").tBodies[0].insertRow(0);
				var td = tr.insertCell(0);
				td.innerHTML = Lina.translate("myPictureGallery.formatText." + format, null, "myPictureGallery");
				td = tr.insertCell(1);
				td.className = "col23";
				td.innerHTML = formatsDif[format];
				td = tr.insertCell(2);
				td.className = "col23";
				td.innerHTML = this._formatNumber(sum) + ' EUR';

				if(formats[format].poster){
					versandGebuehr = versandGebuehr2;
				}

				gesammt += sum;
			}

			gesammt += versandGebuehr;

			Lina.get("gesammtSumme").innerHTML = this._formatNumber(gesammt) + " EUR";
			Lina.get("versandPrice").innerHTML = this._formatNumber(versandGebuehr) + " EUR";
			Lina.get("shippingFee").value = versandGebuehr;
			Lina.get("gesammtAnzahl").innerHTML = gesammtAnzahl + "";

		},

		_validateForm: function(){
			var ret = true;
			var errorFelder = [];
			var form = Lina.get("formular2");
			for(var i = 0; i < pflichtFelder.length; i++ ){
				var v = form[pflichtFelder[i]].value
				v = Lina.Utils.String.strip(v);
				if(v == ""){
					ret = false;
					form[pflichtFelder[i]].className = "errorField";
					errorFelder.push(pflichtFelder[i]);
				}else{
					form[pflichtFelder[i]].className = "";
				}
			}

			var alertText = "";

			if(!ret){

				alertText = Lina.translate("myPictureGallery.validate.fill", null, "myPictureGallery") + ":\n";
				for(var i = 0; i < errorFelder.length; i++) {
					alertText += Lina.translate("myPictureGallery.form." + errorFelder[i], null, "myPictureGallery") + "\n";
				}
			}

			if(Lina.Utils.String.strip(form.captcha.value) == ""){
				ret = false;
				alertText += "\n" + Lina.translate("myPictureGallery.form.captcha", null, "myPictureGallery") + "\n";
				form.captcha.className = "errorField";
			}

			if(!form.agb.checked){
				ret = false;
				alertText += "\n" + Lina.translate("myPictureGallery.validate.agb", null, "myPictureGallery") + "\n";
				form.agb.className = "errorField";
			}

			if(!ret){
				alert(alertText);
			}

			return ret;
		},

		_formatNumber: function(number){
			return number.toFixed(2).replace(/\./g, ",");
		},

		_onclick: function(e){
			var action = Lina.Events.Event.getTargetOrParentIf(e, function(el){
				if(Lina.Utils.Dom.HTML.hasClass(el, "remI")){
					return "delete";
				}else if(Lina.Utils.Dom.HTML.hasClass(el, "bs1")){
					return "preview";
				}
				return false
			});
			if(action){
				var tr = Lina.Events.Event.getTargetOrParentIf(e, function(el){
					if(el.nodeName.toUpperCase() == "TR" ){
						return el;
					}
					return false;
				});
				var ind = tr.fotoInd;
				switch (action) {
					case "delete":
						Bestellung.toggleItem(ind);
						break;
					case "preview":
						setCurFoto(ind);
						setPage();
						goToStep(0);
						break;
					default:
						break;
				}
			}
			return false;
		}

	}

}();


function getLinkTo(f, thumb){
	if (thumb == null || thumb == false || thumb == 0) {
		var t = "&big=1";
	} else if (thumb == true || thumb == 1 ) {
		var t = "";
	} else {
		var t = thumb;
	}
	return 	"/control/getThumb?fid=" + list[f][0] + t + "&c=" + list[f][2];
}


function goToStep(stepNo){
	for(var i = 0; i < 3; i++)
		Lina.get("step" + i).style.display = (stepNo == i)?"block":"none";

	switch (stepNo) {
		case 0:
			Lina.Utils.Dom.HTML.removeClass(Lina.get("boxGalleryPict"), "shop");
			Bestellung.checkQuantityForEach();
			break;
		case 1:
			Lina.Utils.Dom.HTML.addClass(Lina.get("boxGalleryPict"), "shop");
			Bestellung.checkQuantityForEach();
			break;
		case 2:
			if(!Bestellung.hasItem()){
				goToStep(1);
			}else{
				Lina.Utils.Dom.HTML.addClass(Lina.get("boxGalleryPict"), "shop");
				Bestellung.calculate();
				Lina.get("formular2").bio.value = (Lina.get("checkBoxBIO").checked)?1:0;
				reloadCaptcha();
			}
			break;
		default:
			break;
	}
}

function reloadCaptcha(){

	var p = Lina.get("captchaImage").parentNode
	var img = new Image();
	Bestellung.captchaNr++;
	Lina.get("cpt").value = Bestellung.captchaNr;
	img.src = "/control/fotoOrderCaptcha;jsessionid=" + sessId + "?cpt=" + Bestellung.captchaNr + "&d=" + (new Date()).getTime();
	Lina.get("captchaImage").parentNode.replaceChild(img,Lina.get("captchaImage"));
	img.id="captchaImage";
	Lina.get("formular2").captcha.value = "";
}

function setCurFoto(ind){
	var f = list[ind];
	var im = new Image();

    im.onload = Lina.Utils.Function.bind(
        function(e, t){
          if(!t){
            t = e;
          }
        	Lina.get("bildContainer").replaceChild(this, Lina.get("pictImg"));
    			this.id = "pictImg";
        	this.title = t[1];
        	this.alt = t[1];
          if(gid == "1500000000000721") {
            Lina.get("fileName").innerHTML = "<a href='" + getLinkTo(ind, "&orig=1") + "' >" + t[1] + "</a>";
          } else {
            Lina.get("fileName").innerHTML = t[1];
          }
        }, im, f);
    	im.src = getLinkTo(ind);
	curFileInd = ind;

	var i = ind % thumbsNumber;

	setSelected(getCells()[i]);

}

function getCells(){
	return Lina.Utils.Dom.HTML.getElementsByClassName("tableCell", "div", Lina.get("thumbsTable"));
}

function setCellImg(cell){
	var img = getCellCheckbox(cell);
	if (img)
		img.src = (Bestellung.isOrdered(cell.fotoIndex))?"/myPictureGallery/img/form_checkbox_checked.gif":"/myPictureGallery/img/form_checkbox.gif";
}


function setPage(){
	var newCurPage = Math.floor(curFileInd / thumbsNumber);

	if(newCurPage != curPage){
		curPage = newCurPage;

		Lina.get("imageNoFrom").innerHTML = curPage * thumbsNumber + 1;
		var to = curPage * thumbsNumber + 9;
		if(to > list.length){
			to = list.length;
		}

		Lina.get("imageNoTo").innerHTML = to;

		var tds = getCells();
		for(var i = 0; i < tds.length; i++){
			var ind = curPage * thumbsNumber + i;
			tds[i].fotoIndex = ind;
			tds[i].id = "cell" + ind;
			if(ind >= list.length ){
				tds[i].style.display = "none";
			}else{
				tds[i].style.display = "block";
				Lina.Utils.Mutate.replace(tds[i].getElementsByTagName("a")[0], "<a style='background-image: url(" + getLinkTo(curPage * thumbsNumber + i, 1) + ");' class='thumbLink' href='#' />");
				setCellImg(tds[i]);
			}
		}
		curPageIs = "";
		if(0 == curPage) curPageIs = "first";
		if(curPage == (pageNrs - 1)) curPageIs = "last";

		Lina.get("ar_ll").style.display = "";
		Lina.get("ar_l").style.display = "";
		Lina.get("ar_rr").style.display = "";
		Lina.get("ar_r").style.display = "";

		switch(curPageIs){
			case "first":
				Lina.get("ar_ll").style.display = "none";
				Lina.get("ar_l").style.display = "none";
				break;
			case "last":
				Lina.get("ar_rr").style.display = "none";
				Lina.get("ar_r").style.display = "none";
				break;
			default:
				break;
		}
	}
}

function getCellCheckbox(cell){
	return cell.getElementsByTagName("img")[0];
}

function setSelected(cell){
	Lina.Utils.Dom.HTML.removeClass(selectedCell, "selected");
	selectedCell = cell;
	Lina.Utils.Dom.HTML.addClass(selectedCell, "selected");
}

var cells = getCells();

for(var i = 0; i < cells.length; i++){
	if(!selectedCell && Lina.Utils.Dom.HTML.hasClass(cells[i], "selected")){
		selectedCell = cells[i];
	}

	cells[i].fotoIndex = curPage * thumbsNumber + i;
	cells[i].id = "cell" + cells[i].fotoIndex;

	Lina.Events.Event.addListener(cells[i], "click", function(e) {
		if(Lina.Events.Event.getTargetOrParentIf(e, function(el){return Lina.Utils.Dom.HTML.hasClass(el, "printLabel")})){
			Bestellung.toggleItem(this.fotoIndex);
			goToStep(1);
		}else{
	    	if(this.fotoIndex < list.length) {
				setCurFoto(this.fotoIndex);
	    		goToStep(0);
			}
		}
		Lina.Events.Event.preventDefault(e);
	});
};


var f = function(ind){
	if(ind < 0){
		ind = 0;
	}else if(ind >= list.length){
		ind = (pageNrs - 1) * thumbsNumber;
	}

	setCurFoto(ind);
	setPage();
	return false;
};

Lina.get("ari_l").onclick = function(){
	var i = curFileInd - 1;
	if(i < 0){
		i = list.length - 1;
	}
	return f(i);
};

Lina.get("ari_r").onclick = function(){
	var i = curFileInd + 1;
	if(i == list.length){
		i = 0;
	}
	return f(i);
};

Lina.get("ar_ll").onclick = function(){return f((curPage - 10) * thumbsNumber)};
Lina.get("ar_l").onclick = function(){return f((curPage - 1) * thumbsNumber)};
Lina.get("ar_rr").onclick = function(){return f((curPage + 10) * thumbsNumber)};
Lina.get("ar_r").onclick = function(){return f((curPage + 1) * thumbsNumber)};

Lina.get("bildPreviewLabel").onclick = function(){
	goToStep(0);
	return false;
}

Lina.get("orderServiceLabel").onclick = function(){
	goToStep(1);
	return false;
}

Lina.get("alert").onclick = function(){
	Lina.get("maske").style.display = "none";
	this.style.display = "none";
	doAfterAlertClose();
	return false;
}

Lina.get("allFotos").onclick = function(){
	Bestellung.toggleAll();
	goToStep(1);
	return false;
}

Lina.Events.Event.addListener("orderEditTable", "click", function(e){
	Bestellung._onclick(e);
	return false;
});

Lina.get("submitStep1").onclick = function(){
	goToStep(2);
	return false;
}

Lina.get("abbrechen").onclick = function(){
	goToStep(1);
	return false;
}

Lina.get("formular2").onsubmit = function(){
	if(Bestellung._validateForm()){
		this.orderSer.value = Bestellung.serialize();
		var params = {};
		var inputs = this.getElementsByTagName('input');
		for(var i = 0; i < inputs.length; i++){
			params[inputs[i].name] = inputs[i].value;
		}
		if(!this.agb.checked){
			params.agb = 0;
		}
		params.g = gid;
		params.c = hashPar;
		params.o = ownerId;

		Lina.get("maske").style.display = "block";

		new Lina.Ajax.Request( "/myPictureGallery/order.jsp;jsessionid=" + sessId ,
			{
				parameters: params,
				onSuccess: function(transport, json){
					if(json && json.ordered){
						alert(Lina.translate("myPictureGallery.thanks", null, "myPictureGallery"));
						document.location.reload();
					}else{
						reloadCaptcha();
						if(json.error == "captcha"){
							alert(Lina.translate("myPictureGallery.order.error.captcha", null, "myPictureGallery"));
						}else{
							alert(Lina.translate("myPictureGallery.order.error.order", null, "myPictureGallery"));
						}
					}
					Lina.get("maske").style.display = "none";
				}
			} );
	}

	return false;
}

Lina.Events.Event.hoverPatcher("abol");
