dojo.require('dojo.parser');
dojo.require('dijit.Dialog');
dojo.require("dijit.form.NumberSpinner");
dojo.require("dijit.form.FilteringSelect");

function editImage(img) {
	console.log(img);
}

var photo = new Object();
photo.format = '0';
photo.mode   = '0';
photo.paper = '0';
photo.border = '0';

function setNewFormat(format, img, idphoto, idline, type) {
	
	if (format == 0) {
		updateDetails(img,idphoto);
		return;
	}

	if(type == 'album'){
		var album = dojo.byId('album').value;
	} else {
		var album = 0;
	}
	
	photo.format = format;

	var request = {action:'updateFormat', format:photo.format, image:img, idline:idline, type: type};
	
	dojo.xhrPost({
		url:'photo.backend.php',
		load:function(data) {
			if(data == 1) {
				successMsg();
				// get the new image with the new parameters
				// no need to give many arguments, image.php recalculate with the data in DB
				dojo.byId(img).src = 'image.php?img=' + img + '&type=' + type + '&format=' + format + '&mode=' + photo.mode + '&paper=' + photo.paper + '&border=' + photo.border + '&album=' + album + '&ordered=1&idline='+idline;
				
				//change CropLink
				var imageIndex = myMooFlowPage.mf.index;
				if (document.getElementById('linkCrop')) { 
					var cropLink = dojo.byId('linkCrop');
					var elements = document.getElementsByName('dtformat');
					var idformat = elements[0].value;
					poster = cropLink.href.indexOf('poster') != -1;
					cropLink.href = '?action=imagix&m=crop&i=' + idphoto + '&pos=' + imageIndex+'&format='+idformat;
					if (poster) cropLink.href += '&poster';
				};
			} else {
				console.log('not updated');
			}
		},
		error:function(e) {console.log('error in setNewFormat() '+e);},
		content:request
	});

	if (album == 0) {
		var request = {action:'getQuality', image:img, type: type};
		dojo.xhrPost({
			url:'photo.backend.php',
			load:function(data) {
				if (data == '1') {
					var quality = 'green';
				} else if (data == '2') {
					var quality = 'yellow';
				} else {
					var quality = 'red';
				}
				document.getElementById('qualityBox').childNodes[1].setAttribute('id',quality);
			},
			error:function(e) {console.log('error in getQualityImage() '+e);},
			content:request
		});
	}
}

function setNewMode(mode, img, idphoto, idline, type) {
	console.log('mode: '+mode+'| img: '+img+'| idphoto: '+idphoto+'| idline: '+idline+'| type: '+type);
	
	if(type == 'album'){
		var album = dojo.byId('album').value;
	} else {
		var album = 0;
	}
	
	console.log(album);
	
	photo.mode = mode;
	
	var request = {action:'updateMode', mode:photo.mode, image:img, idline:idline, type:type};
	
	dojo.xhrPost({
		url:'photo.backend.php',
		load:function(data) {
			if(data == 1) {
				// get the new image with the new parameters
				dojo.byId(img).src = 'image.php?img=' + img + '&type=' + type + '&format=' + photo.format + '&paper=' + photo.paper + '&mode=' + photo.mode + '&border=' + photo.border +  '&album=' + album + '&ordered=1&idline='+idline;
				console.log('image.php?img=' + img + '&type=' + type + '&format=' + photo.format + '&mode=' + photo.mode + 'album=' + album);
				successMsg();
			} else {
				console.log('not updated');
			}
		},
		error:function() {console.log('error in setNewMode()');},
		content:request
	});
}

function setNewPaper(paper, img, idphoto, idline, type) {
	
	if(type == 'album'){
		var album = dojo.byId('album').value;
	} else {
		var album = 0;
	}
	
	photo.paper = paper;
	
	var request = {action:'updatePaper', paper:photo.paper, image:img, idline:idline, type:type};
	
	dojo.xhrPost({
		url:'photo.backend.php',
		load:function(data) {
			if(data == 1) {
				// get the new image with the new parameters
				dojo.byId(img).src = 'image.php?img=' + img + '&type=' + type + '&format=' + photo.format + '&paper=' + photo.paper + '&mode=' + photo.mode + '&border=' + photo.border + '&album=' + album + '&ordered=1&idline='+idline;
				console.log('image.php?img=' + img + '&type=' + type + '&format=' + photo.format + '&paper=' + photo.paper + '&mode=' + photo.mode + 'album=' + album);
				successMsg();
			} else {
				console.log('not updated');
			}
		},
		error:function() {console.log('error in setNewPaper()');},
		content:request
	});
}

function setRemovePhoto(img, order, idphoto, type) {
	console.log('img: '+img);
	console.log('order: '+order);
	console.log('idphoto: '+idphoto);
	console.log('type: '+type);
	
	dojo.xhrPost({
		url: 'photo.backend.php',
		load: function(data) {
			dojo.byId(img).src = 'image.php?img=' + img + '&type=' + type + '&format=' + photo.format + '&paper=' + photo.paper + '&mode=' + photo.mode + '&border=' + photo.border;
			successMsg();
		},
		error: function(e) {console.log('error in setRemovePhoto() '+e);},
		content: {action : 'setRemovePhoto', order : order, img: img, type : type},
		sync: true
	});

	updateDetails(img, idphoto);
}

function addFormat(img, idphoto, type, album) {
	
	dojo.style("details", "display", "none");
	var div = dojo.byId('formatLines_' + idphoto);
	var album = album;
	
	var request = {action:'addNewFormat',image:img,type:type, album:album};
	
	dojo.xhrPost({
		url:'photo.backend.php',
		load:function(result) {
			if(result == 1) {
				
				var content = {action:'getAllLines', image:img, idphoto:idphoto, type:type, album:album};
				dojo.xhrPost({
					url:'photo.backend.php',
					load:function(data) {
						div.innerHTML = data;
					},
					content:content,
					sync:true
				});
				
				// updating the cart
				successMsg();
			} else {
				console.log('Could not make this operation!');
			}
		},
		error:function() {console.log('error in addFormat()');},
		content:request,
		sync:true
	});
	dojo.parser.parse(dojo.byId('details'));
	dojo.style("details", "display", "block");
}



function removeFormat(img,line,idphoto,type) {

	dojo.style("details", "display", "none");
	var div = dojo.byId('formatLines_'+idphoto);
	var request = {action:'removeFormat', idline:line,type:type};
	
	dojo.xhrPost({
		url:'photo.backend.php',
		load:function(result) {
			if(result == 1) {
				var content = {action:'getAllLines', image:img, idphoto:idphoto,type:type};
				if (document.getElementById('album')) {
					var albumid = document.getElementById('album').value;
					var content = {action:'getAllLines', image:img, idphoto:idphoto,type:type,album:albumid};
				} else {
					var content = {action:'getAllLines', image:img, idphoto:idphoto,type:type};
				}
				dojo.xhrPost({
					url:'photo.backend.php',
					load:function(data) {
						div.innerHTML = data;
					},
					content:content,
					sync:true
				});
				
				successMsg();
			} else {
				console.log('Could not make this operation!');
			}
		},
		error:function() {console.log('error in addFormat()');},
		content:request,
		sync:true
	});
	
	dojo.parser.parse(dojo.byId('details'));
	dojo.style("details", "display", "block");
	
	if (type == 'album') {
		var counts = document.getElementsByName('dtcount');
		var reallyZero = true;
		for (var i=0; i<counts.length; i++) {
			if (counts[i].value > 0) {
				reallyZero = false;
			}
		}
		if (reallyZero) {
			removeSelectedPhoto(mf.index,img);
			if (numSelectedPhotos() == 0) deletePhotoSelectedDiv();
		}
	}
}

function updateNumberPhotos(val,img,idline,type) {
	console.log('triggered function updateNumberPhotos()');
	
	if(type == 'album'){
		var album = dojo.byId('album').value;
	} else {
		var album = 0;
	}
	
	var request = {action:'updateNumberPhotos',num:val,image:img,idline:idline,type:type};
	
	dojo.xhrPost({
		url:'photo.backend.php',
		load:function(result) {
			if(result == 1) {
				successMsg();
				// + '&format=' + photo.format + '&paper=' + photo.paper + '&mode=' + photo.mode + '&border=' + photo.border
				//console.log('image.php?img=' + img + '&type=' + type);
				dojo.byId(img).src = 'image.php?img=' + img + '&type=' + type + '&format=' + photo.format + '&paper=' + photo.paper + '&mode=' + photo.mode + '&border=' + photo.border + '&album=' + album + '&ordered=1&idline='+idline;
			} else {
				console.log('Could not update!');
			}
		},
		error:function() {console.log('error in updateNumberPhotos()');},
		content:request
	});

	if (type == 'album') {
		if (val == 0) {
			var counts = document.getElementsByName('dtcount');
			var reallyZero = true;
			for (var i=0; i<counts.length; i++) {
				if (counts[i].value > 0) {
					reallyZero = false;
				}
			}
			if (reallyZero) {
				removeSelectedPhoto(mf.index,img);
				if (numSelectedPhotos() == 0) deletePhotoSelectedDiv();
			}
		} else {
			createPhotoSelectedDiv();
			addSelectedPhoto(myMooFlowPage.mf.index,img);
		}
	}
}

function removeSelectedPhoto(index,name) {
	if (document.getElementById(index+'_'+name)) {
		var div = document.getElementById(index+'_'+name);
		div.parentNode.removeChild(div);
	}
}

function addSelectedPhoto(index,name) {
	if (!document.getElementById(index+'_'+name)) {
		var albumid = document.getElementById('album').value;
		var photodiv = document.createElement('div');
		photodiv.setAttribute('id',index+'_'+name);
		photodiv.innerHTML = '<img src="/images/albums/'+albumid+'/thumbnails/'+name+'" width="52" alt="'+name+'" />';
		photodiv.style.cursor = 'pointer';
		photodiv.style.cssFloat = 'left';
		photodiv.style.margin = '2px';
		photodiv.style.height = '50px';
		photodiv.style.overflow = 'hidden';
		photodiv.onclick = function () {
			if (!myMooFlowPage.mf.isLoading && myMooFlowPage.mf.index!=index) myMooFlowPage.mf.glideTo(index);
		}
		
		var div = document.getElementById('photoselected');
		div.appendChild(photodiv);
	}
}

function numSelectedPhotos() {
	if (document.getElementById('photoselected')) {
		var div = document.getElementById('photoselected');
		return div.childNodes.length-1;
	}
	return 0;
}

function loadSelectedPhotos(json) {
	createPhotoSelectedDiv();
	for(var i=0; i<json.length; i++) {
		addSelectedPhoto(json[i].index,json[i].img);
	}
}

function createPhotoSelectedDiv() {
	if (!document.getElementById('photoselected')) {
		var div = document.createElement('div');
		div.setAttribute('id','photoselected');
		div.setAttribute('class','infos');
		div.setAttribute('className', 'infos'); 
		div.style.maxHeight = '300px';
		div.style.overflowX = 'hidden';
		div.style.overflowY = 'auto';
		
		var title = document.createElement('h1');
		title.innerHTML = orderedPhotos;
		title.style.marginBottom = '5px';
		
		div.appendChild(title);
		
		 
		var right = document.getElementById('right');
		right.appendChild(div);
		
		if (document.getElementById('pricelist')) {
			var pricelist = document.getElementById('pricelist');
			div.parentNode.insertBefore(div,pricelist);
		}
	}
}

function deletePhotoSelectedDiv() {
	if (document.getElementById('photoselected')) {
		var div = document.getElementById('photoselected');
		div.parentNode.removeChild(div);
	}
}

function loadPriceList(idalbum) {
	if (dojo.byId('hash')) {
		var hash = dojo.byId('hash').value;
	} else {
		var hash = '';
	}
	dojo.xhrPost({
		url: 'album.backend.php',
		load: function(data) {
			if (data !='') {
				var div = document.createElement('div');
				div.setAttribute('id','pricelist');
				div.setAttribute('class','infos');
				div.setAttribute('className', 'infos'); 
				div.style.maxHeight = '300px';
				div.style.overflowX = 'hidden';
				div.style.overflowY = 'auto';
				
				var title = document.createElement('h1');
				title.innerHTML = Pricelist;
				title.style.marginBottom = '5px';
				div.appendChild(title);
				
				var prices = document.createElement('div');
				prices.innerHTML = data;
				div.appendChild(prices);
				
				var right = document.getElementById('right');
				right.appendChild(div);
			}
		},
		error: function(e) {alert(e.message);},
		content: {action : 'getPriceList', idalbum: idalbum, hash: hash}
	});
}

function setWhiteBorder(val,img,idphoto,idline,type) {
	console.log('triggered the function setWhiteBorder()');
	
	if(type == 'album'){
		var album = dojo.byId('album').value;
	} else {
		var album = 0;
	}
	
	if(dojo.byId('whiteBorder'+val).checked == true) {
		photo.border = 1;
	} else {
		photo.border = 0;
	}

	dojo.xhrPost({
		url: 'photo.backend.php',
		load: function(data) {
			if(data == 1) {
				dojo.byId(img).src = 'image.php?img=' + img + '&type=' + type + '&format=' + photo.format + '&paper=' + photo.paper + '&mode=' + photo.mode + '&border=' + photo.border + '&album=' + album + '&ordered=1&idline='+idline;
				console.log('image.php?img=' + img + '&type=' + type + '&format=' + photo.format + '&paper=' + photo.paper + '&mode=' + photo.mode + '&border=' + photo.border + 'album=' + album);
			} else { 
				console.log('Error setting the white border');
			}
		},
		error: function(e) {console.log('error in setWhiteBorder() '+e);},
		content: {action : 'setWhiteBorder', check : photo.border, idline : idline, type : type},
		sync: true
	});
}

function requestListUpdate(start,type) {	
	var req = {action:'updateListPhotos', offset:start};
	
	dojo.xhrPost({
		url:'photo.backend.php',
		load:function(data)  {
			dojo.byId('innerContent').innerHTML = data;
		},
		error:function(e) {console.log('error in requestListUpdate() '+e);},
		content:req,
		sync:true
	});
	
	dojoDestroy();
}

function removePhoto(img, idphoto) {
	//console.log(img);
	//console.log(idphoto);
	var req = {action:'removePhoto', image:img, idphoto:idphoto};
	dojo.xhrPost({
		url:'photo.backend.php',
		load:function(data) {
			if(data != '0') {
				dojo.byId('innerContent').innerHTML = data;
			}
		},
		error:function(e) {console.log('error in removePhoto() '+e);},
		content:req,
		sync:true
	});
	
	dojoDestroy();
}
