$(document).ready(function(){  
	//$("//a[@rel='external']").attr("target","_blank");
	
	$("#print").click(function(){
		print();
		return false;
	});

	$("a").focus(function(){
		this.blur();
	});
	
	$('.retail_header').click(function(){
		if ($(this).parent().attr('class') == "retail_section") {
			
			$('.retail_section_active').each(function(){
				$(this).attr('class', 'retail_section');
			});
			
			$(this).parent().attr('class', 'retail_section_active');
		} else {
			$(this).parent().attr('class', 'retail_section');
		}
		return false;
	});
	
	$('.faq_header').click(function(){
		if ($(this).parent().attr('class') == "faq_section") {
			
			$('.faq_section_active').each(function(){
				$(this).attr('class', 'faq_section');
			});
			
			$(this).parent().attr('class', 'faq_section_active');
		} else {
			$(this).parent().attr('class', 'faq_section');
		}
		return false;
	});
	
	$('#send_answers').live("click", function(){
		var q_id = $("#q_id").val();
		var a_id = $("input[@name='answer']:checked").val();

		$('#view').load('answer_receiver.asp?q_id=' + q_id + '&a_id=' + a_id);
		
		var total = parseInt($('#first_question').find('abbr').html()) + 1;
		$('#first_question').find('abbr').html(total);
		
		return false;
	});	
	
	$('#question_results_1').live("click", function(){
		var q_id = $("#q_id").val();
		$('#view').load('answer_receiver.asp?q_id=' + q_id);
		return false;
	});	
	
	$('#question_results_2').live("click", function(){
		var q_id = $("#q_id").val();
		$('#view').load('answer_receiver.asp?back=true&q_id=' + q_id);
		return false;
	});	
	
	
	
	/************************************/
	// Ajax search (start)
	/************************************/
	var url = String(window.location);
	if (navigator.appVersion.search('MSIE 6.0') == -1 && url.search(/planner.asp/) > 0) {
	var selectedElement;
	var previousKey;
	
	$('#fromString').focus(function () {
		selectedElement = 'fromString';		
	});
	
	$('#fromString').blur(function () {
		selectedElement = '';
		//restoreSelect('from');			
	});
	
	$('#toString').focus(function () {
		selectedElement = 'toString';							 
	});	
	
	$('#toString').blur(function () {
		selectedElement = '';
		//restoreSelect('to');
	});	
	
	$('#fromCoords').focus(function () {
		selectedElement = 'fromCoords';	
	});
	
	$('#fromCoords').blur(function () {
		//restoreSelect('from');
	});
	
	$('#toCoords').focus(function () {
		selectedElement = 'toCoords';							 
	});
	
	$('#toCoords').blur(function () {
		//restoreSelect('to');
	});
	
	$('#ext').focus(function () {
		selectedElement = 'ext';
	});
	
	$('#ext').blur(function () {
		selectedElement = '';		
	});

	$('#fromString').attr('autocomplete', 'off');
	$('#toString').attr('autocomplete', 'off');

	$(document).keyup(function(event){
		//alert(selectedElement+' '+event.keyCode);
		if (selectedElement == 'fromString' && event.keyCode != 13 && event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 9) {
			var fromString = $('#fromString').val();
			fromString = fromString.replace(/ /g, "blankspace");
			if (fromString.length >= 3) {
				activateSelect('from', '22px');
				$("#fromCoords").html('<option value="0">S&ouml;ker ...</option>');
				$.ajax({
						type: "POST",
						url: "ajaxsearch.asp",
						data: "searchString="+fromString,
						success: function(html){
							if (html.length > 1) {
								activateSelect('from', '22px');
								$("#fromCoords").html(html);
								restoreSelect('to');
								
							} else {
								restoreSelect('from');							
							}
						}
				});
			}
		} else if (selectedElement == 'fromString' && event.keyCode == 40) {
			var value = $('#fromCoords').find('.secondoption').val();
			$('#fromCoords').selectOptions(value);
			$('#fromCoords').focus();
			$('#fromCoords').find('.firstoption').css('background', '#ffffff');
			$('#fromCoords').find('.firstoption').css('color', '#000000');
			var fromString = $('#fromCoords :selected').text();
			$('#fromString').val(fromString);
		} else if (selectedElement == 'fromCoords' && event.keyCode == 13) {
			var fromString = $('#fromCoords :selected').text();
			$('#fromString').val(fromString);
			restoreSelect('from');
			$('#fromString').focus();			
		} else if (selectedElement == 'fromCoords' && (event.keyCode == 40 || event.keyCode == 38)) {
			var fromString = $('#fromCoords :selected').text();
			$('#fromString').val(fromString);
			previousKey = event.keyCode;
		} else if (selectedElement == 'fromCoords' && (event.keyCode == 9 && previousKey != 40 && previousKey != 38)) {
			var fromString = document.getElementById("fromCoords").options[0].text;
			restoreSelect('from');
			$('#toString').focus();
			$('#fromString').val(fromString);
			$('#fromCoords').find('.firstoption').attr('selected', 'selected');
		} else if (selectedElement == 'toString' && (event.keyCode == 9 && previousKey != 40 && previousKey != 38)) {
			var fromString = document.getElementById("fromCoords").options[0].text;
			restoreSelect('from');
			$('#toString').focus();
			$('#fromString').val(fromString);
		} else if (selectedElement == 'toString' && (event.keyCode == 9 && previousKey == 40 || previousKey == 38)) {
			var fromString = $('#fromCoords :selected').text();
			restoreSelect('from');
			$('#toString').focus();
			$('#fromString').val(fromString);
			previousKey = null;
		}
		
		if (selectedElement == 'toString' && (event.keyCode != 13 && event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 9)) {
			var tString = $('#toString').val();
			tString = tString.replace(/ /g, "blankspace");
			
			if (tString.length >= 3) {
				activateSelect('to', '50px');
				$("#toCoords").html('<option value="0">S&ouml;ker ...</option>');
				$.ajax({
						type: "POST",
						url: "ajaxsearch.asp",
						data: "searchString="+tString,
						success: function(html){
							if (html.length > 1) {
								activateSelect('to', '50px');
								$("#toCoords").html(html);
								restoreSelect('from');	
								
							} else {
								restoreSelect('to');
							}
						}
				});				
			}
		} else if (selectedElement == 'toString' && event.keyCode == 40) {
			var value = $('#toCoords').find('.secondoption').val();
			$('#toCoords').selectOptions(value);
			$('#toCoords').focus();
			$('#toCoords').find('.firstoption').css('background', '#ffffff');
			$('#toCoords').find('.firstoption').css('color', '#000000');
			var tString = $('#toCoords :selected').text();
			$('#toString').val(tString);
		} else if (selectedElement == 'toCoords' && event.keyCode == 13) {
			var tString = $('#toCoords :selected').text();
			$('#toString').val(tString);
			restoreSelect('to');
			$('#toString').focus();
		} else if (selectedElement == 'toCoords' && (event.keyCode == 40 || event.keyCode == 38)) {
			var tString = $('#toCoords :selected').text();
			$('#toString').val(tString);
			previousKey = event.keyCode;
		} else if (selectedElement == 'fromString' && (event.keyCode == 9 && previousKey != 40 && previousKey != 38)) {
			var tString = document.getElementById("toCoords").options[0].text;
			restoreSelect('to');
			$('#toString').val(tString);
		} else if (selectedElement == 'toCoords' && (event.keyCode == 9 && previousKey != 40 && previousKey != 38)) {
			var tString = document.getElementById("toCoords").options[0].text;
			restoreSelect('to');
			$('#toString').val(tString);
			$('#toCoords').find('.firstoption').attr('selected', 'selected');
		} else if (selectedElement == 'ext' && (event.keyCode == 9 && previousKey != 40 && previousKey != 38)) {
			var tString = document.getElementById("toCoords").options[0].text;
			restoreSelect('to');
			$('#toString').val(tString);	
		} else if (selectedElement == 'ext' && (event.keyCode == 9 && previousKey == 40 || previousKey == 38)) {
			var tString = $('#toCoords :selected').text();
			restoreSelect('to');
			$('#toString').val(tString);
			previousKey = null;
		}
		
	});
	
	$('#fromCoords').live("click", function(){
		if ($('#fromSelect').html().search('multiple') != -1) {
			var fromString = $('#fromCoords :selected').text();
			$('#fromString').val(fromString);
			$('#fromSelect').css('display', 'none');
			$('#fromString').focus();
		} else {
			$('#fromSelect').css('display', 'none');
			$('#fromString').css('display', 'block');
			$('#fromString').focus();
		}
		return false;
	});	
	
	$('#toCoords').live("click", function(){
		if ($('#toSelect').html().search('multiple') != -1) {
			var toString = $('#toCoords :selected').text();
			$('#toString').val(toString);
			$('#toSelect').css('display', 'none');
			$('#toString').focus();
		} else {
			$('#toSelect').css('display', 'none');
			$('#toString').css('display', 'block');
			$('#toString').focus();
		}
		return false;
	});

	
	$(document).click(function(){
		if (selectedElement != 'fromString' && selectedElement != 'fromCoords' && selectedElement != 'toString' && selectedElement != 'toCoords') {
			restoreSelect('from');
			restoreSelect('to');
		}
	});
	
	function activateSelect(strObj, top) {
		$('#'+strObj+'Coords').attr('multiple', '""');
		$('#'+strObj+'Coords').css('height', '200px');
		$('#'+strObj+'Select').css('position', 'absolute');
		$('#'+strObj+'Select').css('top', top);
		$('#'+strObj+'Select').css('left', '69px');
		$('#'+strObj+'Select').css('width', '244px');
		$('#'+strObj+'Select').css('display', 'block');	
	}
	
	function restoreSelect(strObj) {
		if ($('#'+strObj+'Select').html().search('multiple') != -1) {
			$('#'+strObj+'Select').css('display', 'none');
			$('#'+strObj+'Coords').removeAttr("multiple");
			$('#'+strObj+'Coords').css('height', '');
			$('#'+strObj+'Select').css('position', '');
			$('#'+strObj+'Select').css('top', '');
			$('#'+strObj+'Select').css('left', '');
			$('#'+strObj+'Select').css('width', '');
		}
	}
	}
	/************************************/
	// Ajax search (end)
	/************************************/
	
	

	/**Traffic info**/
	$('#info_form').submit(function() {
		if ($('#outward_journey').val() != 0 && $('#home_journey').val() != 0) {
			var data = $(this).serialize();
			$.ajax({
					type: "GET",
					url: "http://"+window.location.host+"/traffic_info/ajax_result.asp",
					data: data,
					success: function(html){
						$('#info_form').css('display', 'none');
						$('#switch_stations').removeClass('hide');
						$('#ajax_content').html(html);
					}
			});
		}
		return false;
	});	
	
	$('#loader')
		.ajaxStart(function() {
			$(this).html('<img src="../lib/img/ajax-loader.gif" id="loader_image" />');
		})
		.ajaxStop(function() {
			$(this).html('<input type="image" src="../lib/img/traffic_info_button.gif" id="getInfo" />');
	});

	
	$('#more_info_form').submit(function() {
		if ($('#outward_journey').val() != 0 || $('#home_journey').val() != 0) {
			return true;
		} else {
			return false;
		}
	});
	
	$('#switch_stations').click(function(){
		$('#info_form').css('display', 'block');
		$('#switch_stations').addClass('hide');
		$('#ajax_content').html('');
		return false;
	});
	
	$('#tab_outward_link').live("click", function(){
		$(this).addClass('active');
		$('#tab_home_link').removeClass('active');
		$('#tab_home').css('display', 'none');
		$('#tab_outward').fadeIn("slow");
		return false;
	});
	
	$('#tab_home_link').live("click", function(){
		$(this).addClass('active');
		$('#tab_outward_link').removeClass('active');
		$('#tab_outward').css('display', 'none');
		$('#tab_home').fadeIn('slow');
		return false;
	});
	/**Traffic info**/
	
	/**/
	$('#sidebar .promo').find('img').parent().removeClass('external');
	$('#sidebar .promo').find('img').parent().removeAttr('target');
	
	$('#content-narrow a').find('img').parent().removeClass('external');
	/**/


});

		
 





 
