// MooTools: the javascript framework.
// Load this file's selection again by visiting: http://mootools.net/more/f7005197184c1ad698fa1b435a9aecc0 
// Or build this file again with packager using: packager build More/Drag More/Drag.Move
/*
---
copyrights:
  - [MooTools](http://mootools.net)

licenses:
  - [MIT License](http://mootools.net/license.txt)
...
*/
MooTools.More={version:"1.4.0.1",build:"a4244edf2aa97ac8a196fc96082dd35af1abab87"};var Drag=new Class({Implements:[Events,Options],options:{snap:6,unit:"px",grid:false,style:true,limit:false,handle:false,invert:false,preventDefault:false,stopPropagation:false,modifiers:{x:"left",y:"top"}},initialize:function(){var b=Array.link(arguments,{options:Type.isObject,element:function(c){return c!=null;
}});this.element=document.id(b.element);this.document=this.element.getDocument();this.setOptions(b.options||{});var a=typeOf(this.options.handle);this.handles=((a=="array"||a=="collection")?$$(this.options.handle):document.id(this.options.handle))||this.element;
this.mouse={now:{},pos:{}};this.value={start:{},now:{}};this.selection=(Browser.ie)?"selectstart":"mousedown";if(Browser.ie&&!Drag.ondragstartFixed){document.ondragstart=Function.from(false);
Drag.ondragstartFixed=true;}this.bound={start:this.start.bind(this),check:this.check.bind(this),drag:this.drag.bind(this),stop:this.stop.bind(this),cancel:this.cancel.bind(this),eventStop:Function.from(false)};
this.attach();},attach:function(){this.handles.addEvent("mousedown",this.bound.start);return this;},detach:function(){this.handles.removeEvent("mousedown",this.bound.start);
return this;},start:function(a){var j=this.options;if(a.rightClick){return;}if(j.preventDefault){a.preventDefault();}if(j.stopPropagation){a.stopPropagation();
}this.mouse.start=a.page;this.fireEvent("beforeStart",this.element);var c=j.limit;this.limit={x:[],y:[]};var e,g;for(e in j.modifiers){if(!j.modifiers[e]){continue;
}var b=this.element.getStyle(j.modifiers[e]);if(b&&!b.match(/px$/)){if(!g){g=this.element.getCoordinates(this.element.getOffsetParent());}b=g[j.modifiers[e]];
}if(j.style){this.value.now[e]=(b||0).toInt();}else{this.value.now[e]=this.element[j.modifiers[e]];}if(j.invert){this.value.now[e]*=-1;}this.mouse.pos[e]=a.page[e]-this.value.now[e];
if(c&&c[e]){var d=2;while(d--){var f=c[e][d];if(f||f===0){this.limit[e][d]=(typeof f=="function")?f():f;}}}}if(typeOf(this.options.grid)=="number"){this.options.grid={x:this.options.grid,y:this.options.grid};
}var h={mousemove:this.bound.check,mouseup:this.bound.cancel};h[this.selection]=this.bound.eventStop;this.document.addEvents(h);},check:function(a){if(this.options.preventDefault){a.preventDefault();
}var b=Math.round(Math.sqrt(Math.pow(a.page.x-this.mouse.start.x,2)+Math.pow(a.page.y-this.mouse.start.y,2)));if(b>this.options.snap){this.cancel();this.document.addEvents({mousemove:this.bound.drag,mouseup:this.bound.stop});
this.fireEvent("start",[this.element,a]).fireEvent("snap",this.element);}},drag:function(b){var a=this.options;if(a.preventDefault){b.preventDefault();
}this.mouse.now=b.page;for(var c in a.modifiers){if(!a.modifiers[c]){continue;}this.value.now[c]=this.mouse.now[c]-this.mouse.pos[c];if(a.invert){this.value.now[c]*=-1;
}if(a.limit&&this.limit[c]){if((this.limit[c][1]||this.limit[c][1]===0)&&(this.value.now[c]>this.limit[c][1])){this.value.now[c]=this.limit[c][1];}else{if((this.limit[c][0]||this.limit[c][0]===0)&&(this.value.now[c]<this.limit[c][0])){this.value.now[c]=this.limit[c][0];
}}}if(a.grid[c]){this.value.now[c]-=((this.value.now[c]-(this.limit[c][0]||0))%a.grid[c]);}if(a.style){this.element.setStyle(a.modifiers[c],this.value.now[c]+a.unit);
}else{this.element[a.modifiers[c]]=this.value.now[c];}}this.fireEvent("drag",[this.element,b]);},cancel:function(a){this.document.removeEvents({mousemove:this.bound.check,mouseup:this.bound.cancel});
if(a){this.document.removeEvent(this.selection,this.bound.eventStop);this.fireEvent("cancel",this.element);}},stop:function(b){var a={mousemove:this.bound.drag,mouseup:this.bound.stop};
a[this.selection]=this.bound.eventStop;this.document.removeEvents(a);if(b){this.fireEvent("complete",[this.element,b]);}}});Element.implement({makeResizable:function(a){var b=new Drag(this,Object.merge({modifiers:{x:"width",y:"height"}},a));
this.store("resizer",b);return b.addEvent("drag",function(){this.fireEvent("resize",b);}.bind(this));}});Drag.Move=new Class({Extends:Drag,options:{droppables:[],container:false,precalculate:false,includeMargins:true,checkDroppables:true},initialize:function(b,a){this.parent(b,a);
b=this.element;this.droppables=$$(this.options.droppables);this.container=document.id(this.options.container);if(this.container&&typeOf(this.container)!="element"){this.container=document.id(this.container.getDocument().body);
}if(this.options.style){if(this.options.modifiers.x=="left"&&this.options.modifiers.y=="top"){var c=b.getOffsetParent(),d=b.getStyles("left","top");if(c&&(d.left=="auto"||d.top=="auto")){b.setPosition(b.getPosition(c));
}}if(b.getStyle("position")=="static"){b.setStyle("position","absolute");}}this.addEvent("start",this.checkDroppables,true);this.overed=null;},start:function(a){if(this.container){this.options.limit=this.calculateLimit();
}if(this.options.precalculate){this.positions=this.droppables.map(function(b){return b.getCoordinates();});}this.parent(a);},calculateLimit:function(){var j=this.element,e=this.container,d=document.id(j.getOffsetParent())||document.body,h=e.getCoordinates(d),c={},b={},k={},g={},m={};
["top","right","bottom","left"].each(function(q){c[q]=j.getStyle("margin-"+q).toInt();b[q]=j.getStyle("border-"+q).toInt();k[q]=e.getStyle("margin-"+q).toInt();
g[q]=e.getStyle("border-"+q).toInt();m[q]=d.getStyle("padding-"+q).toInt();},this);var f=j.offsetWidth+c.left+c.right,p=j.offsetHeight+c.top+c.bottom,i=0,l=0,o=h.right-g.right-f,a=h.bottom-g.bottom-p;
if(this.options.includeMargins){i+=c.left;l+=c.top;}else{o+=c.right;a+=c.bottom;}if(j.getStyle("position")=="relative"){var n=j.getCoordinates(d);n.left-=j.getStyle("left").toInt();
n.top-=j.getStyle("top").toInt();i-=n.left;l-=n.top;if(e.getStyle("position")!="relative"){i+=g.left;l+=g.top;}o+=c.left-n.left;a+=c.top-n.top;if(e!=d){i+=k.left+m.left;
l+=((Browser.ie6||Browser.ie7)?0:k.top)+m.top;}}else{i-=c.left;l-=c.top;if(e!=d){i+=h.left+g.left;l+=h.top+g.top;}}return{x:[i,o],y:[l,a]};},getDroppableCoordinates:function(c){var b=c.getCoordinates();
if(c.getStyle("position")=="fixed"){var a=window.getScroll();b.left+=a.x;b.right+=a.x;b.top+=a.y;b.bottom+=a.y;}return b;},checkDroppables:function(){var a=this.droppables.filter(function(d,c){d=this.positions?this.positions[c]:this.getDroppableCoordinates(d);
var b=this.mouse.now;return(b.x>d.left&&b.x<d.right&&b.y<d.bottom&&b.y>d.top);},this).getLast();if(this.overed!=a){if(this.overed){this.fireEvent("leave",[this.element,this.overed]);
}if(a){this.fireEvent("enter",[this.element,a]);}this.overed=a;}},drag:function(a){this.parent(a);if(this.options.checkDroppables&&this.droppables.length){this.checkDroppables();
}},stop:function(a){this.checkDroppables();this.fireEvent("drop",[this.element,this.overed,a]);this.overed=null;return this.parent(a);}});Element.implement({makeDraggable:function(a){var b=new Drag.Move(this,a);
this.store("dragger",b);return b;}});

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;
		}
	}
});

