var jTourNav;
var jTourVar = 'jTour';
var jTourActiveAnim = false;
var jTourSlideFX;
var jTourSlideDuration;
var jTourContainerID = jTourVar + 'Container';
var jTourContainerClass = jTourContainerID;					//reset in the page-level script block to customize classes
var jTourContainer = '#' + jTourContainerID;
var jTourViewID = jTourVar + 'View';
var jTourViewClass = jTourViewID;														//reset in the page-level script block to customize classes
var jTourView = '#' + jTourViewID;
var jTourPrevID = jTourVar + 'Prev';
var jTourPrev = '#' + jTourPrevID;
var jTourNextID = jTourVar + 'Next';
var jTourNext = '#' + jTourNextID;
var jTourPanePadding;
var jTourPaneWidth;
var jTourPanes = new Array();


jQuery.fn.jTour = function (jTourSettings) {
	jTourSettings = jQuery.extend({
		setVar: "jTour",
		paneWidth: "300px",
		panePadding: "0",
		paneBorderWidth: "0",
		slideDuration: 333,
		slideFX: '',
		slideScrollDelay: 5000,
		slideScroll: false
	}, jTourSettings);
	jTourNav = $(this);																										/* global variable to set #jTourNav */
	jTourSlideDuration = jTourSettings.slideDuration;
	jTourSlideFX = jTourSettings.slideFX;				/* possible method for smoothing out standard jQuery animation with additional .animate params, UNTESTED as of 8-21-07  ddcjamesv */
	jTourPaneWidth = parseInt(jTourSettings.paneWidth);/*$($(jTourNav).find('li:eq(1) a').attr('href')).attr('offsetx')*/
	jTourPanePadding = parseInt(jTourSettings.panePadding);

	initMarkup(jTourNav, jTourPaneWidth);			/* setup jTour wrappers */
	//console.log($(jTourContainer +' > div:first-child').css('width'));
	initDOM();
	$(this).find('li:eq(0)').activeClass();														/* active first tab */
	$($(this).find('li.active a').attr('href')).activeClass(); 			/*activate corresponding pane*/
	$(this).find('li a').click(tabClick);
	//console.log('jTour click event handler loaded');
	$(jTourNext + ' a').click(function(){jQuery.nextPane(jTourSlideDuration);});							/*next button click(fn)*/
	$(jTourPrev + ' a').click(function(){jQuery.prevPane(jTourSlideDuration);});								/*prev button flick(fn)*/
	//console.log('Welcome to jTour');
	if(jTourSettings.slideScroll == true ){
		var slideInterval = setInterval("$(jTourNext + ' a').click()", jTourSettings.slideScrollDelay );
		$(jTourView).hover(function(){
			clearInterval(slideInterval);
		},function(){
			var slideInterval = setInterval("$(jTourNext + ' a').click()", jTourSettings.slideScrollDelay );
		});
	};
};

jQuery.nextPane = function(slideDuration){
    if($('.fvSliderPane').length <= 1) return false; // breaks out of animation if there is only one featured vehicle
	if (($(jTourNav).find('.active').is('li:last-child')) && (jTourActiveAnim == false)){
		jTourActiveAnim = true;
		$.jTourPrepDOM(jTourContainer, 'next');
		var fragA = $(jTourNav).find('li:first-child');
		var fragANextLi = $(fragA).next();
		var fragANext = $(fragANextLi).find('a').attr('href');
		slide(fragANext, slideDuration);
		setTimeout("$.jTourTidyDOM($(jTourContainer), 'next')", slideDuration + 50);
		//$(jTourNav).find('li:first-child a').trigger('click');  /* old jCarousel-like behavior */
		return false;
	} else {
		$(jTourNav).find('.active').next().find('a').trigger('click');	/* jTour .next() tab click */
		return false;
	}
};

jQuery.prevPane = function(slideDuration) {
    if($('.fvSliderPane').length <= 1) return false; // breaks out of animation if there is only one featured vechile
	if (($(jTourNav).find('.active').is('li:first-child')) && (jTourActiveAnim == false)){
		jTourActiveAnim = true;
		$.jTourPrepDOM(jTourContainer, 'prev');
		var fragA = $(jTourNav).find('li:last-child');
		var fragAPrevLi = $(fragA).prev();
		var fragAPrev = $(fragAPrevLi).find('a').attr('href');
		slide(fragAPrev, slideDuration);
		setTimeout("$.jTourTidyDOM(jTourContainer, 'prev')", slideDuration + 50);
		//$(jTourNav).find('li:last-child a').trigger('click');  /* old jCarousel-like behavior */
		return false;
	} else {
		$(jTourNav).find('.active').prev().find('a').trigger('click');	/* jTour previous functionality */
		return false;
	}
};

jQuery.jTourPrepDOM = function(container, direction){ /* container is the id of the intended jQuery obj, direction is a string */
	//console.log('jTourPrepDOM');
	if (direction == 'next') {
		$(container + ' > div').not('.active').each(function(){
			$(this).appendTo( $(container) );
		}).parent().css({right: "auto", left: "0px" });
		//console.log('next DOMprep finished')
	} else if (direction == 'prev') {
		var prepDOMPrevX = '-'+((jTourPanes.length)-1)*((jTourPaneWidth)+(jTourPanePadding*2))+'px';
		$(container +' > div').not('.active').each(function(){
			$(this).insertBefore($(container).find('.active'));
		}).parent().css({ right: "auto", left: prepDOMPrevX });

	} else{
		return('No Direction');
	}
};

jQuery.jTourTidyDOM = function(container, direction){ /* container is a jQuery object, direction is a string */
	//console.log('jTourTidyDOM');
	if (direction == 'next') {
		$(jTourNav).find('li:first-child').activeClass();
		$($(jTourNav).find('li:first-child a').attr('href')).activeClass();
		$(container).find('.active').prev().appendTo( $(container) );//, setTimeout('console.log(this)',1000));//css({ right: 'auto', left: '0px'});
		$(container).css({ right: 'auto', left: '0px'});
		jTourActiveAnim = false;
	} else if (direction == 'prev') {
		var tidyDOMPrevX = '-'+((jTourPanes.length)-1)*((jTourPaneWidth)+(jTourPanePadding*2))+'px';
		$(jTourNav).find('li:last-child').activeClass();
		$($(jTourNav).find('li:last-child a').attr('href')).activeClass();
		$(container).find('.active').next().prependTo($(container));
		$(container).css({ left: tidyDOMPrevX, right: "auto" });
		jTourActiveAnim = false;
	} else {
		return('No Direction');
	}
};

function tabClick() {
	if (jTourActiveAnim == false) {
		jTourActiveAnim = true;
		var fragA = $($(this).attr('href'));
		var fragALink = $(this);
		fragALink.parent().activeClass();
		fragA.activeClass();
		//console.log('Slide!');
		slide(fragA, jTourSlideDuration);
		setTimeout('jTourActiveAnim = false', jTourSlideDuration);
		return false;
	} else {
		return false;
	}
};

																																									/*	initMarkup(elem)
																																									This function wraps page fragments in jTour markup and creates prev/next links. */
function initMarkup(elem, paneWidth){
	var jTourViewWidth;
	$(elem).after('<div id=\"' + jTourViewID + '\" class=\"' + jTourViewClass + '\"><div id=\"' + jTourContainerID + '\" class=\"' + jTourContainerClass + '\"></div></div>');
	$(elem).find('a').each(function(){
		$($(this).attr('href')).appendTo(jTourContainer);
	});
																																									/* setup previous & next buttons */
	  jQuery(elem).after('<div id=\"' + jTourPrevID + '\"><a href=\"javascript:void(0);\"><span>Previous</span></a></div><div id=\"' + jTourNextID + '\"><a href=\"javascript:void(0);\"><span>Next</span></a></div>');

		$(jTourContainer + ' > div').width(paneWidth+"px").css({ 'padding': jTourSettings.panePadding, 'float': "left" });
		jTourViewWidth = (parseInt($(jTourContainer + ' > div').css("padding"))*2) + jTourPaneWidth + "px";
		$(jTourView).width(jTourViewWidth);
};

function initDOM() {
	$(jTourContainer + ' > div').each(function(i) {
																																									//console.log($(jTourContainer).css('width'));
																																									//console.log(this);
																																									//console.log($(this).getX());
																																									//console.log($(this).css('width'));
																																									//console.log($(jTourContainer).getX());
	$(this).attr('offsetX', ($(this).getX() - $(jTourContainer).getX()));
																																									//console.log($(this).attr('offsetX'));
	$(this).css({ 'float': "left" });
		jTourPanes.push(this);
	});
																																									//console.log('jTourPanes.length = '+jTourPanes.length);
	$(jTourContainer).width(((jTourPaneWidth)+(jTourPanePadding*2))*(jTourPanes.length));
};

function slide(fragA, slideDuration){
	var slideX = ((-$(fragA).attr('offsetX')));
	//console.log($(fragA).attr('id') + ' slide fn');
	//console.log('offsetX = '+$(fragA).attr('offsetX'));
	//console.log('slideX = ' + slideX);
	if(jTourSlideFX != ''){
		$(jTourContainer).css({ right: 'auto' }).animate({ left: slideX }, slideDuration, jTourSlideFX, function(){
			$(jTourContainer).css('left', slideX);
			//setTimeout('jTourActiveAnim = false', 100);
		});
	} else {
		$(jTourContainer).css({ right: 'auto' }).animate({ left: slideX }, slideDuration, function(){
			$(jTourContainer).css('left', slideX);
			//setTimeout('jTourActiveAnim = false', 100);
		});
	}
};

jQuery.fn.activeClass = function(){
		/* assigns active/inactive classes to $(this) and the element with the ID contained in the desendant anchor */
		$(this).siblings().removeClass('active').addClass('inactive').end().removeClass('inactive').addClass('active');
		return($(this));
	};

																																									// 	$(this).parent().siblings().removeClass('active').addClass('inactive');
																																									// 	$(this).parent().removeClass('inactive').addClass('active');
																																									// 	$($(this).attr('href')).siblings().removeClass('active').addClass('inactive');
																																									// 	$($(this).attr('href')).removeClass('inactive').addClass('active');

/* get xy coordinates jQuery plugins */
jQuery.fn.getXY = function(){
	var obj = jQuery(this).get(0);
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		var curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
};

jQuery.fn.getX = function(){
	var obj = jQuery(this).get(0);
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		var curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return curleft;
};

jQuery.fn.getY = function(){
	var obj = jQuery(this).get(0);
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		var curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return curtop;
};



//  DEPRICATED FUNCTIONS
//		function activeTab(elem){
//		
//			/* a working add/remove function that is jTour-specific*/
//		//$("#jTourNav > li").removeClass("active").addClass("inactive");
//		//$(elem).parent().removeClass("inactive").addClass("active");
//		//$("#jTourContainer > div").removeClass("active").addClass("inactive");
//		//$($(elem).attr("href")).removeClass("inactive").addClass("active");
//		
//		
//		$(elem).parent().siblings().removeClass('active').addClass('inactive');
//		$(elem).parent().removeClass('inactive').addClass('active');
//		$($(elem).attr('href')).siblings().removeClass('active').addClass('inactive');
//		$($(elem).attr('href')).removeClass('inactive').addClass('active');
//		};

//		function slide(fragA, fragB, slideDuration){
//			/* returns the x position of the slideFrom pane */
//			var posAx = $(fragA).getX();
//			//alert('posAx = '+posAx);
//			//var posAy = $(fragA).getY();
//			
//			/* returns the x position of the slideTo pane */
//			var posBx = $(fragB).getX();
//			//alert('posBx ='+posBx);
//			//var posBy = $(fragB).getY();
//			
//			/* sets containerOffsetX equal to the integer value of the left position of #jTourContainer */
//			var containerOffsetX = parseInt($(fragA).parent().css('left'));
//			//	alert('containerOffsetX ='+containerOffsetX);
//			// var containerOffsetY = $(fragA).parent().css('top');
//			
//			/*calculates the distance between the top left corners of fragA and fragB */
//			var paneOffsetX = posAx - posBx;s
//			//	alert('paneOffsetX ='+paneOffsetX);
//			
//			/* adds the current left position of the #jTourContainer to the distance between the top left corners of fragA and fragB */
//			var slideX = containerOffsetX + paneOffsetX;
//				//alert('slideX ='+slideX);
//			
//			$(fragA).parent().animate({ left: slideX }, slideDuration, function() {
//				$(fragA).parent().css('left', slideX);
//				setTimeout('jTourActiveAnim = false', 10);
//			});
//		};
