var xm_ci;


	Event.observe(window, 'load', function() {
		
		new zoomImage();
		xm_ci = new collectionImage();	
	}, false);


	function thisMovie(movieName) {
		
	    if (navigator.appName.indexOf("Microsoft") != -1) {
		// ms?
	        return window[movieName];
	    }
	    else {
	    	
	        return document[movieName];
	    }
	}



	/**
	* 	Collection image
	*
		searches all items with the class: collection image
		Stores them in an array
		Displays the images one by one
	*/	
	collectionImage = Class.create();
	collectionImage.prototype = {
		
		collectionPaths: [],
		coll_array: [], // array with all the image items
		 
		initialize: function (itemClass){
			 this.coll_array = document.getElementsByClassName("collection_image");	
			
			// remove the visual element
				var item = $('collection_image_box');
								
				if (item){
					var myPar = item.parentNode;
					$(myPar).removeChild(item);
				}
				
			if (0 < this.coll_array.length) {
				
				for (var i = 0; i < this.coll_array.length; i++){
					var obj = this.coll_array[i];
					
					if(true == this.ci_display(i) ){
						// initial image is enlarged
						break;
					}
					 
				}
			
			}
			
			// enlarge_text
		
		},
		
		ci_display: function (nr){
			// check if array
			if (0 > this.coll_array){
				alert("no image data");
			}
			var obj = this.coll_array[nr];
			
			// remove thumbnail fron the obj
			img_thumb = obj.getElementsByTagName('img')['0'];
			if (img_thumb){
				var myPar = img_thumb.parentNode;
				$(myPar).removeChild(img_thumb);
			}
			
			obj = obj.getElementsByTagName('a')['0'];
			
			// alert(obj.href);
			// place image	
			var imgLoc = $('portfolio_image');
			if (imgLoc){
				// remove old image if present
				var old_img = $("image_show");
				if (old_img){
					var myPar = old_img.parentNode;
					$(myPar).removeChild(old_img);
				}
				
				var imgPath = obj.href;
				
				/// alert('img' + obj.href );
				// element was found
				var item = Builder.node('img', {'src': imgPath, 'class': 'image_show', 'id': 'image_show'}, ''); // image
				
				imgLoc.appendChild(item);
			}
			
			// place text
			var textLoc = $("portfolio_text");
			if (textLoc){		
				if (obj.text == undefined){
					textLoc.innerHTML = '';
				}else{	
					textLoc.innerHTML = obj.innerHTML;
				}
			}			
			
			// update btn link
			
			// BUTTON FOR NEXT
			var btn_next = $('xmic_btn_next');
			if (btn_next){
			
				var linkobjs = btn_next.getElementsByTagName('a');
				// check if hyperlink objs are found
				if (0 < linkobjs.length){
					for (var i = 0; i < linkobjs.length; i++){
						var obj = linkobjs[i];
						
						var next = nr+1;
						
						if ( (next) >= this.coll_array.length){
							// new number is higher than the image count
							btn_next.style.visibility = "hidden";
						}else{
							btn_next.style.visibility = "visible";
							// 
//							obj.href = "javascript: xm_ci.ci_display(" + next + "); "
							obj.href = "javascript: xm_ci.hellow(" + next + "); "
						}
						//alert(obj.href);
					}
				}
			}
			
			// BUTTON FOR PREVIOUS
			var btn_prev = $('xmic_btn_prev');
			if (btn_prev){
			
				var linkobjs = btn_prev.getElementsByTagName('a');
				// check if hyperlink objs are found
				if (0 < linkobjs.length){
					for (var i = 0; i < linkobjs.length; i++){
						var obj = linkobjs[i];
						
						var prev = nr-1;
						
						if ( (prev) < 0){
							// new number is higher than the image count
							btn_prev.style.visibility = "hidden";
						}else{
							btn_prev.style.visibility = "visible";
							// 
//							obj.href = "javascript: xm_ci.ci_display(" + next + "); "
							obj.href = "javascript: xm_ci.hellow(" + prev + "); "
						}
						//alert(obj.href);
					}
				}
			}
			
			
			
			
			
			return 1;
		} // end display_photo_text
		,hellow: function (val){
			this.ci_display(val);
		}
		
	} // end collection image





	
	/**
	* 	Slide Thumb
	*
	*/	
	slideThumb = Class.create();
	slideThumb.prototype = {
	
		initialize: function (itemClass){
			var zoomImg = document.getElementsByClassName("slideThumb");			
			var paths = '';
			if (zoomImg.length>0) {
				for (var i = 0; i < zoomImg.length; i++){
					var obj = zoomImg[i]
					
					this.zoomPaths[i] = zoomImg[i].href;
					zoomImg[i].href = "#zoom";
					zoomImg[i].target = "_self";
					
					this.setEvent(zoomImg[i], i);
				}
			}
		}
	}
	
	
	/**
	* 	ZOOM IMAGE
	*
	*/	
	zoomImage = Class.create();
	zoomImage.prototype = {
		
		zoomPaths: [],
		
		 
		initialize: function (itemClass){
			
			var zoomImg = document.getElementsByClassName("xm_zoom_overlay");			
			var paths = '';
			if (zoomImg.length>0) {
				for (var i = 0; i < zoomImg.length; i++){
					var obj = zoomImg[i]
					
//					zoomImg[i].onClick = "alert('plop') ; ";
					// Event.observe(obj, 'Click', obj.onClick.ZoomTest(), false);
					// Event.observe(item, 'click', this.onClick.bindAsEventListener(this, item, i), false);
//					paths += zoomImg[i].href;
					this.zoomPaths[i] = zoomImg[i].href;
					zoomImg[i].href = "#zoom";
					zoomImg[i].target = "_self";
					
					this.setEvent(zoomImg[i], i);
				}
				
			}
			
		},
		
		setEvent: function(item, i) {
			Event.observe(item, 'click', this.iZoom.bindAsEventListener(this, item, i), false);
		},
		
		iZoom: function(obj, item, i){
//			alert(item.href + " id:" + i);
//			
//			alert(this.zoomPaths[i]);
			var imgPath = this.zoomPaths[i];
			this.zoomPop(item, imgPath);
		},
		zoomPop: function(item, imgPath){
			
			this.close(this);
			
			// mask
			var zBox = Builder.node('div', {'class': 'zoomPop'}, ' '); 					
					
			document.body.appendChild(zBox);
			// image
			var imgItem = Builder.node('img', {'src': imgPath, 'class': 'zoomImage'}, ''); // image
			// crop image if it is large
			Event.observe(imgItem, 'load', this.imageSize.bindAsEventListener(this, imgItem), false);			
			// image Box
			var imgBox = Builder.node('div', 
							{'class': 'zoomPopImg', 'style': ' cursor: pointer; visibility: hidden; '},
							imgItem); // image
			// close the overlay when clicked				
			Event.observe(imgBox, 'click', this.close.bindAsEventListener(this), false);
							
			document.body.appendChild(imgBox);
			
//			imgBox.appendChild(zoomOptions);
			
		},
		imageSize: function(obj, item){
			
			var w = item.width;
			var h = item.height;
			
			
			if ( (w > 800) || (h > 600) ){
				
				if (w > 800){
					var nw = 800;
					var nh = (nw * h) / w;
				}
				
				if (nh > 600){
					var nh = 600;
					var nw = (nh * w) / h;
				}
				
				item.width = nw;
				item.height = nh;
				
				
			}
			item.parentNode.style.visibility = 'visible';
			
		},
		zoomPlus: function (obj){
			// alert('zoom Plus');
		},
		
		close: function (obj){
						// closes open zoomPop windows
						var pw = document.getElementsByClassName('zoomPop');											
						if (1 <= pw.length){ // window exist
							
							for (var i = 0; i < pw.length; i++){								
								var myPar = pw[i].parentNode;
						   		$(myPar).removeChild(pw[i]);						   		
							}
						}
						// closes open zoomPop windows
						var pw = document.getElementsByClassName('zoomPopImg');											
						if (1 <= pw.length){ // window exist
							for (var i = 0; i < pw.length; i++){
								var myPar = pw[i].parentNode;
						   		$(myPar).removeChild(pw[i]);						   		
							}
						}
		}
	}

	function popthisup(url, name, newWidth, newHeight) { /* , height='150', width='150', toolbar='yes', directories='no', status='no', scrollbars='no', resizable='yes' */ 
		//window.open("url","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400")
		
		var dim = "width=" + newWidth + ", height=" + newHeight;
//		alert(dim + '' +name);
//		newwindow=window.open(url, name=name,"_blank", 'height=200,width=150',  resizable='yes');	
		newwindow=window.open(url,'name=' +name, 'width='+newWidth+',height='+ newHeight +', resizable=yes, scrollbars=yes' )
		if (window.focus) {newwindow.focus()}
		return false;
	}

/* general helper */
	function getChildNodeById(obj, idName){
					var items = obj.childNodes;					
				   for (var i = 0; i < items.length; i++){
					// get identifiers -->		   // NOTE: List is live, Adding or removing children will change the list  // do something with each child as children[i]
				   	if (items[i].id == idName){
				   		return items[i];  	// pid = id for parant (link|ref)					   		
				   	}
				   };
	}
	
	function getChildNodeByName(obj, idName){
					var items = obj.childNodes;					
				   for (var i = 0; i < items.length; i++){
					// get identifiers -->		   // NOTE: List is live, Adding or removing children will change the list  // do something with each child as children[i]
				   	if (items[i].name == idName){
				   		return items[i];  	// pid = id for parant (link|ref)					   		
				   	}

				   };
				   return;
	}

	function getChildinDepth(obj, idName){
				   var items = obj.childNodes;					
				   for (var i = 0; i < items.length; i++){
					// get identifiers -->		   // NOTE: List is live, Adding or removing children will change the list  // do something with each child as children[i]
					if (x = getChildNodeByName(items[i], idName)){
						return x;
					}else{
						if (x = getChildinDepth(items[i], idName) ){
							return x;
						}						
					}					
				   };
				   return 0;
	}
	
	function doc_id(elem){
		return document.getElementById(elem);
	}
	
	function xmSubmitSearch(obj, searchFieldID, baseAddress){ // search 
		if (searchFieldID){
			Q  = doc_id(searchFieldID).value;
			if (ba  = doc_id(baseAddress).value){
				
			}else{
				ba = baseAddress
			}
			go =  ba + '|search='+Q;
			window.location = go;
		}
	}
	/**
	 * 
	 * add button actions to the menu
	 * 
	 */
	function xmMouseOverActions(obj, action, url, newCssStyle){
		

		if (obj){ // check each element, in some cases af few of the vals will not exist
			
			switch (action){
				
			case 'over':
			
				
				if (newCssStyle){
					var oid = obj.id;
					
					obj.id = newCssStyle;
					// obj.style.background ='#bbb';
					
					return oid;
				}else{
					return 0;
				}
				
			case 'down':
				// goto url;
				window.location = url;
				
				break;
			case 'out':

				// redo oldstyle
				if (newCssStyle){
					obj.id = newCssStyle;
				}
				//obj.id = newCssStyle;
				
			}// end sitch
			
		}// end object
		
	}
	
	
	