// JavaScript Document


var ENABLECOMP = {}


ENABLECOMP.INIT = function(site, page){
	ENABLECOMP.SITE = site;
	ENABLECOMP.PAGE = page;
	ENABLECOMP.AJAX = site + "ajax.php";
	ENABLECOMP.HIGHESTDEPTH = 1000;
	ENABLECOMP.navigation = ENABLECOMP.ui.Navigation($('#navigation'));
	ENABLECOMP.contact_form = ENABLECOMP.ui.ContactForm($('#contact_form'));
	$('#get_btn').click(ENABLECOMP.onFooterContactClick);
	ENABLECOMP.onReady();
	ENABLECOMP.onPageReady();
}

ENABLECOMP.onFooterContactClick = function(target){
	ENABLECOMP.contact_form.reset('Deb Miller', 1);
	ENABLECOMP.contact_form.showPopup();
}

ENABLECOMP.onFileRequirementsClick = function(){
	ENABLECOMP.contact_form.reset('Deb Miller', 3);
	ENABLECOMP.contact_form.showPopup();
}

ENABLECOMP.onContactClick = function(target){
		ENABLECOMP.contact_form.reset(target, 2);
		ENABLECOMP.contact_form.showPopup();
}

ENABLECOMP.onReady = function(){
	
	
}

ENABLECOMP.onPageReady = function(){
	
	
	
}



ENABLECOMP.blockAll = function(){
	var blocker = $("<div id = 'blocker' class='Blocker'></div>").appendTo('body')
	var docport = ENABLECOMP.util.getDocumentPort();
	blocker.width(docport.width);
	blocker.height(docport.height);
	blocker.css('z-index', ENABLECOMP.highestDepth());
	blocker.show();
	return blocker;
}

ENABLECOMP.highestDepth = function(){
	ENABLECOMP.HIGHESTDEPTH = ENABLECOMP.HIGHESTDEPTH + 1; 
	return ENABLECOMP.HIGHESTDEPTH;
}


ENABLECOMP.EventDispatcher = function(){
	var object = {};
	object.listeners =  {}; 
	object.addListener= function(type, func){
			if(!this.listeners[type]){
				this.listeners[type] = [];
			}
			this.listeners[type].push(func);
	};
	object.removeListener = function(type, func){
			var items = this.listeners[type]
			if(!items || items.length==0 || !func){
				return;
			}
			var i = items.indexOf(func);
			if(i==-1){
				return;
			}
			if(items.length==1){
				this.listeners[type] = [];
				return;
			}
			var front = items.slice(0,i);
			var end = items.slice(i+1, items.length);
			this.listeners[type] =  front.concat(end);
	};
	object.dispatchEvent= function(type, params){
			if(!this.listeners[type]){
				return;
			}
			for(var i = 0; i < this.listeners[type].length;i++){
				this.listeners[type][i](params);
			}
	}
	
	object.clearListeners = function(){
		object.listeners =  {}; 
	}
	return object;
	 	
}


//AJAX
ENABLECOMP.RemoteFunction = function(name){
	var object = ENABLECOMP.EventDispatcher();
	object.fn = name;
	object.response = {};
	 
	
	object.CALL = function(params){
		 params.fn = object.fn;
		 $.post(ENABLECOMP.AJAX , params,function(data){
			object.ON_RETURN(data);
  		  }, "json");
		
	}
	object.ON_RETURN = function(obj){
		object.response = obj;
		this.dispatchEvent("ON_RETURN", obj);
	}
	return object;
}







ENABLECOMP.ui = {}
ENABLECOMP.ui.Component = function($ui){
	var object = ENABLECOMP.EventDispatcher();
	object.$ui = $ui;//cache jQuery root
	object.id = object.$ui.attr('id');
	
	
	object.INIT = function(){
		 //override in subclasses
	}
	
	object.child = function(id){
		return $('#' + id, this.$ui);
	}
	
	object.val = function(id){
		return $('#' + id, this.$ui).val();
	}
	
	object.showPopup = function(){
		object.blocker = ENABLECOMP.blockAll();
		object.$ui.addClass('popup');
		var viewport = ENABLECOMP.util.getViewport();
		var w = this.$ui.width();
		var h = this.$ui.height();
		object.$ui.css('left', viewport.width/2 - w/2);
		object.$ui.css('top', viewport.scrollTop + 35);
		object.$ui.css('z-index', ENABLECOMP.highestDepth());
		object.$ui.show();
		object.dispatchEvent("ON_SHOW");
	}
	
	object.hidePopup = function(){
		object.blocker.remove();
		object.$ui.hide();
		object.dispatchEvent("ON_HIDE");
	}
	
	object.destroy = function(){
		 $ui.remove();
	}
	
	object.useEventButtons = function(target){
		if(!target){
			target = object.$ui;
		}
		$('.EventButton',target).click(function(event){
		 	var id = $(this).attr('id');
			var eventname = id.split("__")[0];
			var param = id.split("__")[1]
			object.dispatchEvent(eventname, param);
		});
	}
	
	object.addDispatcher = function(target, e){
		object.child(target).click(function(event){
			object.dispatchEvent(e);
		});
	}
	
	object.setEnabled = function(boolean){
		if(boolean){
			this.enabled = true;
			$(object.$ui).children().attr('disabled', '');
			 
		}
		else{
			this.enabled = false;
			$(object.$ui).children().attr('disabled', 'false');
		}
		
	}
	
	object.data = function(key){
		return object.child(key).text();
	}
	
	object.setValue = function(key, value){
		object.child(key).val(value);
	}
	
	
	return object;
}

ENABLECOMP.ui.Navigation = function($ui){
	
	var object = ENABLECOMP.ui.Component($ui); 
	object.items = [];
	object.selected = null;
	
	object.INIT = function(){
		$('.menuItem', object.$ui).each(function(index) {
			var menu_item = ENABLECOMP.ui.MenuItem($(this))
			menu_item.addListener("TRIGGER", object.onTrigger);
			menu_item.addListener("ROLLOUT", object.onRollOut);
			menu_item.addListener("ACTIVITY", object.onActivity);
			object.items.push(menu_item);
		});
		
		 
	}
	
	object.onRollOut = function(obj){
		 object.startTimer();
		
	}
	
	object.onActivity = function(){
		object.cancelTimer();
		
	}
	 
	
	object.onTrigger = function(obj){
		object.cancelTimer();
		if(object.selected == obj){
			return;
		}
		if(object.selected){
			object.selected.close();
		}
		object.selected = obj;
		obj.open();
	}
	
	object.closeAll = function(){
		object.cancelTimer();
		for(var i in object.items){
			object.items[i].close();
		}
		object.selected = null;
	}
	
	object.startTimer = function(){
		if(!object.closetimer){
			object.closetimer = window.setTimeout(object.closeAll, 500);
		}
	}
	
	object.cancelTimer = function(){
		if(object.closetimer){
			window.clearTimeout(object.closetimer);
			object.closetimer = null;
		}
	}
	
	object.INIT();
	
	return object;
	
	
}

ENABLECOMP.ui.MenuItem = function($ui){
	
	var object = ENABLECOMP.ui.Component($ui); 
	object.state = 0;
	 
	
	object.INIT = function(){
		object.btn = object.child('btn');
		object.mask =object.child('mask');
		object.menu = object.child('menu');
		object.offset = object.$ui.offset();
		object.bar =$('.bar',object.$ui);
		
		if(object.id==ENABLECOMP.PAGE){
			object.isSelected = true;
			object.$ui.addClass('whiteBG');
			object.bar.css('top',34);
			object.btn.addClass(object.id + '_btn_over');
		}
		object.btn.click(object.onClick);
		object.btn.bind('mouseover', object.onButton)
		object.btn.bind('mouseout', object.onButtonOut)
		object.mask.css('left', object.offset.left);
	 
		$('div', object.menu).bind('mouseout', object.onMouseOut)
		$('div', object.menu).bind('mousemove', object.onMouseMove);
	}
	
 	object.onClick = function(){
		object.clicked = true;
	}
	
	object.onMouseMove = function(){
		object.dispatchEvent("ACTIVITY", object);
	}
	
	object.hasChildren = function(){
		return object.menu.children();
	}
	
	object.onMouseOut = function(e){
		object.dispatchEvent("ROLLOUT", object);
	}
	
	object.onButtonOut = function(e){
		object.dispatchEvent("ROLLOUT", object);
		 
	}
	
	object.onButton = function(e){
		object.dispatchEvent("TRIGGER", object);
	}
	
	object.open = function(){
		object.state = 1;
		if(!object.isSelected){
			object.bar.css('top',34);
			object.$ui.addClass('whiteBG');
			object.btn.addClass(object.id + '_btn_over');
		}
		 
		object.menu.animate({"top": "4px"}, "slow");
	}
	
	object.close = function(fast){
		if(object.clicked){
			return;
		}
		object.state = 0;
		if(!object.isSelected){
			object.bar.css('top',0);
			object.$ui.removeClass('whiteBG');
			object.btn.removeClass(object.id + '_btn_over');
		}
		if(fast){
			object.menu.animate({"top": "-150px"}, "fast");
		}
		else{
			object.menu.animate({"top": "-150px"}, "slow");
		}
	}
	
	object.INIT();
	
	return object;
	
	
}

ENABLECOMP.ui.ReviewForm = function($ui){
	
	var object = ENABLECOMP.ui.Component($ui); 
	object.errors = [];
	 
	
	object.INIT = function(){
		object.send_btn = object.child('send_btn');

		object.close_btn = object.child('close_btn');
		object.close_btn.show();
		object.close_btn.click(object.hidePopup);
		object.send_btn.click(object.sendClicked);
		object.func = ENABLECOMP.RemoteFunction('review');
		object.func.addListener("ON_RETURN", object.onReviewReturn);
		 
	}
	
	
	object.sendClicked = function(){
		var data = object.getData();
		var errors = object.validate(data);
		if(errors){
			object.showErrors(errors);
		}
		else{
			object.setEnabled(false);
			object.child('loader').show();
			object.func.CALL(data);
		}
		 
	}
	
	object.getData = function(){
		var data = {}
		data.hospital_name = object.val('hospital_name');
		data.city_state = object.val('city_state');
		data.name = object.val('name');
		data.title = object.val('title');
		data.phone = object.val('phone');
		data.email = object.val('email');
		data.trauma_center = $("input[name='trauma_center']:checked").val();
		data.teaching_hospital = $("input[name='teaching_hospital']:checked").val();
		data.revenue = object.val('revenue');
		data.inpatient_percent = object.val('inpatient_percent');
		data.outpatient_percent = object.val('outpatient_percent');
		data.comp_percent = object.val('comp_percent');
		
		return data;
	}
	
	object.validate = function(data){
		var errors = {};
		if(ENABLECOMP.util.validEmail(data.email)){
			return null;
		}
		else{
			errors.email = "Please enter a valid email address.";
			return errors;
		}
	}
	
	object.showErrors = function(errors){
		this.clearErrors();
		for(var field in errors){
			this.showLineError(field, errors[field]);
		}
	}
	
	object.showLineError = function(field, error){
		var element = object.child(field + '_error')
		element.html(error);
		element.show();
		object.errors.push(element);
	}
	
	object.clearErrors = function(){
		if(!this.errors){
			return;
		}
		for(var i in this.errors){
			this.errors[i].hide();
		}
		this.errors = [];
	}
	
	object.onReviewReturn = function(obj){
		object.child('loader').hide();
		object.child('form').hide();
		object.child('response').show();
	}
	
	object.reset = function(){
		object.clearErrors();
		object.child('form').show();
		object.child('response').hide();
	}
	
	
	object.INIT();
	
	return object;
	
	
}

ENABLECOMP.ui.ContactForm = function($ui){
	
	var object = ENABLECOMP.ui.Component($ui); 
	object.errors = [];
	 
	
	object.INIT = function(){
		object.error = ENABLECOMP.ui.Component(object.child('error'));
		object.close_btn = object.child('close_btn');
		object.send_btn = object.child('send_btn');
		object.close_btn.show();
		object.close_btn.click(object.hidePopup);
		object.send_btn.click(object.sendClicked);
		object.func = ENABLECOMP.RemoteFunction('contact');
		object.func.addListener("ON_RETURN", object.onContactReturn);
		 
	}
	
	object.sendClicked = function(){
		var data = object.getData();
		var errors = object.validate(data);
		if(errors){
			object.showErrors(errors);
		}
		else{
			object.setEnabled(false);
			object.child('loader').show();
			object.func.CALL(data);
		}
		 
	}
	
	object.getData = function(){
		var data = {}
		
		data.name = object.val('name');
		data.email = object.val('email');
		data.to = object.val('to');
		data.subject = object.val('subject');
		data.message = object.val('message');
		data.type = object.type;
		
		return data;
	}
	
	object.validate = function(data){
		var errors = {};
		if(ENABLECOMP.util.validEmail(data.email)){
			return null;
		}
		else{
			errors.email = "Please enter a valid email address.";
			return errors;
		}
	}
	
	object.showErrors = function(errors){
		 
		this.clearErrors();
		for(var field in errors){
			this.showLineError(field, errors[field]);
		}
	}
	
	object.showLineError = function(field, error){
		var element = object.child(field + '_error')
		element.html(error);
		element.show();
		object.errors.push(element);
	}
	
	object.clearErrors = function(){
		if(!this.errors){
			return;
		}
		for(var i in this.errors){
			this.errors[i].hide();
		}
		this.errors = [];
	}
	
	object.onContactReturn = function(obj){
		object.child('loader').hide();
		object.child('form').hide();
		object.child('response').show();
	}
	
	object.reset = function(targ, type){
		object.type = type;
		object.setMode(type);
		
		if(targ){
			object.setValue('to', targ);
		}
		else{
			object.setValue('to', 'Deb Miller');
		}
		object.clearErrors();
		object.child('form').show();
		object.child('response').hide();
	}
	
	object.setMode = function(type){
		if(type==1){
			object.child('header1').show();
			object.child('header2').hide();
			object.child('header3').hide();
			object.child('subText').html('You will receive a follow-up email or phone call within 24 hours. We will find out more about your unique situation, set up a customized plan, and get started recovering significant cash for you.');
		}
		else if(type==2){
			object.child('header2').show();
			object.child('header1').hide();
			object.child('header3').hide();
			object.child('subText').html('You will receive a follow-up email or phone call within 24 hours.');
			
		}
		else{
			object.child('header2').hide();
			object.child('header1').hide();
			object.child('header3').show();
			object.child('subText').html('You will receive a follow-up email or phone call within 24 hours.');
		}
	}
	
	object.INIT();
	
	return object;
	
	
}



//util
ENABLECOMP.util = {}

 
 
ENABLECOMP.util.validEmail = function(v) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(v);
}; 

ENABLECOMP.util.validUsername = function(v){
	var reg = /^[A-Za-z0-9_]{4,30}$/;
	return reg.test(v);
}

ENABLECOMP.util.validPassword = function(v){
	var reg =  /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;
	return reg.test(v);
}


ENABLECOMP.util.writeEmail = function(id, A, B, useText){
	var email = $('#' + id)
	email.attr('href', 'mailto:' + A + '@' + B);
	if(useText){
		email.text(A + '@' + B);
	}
}


ENABLECOMP.util.getViewport = function(){
		var $window = $(window);
		var viewport = {}
		viewport.width = $window.width();
		viewport.height = $window.height();
		viewport.scrollTop = $window.scrollTop();
		return viewport;
}

ENABLECOMP.util.getDocumentPort = function(){
	var $document = $(document);
	var viewport = {}
	viewport.width = $(document).width();
	viewport.height = $(document).height();
	return viewport;
}

 
