//Cufon.replace('.nav li a, .content h1, .content h2, .content h2 a, .content h3, .content h4, .content h5, .content h6, #sidebar h3');
(function ($, window) {
	$.bob = {
		external: function (settings) {
			var hostname = window.location.hostname, options = $.extend({
				selector: 'a'
			}, settings);
			if (typeof settings === 'string') { options.selector = options; }
			hostname = hostname.replace('www.', '').toLowerCase();
			return $(options.selector).each(function () {
				if ($(this).attr('href') !== undefined) {
					var href = $(this).attr('href').toLowerCase();
					if (href.indexOf('http://') !== -1 && href.indexOf(hostname) === -1) {
						$(this).attr('target', '_blank');
						$(this).addClass('external');
					}
				}
			});
		},
		forms: function (settings) {
			var valid = false, options = $.extend({
				selector: 'form',
				err: 'This is required',
				errEmail: 'A valid email address is required',
				errUrl: 'A valid URL is required',
				errPhone: 'A valid Phone number is required',
				notValidClass: 'notvalid'
			}, settings);
			if (typeof settings === 'string') { options.selector = settings; }
			function checkLabel(obj) {
				var text = $('label[for=' + $(obj).attr('id') + ']').text();
				return (text === $(obj).val());
			}
			function error(obj, errormsg) {
				var parent = $(obj).parent();
				parent.append('<span class="errors">' + errormsg + '</span>');
				$('span.errors', parent).hide().fadeIn('fast');
				$(obj).addClass(options.notValidClass);
				valid = false;
			}
			function check(obj) {
				if ($(obj).val() === '' || checkLabel(obj)) {
					var errormsg = ($(obj).attr('title') !== '') ? $(obj).attr('title') : options.err;
					error(obj, errormsg);
				}
			}
			function checkRegEx(obj, type) {
				var regEx, err, errormsg, val = $(obj).val();
				switch (type) {
				case 'email':
					regEx = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
					err = options.errEmail;
					break;
				case 'url':
					regEx = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
					err = options.errUrl;
					break;
				case 'phone':
					regEx = /^\s*\(?\s*\d{1,4}\s*\)?\s*[\d\s]{5,8}\s*$/;
					err = options.errPhone;
					break;					
				}
				val = $.trim(val);
				if (val.search(regEx) === -1 || checkLabel(obj)) {
					errormsg = ($(obj).attr('title') !== '') ? $(obj).attr('title') : err;
					error(obj, errormsg);
				}
			}
			$('input.label,textarea.label').each(function () {
				var text = $('label[for=' + $(this).attr('id') + ']').text();
				$('label[for=' + $(this).attr('id') + ']').css('display', 'none');
				$(this).val(text);
				$(this).focus(function () {
					if ($(this).val() === text) { $(this).val(''); }
				});
				$(this).blur(function () {
					if ($(this).val() === '') { $(this).val(text); }
				});
			});
			return $(options.selector).each(function () {
				$(this).submit(function () {
					$('.errors', this).remove();
					$('.' + options.notValidClass, this).removeClass(options.notValidClass);
					valid = true;
					$(':text.required', this).each(function () {
						if ($(this).hasClass('email')) {
							checkRegEx(this, 'email');
						} else if ($(this).hasClass('url')) {
							checkRegEx(this, 'url');
						} else if ($(this).hasClass('phone')) {
							checkRegEx(this, 'phone');
						} else {
							check(this);
						}
					});
					$(':password.required', this).each(function () {
						check(this);
					});
					$('textarea.required', this).each(function () {
						check(this);
					});
					$(':checkbox.required', this).each(function () {
						if (!$(this).attr('checked')) {
							var errormsg = ($(this).attr('title') !== '') ? $(this).attr('title') : options.err;
							error(this, errormsg);
						}
					});
					return valid;
				});
			});
		},
		popup: function (options) {
			var defaults = {
				selector: '.popup',
				popupId: 'bob_popup',
				preloadText: 'Loading...',
				errorText: 'There has been a problem with your request, please click outside this window to close it.',
				closeText: 'Close',
				prevText: '&laquo; Previous',
				nextText: 'Next &raquo;'
			};
			if (typeof options == 'string') defaults.selector = options;
			var options = $.extend(defaults, options);
			return $(options.selector).each(function (i) {
				if ($(this).hasClass('gallery')) {
					var classNames = $(this).attr('class');
					classNames = classNames.split(' ').join('');
					$.data(this, 'gallery', classNames);
					eval('if((typeof ' + classNames + '_arr == "undefined")) ' + classNames + '_arr= new Array()');
					eval(classNames + '_arr').push($(this));
					$.data(this, 'index', eval(classNames + '_arr').length - 1)
				};
				$(this).bind('click', function (e) {
					e.preventDefault();
					if ($.browser.opera) $.support.opacity = true;
					var ie6 = $.browser.msie && $.browser.version.substr(0, 1) < 7;
					var opera95 = $.browser.opera && $.browser.version <= 9.5;
					var w = $(window).width();
					var h = $(document).height();
					var w2 = $(window).width() / 2;
					var h2 = $(window).height() / 2;
					show = function () {
						$('#' + options.popupId + 'preloader').remove();
						if (cw != 0) $('#' + options.popupId + 'content').css('width', cw + 'px');
						if (ch != 0) $('#' + options.popupId + 'content').css('height', ch + 'px');
						set($('#' + options.popupId + 'content'));
						$('#' + options.popupId + 'content').css('visibility', 'visible')
					};
					set = function (obj) {
						$(obj).css({
							'border-radius' : '7px',
							'padding': '12px',
							'background': '#fff',
							'color': '#333',
							'text-align': 'left',
							'float': 'left',
							'position': 'fixed',
							'z-index': '10001',
							'visible': 'hidden'
						});
						var left = w2 - $(obj).width() / 2;
						var top = h2 - $(obj).height() / 2;
						$(obj).css({
							'left': left,
							'top': top,
							'display': 'none'
						}).fadeIn('1000');
						if (ie6) $(obj).css({
							'position': 'absolute',
							'top': (top + $(window).scrollTop()) + 'px'
						});
						if (opera95) $(obj).css({
							'position': 'absolute',
							'top': (document.body['clientHeight'] / 2 - $(obj).height() / 2 + $(window).scrollTop()) + 'px'
						});
						$('.caption', obj).css({
							'width': $(obj).width() + 'px',
							'display': 'block'
						})
					};
					if (ie6) $('embed, object, select').css('visibility', 'hidden');
					error = function () {
						$('#' + options.popupId + 'content').text(options.errorText);
						show()
					};
					remove = function () {
						$('#' + options.popupId).remove();
						$('#' + options.popupId + 'content').remove();
						$('#' + options.popupId + 'preloader').remove();
						if (ie6) $('embed, object, select').css('visibility', 'visible')
					};
					if ($('#' + options.popupId).length == 0) {
						$('<div id="' + options.popupId + '"></div>').appendTo('body').css({
							'width': w,
							'height': h,
							'background': '#000',
							'position': 'absolute',
							'top': '0',
							'left': '0',
							'z-index': '10000',
							'opacity': .7
						}).click(function () {
							remove()
						})
					};
					var href = $(this).attr('href');
					var extension = href.substr(href.lastIndexOf('.')).toLowerCase();
					var content;
					var cw = 0;
					var ch = 0;
					var showOk = false;
					$('<div id="' + options.popupId + 'preloader">' + options.preloadText + '</div>').appendTo('body');
					set($('#' + options.popupId + 'preloader'));
					$('<div id="' + options.popupId + 'content"></div>').appendTo('body');
					$('#' + options.popupId + 'content').css({
						'visibility': 'hidden',
						'position': 'absolute',
						'top': '-10000px',
						'left': '-10000px'
					});
					if ($(this).hasClass('flash')) {
						var flash = '<object width="100%" height="100%"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="' + href + '" /><embed src="' + href + '" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="100%" height="100%"></embed></object>';
						$(flash).appendTo('#' + options.popupId + 'content');
						cw = 600;
						ch = 400;
						showOk = true
					} else {
						if (extension == '.jpg' || extension == '.jpeg' || extension == '.gif' || extension == '.png' || extension == '.bmp') {
							var img = new Image();
							$(img).error(function () {
								error()
							}).appendTo('#' + options.popupId + 'content');
							img.onload = function () {
								show();
								$imageTop = '-' + this.height/2 - 32 + 'px';
								$('.gallery-btn').css('top', $imageTop);
								$imageLeft = this.width + 10 + 'px';
								$('#gallery-next').css('left', $imageLeft);
								img.onload = function () {}
							};
							img.src = href + '?' + (new Date()).getTime() + ' =' + (new Date()).getTime()
						} else if (href.charAt(0) == '#') {
							$(href).clone().removeClass('hidden').appendTo('#' + options.popupId + 'content').show();
							$.jssp.forms('#' + options.popupId + 'content form');
							showOk = true
						} else {
							$('<iframe frameborder="0" scrolling="auto" style="width:100%;height:100%" src="' + href + '" />').appendTo('#' + options.popupId + 'content');
							cw = 900;
							ch = 500;
							showOk = true
						}
					};
					var rel = $(this).attr('rel').split(';');
					$.each(rel, function (i) {
						if (rel[i].indexOf('width') != -1) cw = rel[i].split(':')[1];
						if (rel[i].indexOf('height') != -1) ch = rel[i].split(':')[1]
					});
					if ($(this).attr('title') != '') {
						$('<span class="caption">' + $(this).attr('title') + '</span>').appendTo('#' + options.popupId + 'content').css({
							'display': 'none',
							'padding': '10px 0 0 0',
							'text-align' : 'center',
							'font-size' : '1.3em'
						})
					};
					if (showOk) show();
					$('<small>' + options.closeText + '</small>').appendTo('#' + options.popupId + 'content').css({
						'position': 'absolute',
						'float': 'left',
						'right': '-8px',
						'color': '#fff',
						'cursor': 'pointer',
						'height': '24px',
						'top' : '-14px',
						'width': '24px'
					}).click(function () {
						remove()
					});
					if ($(this).hasClass('gallery')) {
						var arr = $.data(this, 'gallery');
						arr = eval(arr + '_arr');
						var index = $.data(this, 'index');
						if (arr.length > 1) {
							$('<small>' + (index + 1) + '/' + arr.length + '</small>').appendTo('#' + options.popupId + 'content').css({
								'position': 'absolute',
								'float': 'right',
								'font-size' : '1.3em',
								'right': '10px',
								'bottom': '10px',
								'color': '#000',
								'z-index' : '10002'
							});
							$('<small id="' + options.popupId + 'gallery"></small>').appendTo('#' + options.popupId + 'content').css({
								'position': 'absolute',
								'cursor': 'pointer'
							});
							if (index != 0) {
								$('<span id="gallery-prev" class="gallery-btn">' + options.prevText + '</span>').css({
								'position': 'absolute',
								'float': 'left',
								'right': '12px'
								}).appendTo('#' + options.popupId + 'gallery').click(function () {
									$('#' + options.popupId + 'content').remove();
									var obj = arr[index - 1];
									$(obj).trigger('click')
								})
							}
							if (index < arr.length - 1) {
								$('<span id="gallery-next" class="gallery-btn">' + options.nextText + '</span>').css({
								'position': 'absolute'
								}).appendTo('#' + options.popupId + 'gallery').click(function () {
									$('#' + options.popupId + 'content').remove();
									var obj = arr[index + 1];
									$(obj).trigger('click')
								})
							}
						}
					}
				})
			})
		}
	};
}(jQuery, this));
$(document).ready(function()
{
	$.bob.popup({
		preloadText: '<img src="http://www.bobfoxs.com/wp-content/themes/bob/assets/img/loader.gif" />',
		closeText: '<img src="http://www.bobfoxs.com/wp-content/themes/bob/assets/img/bg-close.png" width="24px" height="24px" />'
	});
	$.bob.external();
	$.bob.forms();
	$('#form').submit(function(event){
		event.preventDefault();
		$error = $('#mobile-number').hasClass('notvalid');
		if($error === false)
		{
		$number = $("input#mobile-number").attr('value');
		console.time('AJAX');
		$.ajax({
			type : 'POST',
			url : 'http://www.bobfoxs.com/wp-content/themes/bob/numbers/send.php',
			dataType : 'json',
			data: {
				number : $number
			},
			success: function(data) {	
				if (data.error === true){
					$('#form').append('<span class="errors" style="display: block; ">' + data.msg + '</span>');
				} 
				else {
					$('#form').html('<span class="success" style="display: block; ">' + data.msg + '</span>');	
				}
			},
			error: function(data) {
				$('#form').append('<div class="errors">There was an error with submitting your number. Please come back later.</div>');
			},
		});
		}
		console.timeEnd('AJAX');
	});
	
});
