window.addEvent( "domready", function() { new ECLMain(); } );
window.addEvent( "load", function() { new ECLMainLoad(); } );
function d()
{
	if( window["console"] && console.log ) {
		console.log.apply( console, arguments );
	}
}
var ECLEmailValidator =
{
	validate: function( str )
	{
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if( emailPattern.test( str ) ) {
			return true;
		}
		return false;
	}
};
var ECLMain = new Class
({
	initialize: function()
	{
		$$(".eclDropdown").each( function(drop) {
			new ECLDropdown( drop );
		}.bind(this));

		if( document.id("inputSignup") != null ) {
			new ECLNewsletter();
		}


		if( document.id( "whoStack" ) ) {
			new ECLPeople();
		}

		if( document.id( "contact" ) ) {
			new ECLContact();
		}
	}
});


var ECLContact = new Class
({
	btn: null,
	form: null,
	validator:null,
	hint:null,

	initialize: function()
	{
		this.btn = document.id("btnSubmit");
		this.form = document.id("contactForm");
		this.form.addEvent( "submit", this.submitListener.bind(this) );
		this.validator = new ECLForm( this.form );
		this.hint = this.form.getElement(".hint");
	},
	submitListener: function( e )
	{

		new Event( e ).stop();
		var success = this.validator.validate();
		if( success ) {
			var data = {};
			data.name = document.id("name").get("value");
			data.company = document.id("company").get("value");
			data.job = document.id("job").get("value");
			data.email = document.id("email").get("value");
			data.message = document.id("comments").get("value");
			new Request.JSON( {url: REMOTE_BASE + 'newsletter/contact', onComplete:this.sendListener.bind(this) } ).post( data );

			this.btn.setStyle( "visibility", "hidden" );
		}
	},
	sendListener: function( res )
	{
		this.hint.set( "text", "Your message has been sent. Thank you for your interest.").setStyles( {color:"#4F6621", display:"block"} );
	}
});

var ECLForm = new Class
({
	form:null,
	errorFields:[],
	defaultHint:"Please fill out all the required fields",
	initialize: function( form )
	{
		this.form = form;
	},
	validate: function()
	{
		this.form.getElement(".hint").set("html", this.defaultHint );
		this.errorFields = [];
		this.form.getElements(".valid-required").each( function( element ) {
			if( (element.get("value") == null) || (element.get("value").length == 0 ) ) {
				this.applyInvalid( element );
			}else{
				this.applyValid( element );
			}
		}.bind( this ));
		if( this.errorFields.length == 0 ) this.form.getElement(".hint").set("html", "" );

		this.form.getElements(".valid-email").each( function( element ) {
			var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			if( !emailPattern.test(element.get("value")) ) {
				if( !this.errorFields.contains( element ) ) this.form.getElement(".hint").adopt( new Element("div").set("text", "Your Email address is invalid" ) );
				this.applyInvalid( element );
			}else{
				this.applyValid( element );
			}
		}.bind(this));

		this.form.getElements(".valid-checked").each( function( element ) {
			if( !element.get("checked") ) {
				this.form.getElement(".hint").adopt( new Element("div").set("text", "Please accept our terms and conditions" ) );
				this.applyInvalid( element );
			}else{
				this.applyValid( element );
			}
		}.bind(this));

		if( this.errorFields.length > 0 ) this.form.getElement(".hint").setStyle("display","block");
		else {

			if( this.validateLast() ) {
				this.form.getElement(".hint").setStyle("display","none");
				return true;
			}

		}

		return false;
	},
	validateLast: function()
	{
		var id = this.form.get("id");
		var valid = true;
		switch( id )
		{
			case "requestcallbackform" :
				if( !$("nexthour").get("checked") ) {

				}
				//valid = false;
				break;
		}
		return valid;
	},
	applyValid: function( element )
	{
		element.removeClass("valid-fail");
	},
	applyInvalid: function( element )
	{
		this.errorFields.push(element);
		element.addClass("valid-fail");
	}
});


var ECLPeople = new Class
({
	shell:null,

	initialize: function()
	{
		this.shell = document.id( "whoStack" );
		var prev = null;
		var a = [];
		var tmp = [];

		var panes = $$(".whoStackpane");
		panes.each( function( pane ) {
			pane.setStyle( "display", "block" );
			pane.getElements( "li" ).each( function( li ) {
				prev = li;
				if( li.hasClass("f") ) {
					a.push( tmp );
					tmp = null;
					tmp = [];
				}
				tmp.push( li );
			}.bind(this) );

			a.each( function( pack ) {
				var max = 0;
				var resetStyle = false;
				pack.each( function( li ) {
					if( li.getSize().y > max ) {
						max = li.getSize().y;
					}
				});
				pack.each( function( li ) {
					if( max > 50 ) {
						li.setStyle( "height", max + 10 );
					}
				});
			});
			if( !pane.hasClass("active" ) ) {
				pane.setStyle( "display", "none" );
			}

		}.bind(this) );





	}
});


var ECLMainLoad = new Class
({
	initialize: function()
	{
		
	}
});

var ECLNewsletter = new Class
({
	shell:null,
	i:null,
	defText:null,
	deactivator:null,
	btn:null,
	
	initialize: function()
	{
		this.i = document.id("inputSignup");
		this.btn = document.id("nlGo");
		
		this.shell = this.i.getParent("div");
		this.defText = this.i.get("value");
		this.i.addEvent( "focus", this.focListener.bind(this) );
		this.i.addEvent( "blur", this.blurListener.bind(this) );
		this.i.addEvent( "keyup", this.keyListener.bind(this) );
		this.btn.addEvent( "click", this.clickListener.bind(this) );
	},
	send: function( email )
	{
		email = email != null ? email :  this.getVal().trim();
		var r = new Request( {url:REMOTE_BASE + "newsletter", onComplete:this.completeHandler.bind(this)} );
		r.send( "email="+email );

		if( !this.deactivator ) {
			this.deactivator = new de.fascina.utils.Deactivator( this.shell );
		}
		this.deactivator.show();

		this.i.blur();
	},
	focListener: function()
	{
		if( this.getVal() == this.defText ) {
			this.setVal(" ");
			this.btn.show();
		}
	},
	blurListener: function()
	{
		if( this.getVal() == " "  || this.getVal() == "" ) {
			this.setVal( this.defText );
			 this.btn.hide();
		}
	},
	clickListener: function()
	{
		this.send();
	},
	keyListener: function( e )
	{
		var email = this.getVal().trim();
		/*
		if( !ECLEmailValidator.validate( email ) ) {
			return this.btn.hide();
		}
		*/
		this.btn.show();

		if( e.code != 13 ) {
			return;
		}
		
		this.send( email );

	},
	completeHandler: function()
	{
		this.setVal("Thanks!");
	},
	getVal: function()
	{
		return this.i.get("value");
	},
	setVal: function( str )
	{
		this.i.set("value", str );
	}
});


var ECLDropdown = new Class
({
	element:null,
	button:null,
	panel:null,
	int:null,
	active:false,
	
	initialize:function( wrapperElement )
	{
		this.element = wrapperElement;
		this.button = document.id("speciesDropBtn");
		if( !this.button ) {
			this.button = document.id("homeServiceBoxBtn");
		}
		if( !this.button ) {
			return;
		}
		this.panel = this.element.getElement("ul");

		this.button.addEvent( "click", this.togglerListener.bind(this) );
		this.panel.getElements("li").each( function( li ) {
			li.addEvent( "click", this.clickLiListener.bind(this));
			li.adopt( new Element("img").set("src", LAYOUT_BASE + "gfx/trans.gif").setStyles({ position:"absolute", width:"100%", height:"100%", top:"0", left:"0"}) );
		}.bind(this));

	    this.button.addEvent( "mouseenter", this.hoverListener.bind(this) );
		this.element.addEvent( "mouseleave", this.leaveListener.bind(this) );
	},
	hide:function()
	{
		this.panel.fade("out");
		this.active = false;
	},
	
	togglerListener: function()
	{
		if( this.panel.get("opacity") == 0 ) {
			this.panel.setStyle("display", "block" );
		}else if( this.panel.get("opacity") == 1 ) {
			//this.panel.setStyle("display", "none" );
		}
	},
	
	clickLiListener: function( e )
	{
		var a = $(e.target).getParent("li").getElement("a");
		if( a ) {
			var url = a.get("href").trim();
			window.location.href = url;
		}
	},

	hoverListener: function()
	{
		$clear( this.int );
		if( this.active ) {
			return;
		}
		this.panel.set("opacity", 0 ).setStyles( {display:"block"}).fade("in");
		this.active = true;
	},

	leaveListener: function()
	{
		this.int = this.hide.delay( 200, this );
	}
});

var de = {};
de.fascina = {};
de.fascina.utils = {};
de.fascina.utils.Deactivator = new Class
({
	dim: null,
	elem: null,
	isWaiter:null,
	opacity:null,
	/** @constructor */
	initialize: function( elem, color, opacity, isWaiter )
	{
		var hideAgain = elem.getStyle( "display") == "none";
		elem.setStyle( "display", "block" );
		this.dim = elem.getSize();
		if( hideAgain ) elem.setStyle( "display", "none" );
		this.elem = elem;
		this.isWaiter = isWaiter;
		this.color = $chk( color ) ? color : "#ffffff";
		this.opacity = $chk( opacity ) ? opacity : 0.6;
		elem.store( "deactivator", this );
		return this;
	},
	show: function()
	{
		var overlay = new Element( "div" );
		overlay.set( "opacity", this.opacity );
		overlay.addClass( "deactivator" );
		overlay.setStyles(
			{
				width: this.dim.x,
				height: this.dim.y,
				background: ( this.isWaiter ? this.color + " url("+REMOTE_LAYOUT_BASE+"gfx/ajax/progressbar_loading.gif) center no-repeat" : this.color ),
				position: "absolute",
				"z-index":6000,
				top:0,
				left:0
			}
		);
		overlay.inject( this.elem );
		this.elem.store( "deactivator", overlay );
	},
	hide:function()
	{
		try {
			this.elem.retrieve("deactivator").dispose();
			return true;
		}catch( e ) {
			return false;
		}
	}
});


function getRequestParameter(param)
{
	var q = document.location.search || document.location.hash;
	if(q) {
		var pairs = q.substring(1).split("&");
		for (var i=0; i < pairs.length; i++) {
			if (pairs[i].substring(0, pairs[i].indexOf("=")) == param) {
				return pairs[i].substring((pairs[i].indexOf("=")+1));
			}
		}
	}
	return "";
};
