var globals =
{
	i: 0,
	agent: navigator.userAgent.toLowerCase()
}

var JBC =
{
	load:function(page)
	{
		Event.observe(window, 'load', function ()
		{
			if (!document.getElementsByTagName) return;

			$$("a").each(function(anchor) {
				if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') anchor.target = '_blank';
			}.bind(this));

			// Per-page actions
			if (page == 'contact')
				Event.observe($('form'), 'submit', JBC.contact.bindAsEventListener($('form')));

			if ((page == 'store') || (page == 'product') || (page == 'cart') || (page == 'checkout') || (page == 'search'))
				Event.observe($('search'), 'click', JBC.showModal.bindAsEventListener($('search')));
		});
	},

	showModal:function(e)
	{
		e = e || window.event;
		Event.stop(e);

		var modelId = this;

		if (modelId.identify() == 'search')
		{
			// build DOM elements for modalbox content
			element = Builder.node('div',[
				Builder.node('p','Use the form below to search our store; results will be shown for product title and description matches as well as category matches.'),
				Builder.node('form',{id:'form',method:'GET',action:'search.aspx'},[
					Builder.node('input',{type:'text',name:'q','class':'floatleft'}),
					Builder.node('input',{type:'submit',value:'Search','class':'button'})
				]),
			]);
			sTitle = 'Search our store';
			bFocus = true;
		}

		// show modalbox
		Modalbox.show(element, {title: sTitle, autoFocusing: bFocus});
	},

	fetching:function(running)
	{
		var b = $('send');
		var r = $('reset');
		var l = $('ajax_progress');

		if (running)
		{
			b.addClassName('hide');
			r.addClassName('hide');
			l.removeClassName('hide');
			$('form').disable();
		} else {
			b.removeClassName('hide');
			r.removeClassName('hide');
			l.addClassName('hide');
			$('form').enable();
		}
	},

	contact:function(e)
	{
		e = e || window.event;
		Event.stop(e);

		function checkNodeLength(object, node)
		{
			if (object.getElementsByTagName(node).length > 0) return true;
			else return false;
		}

		var form = this;

		new Ajax.Request('processContactXml.aspx', {
			method: 'post',
			parameters: Form.serialize(form),
			onCreate: JBC.fetching(true),
			onComplete: function(transport) {
				var success = true;
				var response = transport.responseXML;

				if (checkNodeLength(response, 'Success'))
				{
					if (response.getElementsByTagName('Success')[0].firstChild.data == 'false') success = false;
				}
				if (checkNodeLength(response, 'Message')) var message = response.getElementsByTagName('Message')[0].firstChild.data;
				if (checkNodeLength(response, 'ErrorField')) var errorfieldname = response.getElementsByTagName('ErrorField')[0].firstChild.data;

				// success?
				if (success == false) // failure!
				{
					// was there a field problem?
					if (errorfieldname.length > 0) // yes!
					{
						// yes, so we're done fetching
						JBC.fetching(false);

						// build DOM elements for modalbox content
						element = Builder.node('div',[
							Builder.node('p',[
								"The ",
								Builder.node('strong', errorfieldname),
								" field was left blank or unspecified. Fix this and try again."
							]),
						]);
						// show modalbox
						Modalbox.show(element, {
							title: 'Something went wrong...',
							autoFocusing: false,
							afterHide: function() {
								// get the id of the errorfield by using the name
								var field = form.getInputs('text', errorfieldname);

								// perform effect on UI
								new Effect.Highlight(field[0], {duration:4.0});
							}
						});
					}
					else // there wasn't a field problem, report generic error!
					{
						// build DOM elements for modalbox content
						element = Builder.node('div',[
							Builder.node('p',message),
							Builder.node('p',[
								"Please ",
								Builder.node('a',{href: encodeURI('mailto:jeremy@pgmintegrated.com?subject=Contact Form Error&body='+error)},'email this error'),
								" to our web agency, we won't know to fix it otherwise!",
							]),
						]);
						// show modalbox
						Modalbox.show(element, {title: 'Egg on our face...', autoFocusing: false});
					}
				}
				else // success!
				{
					// build DOM elements for modalbox content
					element = Builder.node('div',[
						Builder.node('p',"Thanks for your inquiry, it was successfully submitted and will be reviewed & responded to as soon as possible."),
					]);
					// show modalbox
					Modalbox.show(element, {
						title: 'Loud and clear',
						autoFocusing: false,
						afterHide: function() {
							// we're done fetching
							JBC.fetching(false);

							// perform effect on UI
							new Effect.BlindUp(form);
						}
					});
				}
			}
		});
	}
}