/** * @author Lee McMullen, Qchi Ltd. http://qchi.net */$(function(){    HWGlobal.setup();})// HWGlobal is declared in admin/shared/default subform// Extend it with the globalObject belowvar globalObject = {	setup: function(){		this.enquiry.preSetup();		this.tooltips.setup();	},	enquiry: {		preSetup: function(){			// Add the "Send Message" link into the menu			var messageLink = '<a class="message" href=""><span>Send us a message...</span></a>';			$('.contact-floating div.column:first').prepend(messageLink);			var sellingLink = '<div id="buy-from"><h2>Quality watch to sell?</h2><a href="" class="selling-link"><span>We will buy it...</span></a></div>';			$('.contact-floating div.column:last').append(sellingLink);		},		setup: function(){			// Apply the fancybox to the send message link			$('a.message').fancybox({				'content': $('#general-enquiry-container'),				'hideOnContentClick': false,				'onStart': function(){					// This fancbox will have been displayed from the tooltip menu					// Therefore tooltip needs to be hidden					$('ul#menu li.tooltip.contact a').qtip('hide');				},				'onComplete': function(){					$('#enquiry-submit_general').click(function(e){						HWGlobal.enquiry.submitClick(this, e);					});					$('#name_general').focus();				},				'type': 'html'			});		},		submitClick: function(clicked, e){			var msg = '';						var requiredItems = ['name_general', 'contact_number_general', 'item_comments_general'];			var proceed = HWGlobal.enquiry.validate(requiredItems, clicked);						if (proceed) {				// Everything is filled in correctly. Remove validation message and submit				$('span.invalid-message').remove();								var url = HWGlobal.settings.dbPathFull + "ajax/send/enquiry_email";				var data = {					type: "general",					name: $('#name_general').val(),					contact_number: $('#contact_number_general').val(),					email_address: $('#email_address_general').val(),					item_comments: $('#item_comments_general').val()				};				var ajaxObject = HWGlobal.enquiry.buildAjaxObject(clicked, url, data);				$.ajax(ajaxObject);			}		},		buildAjaxObject: function(clicked, url, data){			var ajaxObject = {				type: "POST",				url: url,				data: data,				dataType: "json",				beforeSend: function(xhr, settings){					HWGlobal.enquiry.beforeSend(xhr, settings, clicked);				},				success: function(data){					HWGlobal.enquiry.success(clicked, data);				}			};			return ajaxObject;		},		beforeSend: function(xhr, settings, clicked){			// Replace send button with a progress image			var msg = '<span class="sending">Sending...</span>';			$(clicked).hide().after(msg);		},		success: function(clicked, data){			var msg = '';			if (data.status === 'sent') {				msg = '<div class="message-sent">Thank you, your message has been sent. Closing in <span class="closing">5</span>...</div>';				$.fancybox(msg);				HWGlobal.enquiry.timer();			} else {				// Display a message saying something went wrong				msg = HWItem.enquiry.createErrorMessage('There was a problem while sending your message. Please try again...');				$(clicked).after(msg);			}			$('.sending').remove();			$(clicked).show();		},		timer: function(){			var timerVar = setTimeout(function(){				var curVal = $('span.closing').html();				curVal--;				$('span.closing').html(curVal);				if (curVal == 0) {					clearTimeout(timerVar);					$.fancybox.close();				} else {					HWGlobal.enquiry.timer();				}			}, 1000);		},		createErrorMessage: function(msg){			return '<span class="invalid-message">' + msg + '</span>';		},		validate: function(items, clicked){			var item;			var retVal = true;			$.each(items, function(i, val){				item = $('#' + val);				if (item.val() === '') {					item.addClass('invalid');					retVal = false;				} else {					item.removeClass('invalid');				}			});			if (!retVal) {				// There were some invalid fields which have now been highlighted				// Give the user a message about what to do				if (!$('span.invalid-message').length) {					msg = HWGlobal.enquiry.createErrorMessage('Please enter a value in the required fields above');					// this is the button we clicked, add the message directly after					$(clicked).after(msg);				}				// Give focus to the first invalid field				$('.invalid:first').focus();			}			return retVal;		}	},	sellingEnquiry: {		setup: function(){			$('a.selling-link').fancybox({				'content': $('#selling-enquiry-container'),				'hideOnContentClick': false,				'onStart': function(){					$('ul#menu li.tooltip.contact a').qtip('hide');				},				'onComplete': function(){					$('#enquiry-submit_selling').click(function(e){						HWGlobal.sellingEnquiry.submitClick(this, e);					});					$('#name_selling').focus();				},				'type': 'html'			});		},		submitClick: function(clicked, e){			var msg = '';						var requiredItems = ['name_selling', 'contact_number_selling', 'item_comments_selling'];			var proceed = HWGlobal.enquiry.validate(requiredItems, clicked);						if (proceed) {				// Everything is filled in correctly. Remove validation message and submit				$('span.invalid-message').remove();								var ajaxObject = HWGlobal.sellingEnquiry.buildAjaxObject(clicked);				$.ajax(ajaxObject);			}		},		buildAjaxObject: function(clicked){			var url = HWGlobal.settings.dbPathFull + "ajax/send/enquiry_email";			var data = {				type: "selling",				name: $('#name_selling').val(),				contact_number: $('#contact_number_selling').val(),				email_address: $('#email_address_selling').val(),				brand: $('#brand_selling').val(),				model: $('#model_selling').val(),				serial_number: $('#serial_number_selling').val(),				year: $('#year_selling').val(),				item_comments: $('#item_comments_selling').val()			};			var ajaxObject = HWGlobal.enquiry.buildAjaxObject(clicked, url, data);			return ajaxObject;		}	},	tooltips: {		setup: function(){			var qtipConfig = {				content: '',				prerender: true,				position: {					my: 'top center',					at: 'bottom center',					adjust: {						y: 5					},					viewport: $(window)				},				style: {					classes: 'ui-tooltip-light ui-tooltip-rounded ui-tooltip-shadow',					tip: {						width: 10,						height: 10					}				},				show: {					effect: function(offset){						$(this).fadeIn(300);					}				},				hide: {					fixed: true,					delay: 100,					effect: function(offset){						$(this).fadeOut(100);					}				}			};			if ($.browser.msie) {				qtipConfig.show.effect = false;				qtipConfig.hide.effect = false;			}			qtipConfig.content = "Return to the homepage!";			$('ul#menu li.tooltip.home a').qtip(qtipConfig);			qtipConfig.content = $('#stock-container').html();			$('ul#menu li.tooltip.stock a').qtip(qtipConfig);			qtipConfig.content = "Read our latest news!";			$('ul#menu li.tooltip.news a').qtip(qtipConfig);						qtipConfig.content = $('.contact-container').html();			var contactObject = {				events: {					render: function(e, api){						HWGlobal.enquiry.setup();						HWGlobal.sellingEnquiry.setup();					}				}			};			$.extend(qtipConfig, contactObject);			$('ul#menu li.tooltip.contact a').qtip(qtipConfig);		}	}};$.extend(HWGlobal, globalObject);
