/**
 * @author Johan Huijkman, Eden design & communication
 */
HTMPlanner = {
	jsonOptions : '',
	// selectLineNr, selectStopFrom, selectStopTo are set to prevent unwanted settings when page is refreshed;
	selectLineNr: 0, // selectedIndex for 'lijn nummer'; Als niet opgegegeven dan selectLineNr = 0;
	selectStopFrom: 0, // selectedIndex for 'halte van'; Als niet opgegegeven dan selectStopFrom = 0;
	selectStopTo: 1, // selectedIndex for 'halte van'; Als niet opgegegeven dan selectStopTo = 1;
	changeLine: function(){
		$.getJSON("htm-planner.php",{id: jQ("#ctlLineNr").val()}, function(j){
			jsonOptions = HTMPlanner.jsonOptions = j;
			if(jQ("#ctlDirectionFrom").length>0){
				var optionsFrom = '';
				for (var i = 0; i < jsonOptions.length-1; i++) {
					optionsFrom += '<option value="' + jsonOptions[i].optionValue + '">' + jsonOptions[i].optionDisplay + '</option>';
				}
				jQ("#ctlDirectionFrom").html(optionsFrom);
				jQ('#ctlDirectionFrom option:first').attr('selected', 'selected');
			}			
			if(jQ("#ctlDirectionTo").length>0){
				var optionsTo = '';
				for (var i = 1; i < jsonOptions.length; i++) {
					optionsTo += '<option value="' + jsonOptions[i].optionValue + '">' + jsonOptions[i].optionDisplay + '</option>';
				}
				jQ("#ctlDirectionTo").html(optionsTo);
				jQ('#ctlDirectionTo option:first').attr('selected', 'selected');
			}			
		})
	},
	changeStopTo: function(){
		var initSelectedTo = (jQ("#ctlDirectionFrom")[0].selectedIndex)+1;
		var optionsTo = '';
		jsonOptions = HTMPlanner.jsonOptions;
		for (var i = initSelectedTo; i < jsonOptions.length; i++) {
			optionsTo += '<option value="' + jsonOptions[i].optionValue + '">' + jsonOptions[i].optionDisplay + '</option>';
		}
		jQ("#ctlDirectionTo").html(optionsTo);
		jQ('#ctlDirectionTo option:first').attr('selected', 'selected');
	},
	changeStopFrom: function(){
		var initSelectedFrom = jQ("#ctlDirectionTo")[0].selectedIndex;
		if(jQ("#ctlDirectionTo")[0].selectedIndex > initSelectedFrom){
			jQ("#ctlDirectionFrom option:eq("+initSelectedFrom+")").attr("selected", "selected");
		}
	},
	clearOptions: function(){
		if(jQ("#ctlDirectionFrom").length>0){ jQ("#ctlDirectionFrom option").remove(); }
		if(jQ("#ctlDirectionTo").length>0){ jQ("#ctlDirectionTo option").remove(); }
	}
};

jQ(function(){
	jQ("#ctlLineNr").change(function(){
		if(jQ("#ctlDirectionTo").length>0 || jQ("#ctlDirectionFrom").length>0){
			if(jQ("#ctlLineNr").val() != "") HTMPlanner.changeLine();
			else HTMPlanner.clearOptions();
		}		
	})		
	jQ("#ctlDirectionFrom").change(function(){			
		if(jQ("#ctlDirectionTo").length>0){ HTMPlanner.changeStopTo(); }
	})	
	jQ("#ctlDirectionTo").change(function(){			
		if(jQ("#ctlDirectionFrom").length>0){ HTMPlanner.changeStopFrom(); }
	})	
});
	
jQ(document).ready(function(){			
		if(isNaN(HTMPlanner.selectLineNr)) return;
		if(jQ("#ctlLineNr").length>0 && jQ("#ctlLineNr")[0].selectedIndex != HTMPlanner.selectLineNr){
			jQ("#ctlLineNr option:eq("+(HTMPlanner.selectLineNr)+")").attr("selected", "selected");
		}
		if(jQ("#ctlLineNr").val() != ""){
			if(jQ("#ctlDirectionTo").length>0 || jQ("#ctlDirectionFrom").length>0){
				$.getJSON("htm-planner.php",{id: jQ("#ctlLineNr").val()}, function(j){
					HTMPlanner.jsonOptions = j;							
					if(jQ("#ctlDirectionFrom").length>0 && jQ("#ctlDirectionFrom")[0].selectedIndex !=  HTMPlanner.selectStopFrom){
						jQ("#ctlDirectionFrom option:eq("+(HTMPlanner.selectStopFrom)+")").attr("selected", "selected");
					}	
					HTMPlanner.changeStopTo();
					if(jQ("#ctlDirectionTo").length>0 && jQ("#ctlDirectionTo")[0].selectedIndex != HTMPlanner.selectStopTo){
						jQ("#ctlDirectionTo option:eq("+(HTMPlanner.selectStopTo-HTMPlanner.selectStopFrom-1)+")").attr("selected", "selected");
					}						
				});	
			}			
		}else{
			HTMPlanner.clearOptions();
		}
});
