/* Date picker */
$(window).ready(function(){
	  var defaults = {
		  yearRange: '-100:0',
		  showOn: 'both', 
		  appendText: '',
		  dateFormat: 'yy-mm-dd'};
//    $('.datepick_input').datepicker(defaults);
    $('.datepick_group').each(function() {
		var val = $(".value", $(this));
		var display = $(".display", $(this));
		var d = $.datepicker.parseDate(defaults.dateFormat, val[0].value);
		display.html(dateDisplayFormat(d));

		defaults.onSelect = function(dateStr, inp) {
			var d = $.datepicker.parseDate(defaults.dateFormat, dateStr);
			display.html(dateDisplayFormat(d));
		}
		val.datepicker(defaults);
	})


	var dt = new Date;
	var tzo = dt.getTimezoneOffset();
	$.cookie('wsp_timezone_offset', tzo, {path: '/'});

	$("#pagecount").change(function() {
		window.location.href = WebSpyPro_UrlRoot + '/projects/index/count:' + this.value;
	});

	$("#daycount").change(function() {
		window.location.href = window.location.href + '/view_ndays:' + this.value;
	});
});

function dateDisplayFormat(d) {
	var m_names = new Array("January", "February", "March", 
		"April", "May", "June", "July", "August", "September", 
		"October", "November", "December");
	
	var curr_date = d.getDate();
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();
	return(m_names[curr_month] + " " + curr_date +', ' + curr_year);
}

/* Confirmation popus */
function confirm_popup(e, msg) {
    if (confirm(msg))
      return true;

	/* prevent default, until data saved */
	e.preventDefault();
	e.stopPropagation();
	return false;
}

function set_confirm($elem, msg) {
    $elem.click(function (e) {
        return confirm_popup(e, msg);
    });
}

/* /projects/index page */
function projects_index_main() {
	$("#checkbox_all").click(function() {
		$(".SelectedProject").attr('checked', this.checked);
	});
   
	set_confirm($("a[name=Delete]"), 'Are you sure you want to delete this project ?');
	set_confirm($("a[name=Pause]"), 'Are you sure you want to pause this project ?');
	set_confirm($("a[name=Resume]"), 'Are you sure you want to resume this project ?');
	set_confirm($("a[name=Undelete]"), 'Are you sure you want to undelete this project ?');
	set_confirm($("a[name=Delete all recordings]"), 'Are you sure you want to delete all recordings for this project ?');
	
}

function projects_view_main() {
	$("#checkbox_all").click(function() {
		$(".SelectedProject").attr('checked', this.checked);
	});
	
   $("#checkbox1_all").click(function() {
       $(".Selected1Project").attr('checked', this.checked);
   });

	set_confirm($("a[name=Delete all recordings for this day]"), 'Are you sure you want to delete all recordings for this day ?');
}

function projects_visits_main() {
   $("#checkbox_all").click(function() {
     $(".SelectedProject").attr('checked', this.checked);
});
	set_confirm($("a[name=Delete this visit]"), 'Are you sure you want to delete all the recordings for this visit ?');
	set_confirm($("a[name=Delete this recording]"), 'Are you sure you want to delete this recording ?');
}

