// ajax object 

var Ajax = new Object();
var timeID = 'time';
var descriptionID = 'description';

var loadingImageID = 'loading-jp';
var loadingImageHTML = '<img alt="activity indicator" src="/assets/site/img/layout/ajax-loader-blue.gif"/>';

// messages
var chooseDepartureStationHTML = '<small>Choose departure station</small>';
var chooseArrivalStationHTML = '<small>Choose arrival station</small>';
var notFoundHTML = 'Not Found!';

// init
Ajax.Journey = function(url, from, to, callbackMethod) {

	if(from == 0){
		
		$("#time").html('');
		$("#description").html(chooseDepartureStationHTML);
	
	}else if(to == 0 ){
		
		$("#time").html('');
		$("#description").html(chooseArrivalStationHTML);
		
	}else if(to == from ){
		
		$("#time").html('');
		$("#description").html(notFoundHTML);
		
	}else{
		
		id = from + '&to=' + to;
		
		fullUrl = url + id;
	
		//alert(fullUrl);
	
		Ajax.request = Ajax.createRequestObject();
		Ajax.request.onreadystatechange = callbackMethod;
		Ajax.request.open("POST", fullUrl, true);
		Ajax.request.send(url);
		
		$("#description").html('');
		$("#time").html('');
		
	}
}

// parshe xmls request
Ajax.TimeResponse = function() {

	if (Ajax.CheckReadyState(Ajax.request)) {

		var response = Ajax.request.responseXML.documentElement;
		var _data = response.getElementsByTagName("time");

		if (_data.length == 0) {
			// not found message
			$("#description").html(notFoundHTML);
		}
		var i
		for (i = 0; i < _data.length; i++) {

			$("#time").html(response.getElementsByTagName("time")[i].firstChild.data);
			
			$("#description").html(response.getElementsByTagName("description")[i].firstChild.data);

		}
	}
}

// create request objest
Ajax.createRequestObject = function() {

	var obj;
	if (window.XMLHttpRequest) {
		obj = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		obj = new ActiveXObject("MSXML2.XMLHTTP");
	}
	return obj;
}

// check ready state
Ajax.CheckReadyState = function(obj) {

	if (obj.readyState < 4) {
		$("#loading-jp").html(loadingImageHTML);
	}

	if (obj.readyState == 4) {
		if (obj.status == 200) {
			//document.getElementById(loadingImageID).innerHTML = '<img alt="activity indicator" src="img/ajax-loader.gif"/> Loading...';
			setTimeout('Page.loadOut()', 200);
			return true;
		} else {
			$("#loading-jp").html("HTTP " + obj.status);
		}
	}
}

//create new page object
var Page = new Object();
Page.width;
Page.height;
Page.top;

Page.loadOut = function() {
	$("#loading-jp").html('');
}

// hide submit button
function hide(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}




