/*********************************************************

	Jbm Base Object
	LM: 06-22-11

**********************************************************/

jQuery(function ($) {
Ttow.require('/js/jquery.preloadCssImages.min.js', function () {
	$.preloadCssImages();
});
Ttow.require('/js/jquery.jnotifica.min.js');


if (Ttow.Browser.msie6) {
	$('input[type=text]').addClass('jbm-text');
	$('input[type=password]').addClass('jbm-password');
}

window.Jbm = (function (window, document, undefined) {
	
	jQuery.root = $(document);
	
	var uri = window.location.href,
		t = function (_s) { return $.trim(_s); },
		byId = function (_id) {return document.getElementById(_id);},
		$con = jQuery.root.find('#container'),
		isLoggedIn = (1 * byId('module_isloggedin').value),
		whosLoggedIn = t($con.find('#module_fullname').val());
	
	var E = {
	
		unloads : {			
			unload : function (e) {
				$('div.rightarea').addClass('hide');
			},			
			beforeunload : function (e) {
				$(this).unbind('unload');
				$('div.rightarea').addClass('hide');				
			}			
		},
		
		checkOutRadioButtons : function (e) {
			var v = t(this.value),
				$loginCon = $('#login-con'),
				$checkoutCon = $('#checkout-con');
			if (v === 'register') {
				$loginCon.hide();
				$checkoutCon.fadeIn('slow');
			}
			else {
				$checkoutCon.hide();
				$loginCon.fadeIn('slow');				
			}
		},
		
		numeric : function (e) {
			if (t(this.value) === '') {return;}
			var label = t($(this).parent().siblings('td').text());
			if (!(/^[0-9,]+$/).test(t(this.value))) {
				this.value = '';
				if (label !== '') {
					Jbm.Dialog.notify('Please enter a numeric value for ' + label, 'warning');
				}
				else {
					Jbm.Dialog.notify('Please enter a numeric value', 'warning');
				}
			}
		},
		
		accordionSlide : function (e) {
			var $me = $(this);
			$me.siblings('ul').slideToggle('slow');			
			e.preventDefault();
			return false;
		}
							
	};
	
	
	var privates = {
	
		loadEvents : function () {
			
			//window unload events //
			$.root.delegate("a[href^='http://'], a[href^='https://']", 'mousedown', function () {
				$(window).unbind('unload').unbind('beforeunload');
			});			
			$(window).bind({				
				'unload' :  E.unloads.unload,
				'beforeunload' : E.unloads.beforeunload
			});
			
			if (Ttow.Browser.msie8) {
					$('noscript').css('display', 'none');
			}
			
			$.root.delegate('input.numeric', 'blur', E.numeric);
			
			// LM: 06-22-11 [fix the recurrent div problem]
			var $hasDiv = $('#sidenav-ul > li > div > a')
			if ($hasDiv.length) {
				$hasDiv.unwrap(); // remove the div
			}
			
			// LM: 11-02-10			
			$.root.delegate('#sidenav-ul > li > a', 'click', E.accordionSlide);	
			
			
			
		},

		log : function (m) {
			if (window.console) {
				window.console.log(m);
			}
		},

		resolveLoginChanges : function () {
			var $registerLink = $('#register-link'),
				$loginLink = $('#login-link');
			//this.log(isLoggedIn);
			
			if (isLoggedIn > 0) {
				$registerLink.attr('href', '/my-account')
							 .text('Welcome ' + whosLoggedIn);
							 
				$loginLink.attr('href', '/LogOutProcess.aspx')
						  .text('LOGOUT');			 
			}
			else {
				
			}
			$registerLink.parent().removeClass('invisible');
		},

		resolveCheckout : function () {
			var $radioDiv = $('#radiodiv'),
				$radio = $radioDiv.find('input:radio');				
	
			if (isLoggedIn === 0) {
				$radio.bind('click', E.checkOutRadioButtons);
				$radioDiv.removeClass('hide');
				
				$('#catseczoneform24738, #catwebformform42059').attr('action', function (i, action) {
					return action + '&PageID=' + self.location.href.replace(/^.{1,}\.com/,'');
				});	
			}
			else {
				$('#checkout-con').removeClass('hide');
				$('#FirstName, #LastName, #Username, #Password').attr('readonly', true);

			} 						
		},
		
		checkActiveAccordion : function () {
			$('#sidenav-ul').find('a').each(function () {
				if (uri.indexOf(this.href) > -1) {
					$(this).closest('ul').show();
					return false;
				}
			});
		}
	};

	return {
		init : function () {			
			privates.loadEvents();
			privates.resolveLoginChanges();
			privates.checkActiveAccordion(); //checks which accordion should be open	
		},
		
		uri : uri,
		$con : $con,
		
		Dialog : {
			formError : function (_msg, _notForm) {
				// <div id="form-error" class="redb hide"></div> //
				var $dialogCon = $('#form-error');
				if ($dialogCon.length <= 0) { return; }				
				if (_notForm === undefined) {
				    $dialogCon.html('').hide();
					_msg = _msg.replace('\n', ''); // under observation
					var warnings = _msg.split('-'),
						warLen = warnings.length,
						html = '';					
					html += '<p>';
					html += '<h4 class="notice">You need to fill in these fields:</h4><br />';
					//html += '<img src="'+PATHS.IMG+'error_button.gif" alt="" style="float: left"/>';		
					html += '<ul id="warning-list">';	
					for (var i=0; i<warLen; i++) {						
						if (t(warnings[i]) === '') {continue;}
						html += '<li>' + t(Ttow.Util.entities(warnings[i])) + '</li>';
					}
					html += '</ul>';
					html += '</p>';	
					$(document.body).Ttow(['scrollToMe']); 
					$dialogCon.html(html)
							  .slideDown();					
				}
				else {
					
				}				
			},			
			
			notify : function (_msg, _mode) {
				if (!$.isFunction($.jnotifica)) {
					privates.log('Unable to find $.jnotifica plugin'); 
					return;
				}
				
				var m = t(_mode) || 'info',
					o = {
						position  : 'top',
						opacity: 0.9,
						border: '1px solid green',
						msgCss : {
							textAlign : 'center',	
							fontWeight: 'bold',
							color: '#fff'
						},
						timeout : 4000
					},
					addGlow = ((!Ttow.Browser.msie) ? 'glow ' : '');
				if (m === 'info') {					
					$.extend(true, o, {
						background : '#023E8F',						
						classes : addGlow + 'jnotifica-info',
						border: '2px solid #4889CD',
						msgCss : {														
							border: '1px solid #4889CD'
						}	
					});
					
				}
				else if (m === 'warning') {
					$.extend(true, o, {
						background : '#B90000',
						classes : addGlow + 'jnotifica-warning',
						border: '2px solid #96CB33'
					});
					
				}
				$.jnotifica(_msg.replace('\n','<br />'), o); // jnotifica from brazil!
				
				//if (Ttow.Browser.msie6) { // handle correct png transparency in ie6
					//correctPNG(byId('jnotifica_main').getElementsByTagName('img'));					
				//}
			}		
		},
		
		formFieldHighlight : function (_form) { 
			$(_form).find('input, textarea, select').bind({
				'focus' : function () {
					$(this).closest('tr').addClass('form-field-highlight');
				},
				
				'blur' : function () {
					$(this).closest('tr').removeClass('form-field-highlight');
				}
			});
		},
		
		byId : byId,
		
		resolveCheckout : function () {
			privates.resolveCheckout();
		},
		
		ifLoggedInGoTo : function (_url) {
			if (isLoggedIn !== 0) {
				window.location.href = _url;
			}			
		},

		checkEmail : function (_e) {
			return (/^[A-z0-9._]{2,}@[A-Za-z0-9]{2,}\.[a-z]{2,}(\.[a-z]{2,})?$/).test(t(_e));
		}	
	};


})(window, document);

// go Jbm!
Jbm.init();

// override alert
/**/
var _oldalert = window.alert;
window.alert = function (_msg, _mode) {
	if (_mode) {
		_oldalert(_msg);
		return;
	}
	Jbm.Dialog.notify(_msg);
};
/**/

});
