

var PHPh_clsZoom = new Class({
  	Implements: [Options, Events],
	elements:[],
	shade:false,
	initialized:false,
	zoomers:2, 
	zoom:[], 
	setup_complete:false,
	currentEl:false,
	currentZoomIndex:-1,
	options: {
   		image_directory : '/images/PHPh_Zoom',
		width: null,
		height: null,
		startOpacity: null,
		max: 1024
	},
	init: function(links, options){
		this.setOptions(options);
		if(links && !is_array(links))links=[links];
		if(!this.initialized) {
			if(!$chk(links))
				links = $$('a').filter(function(el){ if(!el) return false; try { return el.get('href').test(/\.(gif|jpe?g|png)/); } catch(e){} });
			//alert('links.length = ' + links.length);
			this.setup();
			//alert('setup complete');  
		}
		links.each(function(el){
			el.addEvent('mouseover',PHPh_Zoom.loadImg.bindWithEvent(PHPh_Zoom));
			el.addEvent('click',PHPh_Zoom.show.bindWithEvent(PHPh_Zoom));
		});
		if(links)this.elements.combine(links);
		this.initialized=true;
	},
	setup: function (){
		if(this.setup_complete) return true;
		$(document.body).adopt(
			$$(
				this.shade = new Element("div", {id: "PHPh_Shade"}).addEvent("click", close)
			).setStyle("display", "none")
		);
		//this.zoom=[new PHPh_clsZoomer(),new PHPh_clsZoomer()];
		for(x=0;x<this.zoomers;x++) {
			this.zoom[x]=new PHPh_clsZoomer();
			this.zoom[x].init(x,this.options);
		}
		this.setup_complete=true;
	},
	loadImg:function(e){
		e.stop();
		var el=$($(e.target).match('a')?e.target:e.target.getParent('a'));
  		//if(PHPh_Debug) PHPh_Debug.debug('PHPh_Zoom.loadImg: el',el);
		if(!$chk(el.retrieve('loader'))) {
			el.store('loader',new PHPh_clsLoader())
			el.retrieve('loader').load(el);
		}
	},
	show:function(e){
  		e.stop();
  		var el=$(e.target).match('a')?e.target:e.target.getParent('a');
		if(!el.retrieve('loader'))
			this.loadImg(e);
		//if(PHPh_Debug) PHPh_Debug.debug('PHPh_Zoom.show: el.retrieve(\'loader\').thumb',el.retrieve('loader').thumb);
  		//if(PHPh_Debug) PHPh_Debug.debug('PHPh_Zoom.show: el.retrieve(\'loader\').img',el.retrieve('loader').img);
  		//if(PHPh_Debug) PHPh_Debug.debug('PHPh_Zoom.show: el.retrieve(\'loader\').el',el.retrieve('loader').el);
  		var i=this.currentZoomIndex;
		//alert('i = '+i);
  		if(this.currentZoomIndex>-1 && !this.zoom[i].hidden) {
			this.zoom[i].hide();
		}
		if(++i==this.zoom.length) i=0;
		//alert('current zoom index = '+i);
		//alert('this.zoom = '+this.zoom);
		//alert('this.zoom[i] = '+this.zoom[i]);
		this.currentZoomIndex=i;
		this.zoom[i].hidefast();
		this.currentEl=el;
		this.zoom[i].show(el);
		return false;
	},
	hide:function(){
	
	}/*,
	loaded:function(e){
		alert('loaded'+e.target);
		if(!e)e=window.event;
		img=e.target;
		l=img.retrieve('loader');
		l.loading=false;
		l.loaded=true;
		alert('image width = '+img.width);
	}*/
});

var PHPh_clsLoader = new Class({
	el:false,
	loaded:false,
	loading:false,
	href:'',
	img:false,
	thumb:false,
	onload:false,
	load:function(a){
		this.img=$(new Image());
		if(a&&a!=this.el){
			this.el=$(a);
			if(this.el)this.el=a;
			this.loading=this.loaded=false;
			this.thumb=this.el.getElement('img');
		}
		if(!this.loaded && !this.loading) {
			this.loading=true;
			//this.img.store('loader',this);
			//this.img.onload = PHPh_Zoom.loaded;
			this.img.onload = this.loadComplete.bind(this);
			this.img.src = this.el.get('href');
			//alert(this.el.get('href'));
		}
	},
	loadComplete:function(){
		this.loading=false;
		this.loaded=true;
		if(this.onload)this.onload();
		//alert('image width = '+this.img.width);
	}
});

var PHPh_Box = new Class({
	top:-1,
	left:-1,
	width:-1,
	height:-1,
	box:function(t,l,w,h){
		this.top=t;
		this.left=l;
		this.width=w;
		this.height=h;
	}
});

var PHPh_clsZoomer = new Class({
  	Implements: [Options, Events],
	index:-1,
	initialized:false,
	zoom_div:false,
	border_div:false,
	zoom_image:false,
	zoom_label:false,
	zooming:false,
	start_time:false,
	currentEl:false,
	options:{},
	startPos:false,
	targetPos:false,
	borderSize:15,
	cornerRadius:8,
	fx:false,
	img:false,
	thumb:false,
	hidden:true,
	init:function(index,options){
		this.index=index;
		this.setOptions(options);
		if(!this.initialized) {
			$(document.body).adopt(
				this.zoom_div = 
					new Element("div", {id: "PHPh_ZoomDiv_"+index, 
						styles:{display:'none','z-index':'1000',position:'absolute'}})
								.addEvent("click", this.hide.bindWithEvent(this))
			);
			this.zoom_div.adopt(this.zoom_image = new Element("img", {id: "PHPh_ZoomImg_"+index,styles:{position:'relative','z-index':10002,'left':0,'top':0,'display':'none'}}).addEvent("click", this.hide.bindWithEvent(this)));
			this.zoom_div.adopt(this.border_div = new Element("div", {id: "PHPh_Zoom_BorderDiv_"+index,styles:{background:'white',opacity:.5,position:'absolute','z-index':10001,'display':'none'}}).addEvent("click", this.hide.bindWithEvent(this)));
			this.initialized=true;
		}
	},
	wait: function(){
		$$(this.zoom_image,this.border_div).setStyle('display','none');
		this.zoom_div.setStyles({
			display:'block',
			width:this.thumb.width,
			height:this.thumb.height,
			'background-color':'white',
			opacity:.2
		});
		this.hidden=false;
		this.zoom_div.position({relativeTo: this.thumb});
	},
	show: function(el){
		this.zoom_image.src=this.options['image_directory']+'/pixel.gif';
		if(el) this.currentEl=el;
		var l=this.currentEl.retrieve('loader');
		var theImage=this.img=l.img;
		var theThumb=this.thumb=l.thumb;
		if(!l.loaded) l.onload=this.show.bind(this);
		if(!l.loaded) {
			this.wait();
		} else {
			this.zoom_image.src=theImage.src;
			var scroll = window.getScroll()
			var targetL = ((window.getWidth()-theImage.width)/2)+scroll.x;
			var targetT = ((window.getHeight()-theImage.height)/2)+scroll.y;
			if(targetL<0)targetL=0;
			if(targetT<0)targetT=0;
			//startPos=new PHPh_Box(GetElementTopOffset(theThumb),GetElementLeftOffset(theThumb),theThumb.width,theThumb.height);
			this.zoom_div.setStyles({
				display:'block',
				width:theThumb.width,
				height:theThumb.height,
				'background-color':'transparent',
				opacity:.2
			});
			this.zoom_div.position({relativeTo: theThumb});
			//alert('left after mootools position:'+ this.zoom_div.getStyle('left'));
			//alert('top after mootools position:'+ this.zoom_div.getStyle('top'));
			//alert('GetElementLeftOffset(theThumb):'+ GetElementLeftOffset(theThumb));
			//alert('GetElementTopOffset(theThumb):'+ GetElementTopOffset(theThumb));
			this.zoom_image.setStyles({
				display:'',
				width:theThumb.width,
				height:theThumb.height
			});
			this.border_div.setStyles({
				display:'',
				left:0,
				top:0,
				opacity:0,
				width:theThumb.width,
				height:theThumb.width,
				'-moz-border-radius':this.cornerRadius,
				'border-radius':this.cornerRadius,
				'-webkit-border-radius':this.cornerRadius
			});
			this.hidden=false;
			this.fx = new Fx.Elements([$(this.zoom_div),$(this.zoom_image)],{'link':'chain',duration:600,transition: Fx.Transitions.Quint.easeInOut});
			this.fx_border = new Fx.Morph($(this.border_div),{'link':'chain',duration:600,transition: Fx.Transitions.Quint.easeInOut});
			this.fx.start({
				'0':{'left':targetL,'top':targetT,'width':theImage.width,'height':theImage.height,opacity:[.1,1]},
				'1':{'width':theImage.width,'height':theImage.height}
			}).chain(function(){
				//if(PHPh_Debug) PHPh_Debug.debug('PHPh_clsZoomer.show: this.border_div',this.border_div);
				//this.border_div.setStyle(display:'');
				this.fx_border.start({'width':[this.img.width,this.img.width+(this.borderSize*2)],
							'height':[this.img.height,this.img.height+(this.borderSize*2)],
							'left':(this.borderSize*-1),
							'top':(this.borderSize*-1),
							'opacity':[.2,.5]
				});
			}.bind(this));
		}
	},
	stop:function(){
		if(this.fx) this.fx.cancel();
		if(this.fx_border) this.fx_border.cancel();
	},
	hide:function(){
		//if(PHPh_Debug) PHPh_Debug.debug('PHPh_clsZoomer.hide: this.currentEl',this.currentEl);
		//var l=$(this.currentEl).retrieve('loader');
		this.stop();
		var theThumb=this.thumb;
		var loc=theThumb.getCoordinates();
		//alert('loc.left: '+loc.left+'  loc.top: '+loc.top);
		//alert('theThumb.height: '+theThumb.height+'  theThumb.width: '+theThumb.width);
		this.fx = new Fx.Elements([$(this.zoom_div),$(this.zoom_image),$(this.border_div)],{'link':'chain',duration:500,transition: Fx.Transitions.quadIn});
		this.fx.start({
			'0':{'left':loc.left,'top':loc.top,'width':theThumb.width,'height':theThumb.height,opacity:[1,.1]},
			'1':{'width':theThumb.width,'height':theThumb.height},
			'2':{'width':theThumb.width,'height':theThumb.height,'left':0,'top':0,'opacity':0}
		}).chain(this.hidefast.bind(this));
		this.currentEl=false;
	},
	hidefast: function(){
		this.stop();
		this.zoom_div.setStyle('display','none');
		this.currentEl=false;
		this.zoom_image.src=this.options['image_directory']+'/pixel.gif';
		this.hidden=true;
	},
	reset:function(){
		
	}
});

var PHPh_Zoom = new PHPh_clsZoom();

var PHPh_clsDebug = new Class({
	text:'',
	div:false,
	setup_complete:false,
	setup: function (){
		if(this.setup_complete) return true;
		$(document.body).adopt(
			this.div = new Element("div", {id: "PHPh_DebugDiv",styles:{color:'black',width:300,height:500,left:20,top:20,opacity:.5,overflow:'auto',display:'block','z-index':'10000',position:'fixed',background:'#BBF',border:'5px solid white'}})
		);
		this.setup_complete=true;
	},
	reset:function(){
		this.text='';
	},
	debug:function(msg,el){
		this.setup();
		var eltext="";
		if(el) {
			var t=$type(el);
			eltext='	$type(el) = '+t+"<br />";
			try{
				eltext+=this.getObjectDetails(el)+"<br />";
			} catch(e) {
				eltext+="Error iterating over el<br />";
			}
		}
		this.text=msg+"<br />"+(eltext?eltext+'<br /><br /><hr /><br />':'')+this.text;
		this.div.innerHTML=this.text;
	},
	getObjectDetails:function(el){
		var t=$type(el);
		if(!el||(t!='collection'&&t!='object'&&t!='array'&&t!='element'&&t!='event'))return '';
		var rval='';
		if(t=='array'||t=='object'||t=='element'||t=='event'){
			$each(el,function(v,k){
				rval+=k+' : '+v+"<br />";
			});
		}
		return rval;
	}
});

var PHPh_Debug;

window.addEvent('domready', function() { PHPh_Debug = new PHPh_clsDebug(); });
//win.addEvent("domready", function(){alert('ondomready')});
/*win.addEvent("domready", function() {
	alert('ondomready');
	$(document.body).adopt(
		$$(
			shade = new Element("div", {id: "PHPh_Shade"}).addEvent("click", close),
			zoom = new Element("div", {id: "PHPh_Zoom"}),
			label = new Element("div", {id: "PHPh_Label"})
		).setStyle("display", "none")
	);
	zoom.adopt(zoom_image=new Element("img", {id: "PHPh_Zoom_Image"}).setStyle("display", "none"));
	
	alert('done');
});*/


/*
			function PHPh_Zoom(links,duration,maxsize){
				//Adapted from kwiks on MooTools.net
				//var kwicks = $$('#kwick .kwick');
				//PHPh_StretchMenu($$('#PHPh_menu_container .menuitem'),'width',72,172,57,250)
				if(!links)links=$$('a');
				if(!duration)duration=200;
				if(!duration)maxsize=200;
				var fx = new Fx.Elements(MenuItems, {wait: false, duration: duration, transition: Fx.Transitions.quadOut});
				var fxPlate = new Fx.Elements($('food_commercial_left'), {wait: false, duration: (duration/3), transition: Fx.Transitions.quadOut});
				//var fxC = new Fx.Elements($$('div.menulinks'), {wait: false, duration: duration*3, transition: Fx.Transitions.quadOut});
				MenuItems.each(function(mi, i){
					//alert(mi);
					var thisInactiveSize = mi.id=='food_commercial'?88+inactivesize:inactivesize;
					var thisMaxsize = mi.id=='food_commercial'?88+maxsize:maxsize;
					var thisMinsize = mi.id=='food_commercial'?88+minsize:minsize;
					if(!thisMinsize) thisMinsize = minsize;
					mi.addEvent('mouseenter', function(e){
						var obj = {};
						var chobj = {};
						//var child=$(mi.id+'_links');
						//child.setStyle('opacity',0.0);
						//child.setStyle('display','block');
						obj[i] = {
							'width': [mi.getStyle(direction).toInt(), thisMaxsize]
						};
						//chobj[i] = {
						//	'opacity': [0, 1]
						//};
						//alert(1);
						MenuItems.each(function(other, j){
							if (other != mi){
								var start = other.getStyle(direction).toInt();
								if (start != thisMinsize) obj[j] = {'width': [start, other.id=='food_commercial'?88+minsize:minsize]};
								//var thisc=$(other.id+'_links');
								//$$('#'+other.id+'_links .hr').each(function(el){
								//	el.setStyle('opacity',0.0);
 								//});
								//var o = thisc.getStyle('opacity').toFloat();
								//if (o != 0) chobj[j] = {'opacity': [o, 0]};
								other.removeClass('moused');
								//$(other.id+'_bottom').removeClass('moused');
								//$(other.id+'_header').src = $(other.id+'_header').src.replace('_on\\.gif','.gif');
								//$(other.id+'_header').src = 'images/menu/'+other.id.replace(/menu_/,'')+'.gif';
							} else {
								mi.addClass('moused');
								//$(mi.id+'_bottom').addClass('moused');
								//$$('#'+mi.id+'_links .hr').each(function(el){
								//	el.setStyle('opacity',1.0);
 								//});
								//$(mi.id+'_header').src = 'images/menu/'+mi.id.replace(/menu_/,'')+'_on.gif';
							} 
						});
						//fxC.start(chobj);
						if(mi.id=='food_commercial') {
							fx.start(obj)
								.chain(function(){$('food_commercial_left').setStyle('width',86);});
							//fx.start(obj).chain(function(){fxPlate.start({'width':[0,86]})});
							//plateTimer=setTimeout("$('food_commercial_left').setStyle('display','')",350);
						} else {
							$('food_commercial_left').setStyle('width',0);
							//$('food_commercial_left').setStyle('display','none');
							fx.start(obj);
							//if(plateTimer)clearTimeout(plateTimer);
						}
					});
					//mi.addEvent('mouseleave', function(e){
					//});
				});
				
				$('PHPh_menu_container').addEvent('mouseleave', function(e){
					var obj = {};
					//var chobj = {};
					MenuItems.each(function(other, j){
						//var thisc=$(other.id+'_links');
						//$$('#'+other.id+'_links .hr').each(function(el){
						//	el.setStyle('opacity',0.0);
 						//});
						//var o = thisc.getStyle('opacity').toFloat();
						//chobj[j] = {'opacity': [o, 0]};
						obj[j] = {'width': [other.getStyle('width').toInt(), other.id=='food_commercial'?88+inactivesize:inactivesize]};
						other.removeClass('moused');
						//$(other.id+'_bottom').removeClass('moused');
						//$(other.id+'_header').src = 'images/menu/'+other.id.replace(/menu_/,'')+'.gif';
					});
					//if(plateTimer)clearTimeout(plateTimer);
					$('food_commercial_left').setStyle('width',0);
					//fxC.start(chobj);
					fx.start(obj);
				});
			}
			function testFood() {
				$('architecture').setStyle('width','69px');
				$('landscape').setStyle('width','69px');
				$('nature').setStyle('width','69px');
				$('food_commercial').setStyle('width','492px');
				$('food_commercial_left').setStyle('width','86px');
			}		
*/


function GetElementLeftOffset(el){
   	var l = el.offsetLeft;
   	var p = el.offsetParent;
   	while(p != null){
       	if(p.scrollLeft)l-=p.scrollLeft;
		l += p.offsetLeft;
       	p = p.offsetParent;  
   	}
   	return l;
}

function GetElementTopOffset(el){
   	var t = el.offsetTop;
   	var p = el.offsetParent; 
	while (p != null){
		if(p.scrollTop)t-=p.scrollTop;
		t += p.offsetTop;
       	p = p.offsetParent;
   	}
	return t;
}

