/**
 * 
 * Copyright 2005 - 2008 Vereyon
 * http://www.vereyon.nl
 * All rights reserved	
 * 
 */

UI.PlanningProgressChart = function(hElement) {
	this.Initialize(hElement);
}

UI.PlanningProgressChart.prototype.Initialize = function(hElement) {
	
	// Store values
	this.hContainer = hElement;
	
	// Set initial values
	this.aChartSeries = new Array();
	this.aChartVisibleSeries = new Array();
	this.aCheckBoxes = new Array();
	
	
	// Create containers
	this.hChartContainer = document.createElement("div");
	this.hChartContainer.className = "progressChartChart";
	this.hLegendContainer = document.createElement("div");
	this.hLegendContainer.className = "progressChartLegend";
	this.hContainer.appendChild(this.hChartContainer);
	this.hContainer.appendChild(this.hLegendContainer);
}

UI.PlanningProgressChart.prototype.Show = function() {
	
	// Declare variables
	var yWordAxis, xDateAxis;
	var hCheckBox;
	var strEmbed = "<embed ";
	
	// Remove all child nodes
	if(this.hChartContainer) {
		while(this.hChartContainer.firstChild)
			this.hChartContainer.removeChild(this.hChartContainer.firstChild);
	}
	
	this.aChartVisibleSeries = new Array();
	for(var i = 0; i < this.aCheckBoxes.length; i++) {
		hCheckBox = this.aCheckBoxes[i];
		if(hCheckBox.checked) {
			this.aChartVisibleSeries.push(this.aChartSeries[hCheckBox.value])
		}
	}
	
	yWordAxis = new YAHOO.widget.NumericAxis(); 
	yWordAxis.minimum = 0;
	yWordAxis.maximum = this.iTotalWords;
	
	xDateAxis = new YAHOO.widget.NumericAxis();
	xDateAxis.minimum = this.iStartDate;
	xDateAxis.maximum = this.iEndDate;
	xDateAxis.labelFunction = "UI.PlanningProgressChart.AxisDateLabelFunction";
	
	this.oLineChart = new YAHOO.widget.LineChart(this.hChartContainer, this.oDataSource, { wmode: "transparent", xField: "date", xAxis: xDateAxis, yAxis: yWordAxis, series: this.aChartVisibleSeries, dataTipFunction: "UI.PlanningProgressChart.DataTipFunction" });
}

UI.PlanningProgressChart.prototype.CreateLegend = function() {
	
	// Declare variables
	var newCheckBox, newLabel, newH2;
	
	// Remove all child nodes
	if(this.hLegendContainer) {
		while(this.hLegendContainer.firstChild)
			this.hLegendContainer.removeChild(this.hLegendContainer.firstChild);
	}
	
	newH2 = document.createElement("h2");
	newH2.appendChild(document.createTextNode(TEXT['ProgressChart.ShowUserInGraph']));
	this.hLegendContainer.appendChild(newH2);
	this.hLegendContainer.appendChild(document.createElement("br"));
	
	this.aCheckBoxes = new Array();
	for(var i = 0; i < this.aChartSeries.length; i++) {
		
		newCheckBox = document.createElement("input");
		newCheckBox.type = "checkbox";
		newCheckBox.value = i;
		newCheckBox.id = "showUserProgress" + i;
		newCheckBox.onclick = this.OnCheckBoxClick.Bind(this);
		this.aCheckBoxes.push(newCheckBox);
		this.hLegendContainer.appendChild(newCheckBox);
		newCheckBox.checked = true;
		newLabel = document.createElement("label");
		newLabel.htmlFor = "showUserProgress" + i;
		newLabel.innerHTML = this.aChartSeries[i].displayName;
		this.hLegendContainer.appendChild(newLabel);
		this.hLegendContainer.appendChild(document.createElement("br"));
	}
}

UI.PlanningProgressChart.prototype.OnCheckBoxClick = function(e) {
	this.Show();
}

UI.PlanningProgressChart.DataTipFunction = function(item, index, series) {
	return series.displayName + "\n" + item[series.yField + "tip"];
}

UI.PlanningProgressChart.AxisDateLabelFunction = function(value) {
	
	// Declare variables
	var oDate;
	var strResult;
	
	// Return formatted date
	oDate = new Date();
	oDate.setTime(value * 1000);
	return oDate.formatDate();
}

UI.PlanningProgressChart.prototype.__type = "UI.PlanningProgressionChart";
UI.PlanningProgressChart.prototype.hContainer = null;
UI.PlanningProgressChart.prototype.hChartContainer = null;
UI.PlanningProgressChart.prototype.hLegendContainer = null;
UI.PlanningProgressChart.prototype.oLineChart = null;
UI.PlanningProgressChart.prototype.oDataSource = null;
UI.PlanningProgressChart.prototype.aChartSeries = null;
UI.PlanningProgressChart.prototype.aChartVisibleSeries = null;
UI.PlanningProgressChart.prototype.aCheckBoxes = null;
UI.PlanningProgressChart.prototype.iTotalWords = 0;
UI.PlanningProgressChart.prototype.iStartDate = 0;
UI.PlanningProgressChart.prototype.iEndData = 0;

VOCABULUM.widget.PlanningEditor = function(el) {
	
	if(el) {
		this.render(el);
	}
}

VOCABULUM.widget.PlanningEditor.prototype = {
	
	_hParentElement: null,
	
	_oTabView: null,
	_aTabs: null,
	
	_bPlanningDataTabInitialized: false,
	
	_hPlanningDataForm: null,
	_hPlanningDataTable: null,
	_hPlanningDataLabelInput: null,
	_hPlanningDataStartDatePicker: null,
	_hPlanningDataExamDatePicker: null,
	
	render: function(el) {
		
		// Save parent element handle
		this._hParentElement = el;
		
		// Create tab control in parent element
		this._oTabView = new YAHOO.widget.TabView();
		this._oTabView.appendTo(this._hParentElement);
		this._oTabView.on('activeTabChange', this.activeTabChanged, this, true);
		this._aTabs = new Array();
		
		// Create wordlist picker tab
		this._aTabs["wordlist_picker"] = new YAHOO.widget.Tab({
			label: "Wordlists"
		});
		this._oTabView.addTab(this._aTabs["wordlist_picker"]);
		
		// Create planning data tab
		this._aTabs["planning_data"] = new YAHOO.widget.Tab({
			label: "Planning data"
		});
		this._oTabView.addTab(this._aTabs["planning_data"]);
		
		// Create word manager tab
		this._aTabs["word_manager"] = new YAHOO.widget.Tab({
			label: "Woorden"
		});
		this._oTabView.addTab(this._aTabs["word_manager"]);
		
		// Create planning details tab
		this._aTabs["planning_details"] = new YAHOO.widget.Tab({
			label: "Planning details"
		});
		this._oTabView.addTab(this._aTabs["planning_details"]);
	},
	
	activeTabChanged: function(e) {
		
		var i = this._oTabView.get('activeIndex');
		
		switch(this._oTabView.get('activeIndex')) {
			case 0:
				this.showPlanningDataTab();
				break;
		}
	},
	
	initWordlistPickerTab: function() {
		
		// Declare variables
		
	},
	
	initPlanningDataTab: function() {
		
		// Declare variables
		var hContentEl, hRow, hCell, oLabel, oInput, oDiv;
		
		// Make sure the planning data tab is empty
		hContentEl = this._aTabs["planning_data"].get("contentEl");
		while(hContentEl.firstChild)
			hContentEl.removeChild(hContentEl.firstChild);
		
		// Create planning data form
		this._hPlanningDataForm = document.createElement("form");
		this._hPlanningDataForm.className = "clearForm";
		hContentEl.appendChild(this._hPlanningDataForm);
		
		// Create table
		this._hPlanningDataTable = document.createElement("table");
		this._hPlanningDataTable.className = "formTable";
		this._hPlanningDataForm.appendChild(this._hPlanningDataTable);
		
		// Label row
		hRow = this._hPlanningDataTable.insertRow(-1);
		hCell = hRow.insertCell(-1);
		hCell.className = "label";
		oLabel = document.createElement("label");
		oLabel.appendChild(document.createTextNode(TEXT['PlanningEditorDay.Data.Label']));
		hCell.appendChild(oLabel);
		hCell = hRow.insertCell(-1);
		hCell.className = "field";
		this._hPlanningDataLabelInput = document.createElement("input");
		this._hPlanningDataLabelInput.type = "text";
		this._hPlanningDataLabelInput.className = "mediumInput";
		hCell.appendChild(this._hPlanningDataLabelInput);
		hRow = this._hPlanningDataTable.insertRow(-1);
		hCell = hRow.insertCell(-1);
		hCell.colSpan = 2;
		hCell.className = "tip";
		
		// Start date row
		hRow = this._hPlanningDataTable.insertRow(-1);
		hCell = hRow.insertCell(-1);
		hCell.className = "label";
		oLabel = document.createElement("label");
		oLabel.appendChild(document.createTextNode(TEXT['PlanningEditorDay.Data.StartDate']));
		hCell.appendChild(oLabel);
		hCell = hRow.insertCell(-1);
		hCell.className = "field";
		oDiv = document.createElement("div");
		hCell.appendChild(oDiv);
		this._hPlanningDataStartDatePicker = new YAHOO.widget.Calendar(oDiv);
		this._hPlanningDataStartDatePicker.cfg.setProperty('MONTHS_LONG', TEXT['Months.Long']);
		this._hPlanningDataStartDatePicker.cfg.setProperty('WEEKDAYS_SHORT', TEXT['Days.Short']);
		this._hPlanningDataStartDatePicker.render();
		oDiv = document.createElement("div");
		hCell.appendChild(oDiv);
		this._hPlanningDataExamDatePicker = new YAHOO.widget.Calendar(oDiv);
		this._hPlanningDataExamDatePicker.cfg.setProperty('MONTHS_LONG', TEXT['Months.Long']);
		this._hPlanningDataExamDatePicker.cfg.setProperty('WEEKDAYS_SHORT', TEXT['Days.Short']);
		this._hPlanningDataExamDatePicker.render();
		hRow = this._hPlanningDataTable.insertRow(-1);
		hCell = hRow.insertCell(-1);
		hCell.colSpan = 2;
		hCell.className = "tip";
		
		// Initialization done
		this._bPlanningDataTabInitialized = true;
	},
	
	showPlanningDataTab: function() {
		
		// Declare variables
		
		// Make sure 
		if(!this._bPlanningDataTabInitialized)
			this.initPlanningDataTab();
	}
}