/* Projectbox-Funktionen
 *
 */
	ip.projectbox = {
		currID : null,
		
		onChangeDefault : function(obj,e,offsetTop) {
			if(typeof offsetTop == 'undefined') offsetTop = 0;
			ip.helper.stopEvent(e);
			var value = obj.options[obj.selectedIndex].value;
			if(value == 'new') {
				this.show(obj.parentNode.parentNode.parentNode,0,e,offsetTop);
			} else {
				// auf dem client in cookie speichern
				ip.cookie.write('currProjectBoxId',value);
				
				// ist eine Projektbox gewählt?
				if(value > -1) {
					// aktueller PicCount vom Server für Projektbox holen
					var rpc = new DataRequestor();
					rpc.getURL('/rpc/getProjectboxPicCount.php?projectBoxId='+value);
					rpc.onLoad = function(data,obj) {
						// data beinhaltet Anzahl Bilder in dieser Projektbox
						ip.get('txtTabProjectBoxCount').innerHTML = data;
					}
				} else {
					ip.get('txtTabProjectBoxCount').innerHTML = 0;
				}
			}
		},
		
		refreshPage : function(obj,e) {
			ip.helper.stopEvent(e);
			var value = obj.options[obj.selectedIndex].value;
			if(value == 'new') {
				this.show(obj.parentNode.parentNode.parentNode,0,e,0);
			} else {
				this.changeProjectBoxByValue(value);
			}
		},
		
		changeProjectBoxByValue : function(value) {
			//ip.cookie.write('currProjectBoxId',value);
			document.location.href = '/projectbox.php?pbID='+value;
		},
		
		put : function(obj,id,e,search_string) {
			if(!e) e = window.event;
			var bodyEl = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
			var y = e.pageY ? e.pageY : e.clientY + bodyEl.scrollTop;
			var x = e.pageX ? e.pageX : e.clientX + bodyEl.scrollLeft;
			
			this.x = x;
			this.y = y;
			
			if(document.forms.frmResultSettings && document.forms.frmResultSettings.projectbox.selectedIndex == 0) {
				this.show(obj,id,e);
			} else {
				this.save(id, search_string);
			}
		},

		show : function(obj,id,e,offsetTop) {
			if(typeof offsetTop == 'undefined') offsetTop = 0;
			ip.helper.stopEvent(e);
			var popup = ip.get('divNewProjectboxContainer');
			var bodyEl = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
			
			popup.style.left = ip.helper.getLeft(obj)-popup.getElementsByTagName('div')[0].offsetWidth+obj.firstChild.offsetWidth+'px';
			popup.style.top = ip.helper.getTop(obj)+offsetTop+'px';
			popup.style.visibility = 'visible';
			
			popup.getElementsByTagName('input')[0].value = '';
			popup.getElementsByTagName('input')[0].focus();
			
			
			this.currID = id;
			
			bodyEl.onclick = function() {
				ip.get('divNewProjectboxContainer').style.visibility = 'hidden';
				this.onclick = null;
				if(document.forms.frmResultSettings && document.forms.frmResultSettings.projectbox.options[document.forms.frmResultSettings.projectbox.options.selectedIndex].value == 'new') {
					document.forms.frmResultSettings.projectbox.options[0].selected = true;
				}
			}
			popup.onclick = function(e) {
				ip.helper.stopEvent(e);
			}
		},

		createNew : function() {
			var name = document.forms.frmNewProjectbox.txtProjectboxName.value;
			if(name != '') {
				// Auf dem Server erstellen
				var rpc = new DataRequestor();
				rpc.newName = name;
				rpc.getURL('/rpc/newProjectbox.php?name='+escape(name));
				rpc.ref = this;
				rpc.onLoad = function(data,obj) {
					if(data != 'false') { // data beinhaltet die neue ID
						// lokal erstellen
						var name = this.newName;
						var objSelect = (document.forms.frmResultSettings) ? document.forms.frmResultSettings.projectbox : null;
						var oldLastOption = objSelect.options[objSelect.options.length-1];
						objSelect.options[objSelect.options.length-1] = null;
						objSelect.options[objSelect.options.length] = new Option(name,data);
						objSelect.options[objSelect.options.length] = oldLastOption;
						objSelect.options[objSelect.options.length-2].selected = true;
						
						this.ref.onChangeDefault(objSelect);
						
						if(document.location.href.indexOf('projectbox.php') > -1) {
							document.location.href = document.location.href;
						} else {
							ip.get('divNewProjectboxContainer').style.visibility = 'hidden';
							
							// Bild in Box speichern, wenn Bild gewählt
							if(this.ref.currID > 0) {
								this.ref.save(this.ref.currID);
							}
						}
					} else {
						alert('Ein Fehler ist aufgetreten.');
					}
				}
			} else {
				alert('Bitte Bezeichnung eingeben.');
			}
			return false;
		},

		save : function(id, search_string) {
			var objSelect = (document.forms.frmResultSettings) ? document.forms.frmResultSettings.projectbox : null;
			var projectBoxId = objSelect.options[objSelect.options.selectedIndex].value;
			
			// Auf dem Server speichern
			var rpc = new DataRequestor();
			rpc.getURL('/rpc/saveToProjectbox.php?projectBoxId='+projectBoxId+'&picID='+id+'&search='+search_string);
			rpc.x = this.x;
			rpc.y = this.y;
			rpc.onLoad = function(data,obj) {
				if(parseInt(data) > 0) { // data beinhaltet Anzahl Bilder in dieser Projektbox
					ip.get('txtTabProjectBoxCount').innerHTML = data;
					ip.messages.show(ip.messages.getByCode(5),this.x,this.y);
				} else {
					ip.messages.show(ip.messages.getByCode(Math.abs(parseInt(data))),this.x,this.y);
				}
			}
		},
		
		del : function(id) {
			ip.get('divConfirmText').innerHTML = 'Wollen Sie diese Projektbox wirklich l&ouml;schen?';
			ip.confirmProjectBoxDelId = id;
			ip.confirmYes = function() {
				document.location.href = '/projectbox.php?del='+ip.confirmProjectBoxDelId;
			}
			ip.confirmShow();
		},
		
		rename : function(obj,id,name,e,offsetTop) {
			if(typeof offsetTop == 'undefined') offsetTop = 0;
			ip.helper.stopEvent(e);
			var popup = ip.get('divEditProjectboxContainer');
			var bodyEl = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
			
			popup.style.left = ip.helper.getLeft(obj)+'px';//ip.helper.getLeft(obj)-popup.getElementsByTagName('div')[0].offsetWidth+obj.firstChild.offsetWidth+'px';
			popup.style.top = ip.helper.getTop(obj)+offsetTop+'px';
			popup.style.visibility = 'visible';
			
			popup.getElementsByTagName('input')[0].value = id;
			popup.getElementsByTagName('input')[1].value = name;
			popup.getElementsByTagName('input')[1].focus();
			
			bodyEl.onclick = function() {
				ip.get('divEditProjectboxContainer').style.visibility = 'hidden';
				this.onclick = null;
			}
			popup.onclick = function(e) {
				ip.helper.stopEvent(e);
			}
		},
		
		hide : function() {
			var bodyEl = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
			bodyEl.onclick = null;
			ip.get('divEditProjectboxContainer').style.visibility = 'hidden';
		},
		
		update : function() {
			var f = document.forms.frmEditProjectbox;
			if(f.txtProjectboxName != '') {
				f.submit();
			} else {
				alert('Bitte geben Sie einen Namen ein.');
			}
		},
		
		showMore : function(obj,e) {
			if(!e) e = window.event;
			if(typeof e.stopPropagation == 'function') e.stopPropagation();
			else e.cancelBubble = true;
			
			var popup = ip.get('divProjectboxPopupMoreContainer');
			var l = ip.helper.getLeft(obj);
			var h = obj.offsetHeight;
			var w = obj.offsetWidth;
			var pw = popup.getElementsByTagName('div')[0].offsetWidth;
			var bodyEl = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
			
			bodyEl.onmouseover = function() {
				ip.get('divProjectboxPopupMoreContainer').style.visibility = 'hidden';
				this.onmouseover = null;
			}
			popup.onmouseover = function(e) {
				if(!e) e = window.event;
				if(typeof e.stopPropagation == 'function') e.stopPropagation();
				else e.cancelBubble = true;
			}
			//popup.style.top = (141)+'px';
			popup.style.left = l+'px';
			popup.style.visibility = 'visible';
		},
		
		updateNote : function(message, picID, projectboxID,e) {
			var rpc = new DataRequestor();
			
			rpc.id = picID;
			
			rpc.argArray = new Array();
			rpc.argArray[_POST] = new Array();
			rpc.argArray[_POST]['message'] = message;
			
			rpc.getURL('/rpc/update_note.php?picID='+picID+'&projectboxID='+projectboxID);
			rpc.onLoad = function(data,obj) {
				var div = ip.get('divSaveNoteMsg'+this.id);
				if(parseInt(data) == 1) {
					div.innerHTML = ip.messages.getByCode(6);
					div.className = 'divAlertOK';
				}
				if(parseInt(data) == -1) {
					div.innerHTML = ip.messages.getByCode(7);
					div.className = 'divAlertError';
				}
				div.style.display = 'block';
				
				setTimeout('ip.get("divSaveNoteMsg'+this.id+'").style.display = "none"',2500);
			}
		}
	}