// stop window.console from erroring out on other browsers
if (!window.console || !console.firebug) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i < names.length; ++i) { window.console[names[i]] = function() {}; }
}

//************************************************************
//	CLOSURE FOR NON-ACCESSIBLE FUNCTIONS
//************************************************************
(function() {
	
	var lockStorage = {};
	var isLocked = function(el) {	return lockStorage[el] || false; };	
	var lock = function(el)	{	lockStorage[el] = true;	};	
	var unlock = function(el)	{	lockStorage[el] = false;	};
	
	var submitLoginForm = function(f) 
	{
		// check for a lock on this form submit
		if(isLocked('loginForm')) { return false; }
		// lock the form to stop it form submitting multiple times
		lock('loginForm');
		// modify and submit the form 
		var fdata = f.serialize();
		$.post(f.attr('action'), fdata, handleLoginForm, 'json');
	};
	
	var handleLoginForm = function(data,response) 
	{
		if(data.redirect) {
			window.location.href = data.redirect;			
			return true;
		}
		// unlock the login form
		unlock('loginForm');
		$('label.error').removeClass('error');
		$('ul.errorMessages').remove();
		// if we get here then we need to display the error messages
		var f = $('#formLogin');
		var errorList = $('<ul class="errorMessages"></ul>');
		$.each(data, function(key,errors) {
			$('label[for='+key+']').addClass('error');
			$.each(errors, function(errorType,errorMessage) {
				errorList.append('<li>' + errorMessage + '</li>');				
			});
		});
		f.before(errorList);	
	};
	
	var addFormBindings = function()
	{
		// handle key presses
		$('form').bind('keyup', function(event,el) {
			var f = $(this);
			switch(f.attr('id')) {
				case 'formLogin':
					if(event.keyCode == 13) {
						event.preventDefault();
						submitLoginForm(f);
					}
					return false;
					break;
				case 'formLogin2':
					if(event.keyCode == 13) {
						event.preventDefault();
						submitLoginForm(f);
					}
					return false;
					break;
			}
		});
		// handle submits
                // can't make the form use ajax to post due to the motionpoint translation stuff
//		$('form').bind('submit',function(event) {
//			var f = $(this);
//			/*** HANDLE THE LOGIN FORM ***/
//			switch(f.attr('id')) {
//				case 'formLogin':
//					event.preventDefault();
//					submitLoginForm(f);
//					return false;
//					break;
//			}
//		});
	};
	
	var styleFormButtons = function()
	{
		$('form.styled input[type=submit]').each(function() {
			var el = $(this);
			var parentForm = el.parents("form");
			var newLink = $('<a/>'); // create a new link
			newLink.addClass('button'); // make this link a button
			newLink.addClass(el.attr('class')); // add the original form element's classes
			//alert(el.attr('class'));
			newLink.html('<em>' + el.val() + '</em>'); // insert the input's value into our link
			newLink.attr('href',parentForm.attr('action')); // nullify the href
			newLink.attr('rel',el.attr('rel')); // carry over any rel attributes data for tracking
			newLink.attr('onClick',el.attr('onClick')); // carry over any onClick attributes data for tracking
			// we need to insert the hidden form field for this to modify later unless its value is submit
		  
			newLink.bind('click',function(e) {
			    // let's get a reference to our parent form
			    var trigger = $(this);
			    var theForm = trigger.parents("form");
			    // update our hidden form variable with the submit value
			    //theForm.find('input#'+trigger.attr('rel')).val(trigger.find('em').html());
			    return false;
		    });
		    newLink.bind('mouseup',function(evt) {
			  	this.blur();
			  	evt.preventDefault();
				$(this).parents("form").submit();
			  	return true;
		    });
			el.replaceWith(newLink);
		});
	};
	
	var handleBodyClicks = function()
	{
		$('body').bind('click',function(event) {
		});
	};
	
	// add tracking to all a tags with rel attributes
	// the rel attribute has to be a valid omniture pageName value
	var addLinkTracking = function()
	{
		$('a[rel]').each(function() {
			$(this).bind('click', function() {
				var s = s_gi('dalcom'); 
				s.pageName = $(this).attr('rel'); 
				s.channel = 'orientation'; 
				void(s.t());		
			});
		});
		
	}
	
	// run this when the page loads
	$(function() {
		styleFormButtons();
		addFormBindings();
		handleBodyClicks();
		addLinkTracking();
	});

})();

//************************************************************
//	NEW MEMBER QUIZ (NMQ) PUBLIC API FUNCTIONALITY
//************************************************************
var NMQ = (function() {
	
	//************************************************************
	//	PRIVATE METHODS/VARIABLES
	//************************************************************
	
	//************************************************************
	//	PUBLIC/PRIVILEDGED METHODS/VARIABLES
	//************************************************************
	return {

		'trackLink': function() 
		{
			// for links that need to be tracked, pass in the relevant data and post it
			var s=s_gi('dalcom'); s.pageName="marketing:offers:orientation:click:perks"; s.channel='orientation'; void(s.t());
		}
			
	}; // end of public method return
	
})();


