// JavaScript Document
jQuery(function() {

    // BAYAN
    if(jQuery('.bayan_title').length !== 0){
        jQuery('.bayan_body').hide(300);
        jQuery('.bayan_body').eq(0).show(300);
        jQuery('.bayan_title').eq(0).addClass('bayan_title_curret');
        jQuery('.bayan_title').click(function(){
            jQuery(this).toggleClass('bayan_title_curret');
            var x = jQuery('.bayan_title').index(this);
            jQuery('.bayan_body').eq(x).toggle(300);
        });
    }

	// TABS BOX
	if(jQuery('#tbox_body').length !== 0) {
		var n_tabs = jQuery('.tabs_title li').length;
		jQuery('.tabs_title li').eq(0).addClass('current');
		jQuery('.tabs_title li').click(function(){
			  if(($(this).attr('class') != 'current') && ($(this).attr('class') != 'tab_green current')){ // if this tab current, no action!
				  var index = jQuery('.tabs_title li').index(this);
				  jQuery('.tab_body').hide(200);
				  jQuery('.tab_body').eq(index).show(200);
				  jQuery('.tabs_title li').removeClass('current');
				  jQuery('.tabs_title li').eq(index).addClass('current');
			}
		});
	}
	

	//USER PANEL
	jQuery('#up_smol').click(function(){
		jQuery('#up_smol').hide();
		jQuery('#up_big').show(150);
	});
	jQuery('#up_big').click(function(){
		jQuery('#up_smol').show();
		jQuery('#up_big').hide(150);
	});
	
	//MESSAGES bof in page
	jQuery('#order_messages_title').click(function(){
		if(jQuery('#order_messages_budy').is(':hidden')){
			jQuery('#order_messages_title .toggle_view').removeClass('toggle_view_show');
			jQuery('#order_messages_title .toggle_view').addClass('toggle_view_hide');
			jQuery('#order_messages_budy').show(100);
			jQuery('#order_messages_title .secret').hide();
		}else{
			jQuery('#order_messages_title .toggle_view').removeClass('toggle_view_hide');
			jQuery('#order_messages_title .toggle_view').addClass('toggle_view_show');
			jQuery('#order_messages_budy').hide(100);
			jQuery('#order_messages_title .secret').show();
		}
	});



	//ORDER INFO
	jQuery('#order_details_title').click(function(){
		if (jQuery('#order_details_body').is(':hidden')){
			jQuery('#order_details_title .toggle_view').removeClass('toggle_view_show');
			jQuery('#order_details_title .toggle_view').addClass('toggle_view_hide');
			jQuery('#order_details_body').show(300);
		}else{
			jQuery('#order_details_title .toggle_view').removeClass('toggle_view_hide');
			jQuery('#order_details_title .toggle_view').addClass('toggle_view_show');
			jQuery('#order_details_body').hide(300);
		}
	});
	


	//MESSAGES
	//Close message
	jQuery('#massege_close').click(function(){
		jQuery('#s_blank').hide(100);
		jQuery('#show_fon').hide(300);
		jQuery('#ph_message_details').hide();
		jQuery('#new_message_pane').hide();
		
		
	});
	//show history
	jQuery('#history_show').click(function(){
		jQuery('#history_hide').show(100);
		jQuery('.s_blank_body_history').show(100);
		jQuery('#history_show').hide();
		jQuery('#s_blank_body_history').scrollTop(300);
	});
	//hide history
	jQuery('#history_hide').click(function(){
		jQuery('#history_hide').hide(100);
		jQuery('.s_blank_body_history').hide(100);
		jQuery('#history_show').show();
	});
	//Reply message
	jQuery('#btn_reply_this_message').click(function(){
		jQuery('#reply_this_message').show(100);
		jQuery('#btn_reply_this_message').hide();
		$('#mess_reply').focus();
	});
	
	jQuery("input[name=show_compensated]").click(function () {
			if (jQuery(this).attr('checked') == true) {
				jQuery(".compensated").show();
				jQuery("input[name=show_compensated]").attr('checked', 'checked');
				
			} else {
				jQuery(".compensated").hide();
				jQuery("input[name=show_compensated]").removeAttr('checked');
			}
	});

});

//massege panel
function message_view(object, id) {
	$.ajax({
		url: "/messages/message_details/" + id,
		dataType: "json",
		success: function(data) {
	
		var reply_form_id = "";
		var reply_form = "";
		var popup = "";
		
			jQuery('.ph_from').html(data.from);
			jQuery('.ph_to').html(data.to);
			jQuery('.ph_subject').html(data.subject);
			jQuery('.ph_body').html(data.body);
			jQuery('.ph_creation_time').html(new Date(data.creation_time).format('M d, Y H:i'));
			if (data.parent_message_id > 0) {
				jQuery('.ph_parent_message_link').html("<a onclick=\"message_view(this, " + data.parent_message_id  + ");\" href=\"javascript:void(0);\">Parent message</a>");
			}
			if (data.confirmation == false) {
				jQuery('.ph_confirmation_needed').html("");
				reply_form_id = "content_reply_form";
			}
			else {
				jQuery('.ph_confirmation_needed').html("<span class=\"confirmation_needed_" + data.id + "\">Needs confirmation</span>");
				if (data.confirmation == 1) {
					reply_form_id = "content_confirm_1";
				}
				else {
					reply_form_id = "content_confirm_2";
				}
			}

			// Reject form
			jQuery('.val_message_id').val(data.id);
			jQuery('.val_subject').val(data.subject);
			jQuery('.val_recipient_type').val(data.sender_type); // We should switch sender with recipient, because we are going to send a reply
			jQuery('.val_recipient_id').val(data.sender_id);

			jQuery(object).parent().parent().parent().attr("class", "");
			jQuery('#new_message_pane').hide();
			jQuery('#show_fon').show(100);
			jQuery('#s_blank').show(300);

			reply_form = jQuery('#' + reply_form_id).html();
			popup = jQuery('#content_message_popup').html();

			jQuery('#ph_message_details').html(popup);
			if (data.is_reply_allowed == true)
				jQuery('#ph_reply_form').html(reply_form);
			else {
				jQuery('#ph_reply_form').html("");
			}

			jQuery('#ph_message_details').show();
		}
	});
}
//new massege
function message_new(){
	jQuery('#show_fon').show(100);
	jQuery('#s_blank').show(300);
	jQuery('#new_message_pane').show();
	jQuery('#message_details').html("");
	jQuery('.s_blank_create_mess').show();
	jQuery('.s_blank_read_mess').hide();
}
function confirm_message(id) {
	$.get('/messages/confirm_message/'+id, function(data) {
		if (data == "ok.") {
			jQuery(".confirmation_needed_"+id).hide();
			jQuery('#s_blank').hide(100);
			jQuery('#show_fon').hide(300);
		}
	});
	return false;
}

function read_message(id) {
	$.get('/messages/read_message/'+id, function(data) {
		if (data == "ok") {
			//jQuery(".confirmation_needed_"+id).hide();
		}
	});
}

function show_reject_form() {
	jQuery('#reject_form').show(300);
	jQuery('#primary_confirm_controls').hide();
	jQuery('.reason_title').show();
}
function hide_reject_form() {
	jQuery('#reject_form').hide(300);
	jQuery('#primary_confirm_controls').show();
	jQuery('.reason_title').hide();
}

function payment_account_set_default(id) {
	$.get('/financial_overview/payment_account_set_default/'+id, function(data) {
		if (data == "ok") {
			window.location = "/financial_overview.html";
		}
		else {
			alert("Error");
		}
	});
}
function payment_account_delete(id) {
	$.get('/financial_overview/payment_account_delete/'+id, function(data) {
		if (data == "ok") {
			window.location = "/financial_overview.html";
		}
		else {
			alert("Error");
		}
	});
}

function fine_removal(id) {
	jQuery('#show_fon').show(100);
	jQuery('#s_blank').show(300);
	jQuery('#new_message_pane').show();
	jQuery('#message_details').html("");
	jQuery('.s_blank_create_mess').show();
	jQuery('.s_blank_read_mess').hide();
	jQuery("#fine_id").attr("value", id);
}

function fine_removal_send() {
	var explanation = jQuery("#mess_reply").val();
	if (explanation.length > 0 && !/^[\s]+$/.test(explanation)) {
		document.getElementById('fine_removal_form').submit();
		return true;
	}
	alert("Enter your explanation first, please.");
	return false;
}

function new_message_send() {
	if (jQuery("#recipient_type").val() > 0) {
		if (jQuery("#mess_reply").val().length > 0) {
			document.getElementById('new_message_form').submit();

		}
		else {
			alert("Message cannot be empty");
		}
	}
	else {
		alert("Please, select department");
	}
	return false;
}

