document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++)
	{
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};

function textbox_activate(ele, value)
{
 	if(ele.value.toLowerCase() == value.toLowerCase())
	{
		ele.value = '';
		ele.className = ele.className.replace('inactive','active');
	}
}

function textbox_inactivate(ele, value)
{
 	if(ele.value == '' || ele.value.toLowerCase() == value.toLowerCase())
	{
		ele.value=value;
		ele.className = ele.className.replace('active','inactive');
	}
}

function limitText(limitField, limitNum)
{
	if (limitField.value.length > limitNum)
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

function CheckNumeric(e)
{
	if(window.event) // IE coding
	{
	//	Get ASCII value of key that user pressed
		var key = window.event.keyCode;

	//	Was key that was pressed an allowed character?
		if	( key == 44 )				return; // ,
		if	( key == 46 )   			return; // . AND del .... yeah right ...
		if	( key > 47 && key < 58 )	return; // numeric character (0-9)

	//  properly permit the key being entered
		window.event.returnValue = null;
	}
	else
	{
	//	Get ASCII value of key that user pressed
		var key = e.which

	//	Was key that was pressed an allowed character?
		if	( key == 0)					return; // function keys (F1 - F12, arrows, etc.)
		if  ( key == 8 )                return; // backspace
		if	( key == 44 )				return; // ,
		if	( key == 46 )   			return; // . AND del .... yeah right ...
		if	( key > 47 && key < 58 )	return; // numeric character (0-9)

	//  properly permit the key being entered
		e.preventDefault();
 	}
}

function CheckPhone(e)
{
	if(window.event) // IE coding
	{
	//	Get ASCII value of key that user pressed
		var key = window.event.keyCode;
	//	Was key that was pressed an allowed character?
		if	( key == 32 )				return; // space
		if	( key == 35 )				return; // #
		if	( key == 40 )				return; // ,
		if	( key == 41 )				return; // (
		if	( key == 42 )				return; // )
		if	( key == 43 )				return; // +
		//if	( key == 44 )				return; // ,
		if	( key == 45 )				return; // -
		//if	( key == 46 )   			return; // . AND del .... yeah right ...
		if	( key > 47 && key < 58 )	return; // numeric character (0-9)

	//  properly permit the key being entered
		window.event.returnValue = null;
	}
	else
	{
	//	Get ASCII value of key that user pressed
		var key = e.which

	//	Was key that was pressed an allowed character?
		if	( key == 0)					return; // function keys (F1 - F12, arrows, etc.)
		if  ( key == 8 )                return; // backspace
		if	( key == 32 )				return; // space
		if	( key == 35 )				return; // #
		if	( key == 40 )				return; // ,
		if	( key == 41 )				return; // (
		if	( key == 42 )				return; // )
		if	( key == 43 )				return; // +
		//if	( key == 44 )				return; // ,
		if	( key == 45 )				return; // -
		//if	( key == 46 )   			return; // . AND del .... yeah right ...
		if	( key > 47 && key < 58 )	return; // numeric character (0-9)

	//  properly permit the key being entered
		e.preventDefault();
 	}
}

function toggleNext(el)
{
	var next=el.nextSibling;
	while(next.nodeType != 1) next = next.nextSibling;
	next.style.display = ((next.style.display == "none") ? "block" : "none");
}

function toggleNextByClassName(cname)
{
	var clickers = document.getElementsByClassName(cname);
 	for (i=0; i<clickers.length; i++)
	{
		clickers[i].onclick=function() {toggleNext(this)}
		toggleNext(clickers[i]);
	}
}

function toggleDisplay(id)
{
	if(document.getElementById(id).style.display == "block" )
	{
		document.getElementById(id).style.display = "none";
		document.getElementById(id).style.visibility = "hidden";
	}
	else
	{
		document.getElementById(id).style.display = "block";
		document.getElementById(id).style.visibility = "visible";
	}
}




var slideshow=new Class({
	initialize:function(a){
		this.items = a.box.getChildren();
		this.mode = a.mode||'horizontal';
		this.modes = {horizontal:['margin-left','width'],vertical:['top','height']};
		this.size = a.size||a.box.getStyle('width').toInt();
		this.box = a.box.setStyle(this.modes[this.mode][1],(this.size*this.items.length)+'px');
		this.button_event = a.button_event||'click';
		this.handle_event = a.handle_event||'click';
		this.onWalk = a.onWalk||null;
		this.currentIndex=null;
		this.previousIndex=null;
		this.nextIndex=null;
		this.interval=a.interval||5000;
		this.autoPlay=a.autoPlay||false;
		this._play=null;
		this.handles=a.handles||null;
		if(this.handles){
			this.addHandleButtons(this.handles)
		}
		this.buttons={previous:[],next:[],play:[],playback:[],stop:[]};
		if(a.addButtons){
			for(var b in a.addButtons){
				this.addActionButtons(b,$type(a.addButtons[b])=='array'?a.addButtons[b]:[a.addButtons[b]])
			}
		}
		this.fx=new Fx.Tween(this.box,$extend((a.fxOptions||{duration:500,wait:false}),{property:this.modes[this.mode][0]}));
		this.walk((a.startItem||0),true,true)
	},addHandleButtons:function(a){
		for(var i=0;i<a.length;i++){
			a[i].addEvent(this.handle_event,this.walk.bind(this,[i,true]))
		}
	},addActionButtons:function(a,b){
		for(var i=0;i<b.length;i++){
			switch(a){
				case 'previous':
					b[i].addEvent(this.button_event,this.previous.bind(this,[true]));
				break;
				case 'next':
					b[i].addEvent(this.button_event,this.next.bind(this,[true]));
				break;
				case 'play':
					b[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'next',false]));
				break;
				case 'playback':
					b[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'previous',false]));
				break;
				case 'stop':
					b[i].addEvent(this.button_event,this.stop.bind(this));
				break
			}
			this.buttons[a].push(b[i])
		}
	},previous:function(a){
		this.walk((this.currentIndex>0?this.currentIndex-1:this.items.length-1),a)
	},next:function(a){
		this.walk((this.currentIndex<this.items.length-1?this.currentIndex+1:0),a)
	},play:function(a,b,c){
		this.stop();
		if(!c){
			this[b](false)
		}
		this._play=this[b].periodical(a,this,[false])
	},stop:function(){
		$clear(this._play)
	},walk:function(a,b,c){
		if(a!=this.currentIndex){
			this.currentIndex=a;
			this.previousIndex=this.currentIndex+(this.currentIndex>0?-1:this.items.length-1);
			this.nextIndex=this.currentIndex+(this.currentIndex<this.items.length-1?1:1-this.items.length);
			if(b){
				this.stop()
			}
			if(c){
				this.fx.cancel().set((this.size*-this.currentIndex)+'px')
			}else{
				this.fx.start(this.size*-this.currentIndex)
			}
			if(b&&this.autoPlay){
				this.play(this.interval,'next',true)
			}
			if(this.onWalk){
				this.onWalk((this.items[this.currentIndex]||null),(this.handles&&this.handles[this.currentIndex]?this.handles[this.currentIndex]:null))
			}
		}
	}
});

var viewer = new Class({
	mode:'rand',
	modes:['top','right','bottom','left','alpha'],
	sizes:{w:480,h:240},
	fxOptions:{duration:500},
	interval:5000,
	initialize: function(a,b){
		if(b) for(var o in b) this[o]=b[o];
		if(this.buttons){
			this.buttons.previous.addEvent('click',this.previous.bind(this,[true]));
			this.buttons.next.addEvent('click',this.next.bind(this,[true]))
		}
		this._current=0;
		this._previous=null;
		this.items=a.setStyle('display','none');
		this.items[this._current].setStyle('display','block');
		this.disabled=false;
		this.attrs={
			left:['left',-this.sizes.w,0,'px'],
			top:['top',-this.sizes.h,0,'px'],
			right:['left',this.sizes.w,0,'px'],
			bottom:['top',this.sizes.h,0,'px'],
			alpha:['opacity',0,1,'']
		};
		this.rand=this.mode=='rand';
		this.sequence=typeof(this.mode)=='object'?this.mode:false;
		this.curseq=0;
		this.timer=null
	},
	walk: function(n,b){
	//alert(n);
		if(this._current!==n&&!this.disabled){
			this.disabled=true;
			if(b){this.stop()}
			if(this.rand){this.mode=this.modes.getRandom()}
			else if(this.sequence){
				this.mode=this.sequence[this.curseq];
				this.curseq+=this.curseq+1<this.sequence.length?1:-this.curseq
			}
			this._previous = this._current;
			this._current = n;
			var a = this.attrs[this.mode].associate(['p','f','t','u']);
			for(var i = 0; i < this.items.length; i++){
				if(this._current === i){
					this.items[i].setStyles($extend({'display':'block','z-index':'2'},JSON.decode('{"'+a.p+'":"'+a.f+a.u+'"}')));
//					this.items[i].setStyles($extend({},JSON.decode('{"'+a.p+'":"'+a.f+a.u+'"}')));
				}
				else if(this._previous === i){
					this.items[i].setStyles({'z-index':'1'})
				}
				else{
					this.items[i].setStyles({'display':'none','z-index':'0'})
//					this.items[i].setStyles({})
				}
			}
			this.items[n].set('tween',$merge(this.fxOptions,{onStart:this.onComplete.bind(this)})).tween(a.p,a.f,a.t)
		}
	},
	play: function(a){
		this.stop();
		if(!a){this.next()}
		this.timer=this.next.periodical(this.interval,this,[false])
	},
	stop: function(){
		$clear(this.timer)
	},
	next: function(a){
		this.walk(this._current+1<this.items.length?this._current+1:0,a)
	},
	previous: function(a){
		this.walk(this._current>0?this._current-1:this.items.length-1,a)
	},
	onComplete: function(){
		this.disabled=false;
		this.items[this._previous].setStyle('display','none');
		if(this.onWalk)this.onWalk(this._current)
	}
});



			
//window.addEvent('domready', function(){
function initializeViewer() {
//	load the viewer
	if($$('.viewer_mask').length > 0) {
		var v = new viewer($$('.viewer_mask'),{
			mode: 'alpha',
			fxOptions: {duration: 1000}
		});
		$$('.viewer_button').each(function(el,i) {
			el.addEvent('click', v.walk.bind(v,[i,true]));
		});
	}
}


function initializeSlideshow() {	
//	load the slideshows
	$$('.slideshow').each(function(show, i) {
	//	set the start item
		var startItem = 0;
					
	//	add the thumbnails section if it doesn't already exist
		var thumbs = show.getElement('.slideshow_thumbs');
		if(!thumbs)
		{
			var thumbs = new Element('div', {'class': 'slideshow_thumbs'});
			var copy = show.getElement('div.slideshow_viewport ul').clone();
			copy.getElements('li').each(function(el) {
				el.grab(el.getElement('img'));
				//el.dispose('a');
			});
			copy.inject(thumbs);
			thumbs.inject(show, 'bottom');
		}
		var thumb_width = thumbs.getElements('img').getLast().getStyle('width').toInt();
		var view_width = show.getElements('div.slideshow_viewport img').getLast().getStyle('width').toInt();
		
	//	set the options that define the grafix
		var fxOptions = {property:'margin-left', duration:2000, transition:Fx.Transitions.Back.easeOut, wait:false}

	//	set the tween fx for the thumbs
		thumbs.getElement('ul').setStyle('margin-left','0px');
		var thumbsFx = new Fx.Tween(thumbs.getElement('ul'), fxOptions);
					
	//	initiate the slideshow class
		var slide = new slideshow({
			box: show.getElement('.slideshow_viewport ul'),
			handles: show.getElements('.slideshow_thumbs ul li'),
			fxOptions: fxOptions,
			onWalk: function(currentItem) {
//alert((view_width / thumb_width / 2).ceil());	
				if(this.currentIndex > (this.items.length - ((view_width / thumb_width / 2).ceil()+0))) {
					thumbsFx.start((this.items.length - (view_width / thumb_width)) * -thumb_width);
				} 
				else if(this.currentIndex < (view_width / thumb_width / 2).floor()) {
					thumbsFx.start(0);
				}
				else {
					thumbsFx.start((this.currentIndex - ((view_width / thumb_width / 2).ceil()-1)) * -thumb_width);
				}
				thumbs.getElements('img').set('opacity', 0.382);
				this.handles[this.currentIndex].getElement('img').set('opacity', 1);
			},
			startItem: startItem
		});
	//	walk to first with fx
		//alert(slide.items.length);
//		alert(Math.min(3,slide.items.length));
		slide.walk(Math.min(3,slide.items.length)-1);
	});
}
//});
	



