/* 
	BridalC Base Object
	LM: 10-01-10 

*/
jQuery(function ($) {
	$.root = $(document);	
	var uri = window.location.href,
		t = function (_s) { return $.trim(_s); },
		byId = function (_id) {return document.getElementById(_id);};
	
	var CONFIG = {
		LIGHTBOX_OPTIONS : {
			overlayBgColor: '#FFF',
			overlayOpacity: 0.6,
			imageLoading: '/images/lightbox-ico-loading.gif',
			imageBtnClose: '/images/lightbox-btn-close.gif',
			imageBtnPrev: '/images/lightbox-btn-prev.gif',
			imageBtnNext: '/images/lightbox-btn-next.gif'
		}
	};
	
	// BC SPECIFIC FUNCTIONS //
	var BC = {
	
		ShoppingCart : {
			passTotal : function () {
				// add the current total price to the href of the checkout link as a hash //
				$('#catshopbuy').live('mousedown', function () {
					this.href = this.href + '#' + $.trim($('#invoicetotal').text().replace(/[^\d\.]/g, ''));
				});

			}
		},
		
		resolveCheckOut : function () { // follows the donnahay layout See: http://dhonline.smartwebmarketing.com.au/
			var $chooseCon = $('#choose-con'),
				$loginCon = $('#login-con'),
				$checkoutCon = $('#checkout-con'),
				$chooseRadios = $chooseCon.find('input:radio');		
			
			// isLoggedIn - must be declared above ex. var isLoggedIn = {module_isloggedin}
			if (!isLoggedIn) {	// if NOT logged in		
				
				$chooseCon.removeClass('hide');
				$checkoutCon.find('input[readonly]').not('#Amount').removeAttr('readonly');
				$chooseRadios.bind('click', function (e) {
					if ((1*this.value) === 1) {
						$checkoutCon.addClass('hide');
						$loginCon.removeClass('hide');
					}
					else {								
						$loginCon.addClass('hide');
						$checkoutCon.removeClass('hide');
					}
				});	
			}
			else { // if logged in		
				$checkoutCon.removeClass('hide');		
			}

			var amount = $.trim(window.location.hash.replace('#','')); // get Amount here
			if (amount !== '') {
				document.getElementById('Amount').value = amount;
			}	
		},
		
		usernameEqualtoEmail : function () {
			$('#Username').blur(function () {
				$('#EmailAddress').val(this.value);
			});

		},
		
		shippingEqualToBilling : function (_checkbox_selector) {
			//  <input type="checkbox" style="margin-left: 10px;" id="billing-shipping-checkbox" /><small>(Same as Billing details)</small>
			$(_checkbox_selector).bind('click', function () {
				if (!this.checked) {					
					$('#ShippingAddress, #ShippingCity, #ShippingState, #ShippingZip').val('');
					return;
				}				
				var items = ['Address','City','State','Zip'],
					len = items.length,
					i = 0;
				for (;i<len;i++) {
					$('#Shipping' + items[i]).val($('#Billing'+items[i]).val());
				}
				$('#ShippingCountry').find('option[value='+$('#BillingCountry').find('option:selected').val()+']').attr('selected', true);
					
			});
		},
		
		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 {
					
				}				
			}
		},

		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');
				}
			});
		}
	};	
	
	var privates = {
		bodyClassHack : function () {
			var $body=$(document.body),browser=Ttow.Browser;
			/*@cc_on
			$body.addClass('ie');if(browser.msie6){$body.addClass('ie6 ie67 ie68 ie69');}
			else if(browser.msie7){$body.addClass('ie7 ie67 ie78 ie79');}
			else if(browser.msie8){$body.addClass('ie8 ie68 ie78 ie89');}
			else if(browser.msie9){$body.addClass('ie9');}
			return;@*/
			$body.addClass('not-ie');
			if(document.addEventListener){if(browser.mozilla){$body.addClass('moz');}
			else if(browser.chrome){$body.addClass('chrome');}
			else if(browser.safari){$body.addClass('safari');}
			else if(browser.opera){$body.addClass('opera');}
			else if(browser.ff2){$body.addClass('ff2');}}						
		}
	};
	
	var E = {
		unloads : {			
			unload : function (e) {
				//$('div.leftarea').addClass('hide');
			},			
			beforeunload : function (e) {
				$(this).unbind('unload');
				//$('div.leftarea').addClass('hide');	
			}			
		},
		
		numeric : function (e) {
			if (t(this.value) === '') {return;}
			if (!(/^[0-9,]+$/).test(t(this.value))) {
				this.value = '';					
				alert('Please enter a numeric value', 'warning');					
			}
		}								
	};
	
	window.BridalC = {				
		
		BC : BC, // expose the BC object
		
		CONFIG : CONFIG,
		
		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);			
		},
		
		// this scripts will run on the shopping cart page //
		doShoppingCartStuff : function () { 
			if ($('div.shop-cart').length && uri.indexOf('OrderRetrievev2.aspx') > -1) {				
				
			}			
		},
		
		// this scripts will run onf the checkout page //
		doCheckoutPageStuff : function () {
			
		},

		setUpLightBoxPlugin : function (_callback) {
			// See: http://leandrovieira.com/projects/jquery/lightbox/
			Ttow.require([
				'$css(/css/jquery.lightbox-0.5.css)',
				'/js/jquery.lightbox-0.5.min.js'
			], _callback);
		},
		
		upcomingEventsHack : function () {
			$.get('/upcoming-events-ajax', function (res) {	
				// inject a temporary div in the document to place the response html
				$('<div id="tmp-div" class="hide"/>').html(res).appendTo(document.body);				
				var $tmpDiv = $('#tmp-div');					
				$('div.events').find('div.content').html($tmpDiv.find('div.content').html()).removeClass('hide'); 
				
				var $releasedate = $tmpDiv.find('.releasedate').text();
				var $splitdate = $releasedate.split('-');
				$('p.day').text($splitdate[0]).append('<span>th</span>');
				$('p.month').text($splitdate[1]);
				$('p.year').text($splitdate[2]).parent().removeClass('hide');
				$tmpDiv.remove(); // remove the temporary div from thr document	
			});			
		},
			
		init : function () {
			privates.bodyClassHack();
			BridalC.loadEvents();
			// BridalC.upcomingEventsHack();  fix the upcoming events widget at the top right corner of the page
			
		}
		
	};

	BridalC.init();	
});
