

//instantiate scroll position
var currentPosition = 0;
//set animating to "no"
var animating = 0;
jQuery(document).ready(function($){
var siteUrl=themeUrl.site()+'/images/';// global function decleared in the header
function onBefore() {
	$('#text_slide').css('width', $(this).css('width'));
}	
/// submenu presets
submenuObj={
		
	resetAllStates:function(elm,state){
				// goind up the tree to find all the lemons
				
				var aTags=document.getElementsByTagName('A');
				$.each(aTags,function(inx,val){
					$(val).css(state);	
				})
							
	},
	currentItemHover:function(elm,state,resetMe){
		this.resetAllStates(elm,resetMe)	
		// going down the tree to find one lemon	
		$(elm).find('a').css(state);
	}
			
}// end obj	
	
	
	$('#testimonials').cycle({
		fx: 'scrollHorz',
		pause: 1,
		timeout: 5500,
		speed: 600,
		cleartype: !$.support.opacity,
		cleartypeNoBg: true,
		next: '#testimonial_next'
	});
	
	$('#text_slide').cycle({
		fx: 'fade',
		timeout: 2000,
		speed: 600,
		cleartype: !$.support.opacity,
		cleartypeNoBg: true,
		activePagerClass: 'activeSlide',
		before: onBefore
	});

	$("#banner_nav ul> li").mouseover(function(event){
		
		///on mouse over of submenus integration effects, declear function here
			cssSetupOver={backgroundPosition:'0px -27px'}	
			cssSetupOut={backgroundPosition:'0px 0px'}	
			submenuObj.currentItemHover($(this),cssSetupOver,cssSetupOut);
			
		
		//stop anchor links from working
		//get element to scroll to
		
		var to = $(this).find('.banner_link')
		//alert (to.nodeName)
		//do nothing if current link is clicked
		//if (to.parent().attr('class') == 'current') {
			//return false;
		//do nothing if currently animating
		if (animating == 1) {
			return false;
		} else {
			//set animating to "yes"
			animating = 1;
			
			//get id of slide to scroll to
			var toId = to.attr('class').replace(' banner_link', '');
			//set current classes
			$('#banner_nav li').removeClass('current');
			to.parent().addClass('current');
		
			//get offset to scroll
			var toPosition = $('#' + toId).position().left;
			//alert("current: "+ currentPosition + " to: "+toPosition);
			
			//set current position relative to offset
			currentPosition = currentPosition + toPosition;
			
			//change the background image
			cssSetup={backgroundImage:'url('+siteUrl+toId +'.jpg)'}
			$('#banner_wrap').css(cssSetup);
					
			//do the animation
			$('#banner_scroll').stop().animate({ scrollLeft: currentPosition }, 600, function() {
				animating = 0;
			});
			
			return false;
		}
	});// end of ouse over
	
var myintervals=setInterval(function(){$($('#banner_nav li.current').data('next') + ' div.banner_link').mouseover()},8000);

////////////////////////////////////////////////
	
			// sub nav go to location
		$('body #banner_nav ul>li').click(function(){
			clearInterval(myintervals);// just in case
			var _href=$(this).find('a').attr( 'href' );
			if (_href=='#'){return false};// exept for first page					
			window.open( _href,'_self' );
			return true;			
			});	




	
	/*$('[title]').tipTip({
		defaultPosition: 'right',
		delay: 0
	});
	
	$("input[type=range]").each(function(){
	var range = $(this);
	
	// Create <div/> to hold jQuery UI Slider
	var sliderDiv = $("<div/>");
	sliderDiv.width(range.width());
	
	// Insert jQuery UI Slider where the
	// <input type=range/> is located
	range.after(
	sliderDiv.slider({
	// Set values that are set declaratively
	// in the <input type=range/> element
	min: parseFloat(range.attr("min")),
	max: parseFloat(range.attr("max")),
	value: parseFloat(range.val()),
	step: parseFloat(range.attr("step")),
	// Update the <input type=range/> when
	// value of slider changes
	slide: function(evt, ui) {
	range.val(ui.value).trigger('change');
	},
	change: function(evt, ui) {
	// set <input type=range/> value
	range.val(ui.value).trigger('change');
	}
	})
	).
	// Hide <input type=range/> from display
	hide();
	});
	
	$('#amount').live('change', function() {
		$('#amount-display').text('$' + $(this).val());
	});*/
	
	$('#calculator').submit(function() {
		var form = {},
			repayment;
		
		$.each($(this).serializeArray(), function(i, field) {
			form[field.name] = field.value;
		});
		
		if (form.rate === undefined) {
			alert('Please select a type of loan');
			return false;
		}
		
		if (form.term === '') {
			alert('Please select a term');
			return false;
		}
		
		// Adjust values
		form.amount = parseInt(form.amount) + 600; // Add Lending Room Fee
		form.rate   = parseInt(form.rate) / 1200;
		form.term   = parseInt(form.term);
		
		if ($('#lpi').is(':checked')) {
			form.amount += (form.amount * 0.1); // Add 10%
		}
		
		if ($('#warranty').is(':checked')) {
			form.amount += 895;
		}
		
		repayment = ((form.rate + (form.rate / (Math.pow(1 + form.rate, form.term) - 1))) * form.amount) / 4.33;
		$('#repayment span').text(repayment.toFixed(2));
		
		return false;
	});

});
