function getURL(includeOthers) {

	var dateStartElem = document.getElementById('historyStart');
	var dateEndElem = document.getElementById('historyEnd');
	var periodElem = document.getElementById('periodSelect');

	var fundString = '';
	var fundElem = document.getElementById('subfundSelect');
	var funds = fundElem.options.length;

	for (var i = 0; i < funds; i++) {
		if (fundElem.options[i].selected) {
			fundString += fundElem.options[i].value + ',';
		}
	}

	var currencyString = '';
	var indexString = '';

	if (includeOthers) {
		for (var i = 0; i < currencyOptions.length; i++) {
			if (currencyOptions[i].checked) {
				currencyString += ',' + currencyOptions[i].value;
			}
		}
	
		for (var i = 0; i < indexOptions.length; i++) {
			if (indexOptions[i].checked) {
				indexString += ',' + indexOptions[i].value;
			}
		}

		if (fundString == '' && currencyString == '' && indexString == '') return false;
	} else {
		if (fundString == '') return false;
	}

	var url = '';

	if (periodElem.value) {
		url = '/period/' + periodElem.value;
	} else {
		var da = new Date(dateStartElem.value);
		var db = new Date(dateEndElem.value);

		if (da > db) return false;

		url = '/startDate/' + dateStartElem.value + '/endDate/' + dateEndElem.value
	}

	if (fundString) url += '/funds/' + fundString;
	if (currencyString) url += '/currency/' + currencyString;
	if (indexString) url += '/index/' + indexString;

	return url;

}

function showTable() {
	if (activeHistory != 'table') {
		$('#priceGraph').hide();
		activeHistory = 'table';
	}

	if (validTable) {
		$('#priceContent').show();
		return;
	}

	var myURL = getURL(true);

	if (myURL) {
		document.body.style.cursor = 'wait';
		$.get(ajaxURL + myURL, null, updateContent);
	}
}

function exportHistory() {
	var myURL = getURL(true);

	if (myURL) {
		document.location = ajaxURL + myURL + '/action/xls';
	}
}

function updateContent(newContent) {
	document.body.style.cursor = 'default';
	document.getElementById('priceContent').innerHTML = newContent;

	validTable = true;
	if (activeHistory == 'table') {
		$('#priceContent').show();
	}
}

function showGraph() {
	if (activeHistory != 'graph') {
		$('#priceContent').hide();
		activeHistory = 'graph';
	}

	if (validGraph) {
		$('#priceGraph').show();
		return;
	}

	var fundString = '';
	var fundElem = document.getElementById('subfundSelect');
	var funds = fundElem.options.length;
	var fundCount = 0;

	for (var i = 0; i < funds; i++) {
		if (fundElem.options[i].selected) {
			fundString += fundElem.options[i].value + ',';
			++fundCount;
		}
	}

	var currencyString = '';
	for (var i = 0; i < currencyOptions.length; i++) {
		if (currencyOptions[i].checked) {
			currencyString += ',' + currencyOptions[i].value;
		}
	}

	var indexString = '';
	for (var i = 0; i < indexOptions.length; i++) {
		if (indexOptions[i].checked) {
			indexString += ',' + indexOptions[i].value;
		}
	}

	if (fundString == '' && currencyString == '' && indexString == '') return true;

	var dateStartElem = document.getElementById('historyStart');
	var dateEndElem = document.getElementById('historyEnd');
	var periodElem = document.getElementById('periodSelect');

	var myURL = xmlURL;

	if (periodElem.value) {
		myURL += '%26period%3D' + periodElem.value;
	} else {
		var da = new Date(dateStartElem.value);
		var db = new Date(dateEndElem.value);

		if (da > db) return true;

		myURL += '%26startDate%3D' + dateStartElem.value + '%26endDate%3D' + dateEndElem.value;
	}
	if (fundString) myURL += '%26subfunds%3D' + fundString;
	if (currencyString) myURL += '%26currency%3D' + currencyString;
	if (indexString) myURL += '%26index%3D' + indexString;


// Update flash source.
	var aHeight = 189;
	var bHeight = 189;

	var fHeight = fundCount * aHeight;
	if (currencyString) fHeight += bHeight;
	if (indexString) fHeight += bHeight;

	var so = new SWFObject(flashURL,"flashchart_{$key}","600",fHeight,"8");
	so.addParam("wmode","transparent");

	so.addVariable("fwidth","590"); 


	var gi = 0;

	if (fundString) {
		for (var i = 0; i < funds; i++) {
			if (fundElem.options[i].selected) {
				gi++;
				so.addVariable("link" + gi, myURL + '%26type%3Dfunds%26i%3D' + (gi-1)); 
				so.addVariable("title" + gi, fundElem.options[i].innerHTML); 
				so.addVariable("height" + gi, aHeight); 
			}
		}

	}

	if (currencyString) {
		gi++;
		so.addVariable("link" + gi, myURL + '%26type%3Dcurrency'); 
		so.addVariable("title" + gi, "Waluty"); 
		so.addVariable("height" + gi, bHeight); 
		so.addVariable("shownames" + gi, '1'); 
	}

	if (indexString) {
		gi++;
		so.addVariable("link" + gi, myURL + '%26type%3Dindex');
		so.addVariable("title" + gi, "Indeksy"); 
		so.addVariable("height" + gi, bHeight); 
		so.addVariable("shownames" + gi, '1'); 
	}

	so.addVariable("fcount", gi); 

	so.write("priceGraph");

	$('#priceGraph').show();
	validGraph = true;
}

var indexOptions;
var currencyOptions;

function clearPeriod() {
	document.getElementById('periodSelect').value = '';
}

var validTable = true;
var validGraph = false;
var activeHistory = 'table';

function showHistory() {
	validTable = false;
	validGraph = false;

	if (activeHistory == 'table') showTable();
	else if (activeHistory == 'graph') showGraph();
}

function disableDate() {
	if (this.value) {
		$('.date_input').attr('disabled', 'disabled');
	} else {
		$('.date_input').removeAttr('disabled');
	}
}


var selectedSubfunds;

function initPrice() {
	$('#historyButton').click(showHistory);
	$('#historyButtonExport').click(exportHistory);

//	$('.date_input').focus(clearPeriod);
	$('#periodSelect').change(disableDate);

	$('#historyGraph').click(showGraph);
	$('#historyTable').click(showTable);
	
	$('#subfundSelect').change(subfundsChanged);

	indexOptions = $('.indexOption');
	currencyOptions = $('.currencyOption');


	var c = 0;
	var selectElem = document.getElementById('subfundSelect');
	var subfundCount = selectElem.options.length;
	selectedSubfunds = [];
	for (var i = 0; i < subfundCount; i++) {
		if (selectElem.options[i].selected) {
			c++;
			selectedSubfunds.push(selectElem.options[i].value);
			if (c == 5) break;
		}
	}
}

function subfundsChanged() {
	var funds = this.options.length;

	var c = 0;
	var newSelected = [];
	for (var i = 0; i < funds; i++) {
		if (this.options[i].selected) {
			c++;
			newSelected.push(this.options[i].value);
		}
	}

 	if (c > 5) {
 		alert(maxMessage);
 		for (var i = 0; i < funds; i++) {
 			this.options[i].selected = (selectedSubfunds.indexOf(this.options[i].value) >= 0);
 		}
 	} else {
		selectedSubfunds = newSelected;
	}
}

$().ready(initPrice);

