$(function(){
	Page.Sizing.init();
	Page.Validation.init();

	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader h1',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader h2',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader1 h1',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader1 h2',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader2 h1',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader2 h2',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader3 h1',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader3 h2',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader4 h1',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
	SifrConfig.masterReplace(SifrConfig.gothamLight,{
		selector : '#contentHeader4 h2',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #cc0000; } '
		]
	});
		SifrConfigModules.masterReplace(SifrConfigModules.gothamLight,{
		selector : '#teaserContent h2',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #333333;} '
		]
	});
	SifrConfigModules.masterReplace(SifrConfigModules.gothamLight,{
		selector : '#registeredContent h2',
		css : [
			'.sIFR-root { text-align: left; font-weight: normal; color: #333333; } '
		]
	});

	$('#registeredContent div.contentModule').each(function(){
		var $this = $(this);
		
		$this.hover(function(e){
			$this.addClass('hoveredContent');
		},function(e){
			$this.removeClass('hoveredContent');
		}).click(function(){
			var href = $this.find('a').attr('href');
			
			window.location = href;
		});
		
	});
	
});

$(window).resize(function(){
	Page.Sizing.resize();
});


SifrConfig = {

	init: function(){
		sIFR.activate(this.gothamLight);
	},
	
	gothamLight: {
		src: 'assets/flash/gothamLight.swf',
		ratios : [9, 1.27, 13, 1.18, 18, 1.14, 26, 1.13, 32, 1.11, 37, 1.1, 64, 1.09, 105, 1.08, 107, 1.07, 119, 1.08, 122, 1.07, 1.08]
	},
	
	masterReplace: function(swf,options){
		if (typeof(sIFR)=='object'){
			sIFR.replace(swf, {
				selector: options.selector,
				css: options.css,
				wmode: 'transparent'
			});
		}
	}
}

SifrConfigModules = {

	init: function(){
		sIFR.activate(this.gothamLight);
	},
	
	gothamLight: {
		src: 'assets/flash/gothamLight.swf',
		ratios : [9, 1.27, 13, 1.18, 18, 1.14, 26, 1.13, 32, 1.11, 37, 1.1, 64, 1.09, 105, 1.08, 107, 1.07, 119, 1.08, 122, 1.07, 1.08],
		tuneHeight : '-6'
	},
	
	masterReplace: function(swf,options){
		if (typeof(sIFR)=='object'){
			sIFR.replace(swf, {
				selector: options.selector,
				css: options.css,
				wmode: 'transparent'
			});
		}
	}
}
SifrConfigModules.init();


var allerrs = ''; //BGT to remove when integrating with their code

Page = {};
Page.Sizing = {

	init: function(){
		this.page = $('#page');
		this.initHeight = $(document).height();
		this.resize();
	},
	
	resize: function(){
		if (!this.page.get(0)) return;
		var windowHeight = $(window).height();
		var pageHeight = windowHeight > this.initHeight ? windowHeight : this.initHeight;
		pageHeight = pageHeight < 630 ? 630 : pageHeight;
		this.page.height(pageHeight);
		
		//page width logic		
		var windowWidth = $(window).width();
		var pageWidth = windowWidth < 950 ? 950 : windowWidth;
		this.page.width(pageWidth);		
	}
}

Page.Validation = {
	init: function(){
		var self = this;
		this.form = $('#registration');
		this.textFields = this.form.find(':text');
		this.selectBoxes = this.form.find('select');
		this.radioWrapper = this.form.find('div.radioWrapper');
		this.radioInput = this.form.find(':radio');
		this.hiddenFields = this.form.find(':hidden');  // BGT Updates - Submitting hidden fields
		
		this.isValid = false;
		
		this.form.submit(function(e){
			e.preventDefault();
			if (!self.isValid){
				self.validate();
			} 
		});
		
		$('#submitButton').find('input').hover(function(){
			this.className=this.className+' hovered';
		},function(){
			this.className=this.className.replace('hovered','');
		});
		
		/* No radio buttons for this formtype
		this.radioInput.each(function(){
			var $this = $(this);
			if (this.checked) {
				$this.parent('div.radioWrapper').addClass('isValid');
			}
			
			$this.click(function(){
				$this.parent('div.radioWrapper').addClass('isValid');
			})
		})
		*/
	},
	
	validate: function(){
		var self = this;
		this.formParams = '';
		this.isValid = true;
		this.textFields.each(function(){
			if (ValidationChecks.invalidChar(this.value, /.*[<>].*/)){
				self.onError(this);
				return true;
			}
			//if (this.value==this.title){    Updated due to new form layout
			//if (this.className=='notRequired') return;
			//if (this.value==''){
				//self.onError(this);
				//return true;
			//}
			
			//Case statement

			switch (this.name) {
				case 'first_name':
					if (!self.validateHandler(this,'letter')) allerrs += "* First Name\n";
					break;
				case 'last_name':
					if (!self.validateHandler(this,'letter')) allerrs += "* Last Name\n";
					break;
				case 'email':
					if (!self.validateHandler(this,'email')) allerrs += "* Email\n";
					break;
				case 'company':
					if (!self.validateHandler(this,'alphaNumeric'))  allerrs += "* Company\n";
					break;
				case 'phone':
					if (!self.validateHandler(this,'phone')) allerrs += "* Phone\n";	
					break;
				case 'zip':
					if (!self.validateHandler(this,'alphaNumeric')) allerrs += "* Zip\n";			
					break;
				/*Cases for longer validation form
				case 'mobile_phone':
					if (!self.validateHandler(this,'phone')) allerrs += "* Mobile\n";	
					break; 
				case 'state':	
					if (!self.validateHandler(this,'letter')) allerrs += "* County\n";
					break;
				case 'title':
					if (!self.validateHandler(this,'letter')) allerrs += "* Job Title\n";
					break;
				case 'address1':
					if (!self.validateHandler(this,'alphaNumeric')) allerrs += "* Address Line 1\n";
					break;
				case 'address2':
					if (this.value.length>0){
						if (!self.validateHandler(this,'alphaNumeric')) allerrs += "* Address Line 2\n";
					}
					break;
				case 'city':
					if (!self.validateHandler(this,'letter')) allerrs += "* City\n";
					break;
				*/
				default:
					break;
			}
	
		})
		
		//BGT Edits - Adding hidden fields to GET
		this.hiddenFields.each(function(){
			if (this.name=='campaignID'){
				this.value = getQueryVariable('cid');
			} 
			if (this.name=='unk'){
				this.value = 'norq29';
			} 
			if (this.name=='SRE001'){
				this.value = '044A001';
			} 
			if (this.name=='SRE003'){
				this.value = '044A001';
			}  
		})				
				
		this.selectBoxes.each(function(){
			if (this.name =='country') {
				if (this.value==''){
					allerrs += "* Country\n";
					self.onError(this);
				}
			}
			if (this.name =='SRE015') {
				if (this.value==''){
					allerrs += "* Employees with your company\n";
					self.onError(this);
				}
			} 						   
		})
		/*
		this.radioWrapper.each(function(){
			if (!$(this).hasClass('isValid')){
				var label = $(this).parents('tr').find('.label').html();
				var string = "* "+ label.substr(0,label.length-1) + "\n";
				allerrs += string;
				self.onError(this);
			}
		})
		*/
		if (this.isValid){
			//createCookie("norq29");
			this.form.submit();
		} else {
			alert('Please check field(s): \n' + allerrs);
			allerrs = '';
		}
	},
	
	validateHandler: function(element,type){
		var valid=0;
		switch (type){
			case 'letter':
				valid = ValidationChecks.letter(element.value);
				break;
			case 'email':
				valid = ValidationChecks.email(element.value);
				break;
			case 'numeric':
				valid = ValidationChecks.numeric(element.value);
				break;
			case 'phone':
				valid = ValidationChecks.phone(element.value);
				break;
			case 'alphaNumeric':
				valid = ValidationChecks.alphaNumeric(element.value);
				break;
			default:
				valid=0;
				break;
		}
		if (valid==1){
			return true;
		} else {
			this.onError(element);
			return false;
		}
	},
	
	onError: function(element){
		this.isValid = false;
	}
}


var ValidationChecks = {
	valRegex: function(val, r) {
		var s = '' + val;
		var v = (s.match(r) != null);
		return (v ? 1 : 0);
	},
	
	invalidChar: function(val, r){
		var s = '' + val;
		var v = (s.search(r) == 0);
		return (v ? 1 : 0);
	},
	
	email: function(val){
		return this.valRegex(val, /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/);
	},
	
	numeric: function(val){
		return this.valRegex(val, /^\d+$/);
	},
	
	letter: function(val){
		return this.valRegex(val, /^[a-zA-Z][a-zA-Z\-\s]*[a-zA-Z\s]$/);
	},
	
	alphaNumeric: function(val){
		return this.valRegex(val, /^[a-zA-Z0-9\s]+$/);
	},
	
	phone: function(val){
		return this.valRegex(val, /^(\(?[0-9]{3}[\)-\.]?\ ?)?[0-9]{3}[-\.]?[0-9]{4}$/)
	}

}


function getQueryVariable(variable) {
	if (!(unescape(location.href).indexOf("?") == -1)) { 
		var query = location.href.split("?");
		var vars = query[1].split("&");
		var ok = false;
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				ok = true; return pair[1];
			}
		}
		if (!(ok)) { return "undefined"; }
	}
	else { return "undefined"; }
}
