
//if(window.cssTransitions){
//	cssTransitions.bindingURL = "/wp-content/themes/rebathoregon/jquery-css-transitions/bindings.php";
//}

var xblConsole;
if(window.console)
	xblConsole = window.console;

//Supports autofocus attribute
if(jQuery('<input autofocus="autofocus" />')[0].autofocus !== true){
	jQuery(function($){
		$(':input[autofocus]').focus();
	});
}


jQuery(function($){
	//Don't do anything if IE6
	if(/MSIE [1-6]\D/.test(navigator.userAgent))
		return;
	
	var isOldIE = (/MSIE [1-7]\D/.test(navigator.userAgent));
	$("#introduction a.more").click(function(e){
		if(navigator.userAgent.indexOf('MSIE') != -1)
			$('body').addClass('temp-for-ie').removeClass('temp-for-ie'); //this is done so that the left/right and bottom shadows are re-stretched for ie
		
		//So here, we need to find the style before the class is applied and what is added after
		//  and then we need to determine which properties are transitioned (transition-property)
		//  and what the duration is (transition-duration)
		
		//console.info($('#introduction').css('background-color'))
		//$('#introduction').addClass('target');
		//console.info($('#introduction').css('background-color'))
		//$('#introduction').removeClass('target');
		//
		//$('#introduction').css({
		//	color:"#FFFFFF",
		//	backgroundColor:"#780022"
		//}).animate({
		//	backgroundColor:"#FFFFFF",
		//	color:"#780022"
		//}).addClass('target');
		
		$('#introduction').addClass('target');
		
		//For IE, add and remove the classes for absolutely positioned elements on the bottom so that they will reposition themselves
		if(/MSIE [1-8]\D/.test(navigator.userAgent)){
			$('body.home > footer img.duck').removeClass('duck').addClass('duck');
			$('body > footer .credits').removeClass('credits').addClass('credits');
		}
		
		e.preventDefault();
	});
	
	//Add onclick handler to rebath designer URLs
	$('a[href="' + rebathDesignerURL + '"]').click(function(e){
		
		var win = window.open(this.href, 'rebathDesigner', 'width=910,height=720,menubar=no,toolbar=no,location=no,status=no,resizable=yes,scrollbars=yes,dialog=yes');
		
		//If the window was blocked from opening, then continue
		if(win)
			e.preventDefault();
	});
		//Don't let tel and fax links be clickable; auto-select their contents
	$('a[href^="fax:"], a[href^="tel:"]').click(function(e){
		try { //For IE
			if(document.body.createTextRange) {
				var range = document.body.createTextRange();
				range.moveToElementText(this);
				range.select();
			}
			e.preventDefault();
		}
		catch(e){}
	}).mousedown(function(e){
		try {
			//For non-IE
			if(window.getSelection){
				var range = document.createRange();
				range.selectNodeContents(this);
				var sel = window.getSelection();
				sel.removeAllRanges();
				sel.addRange(range);
				e.preventDefault();
			}
			//For IE
			else if(document.body.createTextRange) {
				var range = document.body.createTextRange();
				range.moveToElementText(this);
				range.select();
				e.preventDefault();
			}
		}
		catch(e){}
	});
	
	//Hack for IE
	$('body > nav > ul > li').hover(
		function(){
			$(this).addClass('hover');
		},
		function (){
			$(this).removeClass('hover');
		}
	);
	
	/*************************************************************************************
	 * Handle sign up form submission
	 *************************************************************************************/
	//if(document.implementation && document.implementation.hasFeature && !document.implementation.hasFeature('WebForms', '2.0')){}
	//Emulate form validation if it is not built in
	//$('form#signUpForm').css('padding-top', '20px');
	
	$('form').submit(function(e){
		var inputs = $(this).find(':input');
		
		//Check to see if Web Forms 2.0 validation model is already supported
		//var hasWillvalidate = false;
		//inputs.removeClass('invalid').each(function(){
		//	if(this.willValidate){
		//		hasWillvalidate = true;
		//		return true;
		//	}
		//	return false;
		//});
		
		//If not supported, then validate
		//if(!hasWillvalidate){
		if(this.checkValidity){
			if(!this.checkValidity()){
				e.preventDefault();
				return;
			}
		}
		else {
			var invalid = false;
			$(inputs).each(function(){
				
				if(!this.value){
					//required attribute
					if(this.getAttribute('required')){
						if(!invalid)
							this.focus();
						$(this).addClass('invalid');
						invalid = true;
					}
				}
				//Email
				else if(this.getAttribute('type') == 'email' && !/.+@.+/.test(this.value)) {
					if(!invalid)
						this.focus();
					$(this).addClass('invalid');
					invalid = true;
				}
				//Pattern match
				else if(this.getAttribute('pattern') && !(new RegExp('^(?:' + this.getAttribute('pattern') + ')$', 'i')).test(this.value)){
					if(!invalid)
						this.focus();
					$(this).addClass('invalid');
					invalid = true;
				}
			});
			
			//Prevent form submission
			if(invalid){
				e.preventDefault();
				return;
			}
		}
		
		//Don't abort do ajax if we can't do ajax? We could post into an iframe instead
		if(this.enctype == 'multipart/form-data')
			return;
		e.preventDefault();
		
		
		//Make all the form elements readonly while submitting
		$(this).addClass('submitting');
		$(this).find('input').attr('readonly', 'readonly');
		
		//Create a form for ajax submission
		//this.target = "submission" + (this.name || this.id);
		//var iframe = document.getElementById(iframe);
		//if(!iframe)
		//	iframe = $('<iframe name="' + this.target + '" id="' + this.target + '"></iframe>')[0];
		//this.appendChild(iframe);
		$(this).find('p.thanks').addClass('hidden'); //Add for MSIE since removing @hidden doesn't change style rule
		
		var fields = [];
		for(var i = 0; i < this.elements.length; i++){
			if(this.elements[i].name)
				fields.push(encodeURIComponent(this.elements[i].name) + '=' + encodeURIComponent(this.elements[i].value));
		}
		var data = fields.join('&');
		var form = this;
		$.ajax({
			'type':'POST',
			'data':data,
			'url':form.action,
			'error':function(xhr, textStatus, error){
				var warning = xhr.getResponseHeader('Warning');
				if(!warning)
					warning = "There was an error with your submission.";
				if(!$(form).find('.form_error_message').text(warning).removeAttr('hidden').show().length)
					alert(warning);
				$(form).find(':input').removeAttr('readonly');
				$(form).removeClass('submitting');
			},
			'success':function(data, textStatus){	
				$(form).removeClass('submitting').addClass('submitted');
				$(form).find('.form_error_message').text('').attr('hidden', '').hide();
				$(form).find('p.thanks').removeAttr('hidden').removeClass('hidden');
				$(form).find('button').blur();
				
				//var actionPath = form.action.replace(/http:\/\//, '').replace(/^.+?(?=\/)/, '');
				pageTracker._trackPageview('/special-offers-signup/');
			}
		});
		
		
		//Callbacks for iframe states
		//var form = this;
		//$(iframe).load(function(){
		//	$(form).addClass('submitted');
		//	$(form).find('p.thanks').removeAttr('hidden').removeClass('hidden');
		//	$(form).find('button').blur();
		//}).error(function(){
		//	alert("An error occured. Please try again.");
		//	$('form#signUpForm input').removeAttr('readonly');
		//	$(form).removeClass('submitting');
		//});
		
	});
	
	
	/*************************************************************************************
	 * Handle free estimate
	 *************************************************************************************/
	//$('body.page-16 form').submit(function(){
	//	$('form#signUpForm input').removeClass('invalid');
	//});
	
	/*************************************************************************************
	 * Add "target" class name to emulate :target pseudo class when the hash changes and
	 * smooth-scroll to the targets when clicking on hyperlinks
	 *************************************************************************************/
	var previousTarget;
	var previousHash = "";
	var hashChangeHandler = function(){
		var hash = window.location.hash.substr(1);
		
		//Only execute if hash is different
		if(hash != previousHash){
			//Remove the target class from the previous targets
			if(previousTarget)
				previousTarget.removeClass('target');
			
			//Get the current target, and if it is found, then add the target to it
			var target = $('#' + hash);
			if(target.length){
				target.addClass('target');
				previousTarget = target;
			}
			previousHash = hash;
		}
		
	};
	window.setInterval(hashChangeHandler, 100);
	
	$('a[href*=#]').click(function(e){
		var hash = this.href.replace(/^.*?#/, '');
		//If no hash, then nothing is happening and exit
		if(!hash){
			hashChangeHandler();
			return;
		}
		//Scroll to the target that is in the anchor clicked on
		//var target = $('#' + hash);
		//if(target.length){
		//	$.scrollTo(target, 500, {
		//		onAfter:function(){
		//			window.location.hash = hash;
		//			hashChangeHandler();
		//		}
		//	});
		//	e.preventDefault();
		//}
	});
	
	
	//Lightbox
	if($('#content').hasClass('page-before-and-after-gallery')){
		$('#content.page-before-and-after-gallery .before-and-after-gallery a:has(img)').lightBox({
			fixedNavigation:true,
			imageLoading: '/wp-content/themes/rebathoregon/images/lightbox-ico-loading.gif',
			imageBtnPrev: '/wp-content/themes/rebathoregon/images/lightbox-btn-prev.gif',
			imageBtnNext: '/wp-content/themes/rebathoregon/images/lightbox-btn-next.gif',
			imageBtnClose: '/wp-content/themes/rebathoregon/images/lightbox-btn-close.gif',
			imageBlank: '/wp-content/themes/rebathoregon/images/lightbox-blank.gif'
		});
	}

});

