$(function() {
	//alert("aha");
	$.datepicker.setDefaults(
  $.extend(
    {'dateFormat':'dd.mm.yy'},
    $.datepicker.regional['et']
  )
);
	$("#date_from_visible").datepicker({altField: '#date_from', altFormat: 'yymmdd', firstDay:1});
	$("#date_until_visible").datepicker({altField: '#date_until', altFormat: 'yymmdd', firstDay:1});
	//$("#date_from_visible").datepicker('option', $.datepicker.regional['fr']);
	
	
	
	$("#select_service > button").hide();
	$("#select_type > button").hide();
	$(".booking_capacity > button").hide();
	$("#booking_cap").css("width",40);
	
	
			
	$("#switch_company").change(function() {
		var company_id = $(this).val();
		console.log("aha je "+company_id);
		$.post("/auth/choose_client", { "company_id": company_id },
		   function(data){
		     location.reload();
		   }, "HTML");
	});		
	
					
	
	$("#service_drop").change(function(){this.form.submit();});
	$("#select_type_drop").change(function(){this.form.submit();});
	$(".booking_capacity_drop").change(function(){this.form.submit();});
	
	setTimeout ( "hideResult()", 2000 );
	//
	$("#show_group_edit").show();
	$("#show_choose_rooms").show();
	
	$("#dialog").dialog({ autoOpen: false, height: 'auto', width: 430, modal: true, resizable: false });
	$("#choose_rooms_dialog").dialog({ autoOpen: false, height: 250, width: 300, modal: true, resizable: false });
	
	$("#email_dialog").dialog({ autoOpen: false, height: 'auto', width: 430, modal: true, resizable: false });
	
	$("#more_history").live('click', function() {
		var _uri = $(this).attr('href');
		if (_uri) {
			$("#more_history").remove();
			$.post(_uri, { "foo": "bar" },
			   function(data){
			     //debug(data);
			     $("#history_list").append(data);
			   }, "HTML");
		}
		return false;
	});
	
	
	
	
	$("#show_group_edit").click(function(){ $("#dialog").dialog("open"); return false;});
	$("#show_choose_rooms_button").click(function(){ $("#choose_rooms_dialog").dialog("open"); return false;});
	
	var prev_color = $('#prev_color').val();
	
	var colors = ["#ffffff","#ffff66","#66cc66","#66ccff","#cc99cc","#cc9966","#ff6666","#e4e4e4","#ff9933","#0066cc","#ffcccc"];
	
	if ($('#edit_picker').length != 0) {
	
		$('#edit_picker').colorPicker(
				{			
				  //defaultColor:6, // index of the default color
				  //columns:13,     // number of columns
				  color: colors,
				  
				   
				  defaultColor:colors.indexOf(prev_color), // index of the default color 
				  click:function(c){
				    $('#edit_color').val(c);
				    $('#edit_color_label').css('background-color',c);
				  }
				});
		
		$('#edit_color_label').css('background-color',prev_color);
		//$('#add_color_label').css('background-color','grey');
	
	}
	
	if ($('#add_picker').length != 0) {
	
		$('#add_picker').colorPicker(
			{			
			  defaultColor:0, // index of the default color
			  //columns:13,     // number of columns 
			  color: colors, 
			  click:function(c){
			    $('#add_color').val(c);
			    $('#add_color_label').css('background-color',c);
			  }
			  
			  
			  
			});
	}		
	
	$(".open_email_dialog").click(function(){
		var data = new Object();
		var key = $(this).attr('rel');
		
		data.to_email = $(this).attr('id').replace('email_link_','');
		data.email_body = $('#email_content_'+key).html();
		
		$("#email_to_preview").html(data.to_email);
		$("#email_body_preview").html(data.email_body);
		
		//console.log('data.email_body '+data.email_body);
		
		$("#email_dialog").dialog('open');
		$("#send_email").data('email_data', data);
		$("#email_sent").hide();
		return false;
	});
		
	$("#send_email").click(function(){
		$("#email_sent").hide();
		$(this).attr('disabled','disabled');
		$("#ajax_loader").show();
		var email_data = $("#send_email").data('email_data');
		email_data.email_body = '<p>'+$("#email_additional_info").val()+'</p>' + email_data.email_body;
		//console.log(email_data);
		$.post("/stats/send_email", email_data,
		   function(result){
		     //console.log(result);
		     $("#ajax_loader").hide();
		     $("#send_email").removeAttr('disabled');
		     $("#email_sent").show();
		   }, "json");
		return false;
	});	
			
	// make arrived buttons play nice
	state_buttons();
	
	//

	

});

function hideResult() {
	$("#result").slideToggle("slow");
}

function state_buttons() {

	$(".state_form").each(function(f){
        
        //alert("aha!");
        var action = $(this).attr("action");
      	var $button = $(this).children("button");
      	var $state = $(this).children("input[name='booking_state']");
      	var booking_id = $(this).children("input[name='booking_id']").val();
      	var booking_state = $(this).children("input[name='booking_state']").val();
      	var new_class = "";
      	var booking_labels = Object();
      	
      	booking_labels['yes'] = $(this).children("input[name='booking_yes']").val();
      	booking_labels['no'] = $(this).children("input[name='booking_no']").val();
      	
        $(this).submit(function() {
      		
      		
      		$.post(action, { ajax: "true", booking_id: booking_id, booking_state:booking_state},
  			function(data){
  			  
  			  // //
  			  if (data.result == "ok" && data.new_state) {
  			  	
  			  	new_class = "state_button_" + data.new_state_text;
  			  	$button.html(booking_labels[data.new_state_text]);
  			  	$button.attr("class",new_class);
  			  	$state.val(data.new_state);
  			  	booking_state = data.new_state;
  			  		
  			  } else {
  			  
  			  } 
  			  
  			}, "json");
      		return false;
    	});
    });
	
}

