
//resizes the given child element to the bounds of the given parent element
function resizeToParent(parent, child, minBounds) {
	var parentBounds = Sys.UI.DomElement.getBounds(parent);
	Sys.UI.DomElement.setLocation(child, parentBounds.x, parentBounds.y);
	var width = parentBounds.width;
	var height = parentBounds.height;
	if (minBounds) {
		if (!isNaN(minBounds.width) &&
			minBounds.width > width) {
			width = minBounds.width;
		}
		if (!isNaN(minBounds.height) &&
			minBounds.height > height) {
			height = minBounds.height;
		}
	}
	child.style.width = width + 'px';
	child.style.height = height + 'px';
}

function _ddl_onchange(listItem, preventChange) {
	var selectedItem = $(listItem).parent('ul').parent('li');
	var selectedValueHolder = selectedItem.children('input[type=hidden]');
	var newValueHolder = $(listItem).children('input[type=hidden]');
	if (selectedValueHolder.val() != newValueHolder.val()) {
		selectedValueHolder.val(newValueHolder.val());
		selectedItem.children('span').text($(listItem).children('span').text());
		if (!preventChange) {
			_ddl_triggerChange(selectedItem.parent('ul')[0]);
		}
	}
}

function _ddl_getData(ddl) {
	// find selected value
	var selectedValue = $(ddl).children('li').children('input[type=hidden]').val();
	// get all items list
	var options = $(ddl).find('ul>li');
	// find selected option & index
	var selectedItem = options.find(String.format('input[type=hidden][value={0}]', selectedValue)).parent();
	return {
		options: options,
		selectedValue: selectedValue,
		selectedItem: selectedItem,
		selectedIndex: options.index(selectedItem)
	};
}

function _ddl_triggerChange(ddl) {
	eval(ddl.getAttribute('onchange'));	
}

function yearPickerChangeYear(button, increase) {
	//cancel if already in postback
	if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack() ||
		$(button).parents('table:first').hasClass('disabled')) {
		return false;
	}	
	//find dropdown
	var ddl = $(button).parents('div:first').parent('div').find('ul:first');
	if (ddl && ddl.size() > 0) {
		var data = _ddl_getData(ddl);		
		var newItem = null;		
		// clear timeout if any
		window.clearTimeout(ddl.timerCookie);
		// change index if possible
		if (!increase &&
			data.selectedIndex < data.options.size() - 1) {
			newItem = data.options[data.selectedIndex + 1];
		}
		if (increase &&
			data.selectedIndex > 0) {
			newItem = data.options[data.selectedIndex - 1];
		}

		if (newItem != null) {
			var newIndex = data.options.index(newItem);
			if (newIndex != data.selectedIndex) {
				_ddl_onchange(newItem, true);
				//set change after 500ms - this will allow user click more times
				ddl.timerCookie = window.setTimeout(function () { _ddl_triggerChange(ddl[0]); }, 500);
			}
		}
	}
	return false;
}

function cancelEvent(evt) {
	var objEvent = new Sys.UI.DomEvent(evt);
	if (typeof (objEvent.cancelBubble) != "undefined") { objEvent.cancelBubble = true; }
	objEvent.stopPropagation();
	objEvent.preventDefault();
}

function createComparer(propertyName) {
	return function (a, b) {
		if (a[propertyName] > b[propertyName]) {
			return 1;
		}
		else if (a[propertyName] == b[propertyName]) {
			return 0;
		}
		else {
			return -1;
		}
	}
}
