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

	Meattingplace Base Object
	LM: 04-14-10
	@author Rafael G.

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

var Meattingplace = (function ($, w, d) {
	var undef,
		uri = w.location.href,
		t = function (_s) { return $.trim(_s); },
		PATHS = {
			JS : '/js/',
			CSS : '/css/',
			IMG : '/images/'
		},
		PLUGINS = {
			CSS : {
				JGROWL : 'jquery.jgrowl.css'
			},
			JNOTIFICA : 'jquery.jnotifica.min.js',
			JGROWL : 'jquery.jgrowl.min.js',
			REQ_BG_COLOR : 'require-bgcolor.js',
			DD_ROUNDIES : 'DD_roundies_0.0.2a-min.js',
			JHINT : 'jquery.jhint.js',
			REMEMBER_ME : 'Ttow.RememberMe.js',
			WTOOLTIP : 'wtooltip.min.js'
		};
	
	// the CRAP i have to do bec of W3C validation....WTF W3C!!!		   
	var __WTF_W3C = function () {		
		// NOTE: rounded corners are not supported in Opera & slow on ie8
		var pcss = '<style type="text/css">'+
	           	   '.roundies5 {'+'-moz-border-radius: 5px;'+'-webkit-border-radius: 5px;'+'border-radius: 5px;'+'}'+
			       '.roundies10 {'+'-moz-border-radius: 10px;'+'-webkit-border-radius: 10px;'+'border-radius: 10px;'+'}'+
			       '.opacity50 {opacity: 0.5; filter: alpha(opacity=50);}'+
				   ''+
			       '</style>';
		$('head').append(pcss); // wtf W3C stop bitching!	
    	
		// apply rounded corners in ie on load...currently this only applies to ie 7		
		if (Ttow.Browser.msie && Ttow.Browser.msie7) {						
			DD_roundies.addRule('.roundies5', '5px');
			DD_roundies.addRule('.roundies10', '10px');
			DD_roundies.addRule('.jGrowl .jGrowl-notification, .jGrowl .jGrowl-closer', '5px');
		}		   
	};
	
	var __checkIfLoggedIn = function () {
		var flag = (1 * t($('#is-logged-in').val())),
			$navList = $('#nav_633873');
		if (flag === 0) { // if no one is logged in							
			$('#-sua, #-sup, #-tsa').removeClass('hide');			
			$navList.append('<li><a href="/login">Log In</a></li>'); // add login link to the main nav menu..
			
			if (uri.indexOf('OrderRetrievev2.aspx') !== -1) {
				// hide this area when on shopping cart and checkout form...
				$('div.week-special').addClass('hide');
			}
		}
		else {	// some one is logged in		
			var $loggedin = $('#if-logged-in-area'),
				innHtml = '<h3 class="left" id="iam"><a href="/vip_customers">Welcome ' + $('#cust-fullname').val() + '!</a></h3>'+
						  
						  '<h3 class="right" style="padding-right: 5px"><a href="/my-favorites">My Favorites</a></h3>'+
						  '<div class="clearB"></div>'+
						  '<p>'+
							  '<a title="Go to your VIP page" href="/vip_customers">'+
								  '<img style="border: 0px none; vertical-align: bottom;" alt="VIP" src="/images/user_green.png" /> '+
								  'Click here view your Account Page'+
							  '</a>'+
						  '</p><br />';
				
			$loggedin.prepend(innHtml).removeClass('hide');			
			$navList.append('<li><a href="/LogOutProcess.aspx">Log Out</a></li>'); // add logout link to the main nav menu..
			
			// show vip link (beside the print link)
			// but dont show it when the page is /vip_customers
			if (uri.indexOf('/vip_customers') < 0) {
				$('#vip-page-link').removeClass('hide'); 
			}			
			
			//OrderRetrievev2.aspx   --> shopping cart
			//OrderRetrievev2.aspx?Step=13 --> checkout form
			if (uri.indexOf('OrderRetrievev2.aspx') !== -1) {
				// hide these areas when on shopping cart and checkout form...
				$('div.customer-area-body, div.customer-area-tail').addClass('hide');
			}
			
			// when on the homepage, reduce the height of the .product div
			// so that the gap between the vip link and the latest news section
			// won't be so big.
			if (uri.indexOf('/home') > -1) {
				$('.products').css('height','124px');
			}
			
			// used to remove unwanted markup so that the xtra space below the shoppingcart summary can be reduced //
			$('div.sign-in').css({
								height : 'auto',
								paddingBottom : '5px'
							})
							.find('div.sign-in-body')
								.css('minHeight', '0')
								.find('#-sua, #sidebar-sign-up-validation-script')
									.remove();
		}
	};
	
	var __unloads = function () {
		var transMsg = '<div align="center" class="valign-top"><h3 id="unload-message" class="roundies5">Fetching page...</h3></div>';
		$(w)
			.bind('unload', function () {
				$('div.content-area, div.leftarea').addClass('hide');
			})
			.bind('beforeunload', function () {
				$(this).unbind('unload');
				$('div.content-area, div.leftarea').addClass('hide');
				
			});
		$("a[href^='http://'], a[href^='https://']").bind('mousedown', function () {
			$(w).unbind('unload').unbind('beforeunload');
		});
	};
	
	var __loadRememeberMe = function (_hollaback) { // not applied yet	
		_hollaback = _hollaback || function () {};	
		if (uri.indexOf('.au/login') === -1) {
			$.getScript(PATHS.JS + PLUGINS.REMEMBER_ME, function () {
				_hollaback();
			});		
		}		
	};
	
	var __freeLoginValidationScript = function () {
		if ($.isFunction(window.checkWholeForm75704)) { return; }
		if (uri.indexOf('.au/login') !== -1) { return; }		
		var s = t($('#sidebar-sign-up-validation-script').text());			
		$.globalEval(s);
	};
	
	var __fireEvents = function () {
		__unloads();
	};
	
	
	return {
		init : function () {
			__WTF_W3C();
			__fireEvents();
			__checkIfLoggedIn();
			__freeLoginValidationScript();
			
			//var $faveLink = $('a.favoritelink');
			//if ($faveLink.length > 0) { 
				//$faveLink.text('Remember Me');
			//}			
		},
		
		Dialog : {
			error : function (_msg, _notForm) {
				// <div id="form-error" class="redb hide"></div> //
				var $dialogCon = $('#form-error');
				if ($dialogCon.length <= 0) { return; }				
				if (_notForm === undef) {
				    $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>';	
					scrollTo(0, ($dialogCon.offset().top - 30));
					$dialogCon.html(html)
							  .slideDown('slow');					
				}
				else {
					
				}				
			},
			
			alertOverride : function (_mode) { // make $.jGrowl the default alert
				if (t(_mode) === 'ecommerce') {				
					// use jnotifica....from brazil //
					$.getScript(PATHS.JS + PLUGINS.JNOTIFICA, function () { // hallelujah...it works in ie 6!
						Ttow.BC.alertOverride(function (_msg) {	
							// See: http://jnotifica.lenonmarcel.com.br/			    
							$.jnotifica(Ttow.Util.nl2br(_msg), {
							  position  : 'bottom',
							  background: 'green',							  						  
							  cursor    : 'default'
							});
	
						});
					});		
				}				
			}			
		},
		
		loadRequireBGColor : function (_hollaback) {
			_hollaback = _hollaback || function () {};
			$.getScript(PATHS.JS + PLUGINS.REQ_BG_COLOR, _hollaback);
		}
		
	};
})(window.jQuery, window, document);

jQuery(function ($) {
	Meattingplace.init();
	Meattingplace.Dialog.alertOverride('ecommerce');
	Meattingplace.isLoggedin = (1 * $('#is-logged-in').val()) !== 0;	
});

