$clear = function(inter){ clearTimeout(inter); clearInterval(inter); return null; };
var base = (location.href.contains('localhost')) ? '/mwi-newstyle':'';
var formhints = new Class({
	Implements: [Options],
	options: {
		collection: $$('input[type=text]')
	},
	initialize: function(options) {
		this.setOptions(options);
		this.defaults();
	},
	defaults: function() {
		this.options.collection.each(function(el) {
			el.addClass('hint');
			if (el.get('value')=='') {
				el.set('value',el.get('title'));
				el.addEvent('focus', function() { 
					if(el.get('value') == el.get('title')) { 
						el.set('value',''); 
						el.removeClass('hint');
					}
				});
				el.addEvent('blur', function() {
					if(el.get('value') == '') {
						el.set('value',el.get('title'));
						el.addClass('hint');
					}
				});
			}
		});
	}
});
function popupwin(width,height) {
	if ($('box')) $('box').destroy();
	if ($('full')) $('full').destroy();
	var testdiv = new Element('div').inject(document.body)
	var winwidth = testdiv.getWidth();
	testdiv.destroy();
	var win = new Element('div',{
		id:'full',
		styles:{
			'height':window.getScrollSize().y,
			'width':winwidth,
			'position':'absolute',
			'background':'#000',
			'top':0,
			'left':0,
			'opacity':0,
			'z-index':1
		},
		events:{
			'click': function() {
				closepopwin();
			}
		}
	}).inject(document.body).fade(0.8);
	var box = new Element('div',{
		id:'box',
		styles:{
			'position':'absolute',
			'width':width,
			
			
			'left':(winwidth/2)-(width/2),
			'top':((window.getSize().y/2)+window.getScroll().y-(height/2)),
			'padding':0,
			'z-index':1000,
			'opacity':0
		},
		'class':'red'
	}).inject(document.body);
		
	var close = new Element('p',{'class':'poptop','styles':{'text-align':'right'}}).inject(box);
	var closebtn = new Element('img',{'src':'_imgs/cs_close.png','id':'cs_close','styles':{'cursor':'pointer','margin':'20px 30px 0 0'}}).inject(close);
	var popcontent = new Element('div',{'id':'popcontent'}).inject(box);
	$('cs_close').addEvent('click',function(e) {
		closepopwin();
	});
	document.getElement('body').addEvent('keydown', function(e){
		if (e.key == 'x' || e.key == 'esc') {
			box.fade(0);
			(function(){box.destroy()}).delay(1000);
			win.fade(0);
			(function(){win.destroy()}).delay(1000);
		}
	});
}
function closepopwin() {
	$('box').fade(0);
	(function(){$('box').destroy()}).delay(1000);
	$('full').fade(0);
	(function(){$('full').destroy()}).delay(1000);
	
	
	$$('.activethumb').each(function(a){
		a.removeClass('activethumb');
		a.set('opacity',1);
	});
	
	
}




window.addEvent('domready',function() {
	var hint = new formhints({
		collection: $$('.def')
	});
	
	
	
	if ($('tabbedcontent')) {
		
		var hash = location.href.split('#')[1];
		if (hash) {
			$$('.tab').each(function(t) { 
				if (t.hasClass(hash.replace(/\+/g," "))) {
					t.addClass('sel');
					tc = t.get('rel');
				} else {
					t.removeClass('sel');
				}
			});
			$$('.tabcontent').each(function(tc) { tc.removeClass('show') });
			$(tc).addClass('show');
		}
		
		if ($('tabbedcontent').hasClass('show')) {
			$$('.tab').each(function(tab) {
				tab.addEvent('click',function() {
					$$('.tab').each(function(t) { t.removeClass('sel'); });
					this.addClass('sel');
					$$('.tabcontent').each(function(tc) { tc.removeClass('show') });
					$(this.get('rel')).addClass('show');
				});
			});
		}
	}
	if ($('clients')) {
		$$('.over').each(function(t) {
			var q = t.getNext();
			if (q && q.hasClass('quote')) {
				t.addEvents({
					'mouseover': function() {
						q.fade(1);
					},
					'mouseleave': function() {
						q.fade(0);
					}
				});
			}
			if (t.hasClass('case')) {
				t.addEvents({
					'mouseover': function(){
						this.set('tween',{'duration':250});
						this.tween('opacity',0.01);
					},
					'mouseleave':function() {
						if (!this.hasClass('activethumb')) {
							this.set('tween',{'duration':250});
							this.tween('opacity',1);
						}
					},
					'click':function() {
						$$('.activethumb').each(function(a){
							a.removeClass('activethumb');
							a.set('opacity',1);
						});
						this.addClass('activethumb');
						var id = this.getParent().get('id');
						var frontend = (location.href.contains('mwi-admin')) ? '':'&fe=1';
						$('ccs').setStyle('display','none');
						popupwin(980,614);
						var req = new Request.HTML({
							url:'_casestudy.php?r=' + Math.random(),
							update:$('popcontent'),
							evalScripts:true,
							evalResponse:true,
							onSuccess: function() {
								$('box').fade(1);
							}
						}).send('client=1&id=cs'+t.get('rel'));
					}
				});
			}
		});
	}
	/* form validation */
	var reqs = $$('.req');
	var checked = new Array;
	if (reqs.length>0) {
		if ($('val')) {
			$('val').addEvent('click',function(e) {
				e.stop();
				var err = false;
				reqs.each(function(req) {
					if (req.get('value')==''||req.get('value')==req.get('title')) {
						req.addClass('err');
						err = true;
					}
					if (req.hasClass('email')) {
						var regex = /^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/;
						if (req.get('value').test(regex)==false) {
							req.addClass('err');
							err = true;
						}
					}
					if ($('slideimg')) {
						if ($('slideimg').get('value')=='') {
							$('addslide').addClass('err');
							err=true;
						}
					}
					req.addEvent('keydown',function() {
						req.removeClass('err');
					});
					
					if (req.hasClass('checkbox')) {
						if (req.get('checked')==true) checked.push(i);
					}
				});
				if (checked.length<=0 && $('checkboxsection')) {
					$('checkboxsection').addClass('err');
					err = true;
				}
				if (err==false) this.form.submit();
			});
		}
	}
	
	if ($$('.cs_images').length>0) {
		var gallery = new fadeGallery($$(".cs_images"), {
			speed: 800,
			paging: false,
			pagingEvent: "mouseenter",
			autoplay: false,
			duration: 5000,
			onStart: function() {
				//this.gallery.getElement(".info").set("html", parseInt(this.current+1) + " from " + this.items.length);
			},
			onPlay: function() {
				this.fireEvent("start");
			}
		});
	}	
	
	
	if ($$('.slideitem').length>1) {
		var gallery5 = new fadeGallery($$(".ss"), {
			speed: 800,
			paging: false,
			pagingEvent: "mouseenter",
			autoplay: true,
			duration: 5000,
			onStart: function() {
				//this.gallery.getElement(".info").set("html", parseInt(this.current+1) + " from " + this.items.length);
			},
			onPlay: function() {
				this.fireEvent("start");
			}
		});
	}	
	
	$$('.cs').each(function(cs) {
		if (!cs.hasClass('cs_edit')) {
			cs.addEvent('click',function() {
				popupwin(980,614);
				
				var c = (cs.hasClass('cs_c')) ? '&client=true':'';
				
				var req = new Request.HTML({
					url:'_casestudy.php',
					update:$('popcontent'),
					onSuccess: function() {
						$('box').fade(1);
					}
				}).send('id='+this.get('id') + c);
			});
		}
	});
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
});
