var tourIndex = 0;
$(document).ready(function() {

	var pageId = $('body').attr('id');
	var pageClass = $('body').attr('class');

	$('#navigation a').each(function() {
		if($(this).attr('href') == pageId) {
			$(this).addClass("selected"); 
		} else if($(this).attr('href') == '' && pageId == 'home') {
			$(this).addClass("selected"); 
		} else if($(this).attr('href') == 'account/overview' && pageId == 'account') {
			$(this).addClass("selected"); 
		} else if($(this).attr('href') == 'tour/overview' && pageId == 'tour') {
			$(this).addClass("selected"); 
		}
	});

	$('#subnavigation a').each(function() {
		if($(this).attr('href') == pageClass) {
			$(this).addClass("selected"); 
		} else if($(this).attr('href') == '' && pageClass == 'home') {
			$(this).addClass("selected"); 
		}
	});


	if($.isFunction($.fn.datepicker)) {
		$('#datepicker').datepicker({
			dateFormat: 'yy-mm-dd', 
			changeMonth: true, 
			changeYear: true,
			gotoCurrent: true,
			duration: 0
		});
	}

	$('.account textarea').bind('click', textareaClickListener);

	if($('#tour_guide_3').length > 0) {
		tourIndex = 3;
		$('.tour_guide .next').bind('click', nextClickListener);
		$('.tour_guide .prev').bind('click', prevClickListener);
	}
	if($('#tour_guide_5').length > 0) {
		tourIndex = 5;
		$('.tour_guide .next').bind('click', nextClickListener);
		$('.tour_guide .prev').bind('click', prevClickListener);
	}
});

function textareaClickListener() {
	$(this).focus();
	$(this).select();
}

function nextClickListener() {
	tourIndex++;
	tourPage(tourIndex);
	return false;
}

function prevClickListener() {
	tourIndex--;
	tourPage(tourIndex);
	return false;
}

function tourPage(index) {
	var element = $('.tour_guide');
	if(index == 2) {
		window.location = 'http://pipelineq.com/tour/overview-access';
	} else if(index == 3) {
		element.css('background', '#eaefbb url(../images/tour3.png) no-repeat');
		element.children(':first').html('Data on the chart and map pages can be sorted by clicking at the top of a column.');
	} else if(index == 4) {
		element.css('background', '#eaefbb url(../images/tour4.png) no-repeat');
		element.children(':first').html('Type "knop" in the search box below the chart to quickly search the data.');
	} else if(index == 5) {
		element.css('background', '#eaefbb url(../images/tour5.png) no-repeat');
		element.children(':first').html('Click on bars in the chart to narrow data. Click "Clear Search" to reset data.');
	} else if(index == 6) {
		window.location = 'http://pipelineq.com/tour/map';
	}
}
