

beforeLoad = function(thisBody) {
	thisBody.setStyle('opacity',0);
//	fadeFX(thisBody,0);
}
afterLoad = function(thisBody) {
//	thisBody.setStyle('opacity',0);
	fadeFX(thisBody,1);
}


fadeFX = function(thisBody,lastvalue) {
	if (!thisBody.fadeFx)	thisBody.fadeFx = new Fx.Style(thisBody, 'opacity', {fps:50, duration:1000, wait: true});
	var x=thisBody.fadeFx;
	if (!x.busyFade) {
		x.busyFade=true;
		x.stop();
		x.start(thisBody.getStyle('opacity'),lastvalue);
		setTimeout(function() { x.busyFade=false; },1000);
	} else {
		setTimeout(function() { fadeFX(thisBody,lastvalue); },50);
	}
}

shiftFX = function(thisBody,lastvalue) {
	if (!thisBody.shiftFx)	thisBody.shiftFx = new Fx.Style(thisBody, 'left', {fps:50, duration:1000, wait: true});
	if (!x.busyShift) {
		x.busyShift=true;
		x.stop();
		x.start(thisBody.getStyle('left'),lastvalue);
		setTimeout(function() { x.busyShift=false; },1000);
	} else {
		setTimeout(function() { shiftFX(thisBody,lastvalue); },50);
	}
}


hoverFX = function(a, group) 
{
	hoverFX.over = function(ev) {	
		var xy=a.getPosition([$('wrapper1')]);
		//debug(0,xy.x);
		$(a.extimg).setStyles( { "left": xy.x, "top": xy.y });
		if (!a.hasClass('on')) a.extimg.myFX.set(1);
	}
	
	hoverFX.out = function(ev) {
		a.extimg.myFX.start(a.extimg.getStyle('opacity'),0.01);
	}	
	
	var s=a.getProperty('hoverimg');
  if ((/\.png$/i).test(s)) if (window.ie6) return false;
	//a.addEvent('mouseover', hoverFX.over).addEvent('mouseout', hoverFX.out);
	a.extimg=new Image();
	$(a.extimg).setProperty('src',s).addClass('hoverimg').setOpacity(0.01);
	a.addEvent('mouseover', function(e) {
		var xy=a.getPosition([$('wrapper1')]);
		a.extimg.setStyles( { "left": xy.x, "top": xy.y }).injectInside(document.body)
	});
	a.removeEvent('mouseout', function(e) {
		a.extimg.remove();
		debug(0,'remove');
	});
	a.extimg.addEvent('mouseout', hoverFX.out).addEvent('mouseover', hoverFX.over).addEvent('click', function() { a.onclick(); });
	a.extimg.myFX = new Fx.Style(a.extimg, 'opacity');
	
	if (group) group.push(a);
}

randomTransition =function() { 
	switch (window.status=Math.floor(Math.random()*7)) { 
		case 0:	return Fx.Transitions.Quad;
		case 1:	return Fx.Transitions.Cubic;
		case 2:	return Fx.Transitions.Quart;
		case 3:	return Fx.Transitions.Sine;
		case 4:	return Fx.Transitions.Quint;
		case 5:	return Fx.Transitions.Expo;
		case 6:	return Fx.Transitions.Circ;
		default: return Fx.Transitions.linear;
	}
}

galeryFX = function(img, options) 
{
	if (!options) options={};
	if (!options.group) options.group=[];
	if (!options.duration) options.duration=565;
	if (!options.container) options.container=$('wrapper1');
	if (!options.border) options.border=function() { return 20; } //Math.random()*20; };
	
	options.state=0;
	img.addClass('imgtozoom');
	galeryFX.over = function(ev) {
		if (options.modal) 
			if (options.state!=0) return;	
		img.addClass('on');
	}
	galeryFX.out = function(ev) {
		if (options.modal) 
			if (options.state!=0) return;
		img.removeClass('on');
	}
	galeryFX.zoom=function(n, from, to) {
		n.effect('top', {duration: options.duration, transition: randomTransition()}).start(from.top,to.top);
		n.effect('left', {duration: options.duration, transition: randomTransition()}).start(from.left,to.left);
		n.effect('width', {duration: options.duration, transition: randomTransition()}).start(from.width,to.width);
		n.effect('height', {duration: options.duration, transition: randomTransition()}).start(from.height,to.height);	
		n.effect('opacity', {duration: options.duration, transition: randomTransition()}).start(from.opacity,to.opacity);	
	}
	galeryFX.getCoor=function(obj) {
		var o=obj.getCoordinates([options.container]);
		var bigw=img.getProperty('bigw');
		var bigh=img.getProperty('bigh');
		o.width=bigw?bigw:(obj.w?obj.w:o.width);
		o.height=bigh?bigh:(obj.h?obj.h:o.height);
		return o;
	}
	galeryFX.clickopen = function(ev) {
		debug(0,'clickopen',options.modal);
		options.activeimg=img;
		if (options.modal) {
			if (options.state!=0) return;
			if (!options.modalDiv) {
				var uid='modalDiv';
				options.modalDiv=$(uid)?$(uid):new Element('DIV');
				options.modalDiv.id=uid;
				$(options.modalDiv).addEvent('click', galeryFX.clickclose);
			}
			$(options.modalDiv).setStyles({position:'absolute', opacity:0}).setStyles(options.container.getCoordinates()).injectTop(document.body);
			options.modalDiv.effect('opacity', { duration: options.duration }).start(0,options.modal);
		}
		var xy=galeryFX.getCoor(img);
		xy.opacity=0;
		var c=options.container.getCoordinates();
		c.x=c.width/2+c.left;
		c.y=c.height/2+c.top;
		var s=Math.floor(Math.random()*4);
		options.state=1;
		img.listshow=img.listload;
		var l;
		for(var i=1; i<=img.listshow; i++) {
			var to=galeryFX.getCoor(img.list[i]);
			delete to.right;
			delete to.bottom;
			to.opacity=1;
			l=(i-1+s)%4+1;
			with (to) switch (l) {
				case 1: left=c.x-to.width-options.border(); 
								top=c.y-to.height-options.border();
								break;
				case 2: left=c.x+options.border();
								top=c.y-to.height-options.border();
								break;
				case 3:	left=c.x+options.border();
								top=c.y+options.border();
								break;
				case 4: left=c.x-to.width-options.border();
								top=c.y+options.border();
								break;		
			}
			galeryFX.zoom(img.list[i].setStyles(xy), xy, to);
		}
		var tid=img.getProperty('exthtml').replace(/\W/ig,'_');
		if (!(img.htmlcontent=$(tid))) {	
			img.htmlcontent=new Element('DIV');
			$(img.htmlcontent).setProperty('id',tid).addClass('zoomtext').injectTop(document.body);

			if (!img.ajax) 
				img.ajax=new Ajax(img.getProperty('exthtml'), { update: $(img.htmlcontent), method:'get', onComplete:function() {
					img.htmlcontent.effect('opacity', { duration: options.duration*2 }).start(0,1);
				}}).request();
			
		} else img.htmlcontent.effect('opacity', { duration: options.duration*2 }).start(0,1);
		var to=galeryFX.getCoor(img.htmlcontent);
		with (to) switch (l) {
				case 4: left=c.x-to.width-options.border(); 
								top=c.y-to.height-options.border();
								break;
				case 1: left=c.x+options.border();
								top=c.y-to.height-options.border();
								break;
				case 2:	left=c.x+options.border();
								top=c.y+options.border();
								break;
				case 3: left=c.x-to.width-options.border();
								top=c.y+options.border();
								break;		
		}
		img.htmlcontent.setStyles(to);
		
	}
	galeryFX.clickclose = function(ev) {
		var img=options.activeimg;
		if (options.state!=1) return;
		options.state=2;
		if (options.modal&&options.modalDiv) options.modalDiv.effect('opacity', { duration: options.duration }).start(options.modal,0);		
		var to=galeryFX.getCoor(img);
		to.opacity=0;
		for(var i=1; i<=img.listshow; i++) {
			var from=galeryFX.getCoor(img.list[i]);
			from.opacity=1;
			galeryFX.zoom(img.list[i], from, to);
			setTimeout(function() { options.state=0; }, options.duration);
		}
		img.removeClass('on'); 
		img.htmlcontent.effect('opacity', { duration: options.duration }).start(1,0);
	}
	img.addEvent('mouseout', galeryFX.out).addEvent('mouseover', galeryFX.over).addEvent('click', galeryFX.clickopen);
	img.list=[];
	img.list[img.i=0]=img;
	img.loadimg = function() {
		img.i++;
		var n=img.getProperty('src').replace(/.(\w+)$/,img.i+'.$1');
		var t=n.replace(/\W/ig,"");
		debug(0,t);
		if ($(t)) img.list[img.i]=$(t);
		else img.list[img.i]=new Element('IMG');
		$(img.list[img.i]).setProperties({ 'src':n, 'id':t }).addClass('zoomimg').addEvent('click', galeryFX.clickclose).injectTop(document.body);
	}
	img.listload=0;
	img.loadimg();
	img.cachehandler=setInterval(function() {
		debug(0,img.i);
		if (img.list[img.i].width>0) {
			img.listload++;
			img.list[img.i].w=img.list[img.i].w?img.list[img.i].w:img.list[img.i].width; 
			img.list[img.i].h=img.list[img.i].h?img.list[img.i].h:img.list[img.i].height;  
			if (img.i<img.getProperty('numimg')) img.loadimg();
			else clearInterval(img.cachehandler);
		}
	}.bind(img),200);
	
	options.group.push(img);
}



	
	highlightSearchTerms = function(bodyText,searchText, highlightStartTag, highlightEndTag)
	{
	  var searchArray = searchText.split(/\s+/ig);
	  for (var i = 0; i < searchArray.length; i++) {
	    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
	  }
	  return bodyText; 
	}
	
	
	
	doHighlight=function(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
	{
	  if ((!highlightStartTag) || (!highlightEndTag)) {
	    highlightStartTag = "<b>";
	    highlightEndTag = "</b>";
	  }
	
	  var newText = "";
	  var i = -1;
	  var lcSearchTerm = searchTerm.toLowerCase();
	  var lcBodyText = bodyText.toLowerCase();
	    
	  while (bodyText.length > 0) {
	    i = lcBodyText.indexOf(lcSearchTerm, i+1);
	    if (i < 0) {
	      newText += bodyText;
	      bodyText = "";
	    } else {
	      // skip anything inside an HTML tag
	      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
	        // skip anything inside a <script> block
	        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
	          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
	          bodyText = bodyText.substr(i + searchTerm.length);
	          lcBodyText = bodyText.toLowerCase();
	          i = -1;
	        }
	      }
	    }
	  }
	  
	  return newText;
	}


sendemail=function(myform) {
		debug(0,'sendemail');
		if (!myValidator._isValid()) return false;
		
		$(myform).send({ onComplete: function() {
			debug(0,this.response.text);
			$('formresponse').setStyles({'opacity':1, 'display':'inline'});
			$('sendbtn').setStyles({ 'display':'none', 'opacity':0 });
		}});
		
		function blurit(i) {
			i.disabled=true;
			i.setStyle('opacity',0.6);
		}
		$(myform).getElements('input').each(blurit);			
		$(myform).getElements('select').each(blurit);	
		$(myform).getElements('textarea').each(blurit);					
		return false;
	}
