function isEmpty(val){ if(val == null){return true;} for(var i=0; i < val.length; i++) { if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){ return false; } } return true; }
function isEmail(val){if (isEmpty(val)){ return false; }var i = 1,length = val.length;while ((i < length) && (val.charAt(i) != "@")){i++;}if ((i >= length) || (val.charAt(i) != "@")){ return false; }else { i += 2; }while ((i < length) && (val.charAt(i) != ".")){i++;}if ((i >= length - 1) || (val.charAt(i) != ".")){ return false; }else { return true; }}
function isPhone(val){ pattern = new RegExp(/^[0-9-\s+]{5,16}$/); if (!pattern.test(val)) { return false; } return true; }
function onSelect(what){ var destination = what.options[what.selectedIndex].value; if (destination) location.href = destination; }
function isInt(val){var y = parseInt(val);if(isNaN(y)) return false;return val == y && val.toString() == y.toString() && y > 0;} 
function isChecked(checkboxes,qty){var chks = checkboxes, checkCount = 0, checkValues = [];for (var i = 0; i < chks.length; i++){if (chks[i].checked){ checkCount++; checkValues.include(chks[i].value);}}if (checkCount < qty){ return false; }return checkValues;}
function printArea(id){var html = '<html>';html += document.getElementById(id).innerHTML;html += '</html>';var printWin = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');printWin.document.write(html);printWin.document.close();printWin.focus();printWin.print();printWin.close();}

var jsbox = new Class({
	Implements:[Options,Events],
	options:{from:'.jsbox', width:120, height:120, htmlWidth:550, htmlHeight:250, opacity:0.7, borderWidth:10, borderColor:'#fff'},
	initialize:function(options){
		this.setOptions(options);
		this.timer = 0;
		this.index = 0;
		this.opened = false;
		this.openClosePos = {};
		this.attributes = {};
		this.object = {};
		this.build();
		
		this.from = $$(this.options.from);
		
		this.from.each(function(el,i){
			el.addEvent('click',function(e){
				e.stop();
				this.open(el);
			}.bind(this));
		},this);
	},
	build:function(){
		this.overlay = new Element('div',{
			html: '&nbsp;',
			styles: {display:'none',position:'fixed','opacity':this.options.opacity,background: '#000',left: 0,top: 0,'z-index': 99},
			events:{'click':this.close.bind(this)}
		}).inject(document.body,'top');
		
		var closeButton = new Element('div',{'class':'jswin-close'}).addEvent('click',this.close.bind(this));
		
		this.prevLink = new Element('div',{'class':'jswin-prevlink'}).addEvent('click',this.previous.bind(this));
		this.nextLink = new Element('div',{'class':'jswin-nextlink'}).addEvent('click',this.next.bind(this));
		
		this.content = new Element('div',{'class':'jswin-content'});
		this.win = new Element('div',{'class':'jswin jswin-hide',styles:{display:'none','border-width':this.options.borderWidth,'border-color':this.options.borderColor}}).adopt(closeButton,this.content).inject(document.body,'top');
		
		this.fxwin = new Fx.Morph(this.win,{duration:500,transition: Fx.Transitions.Cubic.easeOut});
		this.fxcontent = new Fx.Tween(this.content,{property:'opacity',duration:250}).set(0);
	},
	showCaption:function(){
		this.captionInner = new Element('div',{'class':'jswin-captionInner'});
		caption = new Element('div',{'class':'jswin-caption'}).adopt(this.captionInner);
		this.fxcaption = new Fx.Tween(caption,{property:'height',duration:250});
	},
	showOverlay:function(){
		var ssize = document.getScrollSize();
		this.overlay.setStyles({display:'', width:ssize.x, height:ssize.y});
	},
	getOpenClosePos:function(el){
		var cords = el.getFirst() ? el.getFirst().getCoordinates() : el.getCoordinates();
		this.openClosePos = {
			width: cords.width < 0 || cords.width > 400 ? 32 : cords.width-(this.options.borderWidth*2),
			height: cords.height < 60 ? 32 : cords.height-(this.options.borderWidth*2),
			top: cords.top,
			left: cords.left
		};
		return this.openClosePos;
	},
	open:function(el){
		this.index = this.from.indexOf(el);
		this.getOpenClosePos(this.from[this.index]);
		if(!this.opened){
			this.opened = true;
			this.showOverlay();
			this.win.setStyles(Object.merge({display:'', opacity: 0.6}, this.openClosePos));
			this.win.addClass('jswin-loading');
			this.load(this.index);
		}
		else{
			this.hide(this.index);
		}
		return false;
	},
	load:function(index){
		var link = this.from[index];
		
		this.attributes = {number: index+1};
		
		this.win.addClass('jswin-loading');
		
		var extension = link.href.split('?')[0].substr(link.href.lastIndexOf('.')+1).toLowerCase();
		
		switch(extension){
			case'jpg':case'gif':case'png':
				this.type = 'image';
				
				this.object = new Image();
				this.object.onload = this.show.bind(this);
				this.object.src = link.href;
			break;
			default:
				this.type = 'html';
				
				var html_height = this.options.htmlHeight, html_width = this.options.htmlWidth, url = link.href;
				
				this.win.setStyle('border',0);
				
				this.object = new Element('div',{'class':'jswin-html', styles:{height:html_height}}).load(url);
				this.object.width = html_width;
				this.object.height = html_height;
				this.show();
			break;
		}
	},
	show:function(){
		var top=(window.getHeight()/2)-(((this.object.height.toInt()+24)/2))+window.getScrollTop(),left=(window.getWidth()/2)-(this.object.width/2);
		
		if(top < 0){top = 30;}
		if(left < 0){left = 0;}
		
		this.fxwin.cancel().start({width:this.object.width,height:this.object.height,top:top,left:left,opacity:1}).chain(function(){
			this.win.setStyle('height','').removeClass('jswin-hide').removeClass('jswin-loading');
			this.content.empty().adopt(this.object, this.prevLink, this.nextLink).fade('in');
			
			if(this.from.length > 1){
				if(this.type != 'html'){
					$$(this.prevLink, this.nextLink).setStyle('height', this.object.height);
					this.attributes.number == 1 ? this.prevLink.style.display = 'none' : this.prevLink.style.display = 'block';
					this.attributes.number == this.from.length ? this.nextLink.style.display = 'none' : this.nextLink.style.display = 'block';
				}
			}
		}.bind(this));
	},
	hide:function(index){
		this.fxcontent.cancel().start(0).chain(function(){
			this.load(index);
		}.bind(this));
	},
	next:function(){
		if(this.index<this.from.length-1){
			this.index++;
			this.getOpenClosePos(this.from[this.index]);
			this.hide(this.index);
		}
	},
	previous:function(){
		if(this.index>0){
			this.index--;
			this.getOpenClosePos(this.from[this.index]);
			this.hide(this.index);
		}
	},
	close:function(){
		this.win.addClass('jswin-hide').setStyle('height',this.object.height);
		this.content.empty();
		this.overlay.setStyles({display:'none',width:0,height:0});
		
		this.fxwin.cancel().start({width:this.openClosePos.width, height:this.openClosePos.height, top:this.openClosePos.top, left:this.openClosePos.left, opacity:0.2}).chain(function(){
			this.opened = false;
			this.win.setStyle('display','none');
			this.openClosePos = {};
		}.bind(this));
	}
});

Element.implement({
	inputHint:function(val){
		switch(this.get('tag')){
			case'form':this.getElements('input[type="text"],textarea').inputHint(val);return this;
			case'input': case'textarea':
				this.store('default',(val || this.get('value')));
				this.addEvents({
					'focus':function(){
						if(this.get('value') == this.retrieve('default')){this.set('value','');}
					},
					'blur':function(){
						if(this.get('value').clean() == ''){ this.set('value',this.retrieve('default')); }
					}
				}).fireEvent('blur');
			default:return this;
		}
	}
});

