$(document).ready(function () {

	$('.startTime').calendricalTime();
	$('.endTime').calendricalTime();

	var dates = $(".startDate, .endDate").datepicker({
		//defaultDate: "+1w",
		changeMonth: true,
		onSelect: function (selectedDate) {
			var option = this.id.indexOf('txtDateStart') != -1 ? "minDate" : "maxDate",
					instance = $(this).data("datepicker");
			date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings);
			dates.not(this).datepicker("option", option, date);
		}
	});


	function changePlusIcon(state) {
		// toggle image icon:
		if (state == 'opening') {
			$('#selecteDateRange-icon').attr('src', 'images/advrmv.png');
		}
		else {
			$('#selecteDateRange-icon').attr('src', 'images/advadd.png');
		}
	}

	var me;
	$('.row-hover-sensitive').hover(
            function () {
            	me = $(this);
            	me.css('background-color', '#e6e6e6');
            },
                    function () {
                    	me.css('background-color', '');
                    });

	$('#selecteDateRange').hover(

            function () {
            	me = $(this);
            	me.css('cursor', 'pointer');
            	me.fadeTo(100, 1);
            },
                    function () {
                    	me.fadeTo(100, 0.8);
                    }
            );

	$('#selecteDateRange').click(function () {
		var drc = $('#dataRangeContainer');
		if (drc.css('display') == 'none') {
			changePlusIcon('opening');
			drc.slideDown('slow');
		}
		else {
			changePlusIcon('closing');
			drc.slideUp('slow');
		}

	});



	$('.all-date-selector').bind('click', function () {
		$('#selecteDateRange').trigger('click');
	}).hover(function () {
		$(this).css('cursor', 'pointer');
	});

	$('.start-date-selector').hover(function () {
		$(this).css('cursor', 'pointer');
	}).click(function () {
		var drc = $('#dataRangeContainer');
		if (drc.css('display') == 'none') {
			changePlusIcon('opening');
			drc.slideDown('slow',
                function () {
                	$(".startDate").focus()
                });
		}
		else {
			changePlusIcon('closing');
			drc.slideUp('slow');
		}

	});


	$('.end-date-selector').hover(function () {
		$(this).css('cursor', 'pointer');
	}).click(function () {
		var drc = $('#dataRangeContainer');
		if (drc.css('display') == 'none') {
			changePlusIcon('opening');
			drc.slideDown('slow',
                function () {
                	$(".endDate").focus()
                });
		}
		else {
			changePlusIcon('closing');
			drc.slideUp('slow');
		}

	});


	$(".tb-invoices").autocomplete({
		source: function (request, response) {
			$.ajax({
				url: "services/AjaxMethodsService.asmx/FetchInvoices",
				data: "{ 'invoiceNumber': '" + request.term + "'}",
				dataType: "json",
				type: "POST",
				contentType: "application/json; charset=utf-8",
				dataFilter: function (data) { return data; },
				success: function (data) {
					response($.map(data.d, function (item) {
						return {
							value: item
						}
					}))
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert(textStatus);
				}
			});
		},
		minLength: 1

	});


});
