/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){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;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

var gfn = {
	setWidthClass : function() {
		if (screen.availHeight > 768) {
			$(document.body).removeClass('w1024').addClass("w1280");
		}
	}
};

// INITIALIZATION ***********************************
jQuery(function($) {

$(document.body).removeClass("jsOff").addClass("jsOn");

gfn.setWidthClass();

$('#contentMid h2:first').addClass('first');

if (document.getElementById('formAppointment')) {
	$('#formAppointment').attr('action','code/forms/process_appointment.php');
	prepareForm('formAppointment');
};

if (document.getElementById('formContact')) {
	$('#formContact').attr('action','code/forms/process_contact.php');
	prepareForm('formContact');
};

if (document.getElementById('phpbb')) {
	$('ul.forums li.row a.forumtitle[href*="viewforum.php?f=11"]').closest("dl.icon").css({'background-image' : 'url(./styles/prosilverJudi/imageset/forum_jh.png)'});
}

if (document.getElementById('contributeTxt')) {
var config = {    
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
     interval: 100, // number = milliseconds for onMouseOver polling interval    
     over: showContrTxt, // function = onMouseOver callback (REQUIRED)    
     timeout: 300, // number = milliseconds delay before onMouseOut    
     out: hideContrTxt // function = onMouseOut callback (REQUIRED)    
	};
	$("#contributeBtn").hoverIntent( config );
}


$('a[href]').each( function() { 
	var id = document.body.getAttribute('id');
	var t = $(this);
	if (t.parent().is('.'+id)) { 
			t.removeAttr('href'); 
		}
	});
	
//START IE
/*@cc_on
//START IE6
if ( $.browser.msie  && (parseInt(jQuery.browser.version)===6)) { 

	$("#contributeBtn").hover(
		  function () {
			$(this).addClass('ieOver');
		  }, 
		  function () {
			$(this).removeClass('ieOver');
		  }
		);

}//end IE6
@*/
//end IE
	
});//end init
// FUNCTIONS ***********************************

//GENERAL HELPER FUNCTIONS

function getBodyId() {
		return document.body.getAttribute('id');
	};
function showContrTxt() { 
	$('#contributeTxt').slideDown('slow');
	}
function hideContrTxt() { 
	$('#contributeTxt').slideUp('slow');
	}
function getStrFromHrefHash(h) {
		if(typeof h === 'object') { h = h.href };
		return h.split('#')[1];
	};
function getStrFromHrefSearch(h) {
		if(typeof h === 'object') { h = h.href };
		return h.split('?p=')[1];
	};

function prepareForm(f) {

	var f = '#'+f, $f = $(f);
	$('form .validate').focus( function() { 
		var n = $(this).parent('li');
		
		if($(n).find('.error').is(':visible')) { $(n).find('.error').hide('slow')}
	});

	if (jQuery.browser.msie) {
		$f.find('#submitBtn').hover(function() { 
			$(this).toggleClass("over"); },function() {
			$(this).toggleClass("over"); 
		});
		$f.find('input, textarea').focus(function() {$(this).addClass('ieFocus')});
		$f.find('input, textarea').blur(function() {$(this).removeClass('ieFocus')})
	};
		
		var options = { 
		beforeSubmit: validate,
		dataType:  'json',
		success: showResponse 
		};
		
		$f.ajaxForm(options);
};

function validate(formData, jqForm, options) { 
	var form = jqForm[0], err = 0, errThis;
	for (var i=0; i < formData.length; i++) {
		var $this = $(form [name=formData[i].name]);
		var v = formData[i].value;
		if ($this.hasClass('validate')) {
			if ($this.hasClass('notempty')) {
				errThis = valNotEmpty(v,$this,formData[i].type);
				err = err + Number(errThis);
				//console.log('169: '+err);
				//if(err) { console.log('Is empty: '+formData[i].name); }
			}
			if ($this.hasClass('email')) {
				errThis = valEmail(v,$this,formData[i].type);
				err = err + Number(errThis);
				//console.log('170: '+err);
				//if(err) { console.log('Email err: '+formData[i].name); }
			}
			if ($this.hasClass('radio')) {
				errThis = valRadio(v,$this,formData[i].type);
				err = err + Number(errThis);
				//if(err) { console.log('Email err: '+formData[i].name); }
			}
		}
    } 

	return !err;
};

function valNotEmpty(v,$field,type) {
	//validation part
	var isErr = !jQuery.trim( v );
		
	//error display part
	if ( isErr ) {
	
		var li = $field.parent('li').find('.error')[0];
		
		if ($field.attr('type')==='radio') {
			var errMsg = '<span class="error">Please select one of the choices!</span>'
		} else {
			var errMsg = '<span class="error">Please fill out the field above!</span>';
		}
		if ( !li ) {
			$field.parent('li').append(errMsg).show();
		} else {
			$(li).show();
		}
	}
	
	//return result
	return isErr;
}
function valEmail(v,$field,type) {

	var isErr = !v.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
	
	if ( isErr ) {
		var li = $field.parent('li').find('.error')[0];
		var errMsg = '<span class="error">Please enter a valid e-mail address!</span>';
		if ( !li ) {
			$field.parent('li').append(errMsg).show();
		} else {
			$(li).show();
		}
	}
	//return result
	return isErr;
};

function showResponse(data)  {
	$('form').unbind('submit');
	var p = $('form').parent('div.formCtner');
	p.slideUp('slow',function() {
		p.empty().append('<p id="amf_response_thank">'+data.message+'</p>').slideDown('slow');	
	});
};

function replaceInStr(a,b,c) {
	var re = new RegExp(a , "g");
	return c.replace(re, b);
}
