// Custom Javascript for ButskoUtility.com/org/net
// Copyright 2009 Matthew Baker, mattbaker@me.com

// Begin hoverIntent code, thank you for this!
// <http://cherne.net/brian/resources/jquery.hoverIntent.html>

(function($) {
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);

//End hoverIntent code, thanks again!




//Begin Frontpage Recent Project Scroller

$(document).ready(function() {
						   
						   function isValidEmailAddress(emailAddress) {
								var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
								return pattern.test(emailAddress);
							}
						   $('.floatForm div:eq(1)').hide();
						   $(".conTright:eq(1) input:eq(0)").keyup(function(){
		
								var email = $(this).val();
							
								if(email != 0)
								{
									if(isValidEmailAddress(email))
									{
										$('.floatForm div:eq(1)').fadeIn();
									} else {
										$('.floatForm div:eq(1)').hide();
									}
								} else {
									$('.floatForm div:eq(1)').hide();
								}
							
							});

						   
						   var url = 'http://mjbaker.com/';
						   var imgdir = 'wp-content/themes/default/images/';
						   
						   //Contact form 
						   
						   $('#cTmenu li:eq(0)').css({'font-weight': 'bold', color: '#222222', 'font-size': '75%'});
						   $('#cTmenu li:eq(1)').css({'font-weight': 'normal', color: '#666666', 'font-size': '75%', 'paddingTop': '1px'});
						   $('#cTmenu li:eq(2)').css({'font-weight': 'normal', color: '#666666', 'font-size': '75%', 'paddingTop': '1px', marginRight: '10px'});
						   $('#conTact p').hide();
						   $('#conTact p:eq(0)').show();
						   $('.conTright:eq(1)').css({marginLeft: '50px'});
						   
						   		//slider
								
								$("#slider-range").slider({
									range: true,
									min: 1500,
									max: 10000,
									values: [1500, 10000],
									step: 500,
									slide: function(event, ui) {
										$("#budget").val('$' + ui.values[0] + ' - $' + ui.values[1]);
									}
								});
								$("#budget").val('$' + $("#slider-range").slider("values", 0) + ' - $' + $("#slider-range").slider("values", 1));

								
								//End slider
						   
						   
						   //End contact form
						   
						   //Here is the Processes item
						   
						   var indexp = 0;
						   
						   $('#conProcess ul a:eq(0)').css({'font-weight': 'bold', color: '#222222'});
						   $('.pItem').hide();
						   $('.pItem:eq(0)').fadeIn(250);
						   
						   var changeP = function() {
							$('#conProcess ul a').css({'font-weight': 'normal', color: '#999999'});
							$('.pItem').hide();
							
							$('#conProcess ul a:eq(' + indexp + ')').css({'font-weight': 'bold', color: '#222222'});
							$('.pItem:eq(' + indexp + ')').fadeIn(500);
						   }
						   
						   $('#conProcess ul a').click(function() {
															 indexp = $('#conProcess ul a').index(this);
															 changeP();
															 return false;
															 });
						   
						   $('#conPbuts a:eq(0)').click(function() {
															 if (indexp == '0') {
																 indexp = 5;
															 }
															 else {
																 indexp = indexp - 1; 
															 }
															 changeP();
															 return false;
															 });
						   
						   $('#conPbuts a:eq(1)').click(function() {
															 if (indexp == '5') {
																 indexp = 0;
															 }
															 else {
																 indexp = indexp + 1; 
															 }
															 changeP();
															 return false;
															 });
						   
						   
						   
						   var himg;
						   var aimg;
						   var pindex;
						   
						   $('#conPbuts a').hover(function() {
														 
														 pindex = $('#conPbuts a').index(this);
														 if (pindex == '0') { 
														 himg = ''+ url + imgdir +'circle-left2.gif';
														 aimg = ''+ url + imgdir +'circle-left.gif';
														 }
														 if (pindex == '1') { 
														 himg = ''+ url + imgdir +'circle-right2.gif';
														 aimg = ''+ url + imgdir +'circle-right.gif';
														 }
														 $('#conPbuts').find('img:eq(' + pindex + ')').attr("src", himg);
														 },function() {
														 
														 $('#conPbuts').find('img:eq(' + pindex + ')').attr("src", aimg);
														 
														 });
						   
						   
						   //End Processes
						   
																								  
						  
						   
						   var himg;
						   var aimg;
						   var pindex;
						   
						   $('#conLeft p').hover(function() {
														 
														 pindex = $('#conLeft p').index(this);
														 if (pindex == '1') { 
														 
														 himg = ''+ url + imgdir +'word-doc2.gif';
														 aimg = ''+ url + imgdir +'word-doc.gif';
														

														 $('#conLeft').find('img:eq(0)').attr("src", himg);
														 }
														 },function() {
														 
														 $('#conLeft').find('img:eq(0)').attr("src", aimg);
														 
														 });

						   var himg;
						   var aimg;
						   var pindex;
						   
						   $('#conCon a').hover(function() {
														 
														 pindex = $('#conCon a').index(this);
														 if (pindex == '0') { 
														 himg = ''+ url + imgdir +'comment-left2.gif';
														 aimg = ''+ url + imgdir +'comment-left.gif';
														 }
														 if (pindex == '1') { 
														 himg = ''+ url + imgdir +'email2.gif';
														 aimg = ''+ url + imgdir +'email.gif';
														 }
														 if (pindex == '2') { 
														 himg = ''+ url + imgdir +'arrow-right2.gif';
														 aimg = ''+ url + imgdir +'arrow-right.gif';
														 }
														 $('#conCon').find('img:eq(' + pindex + ')').attr("src", himg);
														 
														 },function() {
														 
														 $('#conCon').find('img:eq(' + pindex + ')').attr("src", aimg);
														 
														 });
						   
						   $('#topMenu li:eq(1)').click(function() {
														   $('#caseBut').hide();
														   $('#siteBut').hide();
														   $('.featPos').hide();
														   $('.feat2').hide();
														   $('#feature').animate({height: 133}, 500, function() {  $(this).css({backgroundImage: "url(" + url + imgdir + "smalltile.jpg)"}); $('.featImg').fadeIn(); })
														   
														   $('.featMid').animate({height: 133}, 500);
														    $('.featMid').html('<img src="http://www.mjbaker.com/wp-content/themes/default/images/services.png" class="featImg">');
														   
														   
														   });
						   
						   Errormsg = function() { alert('We are putting the finishing touches on our site and will be live within the week.\n\nIn the meantime, please get in touch.\n\nphone: +1.760.525.0257\n\nemail: Hello@MJBAKER.com\n\nThank you.'); };
														
						   $('#topMenu li').click(function() { Errormsg(); });
						   $('#caseBut').click(function() { Errormsg(); });
						   $('#conCon a:eq(1)').click(function() { Errormsg(); });
						   $('#siteBut').click(function() { 
														window.open('http://www.ButskoUtility.com');
														return false; 
														});
						   
						   $('.hLogo1').click(function() { 
						   location.href = url;
						   return false; 
														});						   
						   var imgTest = '0';
						   $('.feat2').css({display: 'none'});
						   $('.featPos').css({display: 'none'}).load(function() { $(this).fadeIn(2000); $('.feat2').fadeIn(2000); imgTest = '1'; });
							doImg = function() { if (imgTest == '0') { $('.featPos').fadeIn(2000); $('.feat2').fadeIn(2000); } }
							imgtime = setTimeout(doImg, 1500);
						   
						   $('#hLogo').hover(function() {

														  $(this).css({cursor: 'pointer'});
														  $('.hLogo1').css({backgroundPosition: '-319px 0'});					  
														  },
														  function() {
														  $('.hLogo1').css({backgroundPosition: '0px 0'});
														  });

						   
						   
						   $('.hLogo1').mousedown(function() { 
															 $(this).css({backgroundPosition: '-638px 0'}); 
															 }).mouseup(function() { 
															 $(this).css({backgroundPosition: '-319px 0'}); 
						   });
						   
						   $('#caseBut').hover(function() {

														  $(this).css({cursor: 'pointer'});
														  $(this).css({backgroundPosition: '-214px 0'});					  
														  },
														  function() {
														  $(this).css({backgroundPosition: '0px 0'});
														  });

						   
						   
						   $('#caseBut').mousedown(function() { 
															 $(this).css({backgroundPosition: '-428px 0'}); 
															 }).mouseup(function() { 
															 $(this).css({backgroundPosition: '-214px 0'}); 
						   });
						   
						   $('#siteBut').hover(function() {

														  $(this).css({cursor: 'pointer'});
														  $(this).css({backgroundPosition: '-214px 0'});					  
														  },
														  function() {
														  $(this).css({backgroundPosition: '0px 0'});
														  });

						   
						   
						   $('#siteBut').mousedown(function() { 
															 $(this).css({backgroundPosition: '-428px 0'}); 
															 }).mouseup(function() { 
															 $(this).css({backgroundPosition: '-214px 0'}); 
						   });
	

});



  

